-
Notifications
You must be signed in to change notification settings - Fork 25
/
client_test.go
150 lines (126 loc) · 3.56 KB
/
client_test.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
package snap
import (
"fmt"
"github.com/midtrans/midtrans-go"
assert "github.com/stretchr/testify/require"
"regexp"
"testing"
"time"
)
const sandboxServerKey = "SB-Mid-server-TOq1a2AVuiyhhOjvfs3U_KeO"
func generateReqWithMap() *RequestParamWithMap {
time.Sleep(3)
return &RequestParamWithMap{
"transaction_details": map[string]interface{}{
"order_id": "MID-GO-TEST-" + time.Now().UTC().Format("2006010215040105"),
"gross_amount": 10000,
},
}
}
func TestSnapCreateTransactionWithMap(t *testing.T) {
midtrans.ServerKey = sandboxServerKey
assert.Equal(t, sandboxServerKey, midtrans.ServerKey)
res, err := CreateTransactionWithMap(generateReqWithMap())
if err != nil {
fmt.Println("Snap Request Error", err.GetMessage())
}
fmt.Println("Snap response", res)
assert.NotNil(t, res)
}
func TestSnapCreateTransactionTokenWithMap(t *testing.T) {
midtrans.ServerKey = sandboxServerKey
assert.Equal(t, sandboxServerKey, midtrans.ServerKey)
res, err := CreateTransactionTokenWithMap(generateReqWithMap())
if err != nil {
fmt.Println("Snap Request Error", err.GetMessage())
}
fmt.Println("Snap response", res)
assert.Equal(t, IsValidUUID(res), true)
}
func TestSnapCreateTransactionUrlWithMap(t *testing.T) {
midtrans.ServerKey = sandboxServerKey
assert.Equal(t, sandboxServerKey, midtrans.ServerKey)
res, err := CreateTransactionUrlWithMap(generateReqWithMap())
if err != nil {
fmt.Println("Snap Request Error", err.GetMessage())
}
fmt.Println("Snap url response", res)
assert.Nil(t, err)
assert.NotNil(t, res)
}
func TestSnapCreateTransaction(t *testing.T) {
s := Client{}
s.New(sandboxServerKey, midtrans.Sandbox)
res, err := s.CreateTransactionToken(GenerateSnapReq())
if err != nil {
fmt.Println("Snap Request Error", err.GetMessage())
}
fmt.Println("Snap response", res)
assert.Nil(t, err)
assert.NotNil(t, res)
}
func TestSnapCreateTransactionToken(t *testing.T) {
s := Client{}
s.New(sandboxServerKey, midtrans.Sandbox)
res, err := s.CreateTransactionToken(GenerateSnapReq())
if err != nil {
fmt.Println("Snap Request Error", err.GetMessage())
}
fmt.Println("Snap response", res)
assert.Nil(t, err)
assert.Equal(t, IsValidUUID(res), true)
}
func TestSnapCreateTransactionUrl(t *testing.T) {
s := Client{}
s.New(sandboxServerKey, midtrans.Sandbox)
res, err := s.CreateTransactionUrl(GenerateSnapReq())
if err != nil {
fmt.Println("Snap Request Error", err.GetMessage())
}
fmt.Println("Snap response", res)
assert.Nil(t, err)
assert.NotNil(t, res)
}
func IsValidUUID(uuid string) bool {
r := regexp.MustCompile("^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$")
return r.MatchString(uuid)
}
func GenerateSnapReq() *Request {
// Initiate Customer address
custAddress := &midtrans.CustomerAddress{
FName: "John",
LName: "Doe",
Phone: "081234567890",
Address: "Baker Street 97th",
City: "Jakarta",
Postcode: "16000",
CountryCode: "IDN",
}
// Initiate Snap Request
snapReq := &Request{
TransactionDetails: midtrans.TransactionDetails{
OrderID: "MID-GO-ID-" + time.Now().UTC().Format("2006010215040105"),
GrossAmt: 200000,
},
CreditCard: &CreditCardDetails{
Secure: true,
},
CustomerDetail: &midtrans.CustomerDetails{
FName: "John",
LName: "Doe",
Email: "[email protected]",
Phone: "081234567890",
BillAddr: custAddress,
ShipAddr: custAddress,
},
Items: &[]midtrans.ItemDetails{
midtrans.ItemDetails{
ID: "ITEM1",
Price: 200000,
Qty: 1,
Name: "Someitem",
},
},
}
return snapReq
}