-
Notifications
You must be signed in to change notification settings - Fork 14
/
bank.go
182 lines (164 loc) · 4.63 KB
/
bank.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
// Copyright 2014 Mathias Monnerville. All rights reserved.
// Use of this source code is governed by a GPL
// license that can be found in the LICENSE file.
package mango
import (
"encoding/json"
"errors"
)
// Bank account type.
type AccountType int
const (
IBAN AccountType = iota
GB
US
CA
OTHER
)
var accountTypes = map[AccountType]string{
IBAN: "IBAN",
GB: "GB",
US: "US",
CA: "CA",
OTHER: "OTHER",
}
// List of bank accounts.
type BankAccountList []*BankAccount
// BankAccount is an item mainly used for pay-out bank wire request. It is
// used as a generic bank account container for all supported account
// types: IBAN, GB, US, CA or OTHER.
//
// This way, only one structure is used to unmarshal any JSON response related
// to bank accounts.
//
// See http://docs.mangopay.com/api-references/bank-accounts/
type BankAccount struct {
ProcessIdent
Type string // IBAN, GB, US, CA or OTHER
OwnerName string
OwnerAddress string
UserId string
// Required for IBAN type
IBAN string
BIC string // For IBAN, OTHER
AccountNumber string // For GB, US, CA, OTHER
// Required for GB type
SortCode string
// Required for US type
ABA string
// Required for CA type
BankName string
InstitutionNumber string
BranchCode string
// Required for OTHER type
Country string
service *MangoPay
atype AccountType
}
type IBANBankAccount struct {
}
func (b *BankAccount) String() string {
return struct2string(b)
}
// NewBankAccount creates a new bank account. Note that depending on the account's
// type, some fields of the newly BankAccount instance must be filled (they are
// required) before a call to Save().
//
// See http://docs.mangopay.com/api-references/bank-accounts/
func (m *MangoPay) NewBankAccount(user Consumer, ownerName, ownerAddress string, t AccountType) (*BankAccount, error) {
id := consumerId(user)
if id == "" {
return nil, errors.New("user has empty Id")
}
b := &BankAccount{
ProcessIdent: ProcessIdent{},
Type: accountTypes[t],
OwnerName: ownerName,
OwnerAddress: ownerAddress,
UserId: id,
service: m,
atype: t,
}
return b, nil
}
// Save sends the HTTP query to create the bank account.
func (b *BankAccount) Save() error {
data := JsonObject{}
j, err := json.Marshal(b)
if err != nil {
return err
}
if err := json.Unmarshal(j, &data); err != nil {
return err
}
// Data fields to remove before sending the HTTP request.
ignore := []string{"Id", "CreationDate"}
switch b.atype {
case IBAN:
if b.IBAN == "" {
return errors.New("missing full IBAN information")
}
ignore = append(ignore, "AccountNumber", "SortCode", "ABA", "BankName",
"InstitutionNumber", "BranchCode", "Country", "BIC")
case GB:
if b.AccountNumber == "" || b.SortCode == "" {
return errors.New("missing full GB information")
}
ignore = append(ignore, "IBAN", "BIC", "ABA", "BankName",
"InstitutionNumber", "BranchCode", "Country")
case US:
if b.AccountNumber == "" || b.ABA == "" {
return errors.New("missing full US information")
}
ignore = append(ignore, "IBAN", "BIC", "SortCode", "BankName",
"InstitutionNumber", "BranchCode", "Country")
case CA:
if b.BankName == "" || b.InstitutionNumber == "" || b.BranchCode == "" ||
b.AccountNumber == "" {
return errors.New("missing full CA information")
}
ignore = append(ignore, "IBAN", "BIC", "SortCode", "ABA", "Country")
case OTHER:
if b.AccountNumber == "" {
return errors.New("missing full OTHER information")
}
ignore = append(ignore, "IBAN", "ABA", "SortCode", "BankName",
"InstitutionNumber", "BranchCode", "BIC")
}
for _, field := range ignore {
delete(data, field)
}
ba, err := b.service.anyRequest(new(BankAccount), actionCreateBankAccount, data)
if err != nil {
return err
}
serv := b.service
*b = *(ba.(*BankAccount))
b.service = serv
return nil
}
// BankAccount returns a user's bank account.
func (m *MangoPay) BankAccount(user Consumer, id string) (*BankAccount, error) {
userId := consumerId(user)
if userId == "" {
return nil, errors.New("user has empty Id")
}
w, err := m.anyRequest(new(BankAccount), actionFetchBankAccount,
JsonObject{"Id": id, "UserId": userId})
if err != nil {
return nil, err
}
return w.(*BankAccount), nil
}
// BankAccounts finds all user's bank accounts.
func (m *MangoPay) BankAccounts(user Consumer) (BankAccountList, error) {
userId := consumerId(user)
if userId == "" {
return nil, errors.New("user has empty Id")
}
accs, err := m.anyRequest(new(BankAccountList), actionFetchUserBankAccounts, JsonObject{"Id": userId})
if err != nil {
return nil, err
}
return *(accs.(*BankAccountList)), nil
}