-
Notifications
You must be signed in to change notification settings - Fork 1
/
vpos.go
281 lines (235 loc) · 7.78 KB
/
vpos.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
package vposgo
import (
"encoding/json"
"errors"
"fmt"
"strings"
"github.com/lithammer/shortuuid"
)
const (
baseURL = "https://vpos.ao/api/v1"
TypePayment TransactionType = "payment"
TypeRefund TransactionType = "refund"
TypeAuthorization TransactionType = "authorization"
TypeCancelation TransactionType = "cancelation"
)
type (
TransactionType string
VPOS struct {
Token string //Your VPOS Token
PosID int64 //Your GPO POS ID
PaymentCallbackURL string //Your Payment Callback URL
RefundCallbackURL string //Your Refund Callback URL
SupervisorCard string //Your GPO Supervisor Card
}
PaymentTransaction struct {
Type TransactionType `json:"type"`
PosID int64 `json:"pos_id"`
Mobile string `json:"mobile"`
Amount string `json:"amount"`
CallbackURL string `json:"callback_url"`
}
AuthorizedPaymentTransaction struct {
Type TransactionType `json:"type"`
ParentTransactionID string `json:"parent_transaction_id"`
Amount string `json:"amount"`
CallbackURL string `json:"callback_url"`
}
RefundTransaction struct {
Type TransactionType `json:"type"`
ParentTransactionID string `json:"parent_transaction_id"`
CallbackURL string `json:"call_back_url"`
}
TransactionStatus struct {
SecondsRemaining json.Number `json:"eta"`
CreatedAt string `json:"inserted_at"`
}
Transaction struct {
ID string `json:"id"`
Amount string `json:"amount"`
ClearingPeriod string `json:"clearing_period"`
Mobile string `json:"mobile"`
ParentTransactionID string `json:"parent_transaction_id"`
PosID int64 `json:"pos_id"`
Status string `json:"status"`
StatusDatetime string `json:"status_datetime"`
StatusReason string `json:"status_reason"`
Type TransactionType `json:"type"`
}
)
func NewVPOS(posID int64, token, paymentCallbackURL, refundCallbackURL, supervisorCard string) *VPOS {
return &VPOS{
Token: token,
PosID: posID,
PaymentCallbackURL: paymentCallbackURL,
RefundCallbackURL: refundCallbackURL,
SupervisorCard: supervisorCard,
}
}
func GetStatusReason(code int64) (reason string, err error) {
reason, ok := statusReason[code]
if !ok {
return "", errors.New("invalid status reason")
}
return
}
func (v *VPOS) GetRequest(transactionID string) (result int64, err error) {
url := fmt.Sprintf("%s/requests/%s", baseURL, transactionID)
response, err := httpGet(
url,
map[string]string{
"Authorization": fmt.Sprintf("Bearer %s", v.Token),
},
)
if err != nil {
return
}
var status TransactionStatus
if err = json.Unmarshal(response, &status); err != nil {
return
}
floatResult, err := status.SecondsRemaining.Float64()
if err != nil {
return
}
return int64(floatResult), nil
}
// transactionType is either 'payment' or 'authorization'
// if transactionType is 'payment', it will create a new payment transaction
// if transactionType is 'authorization', it will create a new authorization transaction
func (v *VPOS) PaymentTransaction(transactionType TransactionType, mobile, amount string) (transactionID, idempotencyKey, nonce string, timeRemaining int64, err error) {
if !(transactionType == TypePayment || transactionType == TypeAuthorization) {
err = errors.New("invalid transaction type")
return
}
url := fmt.Sprintf("%s/transactions/%s", baseURL, transactionID)
idempotencyKey, nonce = shortUUID(), shortUUID()
request := PaymentTransaction{
Type: transactionType,
PosID: v.PosID,
Mobile: mobile,
Amount: amount,
CallbackURL: fmt.Sprintf("%s?nonce=%s", v.PaymentCallbackURL, nonce),
}
_, headers, err := httpPost(
url,
map[string]string{
"Authorization": fmt.Sprintf("Bearer %s", v.Token),
"Idempotency-Key": idempotencyKey,
},
request,
)
if err != nil {
return
}
location := headers.Get("Location")
if location == "" {
err = errors.New("could not retrieve transaction ID from VPOS response")
return
}
locationParts := strings.Split(location, "/")
locationPartsSize := len(locationParts)
if locationPartsSize == 0 {
err = errors.New("could not retrieve transaction ID from VPOS response")
return
}
transactionID = locationParts[locationPartsSize-1]
timeRemaining, _ = v.GetRequest(transactionID)
return
}
// It creates a new payment transaction from a previously accepted authorization transaction
func (v *VPOS) PaymentWithAuthorization(parent_transaction_id, amount string) (transactionID, idempotencyKey, nonce string, timeRemaining int64, err error) {
url := fmt.Sprintf("%s/transactions/%s", baseURL, transactionID)
idempotencyKey, nonce = shortUUID(), shortUUID()
request := AuthorizedPaymentTransaction{
Type: TypePayment,
ParentTransactionID: parent_transaction_id,
Amount: amount,
CallbackURL: fmt.Sprintf("%s?nonce=%s", v.PaymentCallbackURL, nonce),
}
_, headers, err := httpPost(
url,
map[string]string{
"Authorization": fmt.Sprintf("Bearer %s", v.Token),
"Idempotency-Key": idempotencyKey,
},
request,
)
if err != nil {
return
}
location := headers.Get("Location")
if location == "" {
err = errors.New("could not retrieve transaction ID from VPOS response")
return
}
locationParts := strings.Split(location, "/")
locationPartsSize := len(locationParts)
if locationPartsSize == 0 {
err = errors.New("could not retrieve transaction ID from VPOS response")
return
}
transactionID = locationParts[locationPartsSize-1]
timeRemaining, _ = v.GetRequest(transactionID)
return
}
// transactionType is either 'refund' or 'cancelation'
// if transactionType is 'refund', it will create a new refund transaction
// if transactionType is 'cancelation', it will create a new cancelation transaction for a previously accepted payment authorization
func (v *VPOS) RefundOrCancelation(transactionType TransactionType, parentTransactionID string) (transactionID, idempotencyKey, nonce string, timeRemaining int64, err error) {
if !(transactionType == TypeRefund || transactionType == TypeCancelation) {
err = errors.New("invalid transaction type")
return
}
url := fmt.Sprintf("%s/transactions/%s", baseURL, transactionID)
idempotencyKey, nonce = shortUUID(), shortUUID()
request := RefundTransaction{
Type: transactionType,
ParentTransactionID: parentTransactionID,
CallbackURL: fmt.Sprintf("%s?nonce=%s", v.RefundCallbackURL, nonce),
}
_, headers, err := httpPost(
url,
map[string]string{
"Authorization": fmt.Sprintf("Bearer %s", v.Token),
"Idempotency-Key": idempotencyKey,
},
request,
)
if err != nil {
return
}
location := headers.Get("Location")
if location == "" {
err = errors.New("could not retrieve transaction ID from VPOS response")
return
}
locationParts := strings.Split(location, "/")
locationPartsSize := len(locationParts)
if locationPartsSize == 0 {
err = errors.New("could not retrieve transaction ID from VPOS response")
return
}
transactionID = locationParts[locationPartsSize-1]
timeRemaining, _ = v.GetRequest(transactionID)
return
}
func (v *VPOS) GetTransaction(transactionID string) (transaction Transaction, err error) {
url := fmt.Sprintf("%s/transactions/%s", baseURL, transactionID)
response, err := httpGet(
url,
map[string]string{
"Authorization": fmt.Sprintf("Bearer %s", v.Token),
},
)
if err != nil {
return
}
if err = json.Unmarshal(response, &transaction); err != nil {
return
}
return
}
func shortUUID() string {
return shortuuid.New()
}