@@ -2,84 +2,11 @@ package gocardless
2
2
3
3
import (
4
4
"context"
5
- "fmt"
6
5
7
6
"github.com/formancehq/payments/internal/connectors/plugins/public/gocardless/client"
8
7
"github.com/formancehq/payments/internal/models"
9
8
)
10
9
11
- func validateExternalBankAccount (newExternalBankAccount models.BankAccount ) error {
12
- _ , err := extractNamespacedMetadata (newExternalBankAccount .Metadata , client .GocardlessBranchCodeMetadataKey )
13
-
14
- if err != nil {
15
- return fmt .Errorf ("required metadata field com.gocardless.spec/branch_code is missing" )
16
- }
17
-
18
- reqCurrency , err := extractNamespacedMetadata (newExternalBankAccount .Metadata , client .GocardlessCurrencyMetadataKey )
19
- if err != nil {
20
- return fmt .Errorf ("required metadata field com.gocardless.spec/currency is missing" )
21
- }
22
-
23
- _ , ok := SupportedCurrenciesWithDecimal [reqCurrency ]
24
-
25
- if ! ok {
26
-
27
- return fmt .Errorf ("com.gocardless.spec/currency %s not supported" , reqCurrency )
28
- }
29
-
30
- creditor , _ := extractNamespacedMetadata (newExternalBankAccount .Metadata , client .GocardlessCreditorMetadataKey )
31
- customer , _ := extractNamespacedMetadata (newExternalBankAccount .Metadata , client .GocardlessCustomerMetadataKey )
32
-
33
- if len (creditor ) > 1 && creditor [:2 ] != "CR" {
34
- return fmt .Errorf ("com.gocardless.spec/creditor ID must start with 'CR'" )
35
- }
36
-
37
- if len (customer ) > 1 && customer [:2 ] != "CU" {
38
- return fmt .Errorf ("com.gocardless.spec/customer ID must start with 'CU'" )
39
- }
40
-
41
- if customer == "" && creditor == "" {
42
- return fmt .Errorf ("you must provide com.gocardless.spec/customer or com.gocardless.spec/creditor metadata field" )
43
- }
44
-
45
- if customer != "" && creditor != "" {
46
- return fmt .Errorf ("you must provide either com.gocardless.spec/customer or com.gocardless.spec/creditor metadata field but not both" )
47
- }
48
-
49
- if newExternalBankAccount .Country != nil && * newExternalBankAccount .Country == "US" {
50
-
51
- accountType , err := extractNamespacedMetadata (newExternalBankAccount .Metadata , client .GocardlessAccountTypeMetadataKey )
52
-
53
- if err != nil {
54
- return fmt .Errorf ("required metadata field com.gocardless.spec/account_type is missing" )
55
- }
56
-
57
- if accountType != "checking" && accountType != "savings" {
58
- return fmt .Errorf ("metadata field com.gocardless.spec/account_type must be checking or savings" )
59
-
60
- }
61
-
62
- }
63
-
64
- if newExternalBankAccount .AccountNumber == nil {
65
- return fmt .Errorf ("account number is required" )
66
- }
67
-
68
- if newExternalBankAccount .SwiftBicCode == nil {
69
- return fmt .Errorf ("swift bic code is required" )
70
- }
71
-
72
- if newExternalBankAccount .Country == nil {
73
- return fmt .Errorf ("country is required" )
74
- }
75
-
76
- if newExternalBankAccount .IBAN == nil {
77
- return fmt .Errorf ("IBAN is required" )
78
- }
79
-
80
- return nil
81
- }
82
-
83
10
func (p * Plugin ) createBankAccount (ctx context.Context , ba models.BankAccount ) (models.CreateBankAccountResponse , error ) {
84
11
err := validateExternalBankAccount (ba )
85
12
if err != nil {
@@ -91,47 +18,19 @@ func (p *Plugin) createBankAccount(ctx context.Context, ba models.BankAccount) (
91
18
var externalBankAccount client.GocardlessGenericAccount
92
19
93
20
if creditor != "" {
94
- creditorBankAccount , err : = p .client .CreateCreditorBankAccount (ctx , creditor , ba )
21
+ externalBankAccount , err = p .client .CreateCreditorBankAccount (ctx , creditor , ba )
95
22
96
23
if err != nil {
97
24
return models.CreateBankAccountResponse {}, err
98
25
}
99
-
100
- parsedTime , err := ParseGocardlessTimestamp (creditorBankAccount .CreatedAt )
101
- if err != nil {
102
- return models.CreateBankAccountResponse {}, fmt .Errorf ("failed to parse creation time: %w" , err )
103
- }
104
-
105
- externalBankAccount = client.GocardlessGenericAccount {
106
- ID : creditorBankAccount .Id ,
107
- CreatedAt : parsedTime .Unix (),
108
- AccountHolderName : creditorBankAccount .AccountHolderName ,
109
- Metadata : creditorBankAccount .Metadata ,
110
- Currency : creditorBankAccount .Currency ,
111
- AccountType : creditorBankAccount .AccountType ,
112
- }
113
26
}
114
27
115
28
if customer != "" {
116
- customerBankAccount , err : = p .client .CreateCustomerBankAccount (ctx , customer , ba )
29
+ externalBankAccount , err = p .client .CreateCustomerBankAccount (ctx , customer , ba )
117
30
118
31
if err != nil {
119
32
return models.CreateBankAccountResponse {}, err
120
33
}
121
-
122
- parsedTime , err := ParseGocardlessTimestamp (customerBankAccount .CreatedAt )
123
- if err != nil {
124
- return models.CreateBankAccountResponse {}, fmt .Errorf ("failed to parse creation time: %w" , err )
125
- }
126
-
127
- externalBankAccount = client.GocardlessGenericAccount {
128
- ID : customerBankAccount .Id ,
129
- CreatedAt : parsedTime .Unix (),
130
- AccountHolderName : customerBankAccount .AccountHolderName ,
131
- Metadata : customerBankAccount .Metadata ,
132
- Currency : customerBankAccount .Currency ,
133
- AccountType : customerBankAccount .AccountType ,
134
- }
135
34
}
136
35
137
36
bankAccount , err := externalAccountFromGocardlessData (externalBankAccount )
0 commit comments