-
Notifications
You must be signed in to change notification settings - Fork 1
/
beneficiary.go
222 lines (185 loc) · 7.84 KB
/
beneficiary.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
217
218
219
220
221
222
package fincra
import (
"context"
"errors"
)
const (
beneficiariesUrl = "/profile/beneficiaries/business/"
)
// Beneficiary Account Type
type BeneficiaryType string
// This is the type of account payments can be made to e.g bank_account
type PaymentDestinationType string
const (
Individual BeneficiaryType = "individual"
Corporate BeneficiaryType = "corporate"
MobileMoneyWallet PaymentDestinationType = "mobile_money_wallet"
BankAccount PaymentDestinationType = "bank_account"
CryptoWallet PaymentDestinationType = "crypto_wallet"
FliqPayWallet PaymentDestinationType = "fliqpay_wallet"
)
// beneficiary := CreateBeneficiaryBody{
// FirstName: "John",
// AccountHolderName: "John Doe",
// Type: Individual,
// ... other field assignments
// }
type CreateBeneficiaryBody struct {
FirstName string `json:"firstName"`
LastName string `json:"lastName,omitempty"`
Email string `json:"email,omitempty"`
PhoneNumber string `json:"phoneNumber,omitempty"`
AccountHolderName string `json:"accountHolderName"`
Bank `json:"bank,omitempty"`
Type BeneficiaryType `json:"type"` // individual or corporate
Currency string `json:"currency"`
PaymentDestination PaymentDestinationType `json:"paymentDestination"`
DestinationAddress string `json:"destinationAddress"`
UniqueIdentifier string `json:"uniqueIdentifier,omitempty"`
BusinessId string `json:"businessId"` // needed to be passed in params
}
type UpdateBeneficiaryBody struct {
FirstName string `json:"firstName"`
LastName string `json:"lastName,omitempty"`
Email string `json:"email,omitempty"`
PhoneNumber string `json:"phoneNumber,omitempty"`
AccountHolderName string `json:"accountHolderName"`
Bank `json:"bank,omitempty"`
Type BeneficiaryType `json:"type"` // individual or corporate
Currency string `json:"currency"`
PaymentDestination PaymentDestinationType `json:"paymentDestination"`
DestinationAddress string `json:"destinationAddress"`
UniqueIdentifier string `json:"uniqueIdentifier,omitempty"`
BusinessId string `json:"businessId"` // needed to be passed in params
BeneficiaryId string `json:"beneficiaryId"` // needed to be passed in params
}
type Bank struct {
Name string `json:"name,omitempty"`
Code string `json:"code,omitempty"`
SortCode string `json:"sortCode,omitempty"`
SwiftCode string `json:"swiftCode,omitempty"`
Address `json:"address,omitempty"`
}
type Address struct {
Country string `json:"country,omitempty"`
State string `json:"state,omitempty"`
Street string `json:"street,omitempty"`
Zip string `json:"zip,omitempty"`
City string `json:"city,omitempty"`
}
type createBeneficiaryRequest struct {
FirstName string `json:"firstName"`
LastName string `json:"lastName,omitempty"`
Email string `json:"email,omitempty"`
PhoneNumber string `json:"phoneNumber,omitempty"`
AccountHolderName string `json:"accountHolderName"`
Bank `json:"bank,omitempty"`
Type BeneficiaryType `json:"type"` // individual or corporate
Currency string `json:"currency"`
PaymentDestination PaymentDestinationType `json:"paymentDestination"`
DestinationAddress string `json:"destinationAddress"`
UniqueIdentifier string `json:"uniqueIdentifier,omitempty"`
}
type GetAllBeneficiariesParams struct {
BusinessId string `json:"businessId"`
Page string `json:"page"`
PerPage string `json:"perPage"`
}
type getBeneficiariesRequest struct {
Page string `json:"page,omitempty"`
PerPage string `json:"perPage,omitempty"`
}
type GetBeneficiaryParams struct {
BusinessId string `json:"businessId"`
BeneficiaryId string `json:"beneficiaryId"`
}
// CreateBeneficiary creates a beneficiary for a business
// client := fincra.NewClient(apiKey)
// resp, err := client.CreateBeneficiary(ctx, &client.CreateBeneficiaryBody{})
func (c *Client) CreateBeneficiary(ctx context.Context, beneficiary *CreateBeneficiaryBody) (Response, error) {
if beneficiary.BusinessId == "" {
return Response{}, errors.New("business ID is required for beneficiary")
}
path := beneficiariesUrl + beneficiary.BusinessId
// Create a new request object without the BusinessId field
request := createBeneficiaryRequest{
FirstName: beneficiary.FirstName,
LastName: beneficiary.LastName,
Email: beneficiary.Email,
PhoneNumber: beneficiary.PhoneNumber,
AccountHolderName: beneficiary.AccountHolderName,
Bank: beneficiary.Bank,
Type: beneficiary.Type,
Currency: beneficiary.Currency,
PaymentDestination: beneficiary.PaymentDestination,
DestinationAddress: beneficiary.DestinationAddress,
UniqueIdentifier: beneficiary.UniqueIdentifier,
}
return c.sendRequest(ctx, "POST", path, &request)
}
const businessIdRequiredError = "businessId is required to fetch the beneficiary"
// GetAllBeneficiaries gets all beneficiaries under a business
func (c *Client) GetAllBeneficiaries(ctx context.Context, params *GetAllBeneficiariesParams) (Response, error) {
if params.BusinessId == "" {
return Response{}, errors.New(businessIdRequiredError)
}
if params.Page == "" {
params.Page = "1"
}
if params.PerPage == "" {
params.PerPage = "10"
}
path := beneficiariesUrl + params.BusinessId
request := getBeneficiariesRequest{
Page: params.Page,
PerPage: params.PerPage,
}
return c.sendRequest(ctx, "GET", path, &request)
}
// GetBeneficiary gets a beneficiary of the business
func (c *Client) GetBeneficiary(ctx context.Context, params *GetBeneficiaryParams) (Response, error) {
if params.BusinessId == "" {
return Response{}, errors.New(businessIdRequiredError)
}
if params.BeneficiaryId == "" {
return Response{}, errors.New("beneficiaryId is required to fetch the beneficiary")
}
path := beneficiariesUrl + params.BusinessId + "/" + params.BeneficiaryId
return c.sendRequest(ctx, "GET", path, nil)
}
// UpdateBeneficiary updates a beneficiary of the business
// see [https://docs.fincra.com/reference/update-a-beneficiary] for required parameters
func (c *Client) UpdateBeneficiary(ctx context.Context, body *UpdateBeneficiaryBody) (Response, error) {
if body.BusinessId == "" {
return Response{}, errors.New("businessId is required to update the beneficiary")
}
if body.BeneficiaryId == "" {
return Response{}, errors.New("beneficiaryId is required to update the beneficiary")
}
path := beneficiariesUrl + body.BusinessId + "/" + body.BeneficiaryId
request := createBeneficiaryRequest{
FirstName: body.FirstName,
LastName: body.LastName,
Email: body.Email,
PhoneNumber: body.PhoneNumber,
AccountHolderName: body.AccountHolderName,
Bank: body.Bank,
Type: body.Type,
Currency: body.Currency,
PaymentDestination: body.PaymentDestination,
DestinationAddress: body.DestinationAddress,
UniqueIdentifier: body.UniqueIdentifier,
}
return c.sendRequest(ctx, "PATCH", path, &request)
}
// DeleteBeneficiary deletes a beneficiary of the business
func (c *Client) DeleteBeneficiary(ctx context.Context, params *GetBeneficiaryParams) (Response, error) {
if params.BusinessId == "" {
return Response{}, errors.New("businessId is required to fetch the beneficiary")
}
if params.BeneficiaryId == "" {
return Response{}, errors.New("beneficiaryId is required to fetch the beneficiary")
}
path := beneficiariesUrl + params.BusinessId + "/" + params.BeneficiaryId
return c.sendRequest(ctx, "DELETE", path, nil)
}