forked from youjianglong/swiftpass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
192 lines (164 loc) · 4.26 KB
/
client.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
package swiftpass
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"encoding/base64"
"encoding/xml"
"errors"
"io"
"io/ioutil"
"net/http"
"strings"
"github.com/youjianglong/swiftpass/request"
"github.com/youjianglong/swiftpass/response"
)
const (
SWIFTPASS_GETWAY = "https://pay.hstypay.com/v2/pay/gateway"
)
const (
RESULT_STATUS_SUCCESS = "0"
RESULT_CODE_SUCCESS = "0"
)
const (
TRADE_STATE_SUCCESS = "SUCCESS"
TRADE_STATE_REFUND = "REFUND"
TRADE_STATE_NOTPAY = "NOTPAY"
TRADE_STATE_CLOSED = "CLOSED"
TRADE_STATE_REVERSE = "REVERSE"
TRADE_STATE_REVOKED = "REVOKED"
)
const (
SignTypeMd5 = "MD5"
SignTypeRsa = "RSA"
)
var (
ErrNoRsaKey = errors.New("not rsa key")
ErrRsaPrivateKey = errors.New("failed to load rsa private key")
ErrRsaPublicKey = errors.New("failed to load rsa public key")
)
type SwiftClient struct {
Key string
GetWay string
SignType string
RequestXml string
ResponseXml string
PrivateKey []byte
PublicKey []byte
client *http.Client
}
func NewDefaultClient(key string) *SwiftClient {
var client SwiftClient
client.GetWay = SWIFTPASS_GETWAY
client.Key = key
client.SignType = SignTypeMd5
client.client = &http.Client{
Transport: &http.Transport{
MaxIdleConns: 64,
},
}
return &client
}
//加载rsa秘钥文件
func (swiftClient *SwiftClient) LoadRsaKeyFile(priveteKey, publicKey string) error {
readPrivateByte, readErr := ioutil.ReadFile(priveteKey)
if readErr != nil || len(readPrivateByte) == 0 {
return ErrRsaPrivateKey
}
swiftClient.PrivateKey = readPrivateByte
readPublicByte, readErr := ioutil.ReadFile(publicKey)
if readErr != nil || len(readPublicByte) == 0 {
return ErrRsaPublicKey
}
swiftClient.PublicKey = readPublicByte
return nil
}
//加载rsa秘钥文件
func (swiftClient *SwiftClient) LoadRsaKeyByte(priveteKey, publicKey []byte) {
swiftClient.PrivateKey = priveteKey
swiftClient.PublicKey = publicKey
}
//生成md5签名
func (swiftClient *SwiftClient) GenerateSign(req request.Request) string {
params := XmlObject2Map(req)
delete(params, "sign")
paramStr := BuildQueryString(params)
paramStr += "&key=" + swiftClient.Key
return strings.ToUpper(Md5String(paramStr))
}
//生成ras签名
func (swiftClient *SwiftClient) GenerateSignRsa(req request.Request) (string, error) {
if len(swiftClient.PrivateKey) == 0 || len(swiftClient.PublicKey) == 0 {
return "", ErrNoRsaKey
}
params := XmlObject2Map(req)
delete(params, "sign")
data := []byte(BuildQueryString(params))
h := sha256.New()
h.Write(data)
hashed := h.Sum(nil)
privateKey, err := DecodePrivateKey(swiftClient.PrivateKey)
newData, err := rsa.SignPKCS1v15(rand.Reader, privateKey, crypto.SHA256, hashed)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(newData), nil
}
//提交支付
func (c *SwiftClient) Execute(req request.Request, swpResp response.Response) error {
var sign string
var err error
req = req.Full()
if c.SignType == "" || c.SignType == SignTypeMd5 {
sign = c.GenerateSign(req)
} else {
sign, err = c.GenerateSignRsa(req)
}
if err != nil {
return err
}
data := req.Encode(sign)
c.RequestXml = string(data)
httpReq, _ := http.NewRequest("POST", c.GetWay, strings.NewReader(string(data)))
httpReq.Header.Set("Accept", "text/xml,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
httpReq.Header.Set("Charset", "UTF-8")
res, err := c.client.Do(httpReq)
if err != nil {
return err
}
defer res.Body.Close()
content, rerr := ioutil.ReadAll(res.Body)
if rerr != nil {
return rerr
}
c.ResponseXml = string(content)
encodeErr := xml.Unmarshal(content, swpResp)
if encodeErr != nil {
return encodeErr
}
return nil
}
//验证通知签名
func (c *SwiftClient) Verify(body io.Reader) bool {
params := Xml2Map(body)
sign := params["sign"]
if sign == "" {
return false
}
delete(params, "sign")
paramStr := BuildQueryString(params)
signType := params["sign_type"]
if signType == SignTypeMd5 { //MD5签名
return sign == strings.ToUpper(Md5String(paramStr+"&key="+c.Key))
}
key, err := DecodePublicKey(c.PublicKey)
if err != nil {
return false
}
h := sha256.New()
h.Write([]byte(paramStr))
hashed := h.Sum(nil)
sig, _ := base64.StdEncoding.DecodeString(sign)
return rsa.VerifyPKCS1v15(key, crypto.SHA256, hashed, sig) == nil
}