-
Notifications
You must be signed in to change notification settings - Fork 0
/
remittance.go
157 lines (131 loc) · 4.52 KB
/
remittance.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 (
remittancesTokenURL = "/remittance/token/"
remittancesTransferURL = "/remittance/v1_0/transfer"
remittancesBalanceURL = "/remittance/v1_0/account/balance"
remittancesIsAccountActiveURL = "/remittance/v1_0/accountholder/msisdn/"
)
// RemittanceService handles communication with Remittance related methods of the
// Momo API to remit funds to local recipients from the diaspora
type RemittanceService interface {
Transfer(ctx context.Context, mobile 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)
}
// RemittanceServiceOp handles communication with the Remittance related methods of the Momo API.
type RemittanceServiceOp struct {
client *Client
}
// GetBalance returns the balance of the account
func (c *RemittanceServiceOp) GetBalance(ctx context.Context) (*BalanceResponse, error) {
req, err := c.client.NewRequest(ctx, http.MethodGet, remittancesBalanceURL, 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 *RemittanceServiceOp) IsPayeeActive(ctx context.Context, mobileNumber string) (bool, error) {
urlStr := fmt.Sprintf("%s/%s/active", remittancesIsAccountActiveURL, 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 Remittance API
func (c *RemittanceServiceOp) GetToken(ctx context.Context, apiKey, userID string) (string, error) {
req, err := c.client.NewRequest(ctx, http.MethodPost, remittancesTokenURL, 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, nil
}
// Transfer operation is used to transfer an amount from the owner’s account to a payee account.
func (c *RemittanceServiceOp) Transfer(ctx context.Context, mobile 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: mobile,
},
PayerMessage: payerMessage,
PayeeNote: payeeNote,
}
req, err := c.client.NewRequest(ctx, http.MethodPost, remittancesTransferURL, 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 *RemittanceServiceOp) GetTransfer(ctx context.Context, transferID string) (*PaymentStatusResponse, error) {
urlStr := fmt.Sprintf("%s/%s", remittancesTransferURL, 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
}