-
Notifications
You must be signed in to change notification settings - Fork 7
/
consume_refund.go
184 lines (169 loc) · 4.78 KB
/
consume_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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package unionpay
import (
"crypto/rsa"
"fmt"
"net/http"
"net/url"
"time"
)
type ConsumeRefundResponse struct {
Version string
Encoding string
CertID string
Signature string
SignMethod string
TxnType string
TxnSubType string
BizType string
AccessType string
MerID string
OrderID string
TxnTime string
TxnAmt string // 长度为1到12字节的变长整型数值,以分为单位
ReqReserved string
Reserved string
QueryID string
OrigQryID string
RespCode string
RespMsg string
}
func (up *UnionPay) ConsumeRefund(orderID, returnURL string, amount int64, originQueryID, reqReserved, reserved string) (resp *ConsumeRefundResponse, err error) {
kvs := KVpairs{}
kvs = append(kvs, KVpair{K: "version", V: "5.0.0"})
kvs = append(kvs, KVpair{K: "encoding", V: "UTF-8"})
kvs = append(kvs, KVpair{K: "certId", V: up.publicKey.SerialNumber.String()})
kvs = append(kvs, KVpair{K: "signMethod", V: "01"})
kvs = append(kvs, KVpair{K: "txnType", V: "04"})
kvs = append(kvs, KVpair{K: "txnSubType", V: "00"})
kvs = append(kvs, KVpair{K: "bizType", V: "000201"})
kvs = append(kvs, KVpair{K: "backUrl", V: returnURL})
kvs = append(kvs, KVpair{K: "accessType", V: "0"})
kvs = append(kvs, KVpair{K: "merId", V: up.mchID})
kvs = append(kvs, KVpair{K: "orderId", V: orderID})
kvs = append(kvs, KVpair{K: "txnTime", V: time.Now().Format("20060102150405")})
kvs = append(kvs, KVpair{K: "txnAmt", V: fmt.Sprint(amount)})
kvs = append(kvs, KVpair{K: "reqReserved", V: reqReserved})
kvs = append(kvs, KVpair{K: "reserved", V: reserved})
kvs = append(kvs, KVpair{K: "origQryId", V: originQueryID})
kvs = append(kvs, KVpair{K: "channelType", V: "07"})
var sig string
sig, err = signature(up.privateKey, kvs)
if err != nil {
return
}
kvs = append(kvs, KVpair{K: "signature", V: sig})
data := url.Values{}
for _, v := range kvs {
data.Set(v.K, v.V)
}
var u *url.URL
u, err = url.Parse(up.getHost() + backTransReq)
if err != nil {
return
}
var result ConsumeRefundResponse
err = up.client.PostForm(u, data, &result)
if err != nil {
return
}
resp = &result
return
}
type ConsumeRefundNotifyResponse struct {
Version string
Encoding string
CertID string
Signature string
SignMethod string
TxnType string
TxnSubType string
BizType string
AccessType string
MerID string
OrderID string
TxnTime string
CurrencyCode string
TxnAmt string // 长度为1到12字节的变长整型数值,以分为单位
ReqReserved string
Reserved string
QueryID string
OrigQryID string
TraceNo string
TraceTime string
SettleDate string
SettleCurrencyCode string
SettleAmt string
RespCode string
RespMsg string
}
func (up *UnionPay) ConsumeRefundNotify(req *http.Request) (resp *ConsumeRefundNotifyResponse, err error) {
vals := req.Form
if len(vals) == 0 {
if err = req.ParseForm(); err != nil {
return
}
}
if len(vals) == 0 {
err = ErrNotifyDataIsEmpty
return
}
var fields = []string{
"version",
"encoding",
"certId",
"signature",
"signMethod",
"txnType",
"txnSubType",
"bizType",
"accessType",
"merId",
"orderId",
"txnTime",
"currencyCode",
"txnAmt",
"reqReserved",
"reserved",
"queryId",
"origQryId",
"traceNo",
"traceTime",
"settleDate",
"settleCurrencyCode",
"settleAmt",
"respCode",
"respMsg",
"accNo",
}
if err = verify(up.verifySignCert.PublicKey.(*rsa.PublicKey), vals, fields); err != nil {
return
}
resp = &ConsumeRefundNotifyResponse{
Version: vals.Get("version"),
Encoding: vals.Get("encoding"),
CertID: vals.Get("certId"),
Signature: vals.Get("signature"),
SignMethod: vals.Get("signMethod"),
TxnType: vals.Get("txnType"),
TxnSubType: vals.Get("txnSubType"),
BizType: vals.Get("bizType"),
AccessType: vals.Get("accessType"),
MerID: vals.Get("merId"),
OrderID: vals.Get("orderId"),
TxnTime: vals.Get("txnTime"),
CurrencyCode: vals.Get("currencyCode"),
TxnAmt: vals.Get("txnAmt"),
ReqReserved: vals.Get("reqReserved"),
Reserved: vals.Get("reserved"),
QueryID: vals.Get("queryId"),
OrigQryID: vals.Get("origQryId"),
TraceNo: vals.Get("traceNo"),
TraceTime: vals.Get("traceTime"),
SettleDate: vals.Get("settleDate"),
SettleCurrencyCode: vals.Get("settleCurrencyCode"),
SettleAmt: vals.Get("settleAmt"),
RespCode: vals.Get("respCode"),
RespMsg: vals.Get("respMsg"),
}
return
}