forked from gotsunami/mangopay2-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
banking_alias.go
91 lines (79 loc) · 2.06 KB
/
banking_alias.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
package mango
import (
"encoding/json"
"errors"
)
// List of banking aliases.
type BankingAliasList []*BankingAlias
type BankingAlias struct {
ProcessIdent
CreditedUserId string
WalletId string
Type string // IBAN
Country string
OwnerName string
Active bool
IBAN string
BIC string
service *MangoPay
}
// NewBankingAlias creates a new banking alias.
//
// See https://docs.mangopay.com/endpoints/v2.01/banking-aliases
func (m *MangoPay) NewBankingAlias(wallet Wallet, ownerName string, country string) (*BankingAlias, error) {
if wallet.Id == "" {
return nil, errors.New("wallet has empty Id")
}
b := &BankingAlias{
ProcessIdent: ProcessIdent{},
OwnerName: ownerName,
Country: country,
WalletId: wallet.Id,
service: m,
}
return b, nil
}
// Save sends the HTTP query to create the bank alias.
func (b *BankingAlias) 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.
for _, field := range []string{"Id", "CreationDate"} {
delete(data, field)
}
ba, err := b.service.anyRequest(new(BankingAlias), actionCreateBankingAlias, data)
if err != nil {
return err
}
serv := b.service
*b = *(ba.(*BankingAlias))
b.service = serv
return nil
}
// BankingAlias returns a user's banking alias.
func (m *MangoPay) BankingAlias(id string) (*BankingAlias, error) {
w, err := m.anyRequest(new(BankingAlias), actionFetchBankingAlias,
JsonObject{"BankingAliasId": id})
if err != nil {
return nil, err
}
return w.(*BankingAlias), nil
}
// BankingAliases finds all user's bank aliases.
func (m *MangoPay) BankingAliases(wallet Wallet) (BankAccountList, error) {
if wallet.Id == "" {
return nil, errors.New("wallet has empty Id")
}
accs, err := m.anyRequest(new(BankingAliasList), actionFetchBankingAliases,
JsonObject{"WalletId": wallet.Id})
if err != nil {
return nil, err
}
return *(accs.(*BankAccountList)), nil
}