forked from stripe/stripe-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
customer.go
115 lines (100 loc) · 3.54 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
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
package stripe
import (
"encoding/json"
"net/url"
)
// CustomerParams is the set of parameters that can be used when creating or updating a customer.
// For more details see https://stripe.com/docs/api#create_customer and https://stripe.com/docs/api#update_customer.
type CustomerParams struct {
Params
Balance int64
Token, Coupon string
Source *SourceParams
Desc, Email string
Plan string
Quantity uint64
TrialEnd int64
DefaultSource string
Shipping *CustomerShippingDetails
}
// SetSource adds valid sources to a CustomerParams object,
// returning an error for unsupported sources.
func (cp *CustomerParams) SetSource(sp interface{}) error {
source, err := SourceParamsFor(sp)
cp.Source = source
return err
}
// CustomerListParams is the set of parameters that can be used when listing customers.
// For more details see https://stripe.com/docs/api#list_customers.
type CustomerListParams struct {
ListParams
Created int64
}
// Customer is the resource representing a Stripe customer.
// For more details see https://stripe.com/docs/api#customers.
type Customer struct {
ID string `json:"id"`
Live bool `json:"livemode"`
Sources *SourceList `json:"sources"`
Created int64 `json:"created"`
Balance int64 `json:"account_balance"`
Currency Currency `json:"currency"`
DefaultSource *PaymentSource `json:"default_source"`
Delinquent bool `json:"delinquent"`
Desc string `json:"description"`
Discount *Discount `json:"discount"`
Email string `json:"email"`
Meta map[string]string `json:"metadata"`
Subs *SubList `json:"subscriptions"`
Deleted bool `json:"deleted"`
Shipping *CustomerShippingDetails `json:"shipping"`
}
// CustomerList is a list of customers as retrieved from a list endpoint.
type CustomerList struct {
ListMeta
Values []*Customer `json:"data"`
}
// CustomerShippingDetails is the structure containing shipping information.
type CustomerShippingDetails struct {
Name string `json:"name"`
Address Address `json:"address"`
Phone string `json:"phone"`
}
// AppendDetails adds the shipping details to the query string.
func (s *CustomerShippingDetails) AppendDetails(values *url.Values) {
values.Add("shipping[name]", s.Name)
values.Add("shipping[address][line1]", s.Address.Line1)
if len(s.Address.Line2) > 0 {
values.Add("shipping[address][line2]", s.Address.Line2)
}
if len(s.Address.City) > 0 {
values.Add("shipping[address][city]", s.Address.City)
}
if len(s.Address.State) > 0 {
values.Add("shipping[address][state]", s.Address.State)
}
if len(s.Address.Country) > 0 {
values.Add("shipping[address][country]", s.Address.Country)
}
if len(s.Address.Zip) > 0 {
values.Add("shipping[address][postal_code]", s.Address.Zip)
}
if len(s.Phone) > 0 {
values.Add("shipping[phone]", s.Phone)
}
}
// UnmarshalJSON handles deserialization of a Customer.
// This custom unmarshaling is needed because the resulting
// property may be an id or the full struct if it was expanded.
func (c *Customer) UnmarshalJSON(data []byte) error {
type customer Customer
var cc customer
err := json.Unmarshal(data, &cc)
if err == nil {
*c = Customer(cc)
} else {
// the id is surrounded by "\" characters, so strip them
c.ID = string(data[1 : len(data)-1])
}
return nil
}