From d68184fa592534ff40462435d10c0b71d3f03027 Mon Sep 17 00:00:00 2001 From: Mrx <18278715334@163.com> Date: Sat, 9 May 2026 18:02:51 +0800 Subject: [PATCH] f --- app/main/api/desc/front/pay.api | 2 +- .../api/internal/logic/pay/paymentlogic.go | 15 ++++++- .../api/internal/service/wechatpayService.go | 43 ++++++++++++++++--- app/main/api/internal/types/types.go | 2 +- 4 files changed, 53 insertions(+), 9 deletions(-) diff --git a/app/main/api/desc/front/pay.api b/app/main/api/desc/front/pay.api index b322c46..2c07b93 100644 --- a/app/main/api/desc/front/pay.api +++ b/app/main/api/desc/front/pay.api @@ -46,7 +46,7 @@ service main { type ( PaymentReq { Id string `json:"id"` - PayMethod string `json:"pay_method"` + PayMethod string `json:"pay_method" validate:"required,oneof=wechat alipay appleiap test"` PayType string `json:"pay_type" validate:"required,oneof=query agent_vip"` } PaymentResp { diff --git a/app/main/api/internal/logic/pay/paymentlogic.go b/app/main/api/internal/logic/pay/paymentlogic.go index c93369c..24ed7ab 100644 --- a/app/main/api/internal/logic/pay/paymentlogic.go +++ b/app/main/api/internal/logic/pay/paymentlogic.go @@ -78,7 +78,14 @@ func (l *PaymentLogic) Payment(req *types.PaymentReq) (resp *types.PaymentResp, // 支付宝:按订单写入的商户标识(one/two)创建支付订单 prepayData, createOrderErr = l.svcCtx.AlipayService.CreateAlipayOrder(l.ctx, paymentTypeResp.payMerchantID, paymentTypeResp.amount, paymentTypeResp.description, paymentTypeResp.outTradeNo) } else if req.PayMethod == "appleiap" { - prepayData = l.svcCtx.ApplePayService.GetIappayAppID(paymentTypeResp.outTradeNo) + iap := l.svcCtx.ApplePayService.GetIappayAppID(paymentTypeResp.outTradeNo) + prepayData = iap + if iap == "" { + createOrderErr = fmt.Errorf("获取 IAP 参数失败") + } + } else if req.PayMethod == "test" { + // 开发环境测试支付已在上方分支写入 prepayData;若走到此处说明非 development + return errors.Wrapf(xerr.NewErrMsg("测试支付仅在开发环境可用"), "ENV!=development 且 pay_method=test") } if createOrderErr != nil { return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成订单, 创建支付订单失败: %+v", createOrderErr) @@ -89,6 +96,12 @@ func (l *PaymentLogic) Payment(req *types.PaymentReq) (resp *types.PaymentResp, return nil, err } + if req.PayMethod == "wechat" || req.PayMethod == "alipay" { + if prepayData == nil { + return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "创建支付失败: 未生成支付参数") + } + } + // 开发环境测试支付模式:事务提交后处理订单状态更新和后续流程(仅 pay_method=test 且 query 类型 orderID>0) isDevTestPayment := os.Getenv("ENV") == "development" && req.PayMethod == "test" if isDevTestPayment && paymentTypeResp != nil && paymentTypeResp.orderID != 0 { diff --git a/app/main/api/internal/service/wechatpayService.go b/app/main/api/internal/service/wechatpayService.go index e353793..935e969 100644 --- a/app/main/api/internal/service/wechatpayService.go +++ b/app/main/api/internal/service/wechatpayService.go @@ -180,6 +180,36 @@ func (w *WechatPayService) CreateWechatAppOrder(ctx context.Context, amount floa return *resp.PrepayId, nil } +// jsapiRequestPaymentToMap 将 JSAPI 调起参数转为 map[string]string,便于 JSON 序列化给前端 WeixinJSBridge +func jsapiRequestPaymentToMap(resp *jsapi.PrepayWithRequestPaymentResponse) (map[string]string, error) { + if resp == nil { + return nil, fmt.Errorf("微信下单返回为空") + } + m := map[string]string{} + if resp.Appid != nil { + m["appId"] = *resp.Appid + } + if resp.TimeStamp != nil { + m["timeStamp"] = *resp.TimeStamp + } + if resp.NonceStr != nil { + m["nonceStr"] = *resp.NonceStr + } + if resp.Package != nil { + m["package"] = *resp.Package + } + if resp.SignType != nil { + m["signType"] = *resp.SignType + } + if resp.PaySign != nil { + m["paySign"] = *resp.PaySign + } + if len(m) != 6 { + return nil, fmt.Errorf("微信 JSAPI 调起参数不完整") + } + return m, nil +} + // CreateWechatMiniProgramOrder 创建微信小程序支付订单 func (w *WechatPayService) CreateWechatMiniProgramOrder(ctx context.Context, amount float64, description string, outTradeNo string, openid string) (interface{}, error) { totalAmount := lzUtils.ToWechatAmount(amount) @@ -206,8 +236,7 @@ func (w *WechatPayService) CreateWechatMiniProgramOrder(ctx context.Context, amo if err != nil { return "", fmt.Errorf("微信支付订单创建失败: %v, 状态码: %d", err, result.Response.StatusCode) } - // 返回预支付交易会话标识 - return resp, nil + return jsapiRequestPaymentToMap(resp) } // CreateWechatH5Order 创建微信H5支付订单 @@ -237,14 +266,16 @@ func (w *WechatPayService) CreateWechatH5Order(ctx context.Context, amount float if err != nil { return "", fmt.Errorf("微信支付订单创建失败: %v, 状态码: %d", err, result.Response.StatusCode) } - // 返回预支付交易会话标识 - return resp, nil + return jsapiRequestPaymentToMap(resp) } // CreateWechatOrder 创建微信支付订单(集成 APP、H5、小程序) func (w *WechatPayService) CreateWechatOrder(ctx context.Context, amount float64, description string, outTradeNo string) (interface{}, error) { - // 根据 ctx 中的 platform 判断平台 - platform := ctx.Value("platform").(string) + platformVal := ctx.Value("platform") + platform, ok := platformVal.(string) + if !ok || platform == "" { + return "", fmt.Errorf("缺少 X-Platform 请求头(微信内请传 wxh5)") + } var prepayData interface{} var err error diff --git a/app/main/api/internal/types/types.go b/app/main/api/internal/types/types.go index 2ae9578..0023f30 100644 --- a/app/main/api/internal/types/types.go +++ b/app/main/api/internal/types/types.go @@ -1851,7 +1851,7 @@ type PaymentCheckResp struct { type PaymentReq struct { Id string `json:"id"` - PayMethod string `json:"pay_method"` + PayMethod string `json:"pay_method" validate:"required,oneof=wechat alipay appleiap test"` PayType string `json:"pay_type" validate:"required,oneof=query agent_vip"` }