-
Notifications
You must be signed in to change notification settings - Fork 0
/
refund.go
151 lines (136 loc) · 4.21 KB
/
refund.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package refund
import (
"encoding/xml"
"fmt"
"github.com/cnxfire/wechat/pay/config"
"github.com/cnxfire/wechat/util"
)
var refundGateway = "https://api.mch.weixin.qq.com/secapi/pay/refund"
// Refund struct extends context
type Refund struct {
*config.Config
}
// NewRefund return an instance of refund package
func NewRefund(cfg *config.Config) *Refund {
refund := Refund{cfg}
return &refund
}
// Params 调用参数
type Params struct {
TransactionID string
OutRefundNo string
OutTradeNo string
TotalFee string
RefundFee string
RefundDesc string
RootCa string // ca证书
NotifyURL string
SignType string
}
// request 接口请求参数
type request struct {
AppID string `xml:"appid"`
MchID string `xml:"mch_id"`
NonceStr string `xml:"nonce_str"`
Sign string `xml:"sign"`
SignType string `xml:"sign_type,omitempty"`
TransactionID string `xml:"transaction_id,omitempty"`
OutTradeNo string `xml:"out_trade_no,omitempty"`
OutRefundNo string `xml:"out_refund_no"`
TotalFee string `xml:"total_fee"`
RefundFee string `xml:"refund_fee"`
RefundDesc string `xml:"refund_desc,omitempty"`
NotifyURL string `xml:"notify_url,omitempty"`
}
// Response 接口返回
type Response struct {
ReturnCode string `xml:"return_code"`
ReturnMsg string `xml:"return_msg"`
AppID string `xml:"appid,omitempty"`
MchID string `xml:"mch_id,omitempty"`
NonceStr string `xml:"nonce_str,omitempty"`
Sign string `xml:"sign,omitempty"`
ResultCode string `xml:"result_code,omitempty"`
ErrCode string `xml:"err_code,omitempty"`
ErrCodeDes string `xml:"err_code_des,omitempty"`
TransactionID string `xml:"transaction_id,omitempty"`
OutTradeNo string `xml:"out_trade_no,omitempty"`
OutRefundNo string `xml:"out_refund_no,omitempty"`
RefundID string `xml:"refund_id,omitempty"`
RefundFee string `xml:"refund_fee,omitempty"`
SettlementRefundFee string `xml:"settlement_refund_fee,omitempty"`
TotalFee string `xml:"total_fee,omitempty"`
SettlementTotalFee string `xml:"settlement_total_fee,omitempty"`
FeeType string `xml:"fee_type,omitempty"`
CashFee string `xml:"cash_fee,omitempty"`
CashFeeType string `xml:"cash_fee_type,omitempty"`
}
// Refund 退款申请
func (refund *Refund) Refund(p *Params) (rsp Response, err error) {
param := refund.GetSignParam(p)
sign, err := util.ParamSign(param, refund.Key)
if err != nil {
return
}
req := request{
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
}
err = xml.Unmarshal(rawRet, &rsp)
if err != nil {
return
}
if rsp.ReturnCode == "SUCCESS" {
if rsp.ResultCode == "SUCCESS" {
err = nil
return
}
err = fmt.Errorf("refund error, errcode=%s,errmsg=%s", rsp.ErrCode, rsp.ErrCodeDes)
return
}
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
}