-
Notifications
You must be signed in to change notification settings - Fork 0
/
disbursement.go
157 lines (131 loc) · 4.59 KB
/
disbursement.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
package gomomo
import (
"context"
"encoding/json"
"fmt"
"net/http"
)
const (
disbursementsTokenURL = "/disbursement/token/"
disbursementsTransferURL = "/disbursement/v1_0/transfer"
disbursementsBalanceURL = "/disbursement/v1_0/account/balance"
disbursementsIsAccountActiveURL = "/disbursement/v1_0/accountholder/msisdn/"
)
// DisbursementService handles communication with Disbursement related methods of the
// Momo API to automatically deposit funds into multiple users accounts
type DisbursementService interface {
Transfer(ctx context.Context, mobileNumber string, amount int64, id, payeeNote, payerMessage, currency string) (string, error)
GetTransfer(ctx context.Context, transactionID string) (*PaymentStatusResponse, error)
GetBalance(ctx context.Context) (*BalanceResponse, error)
IsPayeeActive(ctx context.Context, mobileNumber string) (bool, error)
GetToken(ctx context.Context, apiKey, userID string) (string, error)
}
// DisbursementServiceOp handles communication with the Disbursement related methods of the Momo API.
type DisbursementServiceOp struct {
client *Client
}
// GetBalance returns the balance of the account
func (c *DisbursementServiceOp) GetBalance(ctx context.Context) (*BalanceResponse, error) {
req, err := c.client.NewRequest(ctx, http.MethodGet, disbursementsBalanceURL, nil)
if err != nil {
return nil, err
}
res, err := c.client.Do(ctx, req)
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("response code: %d with error %s", res.StatusCode, string(res.Body))
}
balance := &BalanceResponse{}
err = json.Unmarshal(res.Body, balance)
if err != nil {
return nil, err
}
return balance, nil
}
// IsPayeeActive checks if an account holder is registered and active in the system
func (c *DisbursementServiceOp) IsPayeeActive(ctx context.Context, mobileNumber string) (bool, error) {
urlStr := fmt.Sprintf("%s/%s/active", disbursementsIsAccountActiveURL, mobileNumber)
req, err := c.client.NewRequest(ctx, http.MethodGet, urlStr, nil)
if err != nil {
return false, err
}
res, err := c.client.Do(ctx, req)
if err != nil {
return false, err
}
if res.StatusCode != http.StatusOK {
return false, fmt.Errorf("response code: %d with error %s", res.StatusCode, string(res.Body))
}
return true, nil
}
// GetToken creates an access token which can then be used to authorize and authenticate towards the other end-points of the Disbursement API
func (c *DisbursementServiceOp) GetToken(ctx context.Context, apiKey, userID string) (string, error) {
req, err := c.client.NewRequest(ctx, http.MethodPost, disbursementsTokenURL, nil)
if err != nil {
return "", err
}
req.SetBasicAuth(userID, apiKey)
res, err := c.client.Do(ctx, req)
if err != nil {
return "", err
}
token := &tokenResponse{}
err = json.Unmarshal(res.Body, token)
if err != nil {
return "", err
}
c.client.Token = token.AccessToken
return token.AccessToken, err
}
// Transfer operation is used to transfer an amount from the owner’s account to a payee account.
func (c *DisbursementServiceOp) Transfer(ctx context.Context, mobileNumber string, amount int64, id, payeeNote, payerMessage, currency string) (string, error) {
if c.client.Environment == "sandbox" {
currency = "EUR"
}
requestBody := transferRequestBody{
Amount: amount,
Currency: currency,
ExternalID: id,
Payee: paymentDetails{
PartyIDType: "MSISDN",
PartyID: mobileNumber,
},
PayerMessage: payerMessage,
PayeeNote: payeeNote,
}
req, err := c.client.NewRequest(ctx, http.MethodPost, disbursementsTransferURL, requestBody)
if err != nil {
return "", err
}
res, err := c.client.Do(ctx, req)
if err != nil {
return "", err
}
if res.StatusCode != http.StatusAccepted {
return "", fmt.Errorf("response code: %d with error %s", res.StatusCode, string(res.Body))
}
return req.Header.Get("X-Reference-Id"), nil
}
// GetTransfer retrieves transfer information using the transactionId returned by Transfer
func (c *DisbursementServiceOp) GetTransfer(ctx context.Context, transferID string) (*PaymentStatusResponse, error) {
urlStr := fmt.Sprintf("%s/%s", disbursementsTransferURL, transferID)
req, err := c.client.NewRequest(ctx, http.MethodGet, urlStr, nil)
if err != nil {
return nil, err
}
res, err := c.client.Do(ctx, req)
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("response code: %d with error %s", res.StatusCode, string(res.Body))
}
status := &PaymentStatusResponse{}
err = json.Unmarshal(res.Body, status)
if err != nil {
return nil, err
}
return status, nil
}