f
This commit is contained in:
@@ -25,14 +25,6 @@ import (
|
|||||||
"github.com/zeromicro/go-zero/core/logx"
|
"github.com/zeromicro/go-zero/core/logx"
|
||||||
)
|
)
|
||||||
|
|
||||||
// wxpayAPIHTTPStatus 安全读取微信支付 API 的 HTTP 状态码;部分错误路径下 result 可能为 nil(例如本地签名失败)。
|
|
||||||
func wxpayAPIHTTPStatus(result *core.APIResult) int {
|
|
||||||
if result == nil || result.Response == nil {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
return result.Response.StatusCode
|
|
||||||
}
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
TradeStateSuccess = "SUCCESS" // 支付成功
|
TradeStateSuccess = "SUCCESS" // 支付成功
|
||||||
TradeStateRefund = "REFUND" // 转入退款
|
TradeStateRefund = "REFUND" // 转入退款
|
||||||
@@ -182,7 +174,7 @@ func (w *WechatPayService) CreateWechatAppOrder(ctx context.Context, amount floa
|
|||||||
// 发起预支付请求
|
// 发起预支付请求
|
||||||
resp, result, err := svc.Prepay(ctx, payRequest)
|
resp, result, err := svc.Prepay(ctx, payRequest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("微信支付订单创建失败: %v, 状态码: %d", err, wxpayAPIHTTPStatus(result))
|
return "", fmt.Errorf("微信支付订单创建失败: %v, 状态码: %d", err, result.Response.StatusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 返回预支付交易会话标识
|
// 返回预支付交易会话标识
|
||||||
@@ -207,23 +199,14 @@ func jsapiRequestPaymentToMap(resp *jsapi.PrepayWithRequestPaymentResponse) (map
|
|||||||
if resp.Package != nil {
|
if resp.Package != nil {
|
||||||
m["package"] = *resp.Package
|
m["package"] = *resp.Package
|
||||||
}
|
}
|
||||||
if resp.SignType != nil && *resp.SignType != "" {
|
if resp.SignType != nil {
|
||||||
m["signType"] = *resp.SignType
|
m["signType"] = *resp.SignType
|
||||||
} else {
|
|
||||||
m["signType"] = "RSA"
|
|
||||||
}
|
}
|
||||||
if resp.PaySign != nil {
|
if resp.PaySign != nil {
|
||||||
m["paySign"] = *resp.PaySign
|
m["paySign"] = *resp.PaySign
|
||||||
}
|
}
|
||||||
var missing []string
|
if len(m) != 6 {
|
||||||
for _, key := range []string{"appId", "timeStamp", "nonceStr", "package", "paySign"} {
|
return nil, fmt.Errorf("微信 JSAPI 调起参数不完整")
|
||||||
if m[key] == "" {
|
|
||||||
missing = append(missing, key)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(missing) > 0 {
|
|
||||||
logx.Errorf("[WechatPay] JSAPI 调起参数缺项: missing=%v resp=%s", missing, resp.String())
|
|
||||||
return nil, fmt.Errorf("微信 JSAPI 调起参数不完整: 缺少或为空 %v", missing)
|
|
||||||
}
|
}
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
@@ -252,7 +235,7 @@ func (w *WechatPayService) CreateWechatMiniProgramOrder(ctx context.Context, amo
|
|||||||
// 发起预支付请求
|
// 发起预支付请求
|
||||||
resp, result, err := svc.PrepayWithRequestPayment(ctx, payRequest)
|
resp, result, err := svc.PrepayWithRequestPayment(ctx, payRequest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("微信支付订单创建失败: %v, 状态码: %d", err, wxpayAPIHTTPStatus(result))
|
return "", fmt.Errorf("微信支付订单创建失败: %v, 状态码: %d", err, result.Response.StatusCode)
|
||||||
}
|
}
|
||||||
return jsapiRequestPaymentToMap(resp)
|
return jsapiRequestPaymentToMap(resp)
|
||||||
}
|
}
|
||||||
@@ -282,7 +265,7 @@ func (w *WechatPayService) CreateWechatH5Order(ctx context.Context, amount float
|
|||||||
resp, result, err := svc.PrepayWithRequestPayment(ctx, payRequest)
|
resp, result, err := svc.PrepayWithRequestPayment(ctx, payRequest)
|
||||||
logx.Infof("微信h5支付订单:resp: %+v, result: %+v, err: %+v", resp, result, err)
|
logx.Infof("微信h5支付订单:resp: %+v, result: %+v, err: %+v", resp, result, err)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("微信支付订单创建失败: %v, 状态码: %d", err, wxpayAPIHTTPStatus(result))
|
return "", fmt.Errorf("微信支付订单创建失败: %v, 状态码: %d", err, result.Response.StatusCode)
|
||||||
}
|
}
|
||||||
return jsapiRequestPaymentToMap(resp)
|
return jsapiRequestPaymentToMap(resp)
|
||||||
}
|
}
|
||||||
@@ -291,14 +274,17 @@ func (w *WechatPayService) CreateWechatH5Order(ctx context.Context, amount float
|
|||||||
func (w *WechatPayService) CreateWechatOrder(ctx context.Context, amount float64, description string, outTradeNo string) (interface{}, error) {
|
func (w *WechatPayService) CreateWechatOrder(ctx context.Context, amount float64, description string, outTradeNo string) (interface{}, error) {
|
||||||
platformVal := ctx.Value("platform")
|
platformVal := ctx.Value("platform")
|
||||||
platform, ok := platformVal.(string)
|
platform, ok := platformVal.(string)
|
||||||
|
rawPlatform := platform
|
||||||
platform = strings.TrimSpace(platform)
|
platform = strings.TrimSpace(platform)
|
||||||
|
logx.WithContext(ctx).Infof(
|
||||||
|
"[WechatPay] CreateWechatOrder platform ctx: value_type=%T assert_ok=%v raw=%q trimmed=%q ref_wxh5=%q ref_wxmini=%q ref_app=%q out_trade_no=%s",
|
||||||
|
platformVal, ok, rawPlatform, platform, model.PlatformWxH5, model.PlatformWxMini, model.PlatformApp, outTradeNo,
|
||||||
|
)
|
||||||
if !ok || platform == "" {
|
if !ok || platform == "" {
|
||||||
logx.WithContext(ctx).Errorf("[WechatPay] CreateWechatOrder 缺少 X-Platform")
|
logx.WithContext(ctx).Errorf("[WechatPay] CreateWechatOrder 缺少 X-Platform")
|
||||||
return "", fmt.Errorf("缺少 X-Platform 请求头(微信内请传 wxh5)")
|
return "", fmt.Errorf("缺少 X-Platform 请求头(微信内请传 wxh5)")
|
||||||
}
|
}
|
||||||
|
|
||||||
logx.WithContext(ctx).Infof("[WechatPay] CreateWechatOrder platform=%q out_trade_no=%s", platform, outTradeNo)
|
|
||||||
|
|
||||||
var prepayData interface{}
|
var prepayData interface{}
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
@@ -317,6 +303,10 @@ func (w *WechatPayService) CreateWechatOrder(ctx context.Context, amount float64
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
case model.PlatformWxH5:
|
case model.PlatformWxH5:
|
||||||
|
logx.WithContext(ctx).Infof(
|
||||||
|
"[WechatPay] CreateWechatOrder branch=wxh5 out_trade_no=%s amount=%.2f desc_len=%d",
|
||||||
|
outTradeNo, amount, len(description),
|
||||||
|
)
|
||||||
userID, getUidErr := ctxdata.GetUidFromCtx(ctx)
|
userID, getUidErr := ctxdata.GetUidFromCtx(ctx)
|
||||||
if getUidErr != nil {
|
if getUidErr != nil {
|
||||||
return "", getUidErr
|
return "", getUidErr
|
||||||
@@ -343,6 +333,14 @@ func (w *WechatPayService) CreateWechatOrder(ctx context.Context, amount float64
|
|||||||
|
|
||||||
if prepayData == nil {
|
if prepayData == nil {
|
||||||
logx.WithContext(ctx).Errorf("[WechatPay] CreateWechatOrder 返回 prepayData 为 nil platform=%q", platform)
|
logx.WithContext(ctx).Errorf("[WechatPay] CreateWechatOrder 返回 prepayData 为 nil platform=%q", platform)
|
||||||
|
} else if m, isMap := prepayData.(map[string]string); isMap {
|
||||||
|
keys := make([]string, 0, len(m))
|
||||||
|
for k := range m {
|
||||||
|
keys = append(keys, k)
|
||||||
|
}
|
||||||
|
logx.WithContext(ctx).Infof("[WechatPay] CreateWechatOrder return prepay: platform=%q type=map[string]string len=%d keys=%v", platform, len(m), keys)
|
||||||
|
} else {
|
||||||
|
logx.WithContext(ctx).Infof("[WechatPay] CreateWechatOrder return prepay: platform=%q type=%T", platform, prepayData)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 返回预支付ID
|
// 返回预支付ID
|
||||||
@@ -380,7 +378,7 @@ func (w *WechatPayService) QueryOrderStatus(ctx context.Context, transactionID s
|
|||||||
Mchid: core.String(w.config.Wxpay.MchID),
|
Mchid: core.String(w.config.Wxpay.MchID),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("订单查询失败: %v, 状态码: %d", err, wxpayAPIHTTPStatus(result))
|
return nil, fmt.Errorf("订单查询失败: %v, 状态码: %d", err, result.Response.StatusCode)
|
||||||
}
|
}
|
||||||
return resp, nil
|
return resp, nil
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user