forked from blacklightcms/recurly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbilling_service.go
141 lines (120 loc) · 4.58 KB
/
billing_service.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
139
140
141
package recurly
import (
"fmt"
"net/http"
)
var _ BillingService = &billingImpl{}
// billingImpl handles all interaction with the billing info portion
// of the recurly API.
type billingImpl struct {
client *Client
}
// Get returns only the account's current billing information.
// https://dev.recurly.com/docs/lookup-an-accounts-billing-info
func (s *billingImpl) Get(accountCode string) (*Response, *Billing, error) {
action := fmt.Sprintf("accounts/%s/billing_info", accountCode)
req, err := s.client.newRequest("GET", action, nil, nil)
if err != nil {
return nil, nil, err
}
var dst Billing
resp, err := s.client.do(req, &dst)
if err != nil || resp.StatusCode >= http.StatusBadRequest {
return resp, nil, err
}
return resp, &dst, err
}
// Create creates the account's billing information with credit card or
// bank account info. It is recommended you use recurly.js and a token with the CreateWithToken
// method instead.
// https://dev.recurly.com/docs/create-an-accounts-billing-info-credit-card
// https://dev.recurly.com/docs/create-an-accounts-billing-info-bank-account
func (s *billingImpl) Create(accountCode string, b Billing) (*Response, *Billing, error) {
action := fmt.Sprintf("accounts/%s/billing_info", accountCode)
req, err := s.client.newRequest("POST", action, nil, b)
if err != nil {
return nil, nil, err
}
var dst Billing
resp, err := s.client.do(req, &dst)
return resp, &dst, err
}
// CreateWithToken creates an account's billing information using a token
// generated by Recurly.js. Returns the account's created Billing Information.
// https://dev.recurly.com/docs/create-an-accounts-billing-info-token
func (s *billingImpl) CreateWithToken(accountCode string, token string) (*Response, *Billing, error) {
action := fmt.Sprintf("accounts/%s/billing_info", accountCode)
req, err := s.client.newRequest("POST", action, nil, Billing{Token: token})
if err != nil {
return nil, nil, err
}
var dst Billing
resp, err := s.client.do(req, &dst)
return resp, &dst, err
}
// Update updates the account's billing information with credit card or
// bank info.It is recommended you use recurly.js and a token with the
// UpdateWithToken method instead.
// https://dev.recurly.com/docs/update-an-accounts-billing-info-credit-card
// https://dev.recurly.com/docs/update-an-accounts-billing-info-bank-account
func (s *billingImpl) Update(accountCode string, b Billing) (*Response, *Billing, error) {
// Create clean billing object with write-only fields to avoid errors
// like sending additional/unknown/read-only fields.
clean := Billing{
FirstName: b.FirstName,
LastName: b.LastName,
Address: b.Address,
Address2: b.Address2,
City: b.City,
State: b.State,
Zip: b.Zip,
Country: b.Country,
Phone: b.Phone,
VATNumber: b.VATNumber,
IPAddress: b.IPAddress,
Number: b.Number,
Month: b.Month,
Year: b.Year,
VerificationValue: b.VerificationValue,
NameOnAccount: b.NameOnAccount,
RoutingNumber: b.RoutingNumber,
AccountNumber: b.AccountNumber,
AccountType: b.AccountType,
}
action := fmt.Sprintf("accounts/%s/billing_info", accountCode)
req, err := s.client.newRequest("PUT", action, nil, clean)
if err != nil {
return nil, nil, err
}
var dst Billing
resp, err := s.client.do(req, &dst)
return resp, &dst, err
}
// UpdateWithToken updates an account's billing information using a token
// generated by Recurly.js. Returns the account's created Billing Information.
// https://dev.recurly.com/docs/update-an-accounts-billing-info-token
func (s *billingImpl) UpdateWithToken(accountCode string, token string) (*Response, *Billing, error) {
action := fmt.Sprintf("accounts/%s/billing_info", accountCode)
req, err := s.client.newRequest("PUT", action, nil, Billing{Token: token})
if err != nil {
return nil, nil, err
}
var dst Billing
resp, err := s.client.do(req, &dst)
if err != nil || resp.StatusCode >= http.StatusBadRequest {
return resp, nil, err
}
return resp, &dst, err
}
// Clear removes any stored billing information for an account. If the account
// has a subscription, the renewal will go into past due unless you update the
// billing info before the renewal occurs.
// https://dev.recurly.com/docs/clear-an-accounts-billing-info
func (s *billingImpl) Clear(accountCode string) (*Response, error) {
action := fmt.Sprintf("accounts/%s/billing_info", accountCode)
req, err := s.client.newRequest("DELETE", action, nil, nil)
if err != nil {
return nil, err
}
return s.client.do(req, nil)
}