-
Notifications
You must be signed in to change notification settings - Fork 2
/
ClearBankApi.fs
435 lines (377 loc) · 17.4 KB
/
ClearBankApi.fs
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
module ClearBank
open System
open System.Net.Http
open System.Net
open SwaggerProvider
type KeyVaultCredentials =
| DefaultCredentials
| CredentialsWithOptions of Azure.Identity.DefaultAzureCredentialOptions
type ClearbankConfiguration =
{
BaseUrl: string
PrivateKey: string
AzureKeyVaultName: string
AzureKeyVaultCredentials: KeyVaultCredentials
LogUnsuccessfulHandler: Option<HttpStatusCode*string -> Unit>
}
type BankAccount =
| IBAN of string
| BBAN of string
| UK_Domestic of SortCode: string * AccountNumber: string
// Schema: https://institution-api-sim.clearbank.co.uk/docs/index.html
// FPS, CHAPS
// Account Holder Name - Required, alpha-numeric, space, comma, full-stop, hyphen (Max 18 characters)
// Sort code - 6 character length, must be numeric
// Account Number - required, 8 characters length, must be numeric
// Amount - required, must be numeric and greater than 0
// Payment Reference - required, alphanumeric, space, comma, full stop, a hyphen, maximum length 18 characters (Description to be provided -if the customer exceeds 18 characters this will be truncated)
let [<Literal>]schemaV1 = __SOURCE_DIRECTORY__ + @"/clearbank-api-v1.json"
let [<Literal>]schemaV2 = __SOURCE_DIRECTORY__ + @"/clearbank-api-v2.json"
let [<Literal>]schemaV3Accounts = __SOURCE_DIRECTORY__ + @"/clearbank-api-v3-accounts.json"
let [<Literal>]schemaV3PaymentsFps = __SOURCE_DIRECTORY__ + @"/fps-initiate-payment-v3.json"
type internal ClearBankSwaggerV1 = SwaggerClientProvider<schemaV1, PreferAsync=true>
type ClearBankSwaggerV2 = SwaggerClientProvider<schemaV2, PreferAsync=true>
type ClearBankOpenApiV3Accounts = OpenApiClientProvider<schemaV3Accounts, PreferAsync=true>
type FpsPaymentsV3 = OpenApiClientProvider<schemaV3PaymentsFps, PreferAsync=true>
let unSuccessStatusCode = new Event<_>() // id, status, content
type ErrorHandler(messageHandler) =
inherit DelegatingHandler(messageHandler)
override __.SendAsync(request, cancellationToken) =
let resp = base.SendAsync(request, cancellationToken)
async {
let! result = resp |> Async.AwaitTask
if not result.IsSuccessStatusCode then
let! cont = result.Content.ReadAsStringAsync() |> Async.AwaitTask
let hasId, idvals = request.Headers.TryGetValues("X-Request-ID") // Some unique id
unSuccessStatusCode.Trigger(
(if not hasId then None else idvals |> Seq.tryHead),
result.StatusCode,
cont)
return result
} |> Async.StartImmediateAsTask
let internal reportUnsuccessfulEvents xRequestId handler =
let evt =
unSuccessStatusCode.Publish
|> Event.filter(fun (id,status,content) -> id = Some xRequestId)
|> Event.map(fun (id,status,content) -> status, content)
evt.Subscribe(fun (s,c) -> handler(s,c))
/// Errors
/// Possible response values:
/// Accepted, AccountDisabled, InsufficientFunds, InvalidAccount
/// InvalidCurrency, Rejected, DebitPaymentDisabled
type ClearBankErrorJson = FSharp.Data.JsonProvider<"""[{
"transactions": [{
"endToEndIdentification": "string",
"response": "Accepted"
}],
"halLinks": [{
"name": "string",
"href": "string",
"templated": true
}]
},{
"errors": {},
"type": "string",
"title": "string",
"status": 3,
"detail": "string",
"instance": "string"
}
]""", SampleIsList=true>
type ClearBankErrorResponse = ClearBankErrorJson.Root
type ClearBankErrorStyle =
| ClearBankEmptyResponse
| ClearBankTransactionError of Errors: (string * string) seq //id and reason
| ClearBankGeneralError of Title: string * Detail: string
| ClearBankUnknownError of Content: string
let parseClearBankErrorContent(content:string) =
if String.IsNullOrEmpty content then
ClearBankEmptyResponse
else
try
let parsed =
ClearBankErrorJson.Parse content
match parsed.Transactions |> Seq.tryHead with
| Some t -> parsed.Transactions |> Seq.map(fun t -> t.EndToEndIdentification, t.Response) |> ClearBankTransactionError
| _ ->
if parsed.Title.IsSome && parsed.Detail.IsSome && (not (String.IsNullOrEmpty parsed.Title.Value)) then
(parsed.Title.Value, parsed.Detail.Value) |> ClearBankGeneralError
else
content |> ClearBankUnknownError
with
| _ -> content |> ClearBankUnknownError
type ModelsV3Accounts = ClearBankOpenApiV3Accounts.ClearBank.FI.API.Accounts.Versions.V3.Models
type AccountsV3 = ModelsV3Accounts.Binding.Accounts
let internal setKeyVaultCredentials options =
match options with
| DefaultCredentials -> ()
| CredentialsWithOptions opts ->
KeyVault.configureAzureCredentials <- fun() ->
Azure.Identity.DefaultAzureCredential opts
let internal calculateSignature config azureKeyVaultCertificateName requestBody =
task {
setKeyVaultCredentials config.AzureKeyVaultCredentials
let! signature_bodyhash = KeyVault.signAsync config.AzureKeyVaultName azureKeyVaultCertificateName requestBody
let signature_bodyhash_string =
signature_bodyhash.Signature
|> Convert.ToBase64String
return signature_bodyhash_string
}
let verifySignature publicKeyXml signature requestBody =
task {
let verifyResult = KeyVault.verifyPublic publicKeyXml signature requestBody
return verifyResult
}
let verifySignatureFromSecret config secretName signature requestBody =
task {
setKeyVaultCredentials config.AzureKeyVaultCredentials
let! publicKeyXml = KeyVault.getSecretAsync config.AzureKeyVaultName secretName
let verifyResult = KeyVault.verifyPublic publicKeyXml.Value signature requestBody
return verifyResult
}
let internal getErrorDetails : Exception -> string = function
| :? WebException as wex when not(isNull(wex.Response)) ->
use stream = wex.Response.GetResponseStream()
use reader = new System.IO.StreamReader(stream)
let err = reader.ReadToEnd()
err
| :? TimeoutException as e ->
"Timeout"
| _ ->
""
let callTestEndpoint config azureKeyVaultCertificateName =
let httpClient =
if config.LogUnsuccessfulHandler.IsNone then
new System.Net.Http.HttpClient(BaseAddress= Uri config.BaseUrl)
else
let handler1 = new HttpClientHandler (UseCookies = false)
let handler2 = new ErrorHandler(handler1)
new System.Net.Http.HttpClient(handler2, BaseAddress= Uri config.BaseUrl)
let client = ClearBankSwaggerV1.Client httpClient
async {
let authToken = "Bearer " + config.PrivateKey
let payload = System.Text.Json.JsonDocument.Parse("""{"institutionId": "string","body": "hello world!"}""") |> box
let payloaStr = client.Serialize payload
let! signature_bodyhash_string = calculateSignature config azureKeyVaultCertificateName payloaStr |> Async.AwaitTask
let requestId = Guid.NewGuid().ToString("N")
let subscription =
if config.LogUnsuccessfulHandler.IsSome then
Some (reportUnsuccessfulEvents requestId config.LogUnsuccessfulHandler.Value)
else None
let! r = client.V1TestPost(authToken, signature_bodyhash_string, requestId, payload) |> Async.Catch
httpClient.Dispose()
if subscription.IsSome then
subscription.Value.Dispose()
match r with
| Choice1Of2 x -> return Ok x
| Choice2Of2 err ->
let details = getErrorDetails err
//printfn "Used signature: %s" signature_bodyhash_string
return Error(err, details)
}
let ``account to string`` acc =
match acc with
| IBAN nr ->
FpsPaymentsV3.BatchPaymentInstructionCounterpartAccountIdentification(iban = nr)
| BBAN nr ->
FpsPaymentsV3.BatchPaymentInstructionCounterpartAccountIdentification(
//iban = "iban",
other =
FpsPaymentsV3.BatchCounterpartAccountGenericIdentification(
nr,
schemeName = FpsPaymentsV3.CounterpartAccountGenericIdentificationScheme(
proprietary = "BBAN"
)
//,"issuer"
))
| UK_Domestic(sortcode, account) ->
let identifier =
"GBR" +
sortcode.Replace(" ", "").Replace("-", "") +
account.Replace(" ", "").Replace("-", "")
FpsPaymentsV3.BatchPaymentInstructionCounterpartAccountIdentification(
//iban = "iban",
other =
FpsPaymentsV3.BatchCounterpartAccountGenericIdentification(
identifier,
schemeName = FpsPaymentsV3.CounterpartAccountGenericIdentificationScheme(
proprietary = "PRTY_COUNTRY_SPECIFIC"
)
//,"issuer"
))
type PaymentTransfer = {
To: BankAccount
AccountHolder: string
Sum: decimal
Currency: string
Description: string
PaymentReference: string
TransactionId: string
}
/// Creates credit transfer for createPaymentInstruction
let createCreditTransfer (payment:PaymentTransfer) =
FpsPaymentsV3.BatchCreditTransfer(
paymentIdentification = FpsPaymentsV3.BatchPaymentIdentification(
payment.Description, // instructionIdentification
payment.TransactionId // endToEndIdentification
),
amount = FpsPaymentsV3.BatchAmount(Convert.ToDouble(payment.Sum), payment.Currency),
creditor = FpsPaymentsV3.BatchCreditorPartyIdentifier(payment.AccountHolder (*, "legalEntityIdentifier"*)),
creditorAccount = FpsPaymentsV3.BatchPaymentInstructionCounterpartAccount(
identification = ``account to string`` payment.To),
remittanceInformation =
FpsPaymentsV3.BatchRemittanceInformation(structured =
FpsPaymentsV3.BatchStructured(creditorReferenceInformation =
FpsPaymentsV3.BatchCreditorReferenceInformation(payment.PaymentReference // reference
)
)
)
)
/// Creates payment instructions from createCreditTransfer for transferPayments
let createPaymentInstruction address legalEntityIdentifier batchId account transfers =
let req =
FpsPaymentsV3.BatchPaymentInstruction(
debtor = FpsPaymentsV3.BatchDebtorPartyIdentifier(
address = address,
legalEntityIdentifier = (legalEntityIdentifier |> Option.defaultValue "")),
paymentInstructionIdentification = batchId,
debtorAccount = FpsPaymentsV3.BatchPaymentInstructionCounterpartAccount(
identification = ``account to string`` account
),
creditTransfers = transfers
)
req
/// Creates a new account
let createNewAccount config azureKeyVaultCertificateName (requestId:Guid) (sortCode:string) accountName ownerName =
let req =
let owner = AccountsV3.PartyIdentification(ownerName)
AccountsV3.CreateAccountRequest(accountName, owner, (sortCode.Replace("-", "")))
let requestIdS = requestId.ToString("N") //todo, unique, save to db
let httpClient =
if config.LogUnsuccessfulHandler.IsNone then
new System.Net.Http.HttpClient(BaseAddress= Uri config.BaseUrl)
else
let handler1 = new HttpClientHandler (UseCookies = false)
let handler2 = new ErrorHandler(handler1)
new System.Net.Http.HttpClient(handler2, BaseAddress= Uri config.BaseUrl)
let client = ClearBankOpenApiV3Accounts.Client httpClient
async {
let authToken = "Bearer " + config.PrivateKey
let toHash = client.Serialize req
let! signature_bodyhash_string = calculateSignature config azureKeyVaultCertificateName toHash |> Async.AwaitTask
let subscription =
if config.LogUnsuccessfulHandler.IsSome then
Some (reportUnsuccessfulEvents requestIdS config.LogUnsuccessfulHandler.Value)
else None
let! r = client.V3InstitutionsByInstitutionIdAccountsPost(authToken, signature_bodyhash_string, requestIdS, req) |> Async.Catch
httpClient.Dispose()
if subscription.IsSome then
subscription.Value.Dispose()
match r with
| Choice1Of2 x -> return Ok x
| Choice2Of2 err ->
let details = getErrorDetails err
return Error(err, details)
}
/// Get all the accounts
let getAccounts config =
let httpClient =
if config.LogUnsuccessfulHandler.IsNone then
new System.Net.Http.HttpClient(BaseAddress= Uri config.BaseUrl)
else
let handler1 = new HttpClientHandler (UseCookies = false)
let handler2 = new ErrorHandler(handler1)
new System.Net.Http.HttpClient(handler2, BaseAddress= Uri config.BaseUrl)
let client = ClearBankOpenApiV3Accounts.Client httpClient
async {
let authToken = "Bearer " + config.PrivateKey
let! r = client.V3InstitutionsByInstitutionIdAccountsGet(authToken) |> Async.Catch
httpClient.Dispose()
match r with
| Choice1Of2 x -> return Ok x
| Choice2Of2 err ->
let details = getErrorDetails err
return Error(err, details)
}
/// Get all the transactions
let getTransactions config pageSize pageNumber startDate endDate =
let httpClient =
if config.LogUnsuccessfulHandler.IsNone then
new System.Net.Http.HttpClient(BaseAddress= Uri config.BaseUrl)
else
let handler1 = new HttpClientHandler (UseCookies = false)
let handler2 = new ErrorHandler(handler1)
new System.Net.Http.HttpClient(handler2, BaseAddress= Uri config.BaseUrl)
let client = ClearBankSwaggerV2.Client httpClient
async {
let authToken = "Bearer " + config.PrivateKey
let! r = client.V2TransactionsGet(authToken, pageNumber, pageSize, startDate, endDate) |> Async.Catch
httpClient.Dispose()
match r with
| Choice1Of2 x -> return Ok x
| Choice2Of2 err ->
let details = getErrorDetails err
return Error(err, details)
}
/// Get a transaction with correct end-to-end-id
/// if you are lucky enough to own accountId (from getAccounts) and transactionId (from getTransactions)
let getAccountTransaction config (accountId:string) (transactionId:string) =
let httpClient =
if config.LogUnsuccessfulHandler.IsNone then
new System.Net.Http.HttpClient(BaseAddress= Uri config.BaseUrl)
else
let handler1 = new HttpClientHandler (UseCookies = false)
let handler2 = new ErrorHandler(handler1)
new System.Net.Http.HttpClient(handler2, BaseAddress= Uri config.BaseUrl)
let clientV2 = ClearBankSwaggerV2.Client httpClient
async {
let authToken = "Bearer " + config.PrivateKey
let! r = clientV2.V2AccountsByAccountIdTransactionsByTransactionIdGet(accountId, transactionId, authToken) |> Async.Catch
httpClient.Dispose()
match r with
| Choice1Of2 x -> return Ok x
| Choice2Of2 err ->
let details = getErrorDetails err
return Error(err, details)
}
type TwoDecimalsConverter() =
inherit System.Text.Json.Serialization.JsonConverter<float>()
override _.Write(writer: System.Text.Json.Utf8JsonWriter, value, serializer) =
writer.WriteRawValue(value.ToString "0.00")
override _.Read(reader, typeToConvert, options) =
failwith "Not implemented"
/// Post payments created with createPaymentInstruction
let transferPayments config azureKeyVaultCertificateName (requestId:Guid) paymentInstructions =
let req = FpsPaymentsV3.BatchCreateCreditTransferRequest(
paymentInstructions = paymentInstructions)
let requestIdS = requestId.ToString("N") //todo, unique, save to db
let httpClient =
if config.LogUnsuccessfulHandler.IsNone then
new System.Net.Http.HttpClient(BaseAddress= Uri config.BaseUrl)
else
let handler1 = new HttpClientHandler (UseCookies = false)
let handler2 = new ErrorHandler(handler1)
new System.Net.Http.HttpClient(handler2, BaseAddress= Uri config.BaseUrl)
let opts = System.Text.Json.JsonSerializerOptions()
opts.Converters.Add(TwoDecimalsConverter())
let client = FpsPaymentsV3.Client(httpClient, opts)
async {
let authToken = "Bearer " + config.PrivateKey
let toHash = client.Serialize req
let! signature_bodyhash_string = calculateSignature config azureKeyVaultCertificateName toHash |> Async.AwaitTask
let subscription =
if config.LogUnsuccessfulHandler.IsSome then
Some (reportUnsuccessfulEvents requestIdS config.LogUnsuccessfulHandler.Value)
else None
let! r = client.Post(authToken, signature_bodyhash_string, requestIdS, req) |> Async.Catch
httpClient.Dispose()
if subscription.IsSome then
subscription.Value.Dispose()
match r with
| Choice1Of2 x -> return Ok x
| Choice2Of2 err ->
// You can use Fiddler to see the Request
let details = getErrorDetails err
//printfn "Used signature: %s" signature_bodyhash_string
return Error(err, details)
}