-
Notifications
You must be signed in to change notification settings - Fork 7
/
unionpay.go
311 lines (251 loc) · 6.51 KB
/
unionpay.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package unionpay
import (
"crypto"
"encoding/base64"
"encoding/pem"
"errors"
"fmt"
"log"
"net/http"
"net/url"
"github.com/mitchellh/mapstructure"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"io/ioutil"
"strings"
)
const (
frontTransReq = "/gateway/api/frontTransReq.do"
backTransReq = "/gateway/api/backTransReq.do"
queryTrans = "/gateway/api/queryTrans.do"
appTransReq = "/gateway/api/appTransReq.do"
)
var ErrNotifyDataIsEmpty = errors.New("notify data is empty")
type UnionPay struct {
testEnv bool
mchID string // 测试商户号 700000000000001
/*
卡号 卡性质 机构名称 手机号码 密码 CVN2 有效期 证件号 姓名
6216261000000000018 借记卡 平安银行 13552535506 123456 341126197709218366 全渠道
6221558812340000 贷记卡 平安银行 13552535506 123456 123 1711 341126197709218366 互联网
短信验证码 111111
*/
verifySignCert *x509.Certificate //verify_sign_acp.cer
publicKey *x509.Certificate //加密密钥路径(openssl pkcs12 -in PM_700000000000001_acp.pfx -clcerts -nokeys -out key.cert)
privateKey *rsa.PrivateKey //加密证书路径(openssl pkcs12 -in PM_700000000000001_acp.pfx -nocerts -nodes -out key.pem)
client *unionPayClient
}
func (up *UnionPay) getHost() string {
if up.testEnv {
return "https://101.231.204.80:5000"
}
return "https://gateway.95516.com"
}
func (up *UnionPay) SetTestEnv(b bool) *UnionPay {
up.testEnv = b
return up
}
type unionPayClient struct {
client *http.Client
verifySignCert *x509.Certificate
}
func (c *unionPayClient) PostForm(u *url.URL, form map[string][]string, ret interface{}) error {
msg := url.Values(form).Encode()
req, err := http.NewRequest("POST", u.String(), strings.NewReader(msg))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.ContentLength = int64(len(msg))
resp, err := c.client.Do(req)
if err != nil {
return err
}
return c.Parse(ret, resp)
}
func (upp *unionPayClient) Parse(ret interface{}, resp *http.Response) (err error) {
defer resp.Body.Close()
var body []byte
body, err = ioutil.ReadAll(resp.Body)
if err != nil {
return
}
if resp.StatusCode != 200 {
err = fmt.Errorf("response code:%d", resp.StatusCode)
return
}
var fields []string
fields = strings.Split(string(body), "&")
vals := url.Values{}
data := map[string]string{}
for _, field := range fields {
f := strings.SplitN(field, "=", 2)
if len(f) >= 2 {
key, val := f[0], f[1]
data[key] = val
vals.Set(key, val)
}
}
if err = verify(upp.verifySignCert.PublicKey.(*rsa.PublicKey), vals, nil); err != nil {
return
}
if data["respCode"] != "00" {
err = fmt.Errorf("response error:%s", data["respMsg"])
return
}
err = mapstructure.Decode(data, ret)
return err
}
func NewPayment(mchID, pubPath, priPath, certPath string) (up *UnionPay) {
var (
err error
cert *x509.Certificate
publicKey *x509.Certificate
privateKey *rsa.PrivateKey
)
publicKey, err = newPublicKey(pubPath)
if err != nil {
log.Fatal(err)
}
privateKey, err = newPrivateKey(priPath)
if err != nil {
log.Fatal(err)
}
cert, err = newCertificate(certPath)
if err != nil {
log.Fatal(err)
}
client, err := newHTTPSClient()
if err != nil {
log.Fatal(err)
}
up = &UnionPay{
mchID: mchID,
verifySignCert: cert,
publicKey: publicKey,
privateKey: privateKey,
client: &unionPayClient{
client: client,
verifySignCert: cert,
},
}
return
}
func newHTTPSClient() (c *http.Client, err error) {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
c = &http.Client{Transport: tr}
return
}
func newPublicKey(path string) (cert *x509.Certificate, err error) {
// Read the verify sign certification key
pemData, err := ioutil.ReadFile(path)
if err != nil {
return
}
// Extract the PEM-encoded data block
block, _ := pem.Decode(pemData)
if block == nil {
err = fmt.Errorf("bad key data: %s", "not PEM-encoded")
return
}
if got, want := block.Type, "CERTIFICATE"; got != want {
err = fmt.Errorf("unknown key type %q, want %q", got, want)
return
}
// Decode the certification
cert, err = x509.ParseCertificate(block.Bytes)
if err != nil {
err = fmt.Errorf("bad private key: %s", err)
return
}
return
}
func newPrivateKey(path string) (priKey *rsa.PrivateKey, err error) {
// Read the private key
pemData, err := ioutil.ReadFile(path)
if err != nil {
err = fmt.Errorf("read key file: %s", err)
return
}
// Extract the PEM-encoded data block
block, _ := pem.Decode(pemData)
if block == nil {
err = fmt.Errorf("bad key data: %s", "not PEM-encoded")
return
}
if got, want := block.Type, "RSA PRIVATE KEY"; got != want {
err = fmt.Errorf("unknown key type %q, want %q", got, want)
return
}
// Decode the RSA private key
priKey, err = x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
err = fmt.Errorf("bad private key: %s", err)
return
}
return
}
func newCertificate(certpath string) (cert *x509.Certificate, err error) {
pemData, err := ioutil.ReadFile(certpath)
if err != nil {
return
}
block, _ := pem.Decode(pemData)
if block == nil {
err = errors.New("cannot decode the pem file")
return
}
if got, want := block.Type, "CERTIFICATE"; got != want {
err = fmt.Errorf("unknown key type %q, want %q", got, want)
return
}
// Decode the certificate
cert, err = x509.ParseCertificate(block.Bytes)
if err != nil {
err = fmt.Errorf("bad private key: %s", err)
return
}
return
}
func verify(certPubKey *rsa.PublicKey, vals url.Values, fields []string) (err error) {
var signature string
kvs := KVpairs{}
for k := range vals {
if len(fields) > 0 && !Contains(fields, k) {
continue
}
if k == "signature" {
signature = vals.Get(k)
continue
}
if vals.Get(k) == "" {
continue
}
kvs = append(kvs, KVpair{K: k, V: vals.Get(k)})
}
sig := SHA1([]byte(kvs.RemoveEmpty().Sort().Join("&")))
hashed := SHA1([]byte(fmt.Sprintf("%x", sig)))
var inSign []byte
inSign, err = base64.StdEncoding.DecodeString(signature)
if err != nil {
return
}
err = rsa.VerifyPKCS1v15(certPubKey, crypto.SHA1, hashed, inSign)
if err != nil {
return
}
return
}
func signature(priKey *rsa.PrivateKey, kvs KVpairs) (sig string, err error) {
sha1ParamsStr := SHA1([]byte(kvs.RemoveEmpty().Sort().Join("&")))
hashed := SHA1([]byte(fmt.Sprintf("%x", sha1ParamsStr)))
rsaSign, err := rsa.SignPKCS1v15(nil, priKey, crypto.SHA1, hashed)
if err != nil {
return
}
sig = base64.StdEncoding.EncodeToString(rsaSign)
return
}