generated from NdoleStudio/go-http-client
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdisbursement_service.go
153 lines (123 loc) · 4.08 KB
/
disbursement_service.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
package mtnmomo
import (
"context"
"encoding/json"
"net/http"
"time"
)
// disbursementService is the API client for the `/disbursement` endpoint
type disbursementsService service
// Token is used to create an access token which can then be used to authorize and authenticate towards the other end-points of the API.
//
// API Docs: https://momodeveloper.mtn.com/docs/services/disbursement/operations/CreateAccessToken
func (service *disbursementsService) Token(ctx context.Context) (*AuthToken, *Response, error) {
request, err := service.client.newRequest(ctx, http.MethodPost, "/disbursement/token/", nil)
if err != nil {
return nil, nil, err
}
service.client.addBasicAuth(request)
response, err := service.client.do(request)
if err != nil {
return nil, response, err
}
authToken := new(AuthToken)
if err = json.Unmarshal(*response.Body, authToken); err != nil {
return nil, response, err
}
return authToken, response, err
}
// Transfer is used to transfer an amount from the owner account to a payee account.
//
// API Docs: https://momodeveloper.mtn.com/docs/services/disbursement/operations/Transfer?
func (service *disbursementsService) Transfer(
ctx context.Context,
referenceID string,
params *TransferParams,
callbackURL *string,
) (*Response, error) {
err := service.refreshToken(ctx)
if err != nil {
return nil, err
}
request, err := service.client.newRequest(ctx, http.MethodPost, "/disbursement/v1_0/transfer", params)
if err != nil {
return nil, err
}
if callbackURL != nil && len(*callbackURL) != 0 {
service.client.addCallbackURL(request, *callbackURL)
}
service.client.addTargetEnvironment(request)
service.client.addReferenceID(request, referenceID)
service.client.addAccessToken(request)
response, err := service.client.do(request)
return response, err
}
// GetTransferStatus is used to get the status of a transfer.
//
// API Docs: https://momodeveloper.mtn.com/docs/services/disbursement/operations/GetTransferStatus
func (service *disbursementsService) GetTransferStatus(
ctx context.Context,
referenceID string,
) (*DisbursementTransactionStatus, *Response, error) {
err := service.refreshToken(ctx)
if err != nil {
return nil, nil, err
}
request, err := service.client.newRequest(ctx, http.MethodGet, "/disbursement/v1_0/transfer/"+referenceID, nil)
if err != nil {
return nil, nil, err
}
service.client.addTargetEnvironment(request)
service.client.addAccessToken(request)
response, err := service.client.do(request)
if err != nil {
return nil, response, err
}
status := new(DisbursementTransactionStatus)
if err = json.Unmarshal(*response.Body, status); err != nil {
return nil, response, err
}
status.ReferenceID = referenceID
return status, response, err
}
// GetAccountBalance returns the balance of the account.
//
// API Docs: https://momodeveloper.mtn.com/docs/services/disbursement/operations/GetAccountBalance
func (service *disbursementsService) GetAccountBalance(ctx context.Context) (*AccountBalance, *Response, error) {
err := service.refreshToken(ctx)
if err != nil {
return nil, nil, err
}
request, err := service.client.newRequest(ctx, http.MethodGet, "/disbursement/v1_0/account/balance", nil)
if err != nil {
return nil, nil, err
}
service.client.addAccessToken(request)
service.client.addTargetEnvironment(request)
response, err := service.client.do(request)
if err != nil {
return nil, response, err
}
balance := new(AccountBalance)
if err = json.Unmarshal(*response.Body, balance); err != nil {
return nil, response, err
}
return balance, response, err
}
func (service *disbursementsService) tokenIsValid() bool {
return time.Now().UTC().Unix() < service.client.accessTokenExpiresAt
}
func (service *disbursementsService) refreshToken(ctx context.Context) error {
service.client.accessTokenLock.Lock()
defer service.client.accessTokenLock.Unlock()
if service.tokenIsValid() {
return nil
}
token, _, err := service.Token(ctx)
if err != nil {
return err
}
service.client.accessToken = token.AccessToken
service.client.accessTokenExpiresAt = time.Now().UTC().Unix() + token.ExpiresIn - 100
return nil
}