first commit
This commit is contained in:
		
							
								
								
									
										74
									
								
								app/user/cmd/api/internal/logic/pay/alipaycallbacklogic.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										74
									
								
								app/user/cmd/api/internal/logic/pay/alipaycallbacklogic.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,74 @@ | ||||
| package pay | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"github.com/smartwalle/alipay/v3" | ||||
| 	"net/http" | ||||
| 	"time" | ||||
| 	"tydata-server/pkg/lzkit/lzUtils" | ||||
|  | ||||
| 	"github.com/zeromicro/go-zero/core/logx" | ||||
| 	"tydata-server/app/user/cmd/api/internal/svc" | ||||
| ) | ||||
|  | ||||
| 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 { | ||||
| 		logx.Errorf("支付宝支付回调,%+v", err) | ||||
| 		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 | ||||
| 	} | ||||
| 	amount := lzUtils.ToAlipayAmount(order.Amount) | ||||
| 	// 确保订单金额和状态正确,防止重复更新 | ||||
| 	if amount != notification.TotalAmount { | ||||
| 		logx.Errorf("支付宝支付回调,金额不一致") | ||||
| 		return nil | ||||
| 	} | ||||
| 	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 | ||||
| 	} | ||||
| 	order.PlatformOrderId = lzUtils.StringToNullString(notification.OutTradeNo) | ||||
| 	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 | ||||
| } | ||||
		Reference in New Issue
	
	Block a user