2024-10-15 00:23:07 +08:00
|
|
|
|
package topuplogic
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"errors"
|
|
|
|
|
"github.com/smartwalle/alipay/v3"
|
|
|
|
|
"math"
|
|
|
|
|
"net/url"
|
|
|
|
|
"strconv"
|
2024-10-15 17:19:23 +08:00
|
|
|
|
"tianyuan-api/apps/user/user"
|
2024-10-15 00:23:07 +08:00
|
|
|
|
|
|
|
|
|
"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 {
|
2024-10-15 17:19:23 +08:00
|
|
|
|
payOrder.Status = 1
|
|
|
|
|
updateErr := l.svcCtx.PayOrderModel.Update(l.ctx, payOrder)
|
|
|
|
|
if updateErr != nil {
|
|
|
|
|
return nil, globalErr
|
|
|
|
|
}
|
|
|
|
|
_, rechargeWallet := l.svcCtx.WalletRpc.RechargeWallet(l.ctx, &user.RechargeWalletRequest{
|
|
|
|
|
UserId: payOrder.UserId,
|
|
|
|
|
OutTradeNo: payOrder.OutTradeNo,
|
|
|
|
|
Amount: int64(payOrder.Amount),
|
2024-10-21 17:07:25 +08:00
|
|
|
|
PaymentMethod: 1,
|
2024-10-15 17:19:23 +08:00
|
|
|
|
})
|
|
|
|
|
if rechargeWallet != nil {
|
|
|
|
|
return nil, globalErr
|
|
|
|
|
}
|
2024-10-15 00:23:07 +08:00
|
|
|
|
} else {
|
|
|
|
|
return nil, globalErr
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
l.Logger.Infof("订单 %s 支付状态: %s", notify.OutTradeNo, notify.TradeStatus)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 返回成功响应
|
|
|
|
|
return &sentinel.AliTopUpNotifyResponse{Success: true}, nil
|
|
|
|
|
}
|