This commit is contained in:
Mrx
2026-05-09 18:02:51 +08:00
parent 5d51b7a400
commit d68184fa59
4 changed files with 53 additions and 9 deletions

View File

@@ -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