-
Notifications
You must be signed in to change notification settings - Fork 51
/
snap.go
76 lines (63 loc) · 1.97 KB
/
snap.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
package midtrans
import (
"bytes"
"encoding/json"
"errors"
"io"
"strings"
)
// SnapGateway struct
type SnapGateway struct {
Client Client
}
// Call : base method to call Snap API
func (gateway *SnapGateway) Call(method, path string, body io.Reader, v interface{}) error {
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
path = gateway.Client.APIEnvType.SnapURL() + path
return gateway.Client.Call(method, path, body, v)
}
// GetTokenQuick : Quickly get token without constructing the body manually
func (gateway *SnapGateway) GetTokenQuick(orderID string, grossAmount int64) (SnapResponse, error) {
return gateway.GetToken(&SnapReq{
TransactionDetails: TransactionDetails{
OrderID: orderID,
GrossAmt: grossAmount,
},
})
}
// GetToken : Get token by consuming SnapReq
func (gateway *SnapGateway) GetToken(r *SnapReq) (SnapResponse, error) {
resp := SnapResponse{}
jsonReq, _ := json.Marshal(r)
err := gateway.Call("POST", "snap/v1/transactions", bytes.NewBuffer(jsonReq), &resp)
if err != nil {
gateway.Client.Logger.Println("Error getting snap token: ", err)
return resp, err
}
if len(resp.ErrorMessages) > 0 {
return resp, errors.New(strings.Join(resp.ErrorMessages, ", "))
}
return resp, nil
}
// GetTokenQuickWithMap : Quickly get token without constructing the body manually
func (gateway *SnapGateway) GetTokenQuickWithMap(orderID string, grossAmount int64) (ResponseWithMap, error) {
return gateway.GetTokenWithMap(&SnapReqWithMap{
"transaction_details": TransactionDetails{
OrderID: orderID,
GrossAmt: grossAmount,
},
})
}
// GetTokenWithMap : Get token by consuming SnapReqWithMap
func (gateway *SnapGateway) GetTokenWithMap(r *SnapReqWithMap) (ResponseWithMap, error) {
resp := ResponseWithMap{}
jsonReq, _ := json.Marshal(r)
err := gateway.Call("POST", "snap/v1/transactions", bytes.NewBuffer(jsonReq), &resp)
if err != nil {
gateway.Client.Logger.Println("Error getting snap token: ", err)
return resp, err
}
return resp, nil
}