-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathlegaluser.go
138 lines (124 loc) · 3.98 KB
/
legaluser.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
// 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"
)
// LegalUser describes all the properties of a MangoPay legal user object.
type LegalUser struct {
User
Name string
LegalPersonType string
UserCategory string
HeadquartersAddress string
LegalRepresentativeFirstName string
LegalRepresentativeLastName string
LegalRepresentativeAddress string
LegalRepresentativeEmail string
LegalRepresentativeBirthday int64
LegalRepresentativeNationality string
LegalRepresentativeCountryOfResidence string
Statute string
ProofOfRegistration string
ShareholderDeclaration string
CompanyNumber string
TermsAndConditionsAccepted bool
service *MangoPay // Current service
wallets WalletList
}
func (u *LegalUser) String() string {
return struct2string(u)
}
// NewLegalUser creates a new legal user.
func (m *MangoPay) NewLegalUser(name string, email string, userCategory string, address string, companyNumber string, personType string, legalFirstName string, legalLastName string, birthday int64, nationality string, country string, terms bool) *LegalUser {
u := &LegalUser{
Name: name,
UserCategory: userCategory,
HeadquartersAddress: address,
CompanyNumber: companyNumber,
LegalPersonType: personType,
LegalRepresentativeFirstName: legalFirstName,
LegalRepresentativeLastName: legalLastName,
LegalRepresentativeBirthday: birthday,
LegalRepresentativeNationality: nationality,
LegalRepresentativeCountryOfResidence: country,
TermsAndConditionsAccepted: terms,
}
u.User = User{Email: email}
u.service = m
return u
}
// Wallets returns user's wallets.
func (u *LegalUser) Wallets() (WalletList, error) {
ws, err := u.service.wallets(u)
return ws, err
}
// Transfer gets all user's transaction.
func (u *LegalUser) Transfers() (TransferList, error) {
trs, err := u.service.transfers(u)
return trs, err
}
// Save creates or updates a legal user. The Create API is used
// if the user's Id is an empty string. The Edit API is used when
// the Id is a non-empty string.
func (u *LegalUser) Save() error {
var action mangoAction
if u.Id == "" {
action = actionCreateLegalUser
} else {
action = actionEditLegalUser
}
data := JsonObject{}
j, err := json.Marshal(u)
if err != nil {
return err
}
if err := json.Unmarshal(j, &data); err != nil {
return err
}
// Force float64 to int conversion after unmarshalling.
for _, field := range []string{"LegalRepresentativeBirthday", "CreationDate"} {
data[field] = int(data[field].(float64))
}
// Fields not allowed when creating a user
if action == actionCreateLegalUser {
delete(data, "Id")
}
delete(data, "CreationDate")
if action == actionEditLegalUser {
// Delete empty values so that existing ones don't get
// overwritten with empty values.
for k, v := range data {
switch v.(type) {
case string:
if v.(string) == "" {
delete(data, k)
}
case int:
if v.(int) == 0 {
delete(data, k)
}
}
}
}
ins, err := u.service.anyRequest(new(LegalUser), action, data)
if err != nil {
return err
}
serv := u.service
*u = *(ins.(*LegalUser))
u.service = serv
return nil
}
// LegalUser finds a legal user using the user_id attribute.
func (m *MangoPay) LegalUser(id string) (*LegalUser, error) {
u := new(LegalUser)
ins, err := m.anyRequest(u, actionFetchLegalUser, JsonObject{"Id": id})
if err != nil {
return nil, err
}
lu := ins.(*LegalUser)
lu.service = m
return lu, nil
}