fix2
This commit is contained in:
@@ -25,6 +25,14 @@ import (
|
||||
"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 (
|
||||
TradeStateSuccess = "SUCCESS" // 支付成功
|
||||
TradeStateRefund = "REFUND" // 转入退款
|
||||
@@ -174,7 +182,7 @@ func (w *WechatPayService) CreateWechatAppOrder(ctx context.Context, amount floa
|
||||
// 发起预支付请求
|
||||
resp, result, err := svc.Prepay(ctx, payRequest)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("微信支付订单创建失败: %v, 状态码: %d", err, result.Response.StatusCode)
|
||||
return "", fmt.Errorf("微信支付订单创建失败: %v, 状态码: %d", err, wxpayAPIHTTPStatus(result))
|
||||
}
|
||||
|
||||
// 返回预支付交易会话标识
|
||||
@@ -199,14 +207,23 @@ func jsapiRequestPaymentToMap(resp *jsapi.PrepayWithRequestPaymentResponse) (map
|
||||
if resp.Package != nil {
|
||||
m["package"] = *resp.Package
|
||||
}
|
||||
if resp.SignType != nil {
|
||||
if resp.SignType != nil && *resp.SignType != "" {
|
||||
m["signType"] = *resp.SignType
|
||||
} else {
|
||||
m["signType"] = "RSA"
|
||||
}
|
||||
if resp.PaySign != nil {
|
||||
m["paySign"] = *resp.PaySign
|
||||
}
|
||||
if len(m) != 6 {
|
||||
return nil, fmt.Errorf("微信 JSAPI 调起参数不完整")
|
||||
var missing []string
|
||||
for _, key := range []string{"appId", "timeStamp", "nonceStr", "package", "paySign"} {
|
||||
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
|
||||
}
|
||||
@@ -235,7 +252,7 @@ func (w *WechatPayService) CreateWechatMiniProgramOrder(ctx context.Context, amo
|
||||
// 发起预支付请求
|
||||
resp, result, err := svc.PrepayWithRequestPayment(ctx, payRequest)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("微信支付订单创建失败: %v, 状态码: %d", err, result.Response.StatusCode)
|
||||
return "", fmt.Errorf("微信支付订单创建失败: %v, 状态码: %d", err, wxpayAPIHTTPStatus(result))
|
||||
}
|
||||
return jsapiRequestPaymentToMap(resp)
|
||||
}
|
||||
@@ -265,7 +282,7 @@ func (w *WechatPayService) CreateWechatH5Order(ctx context.Context, amount float
|
||||
resp, result, err := svc.PrepayWithRequestPayment(ctx, payRequest)
|
||||
logx.Infof("微信h5支付订单:resp: %+v, result: %+v, err: %+v", resp, result, err)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("微信支付订单创建失败: %v, 状态码: %d", err, result.Response.StatusCode)
|
||||
return "", fmt.Errorf("微信支付订单创建失败: %v, 状态码: %d", err, wxpayAPIHTTPStatus(result))
|
||||
}
|
||||
return jsapiRequestPaymentToMap(resp)
|
||||
}
|
||||
@@ -363,7 +380,7 @@ func (w *WechatPayService) QueryOrderStatus(ctx context.Context, transactionID s
|
||||
Mchid: core.String(w.config.Wxpay.MchID),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("订单查询失败: %v, 状态码: %d", err, result.Response.StatusCode)
|
||||
return nil, fmt.Errorf("订单查询失败: %v, 状态码: %d", err, wxpayAPIHTTPStatus(result))
|
||||
}
|
||||
return resp, nil
|
||||
|
||||
|
||||
Reference in New Issue
Block a user