-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsasapay.go
471 lines (432 loc) · 13.2 KB
/
sasapay.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
package sasapay
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"time"
"github.com/danchengash/sasapay-go-plugin/helpers"
"github.com/danchengash/sasapay-go-plugin/models"
)
type SasaPay struct {
Environment int
ClientId string
ClientSecret string
MerchantCode string
cacheToken models.AccessTokenResponse
Showlogs bool
}
func NewSasaPay(clientId string, clientSecret string, merchantcode string, environment int, showLogs bool) *SasaPay {
var accessToken = models.AccessTokenResponse{}
return &SasaPay{
ClientId: clientId,
ClientSecret: clientSecret,
MerchantCode: merchantcode,
Environment: environment,
cacheToken: accessToken,
Showlogs: showLogs,
}
}
// setAccessToken returns a time bound access token to call allowed APIs.
// This token should be used in all other subsequent responses to the APIs
func (s *SasaPay) setAccessToken() (string, error) {
if time.Until(s.cacheToken.ExpiresAt.UTC()).Seconds() > 0 {
fmt.Println("cache token")
return s.cacheToken.AccessToken, nil
}
url := s.baseURL() + SetAccessTokenUrl
b := []byte(s.ClientId + ":" + s.ClientSecret)
encoded := base64.StdEncoding.EncodeToString(b)
headers := make(map[string]string)
headers["Authorization"] = "Basic " + encoded
res, err := helpers.NewReq(url, nil, &headers, false)
if err != nil {
fmt.Println(err)
return "", &models.RequestError{StatusCode: res.StatusCode(), Message: string(res.Body()), Url: res.LocalAddr().String()}
}
if res.StatusCode() >= 200 && res.StatusCode() <= 300 {
s.cacheToken, err = models.UnmarshalAccessTokenResponse(res.Body())
s.cacheToken.ExpiresAt = time.Now().Add(time.Duration(s.cacheToken.ExpiresIn) * time.Second)
if err != nil {
return "", err
}
}
return s.cacheToken.AccessToken, nil
}
func (s *SasaPay) baseURL() string {
if s.Environment == int(Production) {
return "https://api.sasapay.app/api/v1/"
}
return "https://api.sasapay.me/api/v1/"
}
func (s *SasaPay) RegisterCallBackUrl(confirmationUrl string) (*models.RegisterConfirmationURLResponse, error) {
token, err := s.setAccessToken()
if err != nil {
return nil, err
}
headers := make(map[string]string)
params := make(map[string]interface{})
params["MerchantCode"] = s.MerchantCode
params["ConfirmationUrl"] = confirmationUrl
reqEntityBytes, _ := json.Marshal(params)
headers["Authorization"] = "Bearer " + token
url := s.baseURL() + registerCallBack
res, err := helpers.NewReq(url, &reqEntityBytes, &headers, s.Showlogs)
if err != nil {
return nil, &models.RequestError{StatusCode: res.StatusCode(), Message: string(res.Body()), Url: res.LocalAddr().String()}
}
resp, err := models.UnmarshalRegisterConfirmationURLResponse(res.Body())
if err != nil {
return nil, err
}
return &resp, nil
}
func (s *SasaPay) Customer2Business(body models.C2BRequest) (*models.C2BResponse, error) {
token, err := s.setAccessToken()
if err != nil {
return nil, err
}
headers := make(map[string]string)
headers["Authorization"] = "Bearer " + token
url := s.baseURL() + c2burl
reqbody, err := body.Marshal()
if err != nil {
return nil, err
}
resp, err := helpers.NewReq(url, &reqbody, &headers, s.Showlogs)
if err != nil {
return nil, err
}
if resp.StatusCode() != 200 {
errRespose, err := models.UnmarshalAPIResponse(resp.Body())
if err != nil {
return nil, errors.New(string(resp.Body()))
}
return nil, errors.New(errRespose.Detail)
}
response, err := models.UnmarshalC2BResponse(resp.Body())
if err != nil {
return nil, err
}
return &response, nil
}
func (s *SasaPay) C2BProcess(checkoutRequestID string, otpCode string) (*models.APIResponse, error) {
params := make(map[string]interface{})
params["CheckoutRequestID"] = checkoutRequestID
params["MerchantCode"] = s.MerchantCode
params["VerificationCode"] = otpCode
token, err := s.setAccessToken()
if err != nil {
return nil, err
}
headers := make(map[string]string)
headers["Authorization"] = "Bearer " + token
url := s.baseURL() + c2bProcess
paramsBytes, _ := json.Marshal(params)
resp, err := helpers.NewReq(url, ¶msBytes, &headers, s.Showlogs)
if err != nil {
return nil, err
}
if resp.StatusCode() != 200 {
errRespose, err := models.UnmarshalAPIResponse(resp.Body())
if err != nil {
return nil, errors.New(string(resp.Body()))
}
return nil, errors.New(errRespose.Detail)
}
response, err := models.UnmarshalAPIResponse(resp.Body())
if err != nil {
return nil, err
}
return &response, nil
}
/// look at the README File to get a list of all the channel codes
func (s *SasaPay) Business2Customer(params models.B2CRequest) (*models.B2CResponse, error) {
token, err := s.setAccessToken()
if err != nil {
return nil, err
}
headers := make(map[string]string)
headers["Authorization"] = "Bearer " + token
url := s.baseURL() + b2cUrl
body, _ := params.Marshal()
resp, err := helpers.NewReq(url, &body, &headers, s.Showlogs)
if err != nil {
return nil, err
}
if resp.StatusCode() != 200 {
errRespose, err := models.UnmarshalAPIResponse(resp.Body())
if err != nil {
return nil, errors.New(string(resp.Body()))
}
return nil, errors.New(errRespose.Detail)
}
response, err := models.UnmarshalB2CResponse(resp.Body())
if err != nil {
return nil, err
}
return &response, nil
}
func (s *SasaPay) Business2Business(params models.B2BRequest) (*models.B2BResponse, error) {
token, err := s.setAccessToken()
if err != nil {
return nil, err
}
headers := make(map[string]string)
headers["Authorization"] = "Bearer " + token
url := s.baseURL() + b2bUrl
body, _ := params.Marshal()
resp, err := helpers.NewReq(url, &body, &headers, s.Showlogs)
if err != nil {
return nil, err
}
if resp.StatusCode() != 200 {
errRespose, err := models.UnmarshalAPIResponse(resp.Body())
if err != nil {
return nil, errors.New(string(resp.Body()))
}
return nil, errors.New(errRespose.Detail)
}
response, err := models.UnmarshalB2BResponse(resp.Body())
if err != nil {
return nil, err
}
return &response, nil
}
func (s *SasaPay) CheckTransactionStatus(checkSta models.CheckTransactionStatusRequest) (*models.TransactionStatusResponse, error) {
token, err := s.setAccessToken()
if err != nil {
return nil, err
}
headers := make(map[string]string)
params := make(map[string]interface{})
headers["Authorization"] = "Bearer " + token
url := s.baseURL() + check_transaction_status
if checkSta.CallBackUrl != "" {
url = s.baseURL() + check_transaction_status_query
params["CallbackUrl"] = checkSta.CallBackUrl
}
checkSta.MerchantCode = s.MerchantCode
if checkSta.CheckoutRequestID != nil {
params["CheckoutRequestId"] = checkSta.CheckoutRequestID
}
params["MerchantCode"] = s.MerchantCode
params["MerchantTransactionReference"] = checkSta.MerchantTransactionReference
params["TransactionCode"] = checkSta.TransactionCode
paramsBytes, _ := json.Marshal(params)
resp, err := helpers.NewReq(url, ¶msBytes, &headers, s.Showlogs)
if err != nil {
return nil, err
}
if resp.StatusCode() != 200 {
// if resp.StatusCode() == 400 {
// return nil, errors.New(string(resp.Body()))
// }
errRespose, err := models.UnmarshalAPIResponse(resp.Body())
if err != nil {
return nil, fmt.Errorf("%s-%s", err.Error(), resp.Body())
}
if errRespose.Detail == "" {
return nil, errors.New(errRespose.Message)
} else {
return nil, errors.New(errRespose.Detail)
}
}
response, err := models.UnmarshalTransactionStatusResponse(resp.Body())
if err != nil {
return nil, err
}
return &response, nil
}
func (s *SasaPay) VerifyTransaction(transactionCode string) (*models.VerifyTransactionResponse, error) {
token, err := s.setAccessToken()
if err != nil {
return nil, err
}
headers := make(map[string]string)
headers["Authorization"] = "Bearer " + token
url := s.baseURL() + verify_transaction
params := make(map[string]interface{})
params["MerchantCode"] = s.MerchantCode
params["TransactionCode"] = transactionCode
paramsBytes, _ := json.Marshal(params)
resp, err := helpers.NewReq(url, ¶msBytes, &headers, s.Showlogs)
if err != nil {
return nil, err
}
if resp.StatusCode() != 200 {
errRespose, err := models.UnmarshalAPIRespSecond(resp.Body())
if err != nil {
return nil, errors.New(string(resp.Body()))
}
return nil, errors.New(errRespose.Message)
}
response, err := models.UnmarshalVerifyTransactionResponse(resp.Body())
if err != nil {
return nil, err
}
return &response, nil
}
func (s *SasaPay) CheckMerchantBalance() (*models.MerchantBalanceResp, error) {
token, err := s.setAccessToken()
if err != nil {
return nil, err
}
headers := make(map[string]string)
headers["Authorization"] = "Bearer " + token
url := s.baseURL() + check_merchant_balance + s.MerchantCode
resp, err := helpers.NewReq(url, nil, &headers, s.Showlogs)
if err != nil {
return nil, err
}
if resp.StatusCode() != 200 {
errRespose, err := models.UnmarshalAPIResponse(resp.Body())
if err != nil {
return nil, errors.New(string(resp.Body()))
}
return nil, errors.New(errRespose.Detail)
}
response, err := models.UnmarshalMerchantBalanceResp(resp.Body())
if err != nil {
return nil, err
}
return &response, nil
}
func (s *SasaPay) Business2Benefiary(params models.Business2BeneficiaryReq) (*models.Business2BeneficiaryResp, error) {
token, err := s.setAccessToken()
if err != nil {
return nil, err
}
headers := make(map[string]string)
headers["Authorization"] = "Bearer " + token
url := s.baseURL() + business2Beneficiary
body, _ := params.Marshal()
resp, err := helpers.NewReq(url, &body, &headers, s.Showlogs)
if err != nil {
return nil, err
}
if resp.StatusCode() != 200 {
errRespose, err := models.UnmarshalAPIRespSecond(resp.Body())
if err != nil {
return nil, errors.New(string(resp.Body()))
}
return nil, errors.New(errRespose.Message)
}
response, err := models.UnmarshalBusiness2BeneficiaryResp(resp.Body())
if err != nil {
return nil, err
}
return &response, nil
}
func (s *SasaPay) AccountValidate(acct string, channel string) (*models.AccountValidationRes, error) {
token, err := s.setAccessToken()
if err != nil {
return nil, err
}
headers := make(map[string]string)
headers["Authorization"] = "Bearer " + token
url := s.baseURL() + accountValidation
params := make(map[string]interface{})
params["merchant_code"] = s.MerchantCode
params["channel_code"] = channel
params["account_number"] = acct
paramsBytes, _ := json.Marshal(params)
resp, err := helpers.NewReq(url, ¶msBytes, &headers, s.Showlogs)
if err != nil {
return nil, err
}
if resp.StatusCode() != 200 {
errRespose, err := models.UnmarshalAPIResponse(resp.Body())
if err != nil {
return nil, errors.New(string(resp.Body()))
}
return nil, errors.New(errRespose.Detail)
}
response, err := models.UnmarshalAccountValidationRes(resp.Body())
if err != nil {
return nil, err
}
return &response, nil
}
func (s *SasaPay) GetTransactions(page int, pageSize int, startEndate string, endDate string, transactionCode string) (*models.TransactionsResponse, error) {
token, err := s.setAccessToken()
if err != nil {
return nil, err
}
headers := make(map[string]string)
headers["Authorization"] = "Bearer " + token
url := s.baseURL() + transactionsUrl + s.MerchantCode + fmt.Sprintf("&page=%d&page_size=%d&start_date=%s&end_date=%s", page, pageSize, startEndate, endDate)
if transactionCode != "" {
url = url + fmt.Sprintf("&transaction_code=%s", transactionCode)
}
resp, err := helpers.NewReq(url, nil, &headers, s.Showlogs)
if err != nil {
return nil, err
}
if resp.StatusCode() != 200 {
errRespose, err := models.UnmarshalAPIRespSecond(resp.Body())
if err != nil {
return nil, errors.New(string(resp.Body()))
}
return nil, errors.New(errRespose.Message)
}
response, err := models.UnmarshalTransactionsResponse(resp.Body())
if err != nil {
return nil, err
}
return &response, nil
}
func (s *SasaPay) CardPayment(param models.CardPaymentRequest) (*models.CardPaymentResponse, error) {
token, err := s.setAccessToken()
if err != nil {
return nil, err
}
headers := make(map[string]string)
headers["Authorization"] = "Bearer " + token
url := s.baseURL() + cardPaymentUrl
paramsBytes, _ := param.Marshal()
resp, err := helpers.NewReq(url, ¶msBytes, &headers, s.Showlogs)
if err != nil {
return nil, err
}
if resp.StatusCode() != 200 {
errRespose, err := models.UnmarshalAPIResponse(resp.Body())
if err != nil {
return nil, errors.New(string(resp.Body()))
}
return nil, errors.New(errRespose.Detail)
}
response, err := models.UnmarshalCardPaymentResponse(resp.Body())
if err != nil {
return nil, err
}
return &response, nil
}
func (s *SasaPay) GetChannelCodes() (*[]models.BankList, error) {
token, err := s.setAccessToken()
if err != nil {
return nil, err
}
headers := make(map[string]string)
headers["Authorization"] = "Bearer " + token
url := s.baseURL() + getBankList
resp, err := helpers.NewReq(url, nil, &headers, s.Showlogs)
if err != nil {
return nil, err
}
if resp.StatusCode() != 200 {
errRespose, err := models.UnmarshalAPIResponse(resp.Body())
if err != nil {
return nil, errors.New(string(resp.Body()))
}
return nil, errors.New(errRespose.Detail)
}
response, err := models.UnmarshalBankListResp(resp.Body())
if err != nil {
return nil, err
}
if response.Data != nil {
return &response.Data, nil
}
return nil, fmt.Errorf("no bank list")
}