-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathammana.go
293 lines (251 loc) · 9.04 KB
/
ammana.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
package ammana
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"strings"
"time"
)
// ammana entry struick for ammana
type ammana struct {
ClientID string
ClientSecret string
BaseURL string
client *ammanaHttpClient
*logger
}
/*New Function, create ammana pointer
Required parameter :
1. Your ClientID (this from Team Ammana)
2. Your ClientSecret (this from Team Ammana)
3. BaseURL (hit to endpoint ex: https://paylater-dev.ammana.id/api/v1 for sandbox or https://paylater.ammana.id/api/v1 for production.
this value based on https://paylater-dev.ammana.id/docs/1.x/overview)
*/
func New(baseUrl string, clientId string, clientSecret string, timeout time.Duration) *ammana {
httpRequest := newRequest(timeout)
return &ammana{
ClientID: clientId,
ClientSecret: clientSecret,
BaseURL: baseUrl,
client: httpRequest,
logger: newLogger(),
}
}
func (a *ammana) call(method string, path string, body io.Reader, v interface{}, headers map[string]string) error {
a.info().Println("Starting http call..")
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
path = fmt.Sprintf("%s%s", a.BaseURL, path)
return a.client.exec(method, path, body, v, headers)
}
func (a *ammana) GetAuth() (DataAuth GenerateAccessTokenResponse, err error) {
a.info().Println("Starting Get Authentication Ammana")
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("panic: %v", r)
}
if err != nil {
a.error().Println(err.Error())
}
}()
var (
generateAccessTokenRequest GenerateAccessTokenRequest
generateAccessTokenResponse GenerateAccessTokenResponse
)
headers := make(map[string]string)
headers["Content-Type"] = "application/json"
headers["Accept"] = "application/json"
generateAccessTokenRequest.ClientSecret = a.ClientSecret
payload, err := json.Marshal(generateAccessTokenRequest)
if err != nil {
return generateAccessTokenResponse, err
}
pathGenerateAccessToken := fmt.Sprintf("clients/%s/auth", a.ClientID)
err = a.call("POST", pathGenerateAccessToken, bytes.NewBuffer(payload), &generateAccessTokenResponse, headers)
if err != nil {
return generateAccessTokenResponse, err
}
if len(generateAccessTokenResponse.Message) > 0 {
err = errors.New(generateAccessTokenResponse.Message)
return generateAccessTokenResponse, err
}
return generateAccessTokenResponse, nil
}
func (a *ammana) GetCardDetail(request GetCardDetailRequest) (resp GetCardDetailResponse, err error) {
a.info().Println("Starting Get Card Detail Ammana")
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("panic: %v", r)
}
if err != nil {
a.error().Println(err.Error())
}
}()
var getCardDetailResponse GetCardDetailResponse
// get auth data
auth, err := a.GetAuth()
if err != nil {
return getCardDetailResponse, err
}
// set header
headers := make(map[string]string)
headers["Content-Type"] = "application/json"
headers["Accept"] = "application/json"
headers["Authorization"] = fmt.Sprintf("%s %s", auth.Data.TokenType, auth.Data.AccessToken)
// set path
queryStringGetCardDetail := ""
if len(request.PhoneNumber) > 0 {
queryStringGetCardDetail = fmt.Sprintf("phone_number=%s", request.PhoneNumber)
} else if len(request.IdentityNumber) > 0 {
queryStringGetCardDetail = fmt.Sprintf("identity_number=%s", request.IdentityNumber)
} else {
err = errors.New("invalid query string")
return getCardDetailResponse, err
}
pathGetCardDetail := fmt.Sprintf("clients/%s/card?%s", a.ClientID, queryStringGetCardDetail)
err = a.call("GET", pathGetCardDetail, nil, &getCardDetailResponse, headers)
if len(getCardDetailResponse.Message) > 0 {
err = errors.New(getCardDetailResponse.Message)
return getCardDetailResponse, err
}
return getCardDetailResponse, nil
}
func (a *ammana) GetOrderURL(request OrderRequest) (resp OrderResponse, err error) {
a.info().Println("Starting Get Order URL Ammana")
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("panic: %v", r)
}
if err != nil {
a.error().Println(err.Error())
}
}()
var getOrderURLResponse OrderResponse
// get auth data
auth, err := a.GetAuth()
if err != nil {
a.error().Println(err.Error())
return getOrderURLResponse, err
}
// set header
headers := make(map[string]string)
headers["Content-Type"] = "application/json"
headers["Accept"] = "application/json"
headers["Authorization"] = fmt.Sprintf("%s %s", auth.Data.TokenType, auth.Data.AccessToken)
pathGetOrderURL := fmt.Sprintf("clients/%s/cards/%s/orders", a.ClientID, request.CardID)
//Marshal Order
payload, errPayload := json.Marshal(request)
if errPayload != nil {
return getOrderURLResponse, err
}
err = a.call("POST", pathGetOrderURL, bytes.NewBuffer(payload), &getOrderURLResponse, headers)
if err != nil {
return getOrderURLResponse, err
}
if len(getOrderURLResponse.Message) > 0 {
err = errors.New(getOrderURLResponse.Message)
return getOrderURLResponse, err
}
return getOrderURLResponse, nil
}
func (a *ammana) GetPaymentDetail(invoiceCode string) (resp GetPaymentDetailResponse, err error) {
a.info().Println("Starting Get Payment Detail Ammana")
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("panic: %v", r)
}
if err != nil {
a.error().Println(err.Error())
}
}()
var getPaymentDetailReponse GetPaymentDetailResponse
// get auth data
auth, err := a.GetAuth()
if err != nil {
a.error().Println(err.Error())
return getPaymentDetailReponse, err
}
// set header
headers := make(map[string]string)
headers["Content-Type"] = "application/json"
headers["Accept"] = "application/json"
headers["Authorization"] = fmt.Sprintf("%s %s", auth.Data.TokenType, auth.Data.AccessToken)
pathGetPaymentDetail := fmt.Sprintf("clients/%s/orders/%s", a.ClientID, invoiceCode)
err = a.call("GET", pathGetPaymentDetail, nil, &getPaymentDetailReponse, headers)
if err != nil {
return getPaymentDetailReponse, err
}
if len(getPaymentDetailReponse.Message) > 0 {
err = errors.New(getPaymentDetailReponse.Message)
return getPaymentDetailReponse, err
}
return getPaymentDetailReponse, nil
}
func (a *ammana) SetSettleOrder(invoiceCode string) (resp SetSettlePaymentResponse, err error) {
a.info().Println("Starting Set Settle Order Ammana")
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("panic: %v", r)
}
if err != nil {
a.error().Println(err.Error())
}
}()
var setSettleOrderResponse SetSettlePaymentResponse
// get auth data
auth, err := a.GetAuth()
if err != nil {
a.error().Println(err.Error())
return setSettleOrderResponse, err
}
// set header
headers := make(map[string]string)
headers["Content-Type"] = "application/json"
headers["Accept"] = "application/json"
headers["Authorization"] = fmt.Sprintf("%s %s", auth.Data.TokenType, auth.Data.AccessToken)
pathSetSettleOrder := fmt.Sprintf("clients/%s/orders/%s/settle", a.ClientID, invoiceCode)
err = a.call("PATCH", pathSetSettleOrder, nil, &setSettleOrderResponse, headers)
if err != nil {
return setSettleOrderResponse, err
}
if len(setSettleOrderResponse.Message) > 0 {
err = errors.New(setSettleOrderResponse.Message)
return setSettleOrderResponse, err
}
return setSettleOrderResponse, nil
}
func (a *ammana) SetVoidOrder(invoiceCode string) (resp SetVoidPaymentResponse, err error) {
a.info().Println("Starting Set Void Order Ammana")
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("panic: %v", r)
}
if err != nil {
a.error().Println(err.Error())
}
}()
var setVoidOrderReponse SetVoidPaymentResponse
// get auth data
auth, err := a.GetAuth()
if err != nil {
return setVoidOrderReponse, err
}
// set header
headers := make(map[string]string)
headers["Content-Type"] = "application/json"
headers["Accept"] = "application/json"
headers["Authorization"] = fmt.Sprintf("%s %s", auth.Data.TokenType, auth.Data.AccessToken)
pathSetVoidOrder := fmt.Sprintf("clients/%s/orders/%s", a.ClientID, invoiceCode)
err = a.call("DELETE", pathSetVoidOrder, nil, &setVoidOrderReponse, headers)
if err != nil {
return setVoidOrderReponse, err
}
if len(setVoidOrderReponse.Message) > 0 {
err = errors.New(setVoidOrderReponse.Message)
return setVoidOrderReponse, err
}
return setVoidOrderReponse, nil
}