-
Notifications
You must be signed in to change notification settings - Fork 14
/
naturaluser.go
133 lines (119 loc) · 3.36 KB
/
naturaluser.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
// 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"
)
// NaturalUser describes all the properties of a MangoPay natural user object.
type NaturalUser struct {
User
FirstName, LastName string
Address string
UserCategory string
TermsAndConditionsAccepted bool
Birthday int64
Nationality string
CountryOfResidence string
Occupation string
IncomeRange int
ProofOfIdentity string
ProofOfAddress string
service *MangoPay // Current service
wallets WalletList
}
func (u *NaturalUser) String() string {
return struct2string(u)
}
// NewNaturalUser creates a new natural user.
func (m *MangoPay) NewNaturalUser(first, last string, email string, userCategory string, birthday int64, nationality, country string, terms bool) *NaturalUser {
u := &NaturalUser{
FirstName: first,
LastName: last,
UserCategory: userCategory,
TermsAndConditionsAccepted: terms,
Birthday: birthday,
Nationality: nationality,
CountryOfResidence: country,
}
u.User = User{Email: email}
u.service = m
return u
}
// Wallets returns user's wallets.
func (u *NaturalUser) Wallets() (WalletList, error) {
ws, err := u.service.wallets(u)
return ws, err
}
// Transfer gets all user's transaction.
func (u *NaturalUser) Transfers() (TransferList, error) {
trs, err := u.service.transfers(u)
return trs, err
}
// Save creates or updates a natural 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 *NaturalUser) Save() error {
var action mangoAction
if u.Id == "" {
action = actionCreateNaturalUser
} else {
action = actionEditNaturalUser
}
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{"Birthday", "CreationDate"} {
data[field] = int(data[field].(float64))
}
// Fields not allowed when creating a user
if action == actionCreateNaturalUser {
delete(data, "Id")
}
delete(data, "CreationDate")
if action == actionEditNaturalUser {
// Delete empty values so that existing ones don't get
// overwritten with empty values.
for k, v := range data {
switch casted := v.(type) {
case string:
if casted == "" {
delete(data, k)
}
case int:
if casted == 0 {
delete(data, k)
}
}
}
}
if action == actionCreateNaturalUser {
if data["IncomeRange"].(float64) == 0 {
delete(data, "IncomeRange")
}
}
user, err := u.service.anyRequest(new(NaturalUser), action, data)
if err != nil {
return err
}
serv := u.service
*u = *(user.(*NaturalUser))
u.service = serv
return nil
}
// NaturalUser finds a natural user using the user_id attribute.
func (m *MangoPay) NaturalUser(id string) (*NaturalUser, error) {
u, err := m.anyRequest(new(NaturalUser), actionFetchNaturalUser, JsonObject{"Id": id})
if err != nil {
return nil, err
}
nu := u.(*NaturalUser)
nu.service = m
return nu, nil
}