修复产品修改、删除
This commit is contained in:
65
apps/sentinel/internal/logic/topup/alitopuplogic.go
Normal file
65
apps/sentinel/internal/logic/topup/alitopuplogic.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package topuplogic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/smartwalle/alipay/v3"
|
||||
"tianyuan-api/apps/sentinel/internal/model"
|
||||
"tianyuan-api/apps/sentinel/internal/svc"
|
||||
"tianyuan-api/apps/sentinel/sentinel"
|
||||
"tianyuan-api/pkg/generate"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type AliTopUpLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewAliTopUpLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AliTopUpLogic {
|
||||
return &AliTopUpLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *AliTopUpLogic) AliTopUp(in *sentinel.AliTopUpRequest) (*sentinel.AliTopUpResponse, error) {
|
||||
if in.Amount > l.svcCtx.Config.TopUp.MaxTopUpAmount {
|
||||
return &sentinel.AliTopUpResponse{}, fmt.Errorf("充值金额最大不能超过%d元", l.svcCtx.Config.TopUp.MaxTopUpAmount)
|
||||
}
|
||||
// 使用从 svcCtx 中获取的支付宝客户端
|
||||
client := l.svcCtx.AlipayClient
|
||||
outTradeNo := generate.GenerateTransactionID()
|
||||
totalAmount := fmt.Sprintf("%.2f", float64(in.Amount))
|
||||
// 构造支付请求
|
||||
var p = alipay.TradePagePay{
|
||||
Trade: alipay.Trade{
|
||||
Subject: "天远数据API使用额度",
|
||||
OutTradeNo: outTradeNo,
|
||||
TotalAmount: totalAmount,
|
||||
ProductCode: "FAST_INSTANT_TRADE_PAY",
|
||||
NotifyURL: "https://console.tianyuanapi.com/api/console/", // 异步回调通知地址
|
||||
ReturnURL: "https://console.tianyuanapi.com/charge/recharge", // 支付成功后的跳转地址
|
||||
},
|
||||
}
|
||||
_, inserErr := l.svcCtx.PayOrderModel.Insert(l.ctx, &model.PayOrder{
|
||||
UserId: in.UserId,
|
||||
OutTradeNo: outTradeNo,
|
||||
Amount: float64(in.Amount),
|
||||
})
|
||||
if inserErr != nil {
|
||||
return nil, inserErr
|
||||
}
|
||||
// 生成支付链接
|
||||
url, err := client.TradePagePay(p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &sentinel.AliTopUpResponse{
|
||||
PayUrl: url.String(),
|
||||
}, nil
|
||||
}
|
||||
77
apps/sentinel/internal/logic/topup/alitopupnotifylogic.go
Normal file
77
apps/sentinel/internal/logic/topup/alitopupnotifylogic.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package topuplogic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/smartwalle/alipay/v3"
|
||||
"math"
|
||||
"net/url"
|
||||
"strconv"
|
||||
|
||||
"tianyuan-api/apps/sentinel/internal/svc"
|
||||
"tianyuan-api/apps/sentinel/sentinel"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type AliTopUpNotifyLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewAliTopUpNotifyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AliTopUpNotifyLogic {
|
||||
return &AliTopUpNotifyLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *AliTopUpNotifyLogic) AliTopUpNotify(in *sentinel.AliTopUpNotifyRequest) (*sentinel.AliTopUpNotifyResponse, error) {
|
||||
globalErr := errors.New("error")
|
||||
// 初始化支付宝客户端
|
||||
client := l.svcCtx.AlipayClient
|
||||
|
||||
// 解析表单数据
|
||||
formData, err := url.ParseQuery(in.RawForm)
|
||||
if err != nil {
|
||||
l.Logger.Errorf("解析表单数据失败: %v", err)
|
||||
return nil, globalErr
|
||||
}
|
||||
|
||||
// 验证签名并解析通知
|
||||
notify, err := client.DecodeNotification(formData)
|
||||
if err != nil {
|
||||
l.Logger.Errorf("通知解析失败: %v", err)
|
||||
return nil, globalErr
|
||||
}
|
||||
|
||||
// 验证交易状态
|
||||
if notify.TradeStatus == alipay.TradeStatusSuccess {
|
||||
l.Logger.Infof("订单 %s 支付成功", notify.OutTradeNo)
|
||||
payOrder, findErr := l.svcCtx.PayOrderModel.FindOneByOutTradeNo(l.ctx, notify.OutTradeNo)
|
||||
if findErr != nil {
|
||||
return nil, globalErr
|
||||
}
|
||||
if payOrder.OutTradeNo == notify.OutTradeNo && payOrder.Status == 0 {
|
||||
notifyAmount, parseFloatErr := strconv.ParseFloat(notify.TotalAmount, 64)
|
||||
if parseFloatErr != nil {
|
||||
logx.Errorf("金额转换错误: %v", parseFloatErr)
|
||||
return nil, globalErr
|
||||
}
|
||||
logx.Infof("回调金额:%v,订单金额%v", notify.TotalAmount, payOrder.Amount)
|
||||
// 比较支付宝返回的金额与数据库存储的金额
|
||||
if math.Abs(notifyAmount-payOrder.Amount) < 1e-6 {
|
||||
// 金额匹配,继续处理
|
||||
} else {
|
||||
return nil, globalErr
|
||||
}
|
||||
}
|
||||
} else {
|
||||
l.Logger.Infof("订单 %s 支付状态: %s", notify.OutTradeNo, notify.TradeStatus)
|
||||
}
|
||||
|
||||
// 返回成功响应
|
||||
return &sentinel.AliTopUpNotifyResponse{Success: true}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user