generated from NdoleStudio/go-http-client
-
Notifications
You must be signed in to change notification settings - Fork 1
/
collection_service.go
216 lines (174 loc) · 5.98 KB
/
collection_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
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
package mtnmomo
import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
)
// collectionService is the API client for the `/collection` endpoint
type collectionService 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/collection/operations/token-POST
func (service *collectionService) Token(ctx context.Context) (*AuthToken, *Response, error) {
request, err := service.client.newRequest(ctx, http.MethodPost, "/collection/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
}
// RequestToPay is used to request a payment from a consumer (Payer).
//
// API Docs: https://momodeveloper.mtn.com/docs/services/collection/operations/requesttopay-POST
func (service *collectionService) RequestToPay(
ctx context.Context,
referenceID string,
params *RequestToPayParams,
callbackURL *string,
) (*Response, error) {
err := service.refreshToken(ctx)
if err != nil {
return nil, err
}
request, err := service.client.newRequest(ctx, http.MethodPost, "/collection/v1_0/requesttopay", 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
}
// GetRequestToPayStatus is used to get the status of a request to pay.
//
// API Docs: https://momodeveloper.mtn.com/docs/services/collection/operations/requesttopay-referenceId-GET
func (service *collectionService) GetRequestToPayStatus(
ctx context.Context,
referenceID string,
) (*CollectionTransactionStatus, *Response, error) {
err := service.refreshToken(ctx)
if err != nil {
return nil, nil, err
}
request, err := service.client.newRequest(ctx, http.MethodGet, "/collection/v1_0/requesttopay/"+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(CollectionTransactionStatus)
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/collection/operations/get-v1_0-account-balance?
func (service *collectionService) 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, "/collection/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
}
// ValidateAccountHolderStatus is used to check if an account holder is registered and active in the system.
//
// API Docs: https://momodeveloper.mtn.com/api-details#api=collection&operation=ValidateAccountHolderStatus
func (service *collectionService) ValidateAccountHolderStatus(ctx context.Context, IDType AccountHolderIDType, ID string) (*AccountHolderStatus, *Response, error) {
err := service.refreshToken(ctx)
if err != nil {
return nil, nil, err
}
uri := fmt.Sprintf("/collection/v1_0/accountholder/%s/%s/active", IDType, ID)
request, err := service.client.newRequest(ctx, http.MethodGet, uri, 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
}
status := new(AccountHolderStatus)
if err = json.Unmarshal(*response.Body, status); err != nil {
return nil, response, err
}
return status, response, err
}
// GetBasicUserinfo returns personal information of the account holder.
//
// API Docs: https://momodeveloper.mtn.com/api-details#api=collection&operation=GetBasicUserinfo
func (service *collectionService) GetBasicUserinfo(ctx context.Context, IDType AccountHolderIDType, ID string) (*BasicUserInfo, *Response, error) {
err := service.refreshToken(ctx)
if err != nil {
return nil, nil, err
}
uri := fmt.Sprintf("/collection/v1_0/accountholder/%s/%s/basicuserinfo", IDType, ID)
request, err := service.client.newRequest(ctx, http.MethodGet, uri, 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
}
status := new(BasicUserInfo)
if err = json.Unmarshal(*response.Body, status); err != nil {
return nil, response, err
}
return status, response, err
}
func (service *collectionService) tokenIsValid() bool {
return time.Now().UTC().Unix() < service.client.accessTokenExpiresAt
}
func (service *collectionService) 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
}