forked from veritrans/go-midtrans
-
Notifications
You must be signed in to change notification settings - Fork 1
/
snap.go
52 lines (43 loc) · 1.22 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
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
}