Explorar o código

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

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

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

* fix: 误删NotifyURL

* fix: 误删SignType

* fix: 修复 golangci-lint 失败

* refactor: 增加GetSignParam方法

* chore: 调整 go.mod

* refactor: 调整参数
Alfred %!s(int64=5) %!d(string=hai) anos
pai
achega
813684e555
Modificáronse 3 ficheiros con 71 adicións e 1112 borrados
  1. 8 4
      go.mod
  2. 10 1071
      go.sum
  3. 53 37
      pay/refund/refund.go

+ 8 - 4
go.mod

@@ -5,10 +5,14 @@ go 1.14
 require (
 	github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b
 	github.com/fatih/structs v1.1.0
-	github.com/gomodule/redigo v1.8.1
-	github.com/sirupsen/logrus v1.6.0
+	github.com/gomodule/redigo v2.0.0+incompatible
+	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/stretchr/testify v1.5.1
-	golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37
+	github.com/stretchr/testify v1.7.0
+	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
 )

A diferenza do arquivo foi suprimida porque é demasiado grande
+ 10 - 1071
go.sum


+ 53 - 37
pay/refund/refund.go

@@ -21,19 +21,20 @@ func NewRefund(cfg *config.Config) *Refund {
 	return &refund
 }
 
-//Params 调用参数
+// Params 调用参数
 type Params struct {
 	TransactionID string
-	OutTradeNo    string
 	OutRefundNo   string
+	OutTradeNo    string
 	TotalFee      string
 	RefundFee     string
 	RefundDesc    string
-	RootCa        string //ca证书
+	RootCa        string // ca证书
 	NotifyURL     string
+	SignType      string
 }
 
-//request 接口请求参数
+// request 接口请求参数
 type request struct {
 	AppID         string `xml:"appid"`
 	MchID         string `xml:"mch_id"`
@@ -49,7 +50,7 @@ type request struct {
 	NotifyURL     string `xml:"notify_url,omitempty"`
 }
 
-//Response 接口返回
+// Response 接口返回
 type Response struct {
 	ReturnCode          string `xml:"return_code"`
 	ReturnMsg           string `xml:"return_msg"`
@@ -73,27 +74,9 @@ type Response struct {
 	CashFeeType         string `xml:"cash_fee_type,omitempty"`
 }
 
-//Refund 退款申请
+// Refund 退款申请
 func (refund *Refund) Refund(p *Params) (rsp Response, err error) {
-	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
-	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
-	}
+	param := refund.GetSignParam(p)
 
 	sign, err := util.ParamSign(param, refund.Key)
 	if err != nil {
@@ -101,19 +84,24 @@ func (refund *Refund) Refund(p *Params) (rsp Response, err error) {
 	}
 
 	req := request{
-		AppID:         refund.AppID,
-		MchID:         refund.MchID,
-		NonceStr:      nonceStr,
-		Sign:          sign,
-		SignType:      util.SignTypeMD5,
-		TransactionID: p.TransactionID,
-		OutRefundNo:   p.OutRefundNo,
-		OutTradeNo:    p.OutTradeNo,
-		TotalFee:      p.TotalFee,
-		RefundFee:     p.RefundFee,
-		RefundDesc:    p.RefundDesc,
-		NotifyURL:     p.NotifyURL,
+		AppID:       param["appid"],
+		MchID:       param["mch_id"],
+		NonceStr:    param["nonce_str"],
+		Sign:        sign,
+		SignType:    param["sign_type"],
+		OutRefundNo: param["out_refund_no"],
+		TotalFee:    param["total_fee"],
+		RefundFee:   param["refund_fee"],
+		RefundDesc:  param["refund_desc"],
+		NotifyURL:   param["notify_url"],
+	}
+	if p.OutTradeNo != "" {
+		req.OutTradeNo = p.OutTradeNo
+	}
+	if p.TransactionID != "" {
+		req.TransactionID = p.TransactionID
 	}
+
 	rawRet, err := util.PostXMLWithTLS(refundGateway, req, p.RootCa, refund.MchID)
 	if err != nil {
 		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)
 	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
+}