fix
This commit is contained in:
122
app/main/api/internal/logic/pay/easypaycallbacklogic.go
Normal file
122
app/main/api/internal/logic/pay/easypaycallbacklogic.go
Normal file
@@ -0,0 +1,122 @@
|
||||
package pay
|
||||
|
||||
import (
|
||||
"context"
|
||||
"jnc-server/app/main/api/internal/service"
|
||||
"jnc-server/pkg/lzkit/lzUtils"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"jnc-server/app/main/api/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type EasyPayCallbackLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewEasyPayCallbackLogic(ctx context.Context, svcCtx *svc.ServiceContext) *EasyPayCallbackLogic {
|
||||
return &EasyPayCallbackLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *EasyPayCallbackLogic) EasyPayCallback(w http.ResponseWriter, r *http.Request) error {
|
||||
// 检查易支付服务是否启用
|
||||
if l.svcCtx.EasyPayService == nil {
|
||||
logx.Errorf("易支付服务未启用")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return nil
|
||||
}
|
||||
|
||||
notification, err := l.svcCtx.EasyPayService.HandleEasyPayNotification(r)
|
||||
if err != nil {
|
||||
logx.Errorf("易支付回调处理失败: %v", err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return nil
|
||||
}
|
||||
|
||||
// 根据订单号前缀判断订单类型
|
||||
orderNo := notification.OutTradeNo
|
||||
if strings.HasPrefix(orderNo, "Q_") {
|
||||
// 查询订单处理
|
||||
return l.handleQueryOrderPayment(w, notification)
|
||||
} else if strings.HasPrefix(orderNo, "A_") {
|
||||
// 旧系统会员充值订单(已废弃,新系统使用升级功能)
|
||||
// 直接返回成功,避免旧订单影响
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("success"))
|
||||
return nil
|
||||
} else if strings.HasPrefix(orderNo, "U_") {
|
||||
// 系统简化:移除升级功能,直接返回成功
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("success"))
|
||||
return nil
|
||||
} else {
|
||||
// 兼容旧订单,假设没有前缀的是查询订单
|
||||
return l.handleQueryOrderPayment(w, notification)
|
||||
}
|
||||
}
|
||||
|
||||
// 处理查询订单支付
|
||||
func (l *EasyPayCallbackLogic) handleQueryOrderPayment(w http.ResponseWriter, notification *service.EasyPayNotification) error {
|
||||
order, findOrderErr := l.svcCtx.OrderModel.FindOneByOrderNo(l.ctx, notification.OutTradeNo)
|
||||
if findOrderErr != nil {
|
||||
logx.Errorf("易支付回调,查找订单失败: %+v", findOrderErr)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("success"))
|
||||
return nil
|
||||
}
|
||||
|
||||
if order.Status != "pending" {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("success"))
|
||||
return nil
|
||||
}
|
||||
|
||||
// 验证支付状态
|
||||
if !l.svcCtx.EasyPayService.IsPaymentSuccess(notification) {
|
||||
logx.Infof("易支付回调,订单未支付成功,订单号: %s, 状态: %s", notification.OutTradeNo, notification.TradeStatus)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("success"))
|
||||
return nil
|
||||
}
|
||||
|
||||
// 验证金额(转换为字符串比较)
|
||||
amountStr := lzUtils.ToAlipayAmount(order.Amount)
|
||||
if amountStr != notification.Money {
|
||||
logx.Errorf("易支付回调,金额不一致,订单号: %s, 订单金额: %s, 回调金额: %s", notification.OutTradeNo, amountStr, notification.Money)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("success"))
|
||||
return nil
|
||||
}
|
||||
|
||||
// 更新订单状态
|
||||
order.Status = "paid"
|
||||
order.PayTime = lzUtils.TimeToNullTime(time.Now())
|
||||
order.PlatformOrderId = lzUtils.StringToNullString(notification.TradeNo)
|
||||
|
||||
if updateErr := l.svcCtx.OrderModel.UpdateWithVersion(l.ctx, nil, order); updateErr != nil {
|
||||
logx.Errorf("易支付回调,修改订单信息失败: %+v", updateErr)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("success"))
|
||||
return nil
|
||||
}
|
||||
|
||||
// 发送异步任务处理后续流程
|
||||
if asyncErr := l.svcCtx.AsynqService.SendQueryTask(order.Id); asyncErr != nil {
|
||||
logx.Errorf("异步任务调度失败: %v", asyncErr)
|
||||
// 不返回错误,因为订单已经更新成功
|
||||
}
|
||||
|
||||
// 返回success,易支付要求返回纯字符串success
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("success"))
|
||||
return nil
|
||||
}
|
||||
@@ -2,10 +2,12 @@ package pay
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"jnc-server/app/main/api/internal/svc"
|
||||
"jnc-server/app/main/api/internal/types"
|
||||
"jnc-server/common/xerr"
|
||||
"jnc-server/pkg/lzkit/lzUtils"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
@@ -38,12 +40,61 @@ func (l *PaymentCheckLogic) PaymentCheck(req *types.PaymentCheckReq) (resp *type
|
||||
Status: order.Status,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
// 查询订单(包括代理订单)
|
||||
order, err := l.svcCtx.OrderModel.FindOneByOrderNo(l.ctx, req.OrderNo)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询订单失败: %v", err)
|
||||
}
|
||||
|
||||
// 如果订单状态是 pending 且支付平台是易支付,主动查询易支付订单状态
|
||||
if order.Status == "pending" && order.PaymentPlatform == "easypay_alipay" {
|
||||
// 检查易支付服务是否启用
|
||||
if l.svcCtx.EasyPayService != nil {
|
||||
// 主动查询易支付订单状态
|
||||
queryResp, queryErr := l.svcCtx.EasyPayService.QueryOrderStatus(l.ctx, req.OrderNo)
|
||||
if queryErr != nil {
|
||||
logx.Errorf("主动查询易支付订单状态失败,订单号: %s, 错误: %v", req.OrderNo, queryErr)
|
||||
// 查询失败不影响返回,继续返回当前订单状态
|
||||
} else {
|
||||
// 如果易支付返回订单已支付(status == 1),更新本地订单状态
|
||||
if queryResp.Status == 1 {
|
||||
logx.Infof("主动查询发现易支付订单已支付,订单号: %s,开始更新订单状态", req.OrderNo)
|
||||
|
||||
// 重新查询订单(获取最新版本号)
|
||||
order, err = l.svcCtx.OrderModel.FindOneByOrderNo(l.ctx, req.OrderNo)
|
||||
if err != nil {
|
||||
logx.Errorf("更新订单状态前重新查询订单失败: %v", err)
|
||||
} else if order.Status == "pending" {
|
||||
// 更新订单状态
|
||||
order.Status = "paid"
|
||||
order.PayTime = lzUtils.TimeToNullTime(time.Now())
|
||||
if queryResp.TradeNo != "" {
|
||||
order.PlatformOrderId = lzUtils.StringToNullString(queryResp.TradeNo)
|
||||
}
|
||||
|
||||
if updateErr := l.svcCtx.OrderModel.UpdateWithVersion(l.ctx, nil, order); updateErr != nil {
|
||||
logx.Errorf("主动查询后更新订单状态失败: %v", updateErr)
|
||||
} else {
|
||||
logx.Infof("主动查询后成功更新订单状态为已支付,订单号: %s", req.OrderNo)
|
||||
|
||||
// 发送异步任务处理后续流程
|
||||
if asyncErr := l.svcCtx.AsynqService.SendQueryTask(order.Id); asyncErr != nil {
|
||||
logx.Errorf("主动查询后发送异步任务失败: %v", asyncErr)
|
||||
}
|
||||
|
||||
// 返回更新后的状态
|
||||
return &types.PaymentCheckResp{
|
||||
Type: "query",
|
||||
Status: "paid",
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return &types.PaymentCheckResp{
|
||||
Type: "query",
|
||||
Status: order.Status,
|
||||
|
||||
@@ -5,14 +5,14 @@ import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
"jnc-server/app/main/api/internal/svc"
|
||||
"jnc-server/app/main/api/internal/types"
|
||||
"jnc-server/app/main/model"
|
||||
"jnc-server/common/ctxdata"
|
||||
"jnc-server/common/xerr"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/pkg/errors"
|
||||
@@ -92,6 +92,11 @@ func (l *PaymentLogic) Payment(req *types.PaymentReq) (resp *types.PaymentResp,
|
||||
prepayData, createOrderErr = l.svcCtx.WechatPayService.CreateWechatOrder(l.ctx, paymentTypeResp.amount, paymentTypeResp.description, paymentTypeResp.outTradeNo)
|
||||
} else if req.PayMethod == "alipay" {
|
||||
prepayData, createOrderErr = l.svcCtx.AlipayService.CreateAlipayOrder(l.ctx, paymentTypeResp.amount, paymentTypeResp.description, paymentTypeResp.outTradeNo)
|
||||
} else if req.PayMethod == "easypay_alipay" {
|
||||
if l.svcCtx.EasyPayService == nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "易支付服务未启用")
|
||||
}
|
||||
prepayData, createOrderErr = l.svcCtx.EasyPayService.CreateEasyPayOrder(l.ctx, paymentTypeResp.amount, paymentTypeResp.description, paymentTypeResp.outTradeNo)
|
||||
} else if req.PayMethod == "appleiap" {
|
||||
prepayData = l.svcCtx.ApplePayService.GetIappayAppID(paymentTypeResp.outTradeNo)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user