qnc-server-tob/app/user/cmd/api/internal/logic/pay/alipaycallbacklogic.go

224 lines
6.4 KiB
Go
Raw Normal View History

2025-01-10 00:09:25 +08:00
package pay
import (
"context"
2025-04-15 22:52:02 +08:00
"database/sql"
"fmt"
2025-01-10 00:09:25 +08:00
"net/http"
2025-04-11 13:10:17 +08:00
"qnc-server/pkg/lzkit/lzUtils"
2025-04-15 22:52:02 +08:00
"strings"
2025-01-10 00:09:25 +08:00
"time"
2025-04-08 14:50:24 +08:00
"github.com/smartwalle/alipay/v3"
2025-04-11 13:10:17 +08:00
"qnc-server/app/user/cmd/api/internal/svc"
2025-04-15 22:52:02 +08:00
"qnc-server/app/user/model"
2025-04-08 14:50:24 +08:00
"github.com/zeromicro/go-zero/core/logx"
2025-04-15 22:52:02 +08:00
"github.com/zeromicro/go-zero/core/stores/sqlx"
2025-01-10 00:09:25 +08:00
)
type AlipayCallbackLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewAlipayCallbackLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AlipayCallbackLogic {
return &AlipayCallbackLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *AlipayCallbackLogic) AlipayCallback(w http.ResponseWriter, r *http.Request) error {
notification, err := l.svcCtx.AlipayService.HandleAliPaymentNotification(r)
if err != nil {
2025-03-07 03:48:59 +08:00
logx.Errorf("支付宝支付回调,%v", err)
2025-01-10 00:09:25 +08:00
return nil
}
2025-04-15 22:52:02 +08:00
// 根据订单号前缀判断订单类型
orderNo := notification.OutTradeNo
if strings.HasPrefix(orderNo, "Q_") {
// 查询订单处理
return l.handleQueryOrderPayment(w, notification)
} else if strings.HasPrefix(orderNo, "A_") {
// 代理会员订单处理
return l.handleAgentVipOrderPayment(w, notification)
} else {
// 兼容旧订单,假设没有前缀的是查询订单
return l.handleQueryOrderPayment(w, notification)
}
}
// 处理查询订单支付
func (l *AlipayCallbackLogic) handleQueryOrderPayment(w http.ResponseWriter, notification *alipay.Notification) error {
2025-01-10 00:09:25 +08:00
order, findOrderErr := l.svcCtx.OrderModel.FindOneByOrderNo(l.ctx, notification.OutTradeNo)
if findOrderErr != nil {
logx.Errorf("支付宝支付回调,查找订单失败: %+v", findOrderErr)
return nil
}
2025-04-15 22:52:02 +08:00
2025-01-10 00:09:25 +08:00
if order.Status != "pending" {
alipay.ACKNotification(w)
return nil
}
2025-04-15 22:52:02 +08:00
2025-03-07 04:01:24 +08:00
user, err := l.svcCtx.UserModel.FindOne(l.ctx, order.UserId)
if err != nil {
logx.Errorf("支付宝支付回调,查找用户失败: %+v", err)
2025-01-10 00:09:25 +08:00
return nil
}
2025-03-07 04:01:24 +08:00
2025-04-15 22:52:02 +08:00
amount := lzUtils.ToAlipayAmount(order.Amount)
2025-03-07 04:01:24 +08:00
if user.Inside != 1 {
// 确保订单金额和状态正确,防止重复更新
if amount != notification.TotalAmount {
logx.Errorf("支付宝支付回调,金额不一致")
return nil
}
}
2025-01-10 00:09:25 +08:00
switch notification.TradeStatus {
case alipay.TradeStatusSuccess:
order.Status = "paid"
order.PayTime = lzUtils.TimeToNullTime(time.Now())
default:
return nil
}
2025-04-15 22:52:02 +08:00
2025-04-08 14:50:24 +08:00
order.PlatformOrderId = lzUtils.StringToNullString(notification.TradeNo)
2025-01-10 00:09:25 +08:00
if updateErr := l.svcCtx.OrderModel.UpdateWithVersion(l.ctx, nil, order); updateErr != nil {
logx.Errorf("支付宝支付回调,修改订单信息失败: %+v", updateErr)
return nil
}
2025-04-15 22:52:02 +08:00
2025-01-10 00:09:25 +08:00
if order.Status == "paid" {
if asyncErr := l.svcCtx.AsynqService.SendQueryTask(order.Id); asyncErr != nil {
logx.Errorf("异步任务调度失败: %v", asyncErr)
return asyncErr
}
}
2025-04-15 22:52:02 +08:00
alipay.ACKNotification(w)
return nil
}
// 处理代理会员订单支付
func (l *AlipayCallbackLogic) handleAgentVipOrderPayment(w http.ResponseWriter, notification *alipay.Notification) error {
agentOrder, findAgentOrderErr := l.svcCtx.AgentMembershipRechargeOrderModel.FindOneByOrderNo(l.ctx, notification.OutTradeNo)
if findAgentOrderErr != nil {
logx.Errorf("支付宝支付回调,查找代理会员订单失败: %+v", findAgentOrderErr)
return nil
}
if agentOrder.Status != "pending" {
alipay.ACKNotification(w)
return nil
}
user, err := l.svcCtx.UserModel.FindOne(l.ctx, agentOrder.UserId)
if err != nil {
logx.Errorf("支付宝支付回调,查找用户失败: %+v", err)
return nil
}
amount := lzUtils.ToAlipayAmount(agentOrder.Amount)
if user.Inside != 1 {
// 确保订单金额和状态正确,防止重复更新
if amount != notification.TotalAmount {
logx.Errorf("支付宝支付回调,金额不一致")
return nil
}
}
switch notification.TradeStatus {
case alipay.TradeStatusSuccess:
agentOrder.Status = "paid"
default:
return nil
}
if agentOrder.Status == "paid" {
err = l.svcCtx.AgentModel.Trans(l.ctx, func(transCtx context.Context, session sqlx.Session) error {
agentModel, err := l.svcCtx.AgentModel.FindOne(transCtx, agentOrder.AgentId)
if err != nil {
return fmt.Errorf("查找代理信息失败: %+v", err)
}
agentOrder.PlatformOrderId = lzUtils.StringToNullString(notification.TradeNo)
if updateErr := l.svcCtx.AgentMembershipRechargeOrderModel.UpdateWithVersion(l.ctx, nil, agentOrder); updateErr != nil {
return fmt.Errorf("修改代理会员订单信息失败: %+v", updateErr)
}
agentModel.LevelName = agentOrder.LevelName
agentModel.MembershipExpiryTime = RenewMembership(agentModel.MembershipExpiryTime)
if updateErr := l.svcCtx.AgentModel.UpdateWithVersion(l.ctx, nil, agentModel); updateErr != nil {
return fmt.Errorf("修改代理信息失败: %+v", updateErr)
}
return nil
})
if err != nil {
logx.Errorf("支付宝支付回调,处理代理会员订单失败: %+v", err)
refundErr := l.handleRefund(agentOrder)
if refundErr != nil {
logx.Errorf("支付宝支付回调,退款失败: %+v", refundErr)
}
return nil
}
}
2025-01-10 00:09:25 +08:00
alipay.ACKNotification(w)
return nil
}
2025-04-15 22:52:02 +08:00
func RenewMembership(expiry sql.NullTime) sql.NullTime {
// 确定基准时间
var baseTime time.Time
if expiry.Valid {
baseTime = expiry.Time
} else {
baseTime = time.Now()
}
// 增加一年(自动处理闰年)
newTime := baseTime.AddDate(1, 0, 0)
// 返回始终有效的 NullTime
return sql.NullTime{
Time: newTime,
Valid: true,
}
}
func (l *AlipayCallbackLogic) handleRefund(order *model.AgentMembershipRechargeOrder) error {
ctx := context.Background()
// 退款
if order.PaymentMethod == "wechat" {
refundErr := l.svcCtx.WechatPayService.WeChatRefund(ctx, order.OrderNo, order.Amount, order.Amount)
if refundErr != nil {
return refundErr
}
} else {
refund, refundErr := l.svcCtx.AlipayService.AliRefund(ctx, order.OrderNo, order.Amount)
if refundErr != nil {
return refundErr
}
if refund.IsSuccess() {
logx.Errorf("支付宝退款成功, orderID: %d", order.Id)
// 更新订单状态为退款
order.Status = "refunded"
updateOrderErr := l.svcCtx.AgentMembershipRechargeOrderModel.UpdateWithVersion(ctx, nil, order)
if updateOrderErr != nil {
logx.Errorf("更新订单状态失败订单ID: %d, 错误: %v", order.Id, updateOrderErr)
return fmt.Errorf("更新订单状态失败: %v", updateOrderErr)
}
return nil
} else {
logx.Errorf("支付宝退款失败:%v", refundErr)
return refundErr
}
// 直接成功
}
return nil
}