- Notifications
You must be signed in to change notification settings - Fork811
/
Copy pathclient.go
200 lines (186 loc) · 5.52 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
193
194
195
196
197
198
199
200
package lakala
import (
"context"
"crypto/sha256"
"encoding/hex"
"fmt"
"hash"
"net/http"
"strings"
"sync"
"time"
"github.com/go-pay/gopay"
"github.com/go-pay/gopay/pkg/xhttp"
"github.com/go-pay/util"
"github.com/go-pay/xlog"
)
// Client lakala
typeClientstruct {
ctx context.Context// 上下文
PartnerCodestring// partner_code:商户编码,由4~6位大写字母或数字构成
credentialCodestring// credential_code:系统为商户分配的开发校验码,请妥善保管,不要在公开场合泄露
IsProdbool// 是否生产环境
DebugSwitch gopay.DebugSwitch// 调试开关,是否打印日志
logger xlog.XLogger
hc*xhttp.Client
sha256Hash hash.Hash
mu sync.Mutex
}
// NewClient 初始化lakala户端
// partnerCode: 商户编码,由4~6位大写字母或数字构成
// credentialCode: 系统为商户分配的开发校验码,请妥善保管,不要在公开场合泄露
// isProd: 是否生产环境
funcNewClient(partnerCode,credentialCodestring,isProdbool) (client*Client,errerror) {
ifpartnerCode==gopay.NULL||credentialCode==gopay.NULL {
returnnil,gopay.MissLakalaInitParamErr
}
logger:=xlog.NewLogger()
logger.SetLevel(xlog.DebugLevel)
client=&Client{
ctx:context.Background(),
PartnerCode:partnerCode,
credentialCode:credentialCode,
IsProd:isProd,
DebugSwitch:gopay.DebugOff,
logger:logger,
hc:xhttp.NewClient(),
sha256Hash:sha256.New(),
}
returnclient,nil
}
// SetBodySize 设置http response body size(MB)
func (c*Client)SetBodySize(sizeMBint) {
ifsizeMB>0 {
c.hc.SetBodySize(sizeMB)
}
}
// SetHttpClient 设置自定义的xhttp.Client
func (c*Client)SetHttpClient(client*xhttp.Client) {
ifclient!=nil {
c.hc=client
}
}
func (c*Client)SetLogger(logger xlog.XLogger) {
iflogger!=nil {
c.logger=logger
}
}
// 公共参数处理 Query Params
func (c*Client)pubParamsHandle() (paramstring,errerror) {
bm:=make(gopay.BodyMap)
bm.Set("time",time.Now().UnixMilli())
bm.Set("nonce_str",util.RandomString(20))
sign,err:=c.getRsaSign(bm)
iferr!=nil {
return"",fmt.Errorf("GetRsaSign Error: %w",err)
}
bm.Set("sign",sign)
param=bm.EncodeURLParams()
return
}
// 验证签名
funcVerifySign(notifyReq*NotifyRequest,partnerCodestring,credentialCodestring) (errerror) {
validStr:=fmt.Sprintf("%v&%v&%v&%v",partnerCode,notifyReq.Time,notifyReq.NonceStr,credentialCode)
h:=sha256.New()
h.Write([]byte(validStr))
validSign:=strings.ToLower(hex.EncodeToString(h.Sum(nil)))
ifnotifyReq.Sign!=validSign {
returnfmt.Errorf("签名验证失败")
}
return
}
// getRsaSign 获取签名字符串
func (c*Client)getRsaSign(bm gopay.BodyMap) (signstring,errerror) {
var (
partnerCode=c.PartnerCode
ts=bm.Get("time")
nonceStr=bm.Get("nonce_str")
credentialCode=c.credentialCode
)
ifts==""||nonceStr=="" {
return"",fmt.Errorf("签名缺少必要的参数")
}
validStr:=fmt.Sprintf("%v&%v&%v&%v",partnerCode,ts,nonceStr,credentialCode)
c.mu.Lock()
deferfunc() {
c.sha256Hash.Reset()
c.mu.Unlock()
}()
c.sha256Hash.Write([]byte(validStr))
sign=strings.ToLower(hex.EncodeToString(c.sha256Hash.Sum(nil)))
return
}
// PUT 发起请求
func (c*Client)doPut(ctx context.Context,pathstring,bm gopay.BodyMap) (bs []byte,errerror) {
varurl=baseUrlProd+path
param,err:=c.pubParamsHandle()
iferr!=nil {
returnnil,err
}
req:=c.hc.Req()
req.Header.Add("Accept","application/json")
uri:=url+"?"+param
ifc.DebugSwitch==gopay.DebugOn {
c.logger.Debugf("Lakala_Url: %s",uri)
c.logger.Debugf("Lakala_Req_Body: %s",bm.JsonBody())
c.logger.Debugf("Lakala_Req_Headers: %#v",req.Header)
}
res,bs,err:=req.Put(uri).SendBodyMap(bm).EndBytes(ctx)
iferr!=nil {
returnnil,err
}
ifres.StatusCode!=http.StatusOK {
returnnil,fmt.Errorf("HTTP Request Error, StatusCode = %d",res.StatusCode)
}
returnbs,nil
}
// PUT 发起请求
func (c*Client)doPost(ctx context.Context,pathstring,bm gopay.BodyMap) (bs []byte,errerror) {
varurl=baseUrlProd+path
param,err:=c.pubParamsHandle()
iferr!=nil {
returnnil,err
}
req:=c.hc.Req()
req.Header.Add("Accept","application/json")
uri:=url+"?"+param
ifc.DebugSwitch==gopay.DebugOn {
c.logger.Debugf("Lakala_Url: %s",uri)
c.logger.Debugf("Lakala_Req_Body: %s",bm.JsonBody())
c.logger.Debugf("Lakala_Req_Headers: %#v",req.Header)
}
res,bs,err:=req.Post(uri).SendBodyMap(bm).EndBytes(ctx)
iferr!=nil {
returnnil,err
}
ifres.StatusCode!=http.StatusOK {
returnnil,fmt.Errorf("HTTP Request Error, StatusCode = %d",res.StatusCode)
}
returnbs,nil
}
// GET 发起请求
func (c*Client)doGet(ctx context.Context,path,queryParamsstring) (bs []byte,errerror) {
varurl=baseUrlProd+path
param,err:=c.pubParamsHandle()
iferr!=nil {
returnnil,err
}
ifqueryParams!="" {
param=param+"&"+queryParams
}
req:=c.hc.Req()
req.Header.Add("Accept","application/json")
uri:=url+"?"+param
ifc.DebugSwitch==gopay.DebugOn {
c.logger.Debugf("Lakala_Url: %s",uri)
c.logger.Debugf("Lakala_Req_Headers: %#v",req.Header)
}
res,bs,err:=req.Get(uri).EndBytes(ctx)
iferr!=nil {
returnnil,err
}
ifres.StatusCode!=http.StatusOK {
returnnil,fmt.Errorf("HTTP Request Error, StatusCode = %d",res.StatusCode)
}
returnbs,nil
}