1
0
mirror of https://github.com/silenceper/wechat.git synced 2026-02-04 12:52:27 +08:00

fix: 微信支付退款请求参数签名类型不可选 (#383)

* fix: 微信支付退款请求参数签名类型不可选

* fix: 修复微信退款请求参数问题,out_trade_no OR transaction_id 不可选

* fix: 误删NotifyURL

* fix: 误删SignType

* fix: 修复 golangci-lint 失败

* refactor: 增加GetSignParam方法

* chore: 调整 go.mod

* refactor: 调整参数
This commit is contained in:
Alfred
2021-03-10 17:26:01 +08:00
committed by GitHub
parent 7ca0317d84
commit 813684e555
3 changed files with 71 additions and 1112 deletions

12
go.mod
View File

@@ -5,10 +5,14 @@ go 1.14
require ( require (
github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b
github.com/fatih/structs v1.1.0 github.com/fatih/structs v1.1.0
github.com/gomodule/redigo v1.8.1 github.com/gomodule/redigo v2.0.0+incompatible
github.com/sirupsen/logrus v1.6.0 github.com/kr/text v0.2.0 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cast v1.3.1 github.com/spf13/cast v1.3.1
github.com/stretchr/testify v1.5.1 github.com/stretchr/testify v1.7.0
golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37 golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83
golang.org/x/sys v0.0.0-20210309074719-68d13333faf2 // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
gopkg.in/h2non/gock.v1 v1.0.15 gopkg.in/h2non/gock.v1 v1.0.15
) )

1081
go.sum

File diff suppressed because it is too large Load Diff

View File

@@ -21,19 +21,20 @@ func NewRefund(cfg *config.Config) *Refund {
return &refund return &refund
} }
//Params 调用参数 // Params 调用参数
type Params struct { type Params struct {
TransactionID string TransactionID string
OutTradeNo string
OutRefundNo string OutRefundNo string
OutTradeNo string
TotalFee string TotalFee string
RefundFee string RefundFee string
RefundDesc string RefundDesc string
RootCa string //ca证书 RootCa string // ca证书
NotifyURL string NotifyURL string
SignType string
} }
//request 接口请求参数 // request 接口请求参数
type request struct { type request struct {
AppID string `xml:"appid"` AppID string `xml:"appid"`
MchID string `xml:"mch_id"` MchID string `xml:"mch_id"`
@@ -49,7 +50,7 @@ type request struct {
NotifyURL string `xml:"notify_url,omitempty"` NotifyURL string `xml:"notify_url,omitempty"`
} }
//Response 接口返回 // Response 接口返回
type Response struct { type Response struct {
ReturnCode string `xml:"return_code"` ReturnCode string `xml:"return_code"`
ReturnMsg string `xml:"return_msg"` ReturnMsg string `xml:"return_msg"`
@@ -73,27 +74,9 @@ type Response struct {
CashFeeType string `xml:"cash_fee_type,omitempty"` CashFeeType string `xml:"cash_fee_type,omitempty"`
} }
//Refund 退款申请 // Refund 退款申请
func (refund *Refund) Refund(p *Params) (rsp Response, err error) { func (refund *Refund) Refund(p *Params) (rsp Response, err error) {
nonceStr := util.RandomStr(32) param := refund.GetSignParam(p)
param := make(map[string]string)
param["appid"] = refund.AppID
param["mch_id"] = refund.MchID
param["nonce_str"] = nonceStr
param["out_refund_no"] = p.OutRefundNo
param["refund_desc"] = p.RefundDesc
param["refund_fee"] = p.RefundFee
param["total_fee"] = p.TotalFee
param["sign_type"] = util.SignTypeMD5
if p.TransactionID != "" {
param["transaction_id"] = p.TransactionID
}
if p.OutTradeNo != "" {
param["out_trade_no"] = p.OutTradeNo
}
if p.NotifyURL != "" {
param["notify_url"] = p.NotifyURL
}
sign, err := util.ParamSign(param, refund.Key) sign, err := util.ParamSign(param, refund.Key)
if err != nil { if err != nil {
@@ -101,19 +84,24 @@ func (refund *Refund) Refund(p *Params) (rsp Response, err error) {
} }
req := request{ req := request{
AppID: refund.AppID, AppID: param["appid"],
MchID: refund.MchID, MchID: param["mch_id"],
NonceStr: nonceStr, NonceStr: param["nonce_str"],
Sign: sign, Sign: sign,
SignType: util.SignTypeMD5, SignType: param["sign_type"],
TransactionID: p.TransactionID, OutRefundNo: param["out_refund_no"],
OutRefundNo: p.OutRefundNo, TotalFee: param["total_fee"],
OutTradeNo: p.OutTradeNo, RefundFee: param["refund_fee"],
TotalFee: p.TotalFee, RefundDesc: param["refund_desc"],
RefundFee: p.RefundFee, NotifyURL: param["notify_url"],
RefundDesc: p.RefundDesc,
NotifyURL: p.NotifyURL,
} }
if p.OutTradeNo != "" {
req.OutTradeNo = p.OutTradeNo
}
if p.TransactionID != "" {
req.TransactionID = p.TransactionID
}
rawRet, err := util.PostXMLWithTLS(refundGateway, req, p.RootCa, refund.MchID) rawRet, err := util.PostXMLWithTLS(refundGateway, req, p.RootCa, refund.MchID)
if err != nil { if err != nil {
return return
@@ -133,3 +121,31 @@ func (refund *Refund) Refund(p *Params) (rsp Response, err error) {
err = fmt.Errorf("[msg : xmlUnmarshalError] [rawReturn : %s] [sign : %s]", string(rawRet), sign) err = fmt.Errorf("[msg : xmlUnmarshalError] [rawReturn : %s] [sign : %s]", string(rawRet), sign)
return return
} }
// GetSignParam 获取签名的参数
func (refund *Refund) GetSignParam(p *Params) (param map[string]string) {
nonceStr := util.RandomStr(32)
param = make(map[string]string)
param["appid"] = refund.AppID
param["mch_id"] = refund.MchID
param["nonce_str"] = nonceStr
param["out_refund_no"] = p.OutRefundNo
param["refund_desc"] = p.RefundDesc
param["refund_fee"] = p.RefundFee
param["total_fee"] = p.TotalFee
if p.SignType == "" {
param["sign_type"] = util.SignTypeMD5
}
if p.OutTradeNo != "" {
param["out_trade_no"] = p.OutTradeNo
}
if p.TransactionID != "" {
param["transaction_id"] = p.TransactionID
}
if p.NotifyURL != "" {
param["notify_url"] = p.NotifyURL
}
return param
}