/
transactions.go
456 lines (389 loc) · 17.7 KB
/
transactions.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
package clearbank
import (
"context"
"fmt"
"net/http"
"strconv"
"time"
"github.com/google/uuid"
)
type TransactionsClient interface {
InitiateFPSTransactions(context.Context, CreateTransactionsPayload) (TransactionsCreatedResponse, error)
InitiateCHAPSPayment(context.Context, CreateCHAPSPaymentPayload) (PaymentInitiatedResponse, error)
InitiateFPSPaymentOriginatedOverseas(context.Context, CreateFPSPaymentOriginatedOverseasPayload) (TransactionsCreatedResponse, error)
FetchTransactions(ctx context.Context, params FetchTransactionsParams) (TransactionsResponse, error)
FetchTransactionForAccount(ctx context.Context, accountID uuid.UUID, trxID uuid.UUID) (TransactionResponse, error)
FetchTransactionsForAccount(ctx context.Context, accountID uuid.UUID, params FetchTransactionsParams) (TransactionsResponse, error)
FetchTransactionForVirtualAccount(ctx context.Context, accountID, virtualAccountID uuid.UUID, trxID uuid.UUID) (TransactionResponse, error)
FetchTransactionsForVirtualAccount(ctx context.Context, accountID, virtualAccountID uuid.UUID, params FetchTransactionsParams) (TransactionsResponse, error)
}
type CreditTransferOtherIdentification struct {
// Identification assigned by an institution.
Identification string `json:"identification"`
// Name of the identification scheme.
SchemeName struct {
// Name of the identification scheme in coded form.
Code string `json:"code"`
// Name of the identification scheme in free-form text.
Proprietary string `json:"proprietary"`
} `json:"schemeName"`
}
type CreditTransfer struct {
// Identification of the payment instruction.
PaymentIdentification struct {
// Unique identification, as assigned by an instructing party
// for an instructed party, to unambiguously identify the instruction.
InstructionIdentification string `json:"instructionIdentification"`
// Unique identification, as assigned by the initiating party to unambiguously identify the transaction.
// This identification is passed on unchanged throughout the entire end-to-end chain.
EndToEndIdentification string `json:"endToEndIdentification"`
} `json:"paymentIdentification"`
// Indicates the amount and the currency used in a given transaction or account balance.
Amount struct {
Currency string `json:"currency"`
InstructedAmount float64 `json:"instructedAmount"`
} `json:"amount"`
// The name and, optionally, the legal entity identifier of the account.
Creditor struct {
// The name of the account holder.
Name string `json:"name"`
// The legal entity identifier of the account holder (eg: charity number).
// This should be supplied if known.
LegalEntityIndentifier string `json:"legalEntityIdentifier,omitempty"`
} `json:"creditor"`
// Information about the counterpart in a given transaction.
CreditorAccount struct {
// The identifiable information of an account.
Identification struct {
// The International Bank Account Number (IBAN).
IBAN string `json:"iban,omitempty"`
// Unique identification of an account,
// as assigned by the account servicer,
// using an identification scheme.
Other *CreditTransferOtherIdentification `json:"other,omitempty"`
} `json:"identification"`
} `json:"creditorAccount"`
// Information supplied to enable the matching/reconciliation of an entry
// with the items that the payment is intended to settle,
// such as commercial invoices in an accounts’ receivable system.
RemittanceInformation struct {
// Information supplied by the remitter (in a structured form),
// to reconcile an entry with item(s) that the payment intends to settle
// (e.g., a purchase reference number).
Structured struct {
// Reference information provided by the ultimate debtor
// to allow the identification of underlying documents by the creditor.
CreditorReferenceInformation struct {
// A reference, as assigned by the ultimate debtor
// to unambiguously refer to the payment transaction.
// Conditionally required if supplied by the ultimate debtor.
Reference string `json:"reference"`
} `json:"creditorReferenceInformation"`
} `json:"structured,omitempty"`
} `json:"remittanceInformation,omitempty"`
}
type PaymentInstruction struct {
// Details about the account holder.
Debtor struct {
// The legal entity identifier of the account holder (eg: charity number).
// This should be supplied if known.
LegalEntityIdentifier string `json:"legalEntityIdentifier,omitempty"`
// The name of the account holder.
Name string `json:"name,omitempty"`
// The address of the account holder.
Address string `json:"address"`
} `json:"debtor"`
// Information about the counterpart in a given transaction.
DebtorAccount struct {
// The identifiable information of an account.
Identification struct {
// The International Bank Account Number (IBAN).
IBAN string `json:"iban"`
} `json:"identification"`
} `json:"debtorAccount"`
// A series of payments that should be made from the debtor account.
CreditTransfers []CreditTransfer `json:"creditTransfers"`
// The unique identifier for the payment instruction.
PaymentInstructionIdentification string `json:"paymentInstructionIdentification"`
}
type CreateTransactionsPayload struct {
// Details of the payments to be made.
PaymentInstructions []PaymentInstruction `json:"paymentInstructions"`
}
type TransactionsCreatedResponse struct {
Transactions []struct {
EndToEndIdentification string `json:"endToEndIdentification"`
Response string `json:"response"`
}
}
func (c *client) InitiateFPSTransactions(ctx context.Context, payload CreateTransactionsPayload) (data TransactionsCreatedResponse, err error) {
req, err := c.newRequest(ctx, http.MethodPost, "/v3/Payments/FPS", payload)
if err != nil {
return data, fmt.Errorf("failed to create request: %w", err)
}
req.ExpectStatus(http.StatusAccepted)
req.DecodeTo(&data)
return data, c.do(ctx, req)
}
const SchemeNameOther = "Other"
const ProprietarySortCode = "SortcodeAccountNumber"
type Address struct {
BuildingNumber string `json:"buildingNumber,omitempty"`
BuildingName string `json:"buildingName,omitempty"`
StreetName string `json:"streetName,omitempty"`
TownName string `json:"townName"`
PostCode string `json:"postCode"`
Country string `json:"country"`
}
type CreateCHAPSPaymentPayload struct {
// Unique identification, as assigned by you, to unambiguously identify the payment instruction.
InstructionIdentification string `json:"instructionIdentification"`
// Unique identification, as assigned by the initiating party, to unambiguously identify the transaction.
// This identification is passed on, unchanged, throughout the entire end-to-end chain.
EndToEndIdentification string `json:"endToEndIdentification"`
// Amount of money to be moved between the debtor and creditor,
// before deduction of charges, expressed in the currency as ordered by the initiating party.
InstructedAmount struct {
Currency string `json:"currency"`
Amount float64 `json:"amount"`
} `json:"instructedAmount"`
// The ClearBank account that will be credited or debited
// based on the successful completion of the payment instruction.
// You need to include EITHER the iban field OR the schemeName and identification fields in this object.
SourceAccount struct {
IBAN string `json:"iban,omitempty"`
SchemeName string `json:"schemeName,omitempty"`
Proprietary string `json:"proprietary,omitempty"`
Identification string `json:"identification,omitempty"`
} `json:"sourceAccount"`
// Unambiguous identification of the account of the debtor
// to which a debit entry will be made as a result of the transaction.
// You need to include EITHER the iban field OR the schemeName and identification fields in this object.
DebtorAccount struct {
IBAN string `json:"iban,omitempty"`
SchemeName string `json:"schemeName,omitempty"`
Proprietary string `json:"proprietary,omitempty"`
Identification string `json:"identification,omitempty"`
} `json:"debtorAccount"`
// Unambiguous identification of the account of the creditor
// to which a credit entry will be posted as a result of the payment transaction.
CreditorAccount struct {
IBAN string `json:"iban,omitempty"`
SchemeName string `json:"schemeName,omitempty"`
Proprietary string `json:"proprietary,omitempty"`
Identification string `json:"identification,omitempty"`
} `json:"creditorAccount"`
// Party that is owed an amount of money by the (ultimate) debtor.
Creditor struct {
Name string `json:"name"`
Address *Address `json:"postalAddress,omitempty"`
} `json:"creditor"`
// Party that owes an amount of money to the (ultimate) creditor.
Debtor struct {
Name string `json:"name"`
Address Address `json:"postalAddress"`
} `json:"debtor"`
// Underlying reason for the payment transaction, as published in an external purpose code list.
Purpose string `json:"purpose,omitempty"`
// Category purpose, in a proprietary form.
CategoryPurpose string `json:"categoryPurpose,omitempty"`
// Information supplied to enable the matching of an entry with the items that the transfer is intended to settle,
// such as commercial invoices in an accounts' receivable system.
RemittanceInformation struct {
CreditorReferenceInformation string `json:"creditorReferenceInformation"`
} `json:"remittanceInformation"`
}
type PaymentInitiatedResponse struct {
PaymentID uuid.UUID `json:"paymentId"`
}
func (c *client) InitiateCHAPSPayment(ctx context.Context, payload CreateCHAPSPaymentPayload) (data PaymentInitiatedResponse, err error) {
req, err := c.newRequest(ctx, http.MethodPost, "/payments/chaps/v4/customer-payments", payload)
if err != nil {
return data, fmt.Errorf("failed to create request: %w", err)
}
req.ExpectStatus(http.StatusAccepted)
req.DecodeTo(&data)
return data, c.do(ctx, req)
}
type CreateFPSPaymentOriginatedOverseasPayload struct {
// Unique identification, as assigned by you, to unambiguously identify the payment instruction.
InstructionIdentification string `json:"instructionIdentification"`
// Unique identification, as assigned by the initiating party, to unambiguously identify the transaction. This identification is passed on, unchanged, throughout the entire end-to-end chain.
EndToEndIdentification string `json:"endToEndIdentification"`
// Underlying reason for the payment transaction, as published in an external purpose code list.
Purpose string `json:"purpose,omitempty"`
// Category purpose, in a proprietary form.
CategoryPurpose string `json:"categoryPurpose,omitempty"`
// Amount of money to be moved between the debtor and creditor,
// before deduction of charges, expressed in the currency as ordered by the initiating party.
InstructedAmount struct {
Currency string `json:"currency"`
Amount float64 `json:"amount"`
} `json:"instructedAmount"`
// The ClearBank account that will be credited or debited
// based on the successful completion of the payment instruction.
// You need to include EITHER the iban field OR the schemeName and identification fields in this object.
SourceAccount struct {
IBAN string `json:"iban,omitempty"`
SchemeName string `json:"schemeName,omitempty"`
Proprietary string `json:"proprietary,omitempty"`
Identification string `json:"identification,omitempty"`
} `json:"sourceAccount"`
// Unambiguous identification of the account of the debtor
// to which a debit entry will be made as a result of the transaction.
// You need to include EITHER the iban field OR the schemeName and identification fields in this object.
DebtorAccount struct {
IBAN string `json:"iban,omitempty"`
SchemeName string `json:"schemeName,omitempty"`
Proprietary string `json:"proprietary,omitempty"`
Identification string `json:"identification,omitempty"`
} `json:"debtorAccount"`
// Unambiguous identification of the account of the creditor
// to which a credit entry will be posted as a result of the payment transaction.
CreditorAccount struct {
IBAN string `json:"iban,omitempty"`
SchemeName string `json:"schemeName,omitempty"`
Proprietary string `json:"proprietary,omitempty"`
Identification string `json:"identification,omitempty"`
} `json:"creditorAccount"`
// Party that owes an amount of money to the (ultimate) creditor.
Debtor struct {
Name string `json:"name"`
Address Address `json:"postalAddress"`
} `json:"debtor"`
// Party that is owed an amount of money by the (ultimate) debtor.
Creditor struct {
Name string `json:"name"`
BIC string `json:"bic"`
Address Address `json:"postalAddress"`
} `json:"creditor"`
RemittanceInformation struct {
CreditorReferenceInformation string `json:"creditorReferenceInformation"`
} `json:"remittanceInformation"`
// Agent between the debtor's agent and the creditor's agent.
// You must include the Direct Participant's information in this object.
IntermediaryAgent struct {
Name string `json:"name"`
BIC string `json:"bic"`
Address Address `json:"postalAddress"`
} `json:"intermediaryAgent1"`
}
func (c *client) InitiateFPSPaymentOriginatedOverseas(ctx context.Context, payload CreateFPSPaymentOriginatedOverseasPayload) (data TransactionsCreatedResponse, err error) {
req, err := c.newRequest(ctx, http.MethodPost, "/v2/payments/fps/singlepayment", payload)
if err != nil {
return data, fmt.Errorf("failed to create request: %w", err)
}
req.ExpectStatus(http.StatusAccepted)
req.DecodeTo(&data)
return data, c.do(ctx, req)
}
type FetchTransactionsParams struct {
PageNumber int
PageSize int
StartDateTime time.Time
EndDateTime time.Time
}
// ApplyFor applies values to the given request as query parameters.
func (p FetchTransactionsParams) ApplyFor(req *request) {
if p.PageNumber != 0 {
req.AddQueryParam("pageNumber", strconv.Itoa(p.PageNumber))
}
if p.PageSize != 0 {
req.AddQueryParam("pageSize", strconv.Itoa(p.PageSize))
}
if !p.StartDateTime.IsZero() {
req.AddQueryParam("startDateTime", p.StartDateTime.Format("2006-01-02"))
}
if !p.EndDateTime.IsZero() {
req.AddQueryParam("endDateTime", p.EndDateTime.Format("2006-01-02"))
}
}
type TransactionResponseData struct {
Amount struct {
InstructedAmount float64 `json:"instructedAmount"`
Currency string `json:"currency"`
} `json:"amount"`
CounterpartAccount struct {
Identification struct {
IBAN string `json:"iban"`
AccountName string `json:"accountName"`
SortCode string `json:"sortCode"`
AccountNumber string `json:"accountNumber"`
Reference string `json:"reference"`
} `json:"identification"`
} `json:"counterpartAccount"`
DebitCreditCode string `json:"debitCreditCode"`
EndToEndIdentifier string `json:"endToEndIdentifier"`
TransactionID uuid.UUID `json:"transactionId"`
TransactionReference string `json:"transactionReference"`
TransactionTime time.Time `json:"transactionTime"`
Status string `json:"status"`
UltimateBeneficiaryAccount *struct {
ID uuid.UUID `json:"id"`
IBAN string `json:"iban"`
} `json:"ultimateBeneficiaryAccount"`
UltimateRemitterAccount *struct {
ID uuid.UUID `json:"id"`
IBAN string `json:"iban"`
} `json:"ultimateRemitterAccount"`
}
func (r TransactionResponseData) GetAmount() float64 {
if r.DebitCreditCode == "CRDT" {
return r.Amount.InstructedAmount
}
return -r.Amount.InstructedAmount
}
type TransactionResponse struct {
Transaction TransactionResponseData `json:"transaction"`
}
type TransactionsResponse struct {
Transactions []TransactionResponseData `json:"transactions"`
}
func (c *client) FetchTransactions(ctx context.Context, params FetchTransactionsParams) (data TransactionsResponse, err error) {
req, err := c.newRequest(ctx, http.MethodGet, "/v2/Transactions", nil)
if err != nil {
return data, fmt.Errorf("failed to create request: %w", err)
}
params.ApplyFor(req)
req.ExpectStatus(http.StatusOK)
req.DecodeTo(&data)
return data, c.do(ctx, req)
}
func (c *client) FetchTransactionForAccount(ctx context.Context, accountID uuid.UUID, trxID uuid.UUID) (data TransactionResponse, err error) {
req, err := c.newRequest(ctx, http.MethodGet, fmt.Sprintf("/v2/Accounts/%s/Transactions/%s", accountID, trxID), nil)
if err != nil {
return data, fmt.Errorf("failed to create request: %w", err)
}
req.ExpectStatus(http.StatusOK)
req.DecodeTo(&data)
return data, c.do(ctx, req)
}
func (c *client) FetchTransactionsForAccount(ctx context.Context, accountID uuid.UUID, params FetchTransactionsParams) (data TransactionsResponse, err error) {
req, err := c.newRequest(ctx, http.MethodGet, fmt.Sprintf("/v2/Accounts/%s/Transactions", accountID), nil)
if err != nil {
return data, fmt.Errorf("failed to create request: %w", err)
}
params.ApplyFor(req)
req.ExpectStatus(http.StatusOK)
req.DecodeTo(&data)
return data, c.do(ctx, req)
}
func (c *client) FetchTransactionsForVirtualAccount(ctx context.Context, accountID, virtualAccountID uuid.UUID, params FetchTransactionsParams) (data TransactionsResponse, err error) {
req, err := c.newRequest(ctx, http.MethodGet, fmt.Sprintf("/v2/Accounts/%s/Virtual/%s/Transactions", accountID, virtualAccountID), nil)
if err != nil {
return data, fmt.Errorf("failed to create request: %w", err)
}
params.ApplyFor(req)
req.ExpectStatus(http.StatusOK)
req.DecodeTo(&data)
return data, c.do(ctx, req)
}
func (c *client) FetchTransactionForVirtualAccount(ctx context.Context, accountID, virtualAccountID uuid.UUID, trxID uuid.UUID) (data TransactionResponse, err error) {
req, err := c.newRequest(ctx, http.MethodGet, fmt.Sprintf("/v2/Accounts/%s/Virtual/%s/Transactions/%s", accountID, virtualAccountID, trxID), nil)
if err != nil {
return data, fmt.Errorf("failed to create request: %w", err)
}
req.ExpectStatus(http.StatusOK)
req.DecodeTo(&data)
return data, c.do(ctx, req)
}