2025-01-10 00:09:25 +08:00
|
|
|
package pay
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
2025-03-07 03:48:59 +08:00
|
|
|
"os"
|
2025-04-11 13:10:17 +08:00
|
|
|
"qnc-server/pkg/lzkit/lzUtils"
|
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-08 14:50:24 +08:00
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
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 {
|
2025-03-07 03:48:59 +08:00
|
|
|
env := os.Getenv("ENV")
|
|
|
|
if env == "development" {
|
|
|
|
return nil
|
|
|
|
}
|
2025-01-10 00:09:25 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
order, findOrderErr := l.svcCtx.OrderModel.FindOneByOrderNo(l.ctx, notification.OutTradeNo)
|
|
|
|
if findOrderErr != nil {
|
|
|
|
logx.Errorf("支付宝支付回调,查找订单失败: %+v", findOrderErr)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if order.Status != "pending" {
|
|
|
|
alipay.ACKNotification(w)
|
|
|
|
return nil
|
|
|
|
}
|
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
|
|
|
amount := lzUtils.ToAlipayAmount(order.Amount)
|
|
|
|
|
|
|
|
if user.Inside != 1 {
|
|
|
|
// 确保订单金额和状态正确,防止重复更新
|
|
|
|
if amount != notification.TotalAmount {
|
|
|
|
logx.Errorf("支付宝支付回调,金额不一致")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-10 00:09:25 +08:00
|
|
|
if order.Status != "pending" {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
_, _ = w.Write([]byte("success")) // 确保只写入一次响应
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
switch notification.TradeStatus {
|
|
|
|
case alipay.TradeStatusSuccess:
|
|
|
|
order.Status = "paid"
|
|
|
|
order.PayTime = lzUtils.TimeToNullTime(time.Now())
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
if order.Status == "paid" {
|
|
|
|
if asyncErr := l.svcCtx.AsynqService.SendQueryTask(order.Id); asyncErr != nil {
|
|
|
|
logx.Errorf("异步任务调度失败: %v", asyncErr)
|
|
|
|
return asyncErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
alipay.ACKNotification(w)
|
|
|
|
return nil
|
|
|
|
}
|