forked from braintree-go/braintree-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
customer.go
76 lines (67 loc) · 2.14 KB
/
customer.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
package braintree
import (
"encoding/xml"
"github.com/lionelbarrow/braintree-go/nullable"
)
type Customer struct {
XMLName string `xml:"customer"`
Id string `xml:"id,omitempty"`
FirstName string `xml:"first-name,omitempty"`
LastName string `xml:"last-name,omitempty"`
Company string `xml:"company,omitempty"`
Email string `xml:"email,omitempty"`
Phone string `xml:"phone,omitempty"`
Fax string `xml:"fax,omitempty"`
Website string `xml:"website,omitempty"`
CreditCard *CreditCard `xml:"credit-card,omitempty"`
CreditCards *CreditCards `xml:"credit-cards,omitempty"`
Subscriptions *Subscriptions `xml:"subscriptions,omitempty"`
CustomFields CustomFields `xml:"custom-fields"`
Addresses *Addresses `xml:"addresses",omitempty`
PayPalAccounts *PayPalAccounts `xml:"paypal-accounts",omitempty`
}
// DefaultCreditCard returns the default credit card, or nil
func (c *Customer) DefaultCreditCard() *CreditCard {
for _, card := range c.CreditCards.CreditCard {
if card.Default {
return card
}
}
return nil
}
type CustomerSearchResult struct {
XMLName string `xml:"customers"`
CurrentPageNumber *nullable.NullInt64 `xml:"current-page-number"`
PageSize *nullable.NullInt64 `xml:"page-size"`
TotalItems *nullable.NullInt64 `xml:"total-items"`
Customers []*Customer `xml:"customer"`
}
type CustomField struct {
XMLName xml.Name
Value string `xml:",chardata"`
}
type CustomFields struct {
Items []CustomField `xml:",any"`
}
func NewCustomFields(fields map[string]string) CustomFields {
cfs := CustomFields{}
for field, value := range fields {
cfs.Items = append(cfs.Items, CustomField{
XMLName: xml.Name{
Local: field,
},
Value: value,
})
}
return cfs
}
func (cfs *CustomFields) Map() map[string]string {
m := make(map[string]string)
for _, field := range cfs.Items {
m[field.XMLName.Local] = field.Value
}
return m
}
func (cfs *CustomFields) Get(name string) string {
return cfs.Map()[name]
}