bugfix:支付
This commit is contained in:
@@ -147,12 +147,25 @@ func (h *OrderHandler) RefundNotify(c *gin.Context) {
|
||||
}
|
||||
|
||||
// XPayVerify 虚拟支付消息推送 URL 配置验证(GET):校验签名并原样回显 echostr。
|
||||
// 兼容两种模式:安全模式带 msg_signature 且 echostr 为密文(需解密后回显明文);
|
||||
// 明文模式带 signature,直接回显 echostr。
|
||||
func (h *OrderHandler) XPayVerify(c *gin.Context) {
|
||||
timestamp := c.Query("timestamp")
|
||||
nonce := c.Query("nonce")
|
||||
signature := c.Query("signature")
|
||||
echostr := c.Query("echostr")
|
||||
if wechat.VerifyNotifyURL(timestamp, nonce, signature) {
|
||||
|
||||
if msgSignature := c.Query("msg_signature"); msgSignature != "" {
|
||||
plain, err := wechat.DecodeEchoStr(echostr, timestamp, nonce, msgSignature)
|
||||
if err != nil {
|
||||
// 兼容模式:echostr 可能仍是明文(msg_signature 对明文计算),原样回显。
|
||||
c.String(http.StatusOK, echostr)
|
||||
return
|
||||
}
|
||||
c.String(http.StatusOK, plain)
|
||||
return
|
||||
}
|
||||
|
||||
if wechat.VerifyNotifyURL(timestamp, nonce, c.Query("signature")) {
|
||||
c.String(http.StatusOK, echostr)
|
||||
return
|
||||
}
|
||||
@@ -160,14 +173,46 @@ func (h *OrderHandler) XPayVerify(c *gin.Context) {
|
||||
}
|
||||
|
||||
// XPayNotify 虚拟支付消息推送回调(POST,公开,按虚拟支付规范返回 ErrCode 应答)。
|
||||
// 安全模式下包体为密文信封:先验签解密再交给业务层,回包同样加密;明文模式直接透传。
|
||||
func (h *OrderHandler) XPayNotify(c *gin.Context) {
|
||||
raw, err := io.ReadAll(c.Request.Body)
|
||||
if err != nil {
|
||||
c.Data(http.StatusOK, "application/json; charset=utf-8", wechat.BuildNotifyResponse(false))
|
||||
writeXPayNotifyReply(c, false, false)
|
||||
return
|
||||
}
|
||||
resp := h.svc.XPayNotify(c.Request.Context(), raw)
|
||||
c.Data(http.StatusOK, "application/json; charset=utf-8", resp)
|
||||
|
||||
encrypted := wechat.IsEncryptedNotify(c.Query("msg_signature"))
|
||||
body := raw
|
||||
if encrypted {
|
||||
plain, derr := wechat.DecodeEncryptedNotify(raw, c.Query("timestamp"), c.Query("nonce"), c.Query("msg_signature"))
|
||||
if derr != nil {
|
||||
// 验签/解密失败:回失败应答触发微信重试(fail-closed)。
|
||||
writeXPayNotifyReply(c, true, false)
|
||||
return
|
||||
}
|
||||
body = plain
|
||||
}
|
||||
|
||||
resp := h.svc.XPayNotify(c.Request.Context(), body)
|
||||
c.Data(http.StatusOK, "application/json; charset=utf-8", encryptIfNeeded(c, resp, encrypted))
|
||||
}
|
||||
|
||||
// writeXPayNotifyReply 按模式构造应答:加密模式下加密,否则回明文。
|
||||
func writeXPayNotifyReply(c *gin.Context, encrypted, success bool) {
|
||||
resp := wechat.BuildNotifyResponse(success)
|
||||
c.Data(http.StatusOK, "application/json; charset=utf-8", encryptIfNeeded(c, resp, encrypted))
|
||||
}
|
||||
|
||||
// encryptIfNeeded 安全模式下把明文应答加密为信封;加密不可用时退回明文(仍返回 200)。
|
||||
func encryptIfNeeded(c *gin.Context, plain []byte, encrypted bool) []byte {
|
||||
if !encrypted {
|
||||
return plain
|
||||
}
|
||||
out, err := wechat.EncodeEncryptedNotify(plain, c.Query("nonce"))
|
||||
if err != nil {
|
||||
return plain
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// wechatCallbackSuccess 按微信规范返回受理成功,避免微信重复回调。
|
||||
|
||||
Reference in New Issue
Block a user