order_client.go
5.3 KB
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
package socialwork_sdk
import (
"encoding/base64"
"encoding/json"
"fmt"
"gitlab.workai.com.cn/chenang/socialwork-sdk/model"
"log"
)
type OrderClient struct {
Key string `json:"key"`
IV string `json:"iv"`
PublicKey string `json:"public_key"`
CustomerPrivateKey string `json:"customer_private_key"`
PtCode string `json:"pt_code"`
IP string `json:"ip"`
}
func HelloWorld() {
fmt.Println("欢迎使用小爱,socialwork-sdk")
}
func NewOrderClient(key, iv, customerPrivateKey, publicKey, ip, ptCode string) *OrderClient {
return &OrderClient{
Key: key,
IV: iv,
PublicKey: publicKey,
CustomerPrivateKey: customerPrivateKey,
IP: ip,
PtCode: ptCode,
}
}
func (o *OrderClient) EncryptAndSign(bs []byte) (string, string, error) {
cipherText, err := aes_CBC_Encrypt(bs, []byte(o.Key), []byte(o.IV))
if err != nil {
log.Printf("Aes_CBC_Encrypt.failed.err=%v\n", err)
return "", "", err
}
sign, err := rsaSignWithMd5Hex(cipherText, o.CustomerPrivateKey)
if err != nil {
log.Printf("RsaSignWithMd5Hex.failed.err=%v\n", err)
return "", "", err
}
return cipherText, sign, nil
}
func (o *OrderClient) DecryptAndVerySign(cipherText, sign string) ([]byte, error) {
_cipherText, err := base64.StdEncoding.DecodeString(cipherText)
if err != nil {
return nil, err
}
err = rsaVerifySignWithMd5Base64(cipherText, sign, o.PublicKey)
if err != nil {
return nil, err
}
content, err := aes_CBC_Decrypt(_cipherText, []byte(o.Key), []byte(o.IV))
if err != nil {
return nil, err
}
return content, nil
}
func (o *OrderClient) SendRequest(businessCode, reqBody string) (string, error) {
//校验参数 reqbody
checkErr := o.checkParams(businessCode, reqBody)
if checkErr != nil {
return "", checkErr
}
cipherText, sign, err := o.EncryptAndSign([]byte(reqBody))
if err != nil {
return "", err
}
orderModel := model.OrderModel{
Data: cipherText,
Sign: sign,
PtCode: o.PtCode,
BusinessCode: businessCode,
}
_orderModel, err := json.Marshal(orderModel)
if err != nil {
return "", err
}
respBody, _, err := makeHttpRequestV4(post, o.IP+orderUrl, nil, map[string]interface{}{"jsonbody": string(_orderModel)}, nil)
if err != nil {
return "", err
}
orderRes := model.OrderModel{}
json.Unmarshal([]byte(respBody), &orderRes)
_respBody, err := o.DecryptAndVerySign(orderRes.Data, orderRes.Sign)
if err != nil {
return "", err
}
return string(_respBody), nil
}
func (o *OrderClient) checkParams(businessCode, reqBody string) error {
switch businessCode {
case "AUTHORIZE":
var authorize model.Authorize
if err := json.Unmarshal([]byte(reqBody), &authorize); err != nil {
return err
}
return nil
case "CREATEORDER":
var createOrder model.CreateOrder
if err := json.Unmarshal([]byte(reqBody), &createOrder); err != nil {
return err
}
return nil
case "ADDFREEDOMEEMP":
var freedomEmps model.FreedomEmps
if err := json.Unmarshal([]byte(reqBody), &freedomEmps); err != nil {
return err
}
return nil
case "UPLOADATTACHMENT":
var uploadFreedomEmps model.UploadFreedomEmps
if err := json.Unmarshal([]byte(reqBody), &uploadFreedomEmps); err != nil {
return err
}
return nil
case "UPLOADATTACHMENTBYURL":
var uploadFreedomEmpsByUrl model.UploadFreedomEmpsByUrl
if err := json.Unmarshal([]byte(reqBody), &uploadFreedomEmpsByUrl); err != nil {
return err
}
return nil
case "ORDERINFO":
var simpleOrderInfo model.SimpleOrderInfo
if err := json.Unmarshal([]byte(reqBody), &simpleOrderInfo); err != nil {
return err
}
return nil
case "FREEDOMEMOSTATUS":
var freedomEmpStatus model.FreedomEmpStatus
if err := json.Unmarshal([]byte(reqBody), &freedomEmpStatus); err != nil {
return err
}
return nil
case "SUBACCOUNTBALANCE":
var subAccountBalance model.SubAccountBalance
if err := json.Unmarshal([]byte(reqBody), &subAccountBalance); err != nil {
return err
}
return nil
case "UPDATEORDERDETAIL":
var updateOrderDetail model.UpdateOrderDetail
if err := json.Unmarshal([]byte(reqBody), &updateOrderDetail); err != nil {
return err
}
return nil
case "EXISTRECEIPT":
var receipt model.Receipt
if err := json.Unmarshal([]byte(reqBody), &receipt); err != nil {
return err
}
return nil
case "DOWNLOADRECEIPT":
var downloadReceipt model.DownloadReceipt
if err := json.Unmarshal([]byte(reqBody), &downloadReceipt); err != nil {
return err
}
return nil
case "PAYORDER":
var payOrder model.PayOrder
if err := json.Unmarshal([]byte(reqBody), &payOrder); err != nil {
return err
}
return nil
case "USERINFO":
var getUserInfo model.GetUserInfo
if err := json.Unmarshal([]byte(reqBody), &getUserInfo); err != nil {
return err
}
return nil
case "PAYORDERV1":
var mergeCreateOrder model.MergeCreateOrder
if err := json.Unmarshal([]byte(reqBody), &mergeCreateOrder); err != nil {
return err
}
return nil
case "UPDATEMOBILE":
var updateMobile model.UpdateMobile
if err := json.Unmarshal([]byte(reqBody), &updateMobile); err != nil {
return err
}
return nil
case "CREATEEMPLOYMENTPOOL":
var createEmploymentPool model.CreateEmploymentPool
if err := json.Unmarshal([]byte(reqBody), &createEmploymentPool); err != nil {
return err
}
return nil
}
return nil
}