forked from blacklightcms/recurly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
billing.go
183 lines (159 loc) · 6.8 KB
/
billing.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package recurly
import (
"context"
"encoding/xml"
"fmt"
"net"
"net/http"
)
// BillingService manages the interactions for billing.
type BillingService interface {
// Get retrieves billing info for an account. If the account does not exist,
// or the account does not have billing info, a nil billing struct and nil
// error are returned.
//
// https://dev.recurly.com/docs/lookup-an-accounts-billing-info
Get(ctx context.Context, accountCode string) (*Billing, error)
// Create creates an account's billing info. To use the recurly.js token (recommended),
// set b.Token and optionally b.Currency.
// https://dev.recurly.com/docs/create-an-accounts-billing-info-token
//
// To create with credit card, bank info, or other, set the appropriate fields
// on b. See the following links for specifics:
// https://dev.recurly.com/docs/create-an-accounts-billing-info-credit-card
// https://dev.recurly.com/docs/create-an-accounts-billing-info-bank-account
// https://dev.recurly.com/docs/create-an-accounts-billing-info-using-external-token
Create(ctx context.Context, accountCode string, b Billing) (*Billing, error)
// Update updates an account's billing info. To use the recurly.js token (recommended),
// set b.Token and optionally b.Currency.
// https://dev.recurly.com/docs/update-an-accounts-billing-info-token
//
// To update with credit card, bank info, or other, set the appropriate fields
// on b. See the following links for specifics:
// https://dev.recurly.com/docs/update-an-accounts-billing-info-credit-card
// https://dev.recurly.com/docs/update-an-accounts-billing-info-bank-account
// https://dev.recurly.com/docs/update-an-accounts-billing-info-using-external-token
Update(ctx context.Context, accountCode string, b Billing) (*Billing, error)
// Clear removes 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
Clear(ctx context.Context, accountCode string) error
}
// Supported card type constants.
const (
CardTypeAmericanExpress = "american_express"
CardTypeDankort = "dankort"
CardTypeDinersClub = "diners_club"
CardTypeDiscover = "discover"
CardTypeForbrugsforeningen = "forbrugsforeningen"
CardTypeJCB = "jcb"
CardTypeLaser = "laser"
CardTypeMaestro = "maestro"
CardTypeMaster = "master"
CardTypeVisa = "visa"
)
// Billing holds billing info for a single account.
type Billing struct {
XMLName xml.Name `xml:"billing_info"`
FirstName string `xml:"first_name,omitempty"`
LastName string `xml:"last_name,omitempty"`
Company string `xml:"company,omitempty"`
Address string `xml:"address1,omitempty"`
Address2 string `xml:"address2,omitempty"`
City string `xml:"city,omitempty"`
State string `xml:"state,omitempty"`
Zip string `xml:"zip,omitempty"`
Country string `xml:"country,omitempty"`
Phone string `xml:"phone,omitempty"`
VATNumber string `xml:"vat_number,omitempty"`
IPAddress net.IP `xml:"ip_address,omitempty"`
IPAddressCountry string `xml:"ip_address_country,omitempty"`
PaymentType string `xml:"type,attr,omitempty"`
// Credit Card Info
FirstSix string `xml:"first_six,omitempty"`
LastFour string `xml:"last_four,omitempty"`
CardType string `xml:"card_type,omitempty"`
Number int `xml:"number,omitempty"`
Month int `xml:"month,omitempty"`
Year int `xml:"year,omitempty"`
VerificationValue int `xml:"verification_value,omitempty"` // Create/update only
// Paypal
PaypalAgreementID string `xml:"paypal_billing_agreement_id,omitempty"`
// BrainTree
BrainTreePaymentNonce string `xml:"braintree_payment_nonce,omitempty"`
// Amazon
AmazonAgreementID string `xml:"amazon_billing_agreement_id,omitempty"`
AmazonRegion string `xml:"amazon_region,omitempty"` // 'eu', 'us', or 'uk'
// Bank Account
NameOnAccount string `xml:"name_on_account,omitempty"`
RoutingNumber string `xml:"routing_number,omitempty"`
AccountNumber string `xml:"account_number,omitempty"`
AccountType string `xml:"account_type,omitempty"`
ExternalHPPType string `xml:"external_hpp_type,omitempty"` // only usable with purchases API
Currency string `xml:"currency,omitempty"` // Create/update only
Token string `xml:"token_id,omitempty"` // Create/update only
ThreeDSecureActionResultTokenID string `xml:"three_d_secure_action_result_token_id,omitempty"` // Create/update only
TransactionType string `xml:"transaction_type,omitempty"` // Create only
}
// Type returns the billing info type. Returns either "", "bank", or an empty string.
func (b Billing) Type() string {
if b.FirstSix != "" && b.LastFour != "" && b.Month > 0 && b.Year > 0 {
return "card"
} else if b.NameOnAccount != "" && b.RoutingNumber != "" && b.AccountNumber != "" {
return "bank"
}
return ""
}
var _ BillingService = &billingImpl{}
// billingImpl implements BillingService.
type billingImpl serviceImpl
func (s *billingImpl) Get(ctx context.Context, accountCode string) (*Billing, error) {
path := fmt.Sprintf("/accounts/%s/billing_info", accountCode)
req, err := s.client.newRequest("GET", path, nil)
if err != nil {
return nil, err
}
var dst Billing
if _, err := s.client.do(ctx, req, &dst); err != nil {
if e, ok := err.(*ClientError); ok && e.Response.StatusCode == http.StatusNotFound {
return nil, nil
}
return nil, err
}
return &dst, nil
}
func (s *billingImpl) Create(ctx context.Context, accountCode string, b Billing) (*Billing, error) {
path := fmt.Sprintf("/accounts/%s/billing_info", accountCode)
req, err := s.client.newRequest("POST", path, b)
if err != nil {
return nil, err
}
var dst Billing
if _, err := s.client.do(ctx, req, &dst); err != nil {
return nil, err
}
return &dst, nil
}
func (s *billingImpl) Update(ctx context.Context, accountCode string, b Billing) (*Billing, error) {
path := fmt.Sprintf("/accounts/%s/billing_info", accountCode)
req, err := s.client.newRequest("PUT", path, b)
if err != nil {
return nil, err
}
var dst Billing
if _, err := s.client.do(ctx, req, &dst); err != nil {
return nil, err
}
return &dst, nil
}
func (s *billingImpl) Clear(ctx context.Context, accountCode string) error {
path := fmt.Sprintf("/accounts/%s/billing_info", accountCode)
req, err := s.client.newRequest("DELETE", path, nil)
if err != nil {
return err
}
_, err = s.client.do(ctx, req, nil)
return err
}