-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathkredivo.go
261 lines (198 loc) · 6.67 KB
/
kredivo.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
package kredivo
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/url"
"strings"
"time"
)
//kredivo entry struct for kredivo
type kredivo struct {
serverKey string
userCancelURI string
pushURI string
backToStoreURI string
client *kredivoHttpClient
Env string
*logger
}
/*New function, create kredivo pointer
Required parameter :
1. your Kredivo Merchant Key
2. pushURI parameter, your Notification URL (POST METHOD) that used by Kredivo to Send Payment Notification
3. backToStoreURI parameter,
your redirect page when user finished their transaction,
(Your Thank You Page's Website)
4. HTTP Request Timeout
*/
func New(serverKey, pushURI, backToStoreURI string, timeout time.Duration) *kredivo {
httpRequest := newRequest(timeout)
return &kredivo{
serverKey: serverKey,
pushURI: pushURI,
backToStoreURI: backToStoreURI,
//httpClient
client: httpRequest,
//set default env to SandBox,
//latter just simply change with kredivo.Env = Production
Env: SandBox.String(),
logger: newLogger(),
}
}
//call function
func (r *kredivo) call(method, path string, body io.Reader, v interface{}, headers map[string]string) error {
r.info().Println("Starting http call..")
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
path = fmt.Sprintf("%s%s", r.Env, path)
return r.client.exec(method, path, body, v, headers)
}
//Checkout Method
func (r *kredivo) Checkout(order *Order) ServiceResult {
r.info().Println("Starting checkout..")
//create map headers
headers := make(map[string]string)
headers["Content-Type"] = "application/json"
headers["Accept"] = "application/json"
//set ServerKey, PushURI, and BackToStoreURI to order request
order.ServerKey = r.serverKey
order.PushURI = r.pushURI
order.BackToStoreURI = r.backToStoreURI
//Marshal Order
payload, err := json.Marshal(order)
if err != nil {
r.error().Println(err.Error())
return ServiceResult{Error: err}
}
var checkoutResponse CheckoutResponse
//call checkout endpoint
err = r.call("POST", "checkout_url", bytes.NewBuffer(payload), &checkoutResponse, headers)
if err != nil {
r.error().Println(err.Error())
return ServiceResult{Error: err}
}
return ServiceResult{Result: checkoutResponse}
}
//GetPayments Method, for get available list payment for specific order
func (r *kredivo) GetPayments(paymentRequest *PaymentRequest) ServiceResult {
r.info().Println("Starting get available payments..")
//create map headers
headers := make(map[string]string)
headers["Content-Type"] = "application/json"
headers["Accept"] = "application/json"
//set ServerKey, to payment request
paymentRequest.ServerKey = r.serverKey
//Marshal Order
payload, err := json.Marshal(paymentRequest)
if err != nil {
r.error().Println(err.Error())
return ServiceResult{Error: err}
}
var paymentResponse PaymentResponse
//call checkout endpoint
err = r.call("POST", "payments", bytes.NewBuffer(payload), &paymentResponse, headers)
if err != nil {
r.error().Println(err.Error())
return ServiceResult{Error: err}
}
return ServiceResult{Result: paymentResponse}
}
//Confirm Method, for confirm order status
func (r *kredivo) Confirm(confirmRequest *ConfirmRequest) ServiceResult {
r.info().Println("Starting Confirm payment..")
//create map headers
headers := make(map[string]string)
var confirmationResponse ConfirmationResponse
//set query params, transaction_id and signature_key
queryParams := url.Values{}
queryParams.Set("transaction_id", confirmRequest.TransactionID)
queryParams.Set("signature_key", confirmRequest.SignatureKey)
path := fmt.Sprintf("update?%s", queryParams.Encode())
//call confirm endpoint
err := r.call("GET", path, nil, &confirmationResponse, headers)
if err != nil {
r.error().Println(err.Error())
return ServiceResult{Error: err}
}
return ServiceResult{Result: confirmationResponse}
}
//Cancel Method, for cancel transaction
func (r *kredivo) Cancel(cancelRequest *CancelRequest) ServiceResult {
r.info().Println("Starting cancel..")
//create map headers
headers := make(map[string]string)
headers["Content-Type"] = "application/json"
headers["Accept"] = "application/json"
//set ServerKey, to cancel request
cancelRequest.ServerKey = r.serverKey
//Marshal Cancel Request
payload, err := json.Marshal(cancelRequest)
if err != nil {
r.error().Println(err.Error())
return ServiceResult{Error: err}
}
var cancelResponse CancelResponse
//call cancel transaction endpoint
err = r.call("POST", "cancel_transaction", bytes.NewBuffer(payload), &cancelResponse, headers)
if err != nil {
r.error().Println(err.Error())
return ServiceResult{Error: err}
}
return ServiceResult{Result: cancelResponse}
}
//PartialCancel Method, for cancel partial transaction
func (r *kredivo) PartialCancel(partialRequest *PartialCancelRequest) ServiceResult {
r.info().Println("Starting partial cancel..")
//create map headers
headers := make(map[string]string)
headers["Content-Type"] = "application/json"
headers["Accept"] = "application/json"
//set ServerKey, to partial cancel request
partialRequest.ServerKey = r.serverKey
//Marshal Partial Cancel Request
payload, err := json.Marshal(partialRequest)
if err != nil {
r.error().Println(err.Error())
return ServiceResult{Error: err}
}
var cancelResponse CancelResponse
//call partial cancel transaction endpoint
err = r.call("POST", "partial_cancel_transaction", bytes.NewBuffer(payload), &cancelResponse, headers)
if err != nil {
r.error().Println(err.Error())
return ServiceResult{Error: err}
}
return ServiceResult{Result: cancelResponse}
}
//PartialCancel Method, for cancel partial transaction
func (r *kredivo) TransactionStatus(tansactionStatusRequest *TransactionStatusRequest) ServiceResult {
r.info().Println("Starting get transaction status..")
//create map headers
headers := make(map[string]string)
headers["Content-Type"] = "application/json"
headers["Accept"] = "application/json"
//set ServerKey, to partial cancel request
tansactionStatusRequest.ServerKey = r.serverKey
//Marshal Partial Cancel Request
payload, err := json.Marshal(tansactionStatusRequest)
if err != nil {
r.error().Println(err.Error())
return ServiceResult{Error: err}
}
var transactionStatusReponse TransactionStatusResponse
//call partial cancel transaction endpoint
err = r.call("POST", "transaction/status", bytes.NewBuffer(payload), &transactionStatusReponse, headers)
if err != nil {
r.error().Println(err.Error())
return ServiceResult{Error: err}
}
return ServiceResult{Result: transactionStatusReponse}
}
func GenerateServiceResult(data interface{}, err error) ServiceResult {
var output ServiceResult
output = ServiceResult{Result: data, Error: err}
return output
}