f
This commit is contained in:
@@ -33,6 +33,7 @@ type PaymentTypeResp struct {
|
||||
outTradeNo string
|
||||
description string
|
||||
orderID string // 订单ID,用于开发环境测试支付模式
|
||||
productEn string // 产品英文名,xpay 道具 ID
|
||||
}
|
||||
|
||||
// enableDevTestPayment 测试支付功能开关,设置为 true 以启用测试支付功能
|
||||
@@ -50,6 +51,7 @@ func (l *PaymentLogic) Payment(req *types.PaymentReq) (resp *types.PaymentResp,
|
||||
var paymentTypeResp *PaymentTypeResp
|
||||
var prepayData interface{}
|
||||
var orderID string
|
||||
useXpay := l.shouldUseXpay(req)
|
||||
|
||||
// 检查是否为开发环境的测试支付模式
|
||||
env := os.Getenv("ENV")
|
||||
@@ -95,9 +97,11 @@ func (l *PaymentLogic) Payment(req *types.PaymentReq) (resp *types.PaymentResp,
|
||||
return nil
|
||||
}
|
||||
|
||||
// 正常支付流程
|
||||
// 正常支付流程(微信小程序 xpay 在事务外构造支付参数)
|
||||
var createOrderErr error
|
||||
if req.PayMethod == "wechat" {
|
||||
if useXpay {
|
||||
// 订单已在 QueryOrderPayment 中创建,此处跳过 JSAPI 预下单
|
||||
} else if req.PayMethod == "wechat" {
|
||||
prepayData, createOrderErr = l.svcCtx.WechatPayService.CreateWechatOrder(l.ctx, paymentTypeResp.amount, paymentTypeResp.description, paymentTypeResp.outTradeNo, req.Code)
|
||||
} else if req.PayMethod == "alipay" {
|
||||
prepayData, createOrderErr = l.svcCtx.AlipayService.CreateAlipayOrder(l.ctx, paymentTypeResp.amount, paymentTypeResp.description, paymentTypeResp.outTradeNo)
|
||||
@@ -113,6 +117,22 @@ func (l *PaymentLogic) Payment(req *types.PaymentReq) (resp *types.PaymentResp,
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 微信小程序虚拟支付:构造 signData / paySig / signature
|
||||
if useXpay && paymentTypeResp != nil {
|
||||
userID, uidErr := ctxdata.GetUidFromCtx(l.ctx)
|
||||
if uidErr != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取用户信息失败")
|
||||
}
|
||||
xpayParams, buildErr := l.svcCtx.XpayService.BuildPayParams(l.ctx, userID, paymentTypeResp.outTradeNo, paymentTypeResp.productEn, paymentTypeResp.amount)
|
||||
if buildErr != nil {
|
||||
if strings.Contains(buildErr.Error(), "过期") || strings.Contains(buildErr.Error(), "会话") {
|
||||
return nil, errors.Wrapf(xerr.NewErrCodeMsg(xerr.PARAM_VERIFICATION_ERROR, buildErr.Error()), "")
|
||||
}
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "构造虚拟支付参数失败: %v", buildErr)
|
||||
}
|
||||
prepayData = xpayParams
|
||||
}
|
||||
|
||||
// 开发环境测试支付模式:事务提交后处理订单状态更新和后续流程
|
||||
if isDevTestPayment && paymentTypeResp != nil && paymentTypeResp.orderID != "" {
|
||||
// 使用 goroutine 异步处理,确保事务已完全提交
|
||||
@@ -257,7 +277,7 @@ func (l *PaymentLogic) QueryOrderPayment(req *types.PaymentReq, session sqlx.Ses
|
||||
UserId: userID,
|
||||
ProductId: product.Id,
|
||||
PaymentPlatform: req.PayMethod,
|
||||
PaymentScene: "app",
|
||||
PaymentScene: resolvePaymentScene(l.ctx),
|
||||
Amount: amount,
|
||||
Status: "pending",
|
||||
}
|
||||
@@ -330,7 +350,44 @@ func (l *PaymentLogic) QueryOrderPayment(req *types.PaymentReq, session sqlx.Ses
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "生成订单, 保存代理订单失败: %+v", agentOrderInsert)
|
||||
}
|
||||
}
|
||||
return &PaymentTypeResp{amount: amount, outTradeNo: outTradeNo, description: product.ProductName, orderID: orderID}, nil
|
||||
return &PaymentTypeResp{
|
||||
amount: amount,
|
||||
outTradeNo: outTradeNo,
|
||||
description: product.ProductName,
|
||||
orderID: orderID,
|
||||
productEn: product.ProductEn,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (l *PaymentLogic) shouldUseXpay(req *types.PaymentReq) bool {
|
||||
if req.PayMethod != "wechat" || req.PayType != "query" {
|
||||
return false
|
||||
}
|
||||
if l.svcCtx.XpayService == nil || !l.svcCtx.XpayService.Enabled() {
|
||||
return false
|
||||
}
|
||||
platform, err := ctxdata.GetPlatformFromCtx(l.ctx)
|
||||
if err != nil || platform != model.PlatformWxMini {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func resolvePaymentScene(ctx context.Context) string {
|
||||
platform, err := ctxdata.GetPlatformFromCtx(ctx)
|
||||
if err != nil {
|
||||
return "app"
|
||||
}
|
||||
switch platform {
|
||||
case model.PlatformWxMini:
|
||||
return "wxmini"
|
||||
case model.PlatformWxH5:
|
||||
return "wxh5"
|
||||
case model.PlatformH5:
|
||||
return "h5"
|
||||
default:
|
||||
return "app"
|
||||
}
|
||||
}
|
||||
|
||||
// AgentVipOrderPayment 代理会员充值订单(已废弃,新系统使用升级功能替代)
|
||||
|
||||
Reference in New Issue
Block a user