|
1
|
|
|
package campay |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"context" |
|
5
|
|
|
"encoding/json" |
|
6
|
|
|
"fmt" |
|
7
|
|
|
"net/http" |
|
8
|
|
|
"time" |
|
9
|
|
|
) |
|
10
|
|
|
|
|
11
|
|
|
// utilitiesService is the API client for the `/transaction/` endpoint |
|
12
|
|
|
type utilitiesService service |
|
13
|
|
|
|
|
14
|
|
|
// AirtimeTransfer transfers airtime to a mobile number |
|
15
|
|
|
// POST /api/utilities/airtime/transfer/ |
|
16
|
|
|
// API Doc: https://documenter.getpostman.com/view/2391374/T1LV8PVA#544cb091-1104-4bf1-b9ad-893c5067c925 |
|
17
|
|
|
func (service *utilitiesService) AirtimeTransfer(ctx context.Context, params *AirtimeTransferParams) (*AirtimeTransferResponse, *Response, error) { |
|
18
|
|
|
err := service.client.refreshToken(ctx) |
|
19
|
|
|
if err != nil { |
|
20
|
|
|
return nil, nil, err |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
request, err := service.client.newRequest(ctx, http.MethodPost, "/api/utilities/airtime/transfer/", params) |
|
24
|
|
|
if err != nil { |
|
25
|
|
|
return nil, nil, err |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
response, err := service.client.do(request) |
|
29
|
|
|
if err != nil { |
|
30
|
|
|
return nil, response, err |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
transaction := new(AirtimeTransferResponse) |
|
34
|
|
|
if err = json.Unmarshal(*response.Body, transaction); err != nil { |
|
35
|
|
|
return nil, response, err |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
return transaction, response, nil |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
// AirtimeTransferSync transfers airtime to a mobile number and waits for the transaction to be completed. |
|
42
|
|
|
// POST /api/utilities/airtime/transfer/ |
|
43
|
|
|
// API Doc: https://documenter.getpostman.com/view/2391374/T1LV8PVA#544cb091-1104-4bf1-b9ad-893c5067c925 |
|
44
|
|
|
func (service *utilitiesService) AirtimeTransferSync(ctx context.Context, params *AirtimeTransferParams) (*UtilitiesTransaction, *Response, error) { |
|
45
|
|
|
transaction, response, err := service.AirtimeTransfer(ctx, params) |
|
46
|
|
|
if err != nil { |
|
47
|
|
|
return nil, response, err |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
// wait for completion in 2 minutes |
|
51
|
|
|
counter := 1 |
|
52
|
|
|
for { |
|
53
|
|
|
status, response, err := service.TransactionStatus(ctx, transaction.Reference) |
|
54
|
|
|
if err != nil || !status.IsPending() || ctx.Err() != nil || counter == 30 { |
|
55
|
|
|
return status, response, err |
|
56
|
|
|
} |
|
57
|
|
|
time.Sleep(10 * time.Second) |
|
58
|
|
|
counter++ |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
// TransactionStatus checks the status of an initiated utility (Airtime) transaction |
|
63
|
|
|
// GET /api/utilities/transaction/(reference)/ |
|
64
|
|
|
// API Doc: https://documenter.getpostman.com/view/2391374/T1LV8PVA#e2686c97-565d-45dd-86f4-375e39268a44 |
|
65
|
|
|
func (service *utilitiesService) TransactionStatus(ctx context.Context, reference string) (*UtilitiesTransaction, *Response, error) { |
|
66
|
|
|
err := service.client.refreshToken(ctx) |
|
67
|
|
|
if err != nil { |
|
68
|
|
|
return nil, nil, err |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
request, err := service.client.newRequest(ctx, http.MethodGet, fmt.Sprintf("/api/utilities/transaction/%s/", reference), nil) |
|
72
|
|
|
if err != nil { |
|
73
|
|
|
return nil, nil, err |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
response, err := service.client.do(request) |
|
77
|
|
|
if err != nil { |
|
78
|
|
|
return nil, response, err |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
transaction := new(UtilitiesTransaction) |
|
82
|
|
|
if err = json.Unmarshal(*response.Body, transaction); err != nil { |
|
83
|
|
|
return nil, response, err |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return transaction, response, nil |
|
87
|
|
|
} |
|
88
|
|
|
|