forked from blacklightcms/recurly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
purchases.go
185 lines (161 loc) · 7.1 KB
/
purchases.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
184
185
package recurly
import (
"context"
"encoding/xml"
"fmt"
)
// PurchasesService manages the interactions for a purchase
// involving at least one adjustment or one subscription.
type PurchasesService interface {
// Create a purchase. See Recurly's documentation for more details.
//
// https://dev.recurly.com/docs/create-purchase
Create(ctx context.Context, p Purchase) (*InvoiceCollection, error)
// Preview a purchase. See Recurly's documentation for more details.
//
// https://dev.recurly.com/docs/preview-purchase
Preview(ctx context.Context, p Purchase) (*InvoiceCollection, error)
// Authorize creates a pending purchase that can be activated at a later
// time once payment has been completed on an external source (e.g. Adyen's
// Hosted Payment Pages).
//
// p.Account.Email and p.Account.Billing.ExternalHPPType appear to be required.
//
// https://dev.recurly.com/docs/authorize-purchase
Authorize(ctx context.Context, p Purchase) (*Purchase, error)
// Pending is used for Adyen HPP transaction requests. This runs the validations
// but not the transactions. See Recurly's documentation for more info.
//
// https://dev.recurly.com/docs/pending-purchase
Pending(ctx context.Context, p Purchase) (*Purchase, error)
// Capture an open Authorization request. See Recurly's documentation
// for details.
//
// https://dev.recurly.com/docs/capture-purchase
Capture(ctx context.Context, transactionUUID string) (*InvoiceCollection, error)
// Cancel an open Authorization request. See Recurly's documentation
// for details.
//
// https://dev.recurly.com/docs/cancel-purchase
Cancel(ctx context.Context, transactionUUID string) (*InvoiceCollection, error)
}
// Purchase represents an individual checkout holding at least one
// subscription OR one adjustment.
// NOTE: Adjustments cannot contain a Currency field. Use Purchase.Currency instead.
type Purchase struct {
XMLName xml.Name `xml:"purchase"`
Account Account `xml:"account,omitempty"`
Adjustments []Adjustment `xml:"adjustments>adjustment,omitempty"`
CollectionMethod string `xml:"collection_method,omitempty"`
Currency string `xml:"currency"`
PONumber string `xml:"po_number,omitempty"`
NetTerms NullInt `xml:"net_terms,omitempty"`
GiftCard string `xml:"gift_card>redemption_code,omitempty"`
CouponCodes []string `xml:"coupon_codes>coupon_code,omitempty"`
Subscriptions []PurchaseSubscription `xml:"subscriptions>subscription,omitempty"`
CustomerNotes string `xml:"customer_notes,omitempty"`
TermsAndConditions string `xml:"terms_and_conditions,omitempty"`
VATReverseChargeNotes string `xml:"vat_reverse_charge_notes,omitempty"`
ShippingAddressID int64 `xml:"shipping_address_id,omitempty"`
GatewayCode string `xml:"gateway_code,omitempty"`
ShippingFees *[]ShippingFee `xml:"shipping_fees>shipping_fee,omitempty"`
TransactionType string `xml:"transaction_type,omitempty"` // Create only
}
// PurchaseSubscription represents a subscription to purchase some new subscription.
// This is different from the Subscription struct in that only fields allowed to
// be used with the purchases API are available.
type PurchaseSubscription struct {
XMLName xml.Name `xml:"subscription"`
PlanCode string `xml:"plan_code"`
SubscriptionAddOns *[]SubscriptionAddOn `xml:"subscription_add_ons>subscription_add_on,omitempty"`
UnitAmountInCents NullInt `xml:"unit_amount_in_cents,omitempty"`
Quantity int `xml:"quantity,omitempty"`
TrialEndsAt NullTime `xml:"trial_ends_at,omitempty"`
StartsAt NullTime `xml:"starts_at,omitempty"`
TotalBillingCycles int `xml:"total_billing_cycles,omitempty"`
RenewalBillingCycles NullInt `xml:"renewal_billing_cycles,omitempty"`
NextBillDate NullTime `xml:"next_bill_date,omitempty"`
AutoRenew bool `xml:"auto_renew,omitempty"`
CustomFields *CustomFields `xml:"custom_fields,omitempty"`
ShippingAddress *ShippingAddress `xml:"shipping_address,omitempty"`
ShippingAddressID int64 `xml:"shipping_address_id,omitempty"`
ShippingMethodCode string `xml:"shipping_method_code,omitempty"`
ShippingAmountInCents NullInt `xml:"shipping_amount_in_cents,omitempty"`
}
// ShippingFee holds shipping fees for a purchase.
type ShippingFee struct {
XMLName xml.Name `xml:"shipping_fee"`
ShippingMethodCode string `xml:"shipping_method_code,omitempty"`
ShippingAmountInCents NullInt `xml:"shipping_amount_in_cents,omitempty"`
}
var _ PurchasesService = &purchasesImpl{}
// purchasesImpl implements PurchasesService.
type purchasesImpl serviceImpl
func (s *purchasesImpl) Create(ctx context.Context, p Purchase) (*InvoiceCollection, error) {
req, err := s.client.newRequest("POST", "/purchases", p)
if err != nil {
return nil, err
}
var dst InvoiceCollection
if _, err := s.client.do(ctx, req, &dst); err != nil {
return nil, err
}
return &dst, nil
}
func (s *purchasesImpl) Preview(ctx context.Context, p Purchase) (*InvoiceCollection, error) {
req, err := s.client.newRequest("POST", "/purchases/preview", p)
if err != nil {
return nil, err
}
var dst InvoiceCollection
if _, err := s.client.do(ctx, req, &dst); err != nil {
return nil, err
}
return &dst, nil
}
func (s *purchasesImpl) Authorize(ctx context.Context, p Purchase) (*Purchase, error) {
req, err := s.client.newRequest("POST", "/purchases/authorize", p)
if err != nil {
return nil, err
}
var dst Purchase
if _, err := s.client.do(ctx, req, &dst); err != nil {
return nil, err
}
return &dst, nil
}
func (s *purchasesImpl) Pending(ctx context.Context, p Purchase) (*Purchase, error) {
req, err := s.client.newRequest("POST", "/purchases/pending", p)
if err != nil {
return nil, err
}
var dst Purchase
if _, err := s.client.do(ctx, req, &dst); err != nil {
return nil, err
}
return &dst, nil
}
func (s *purchasesImpl) Capture(ctx context.Context, transactionUUID string) (*InvoiceCollection, error) {
path := fmt.Sprintf("/purchases/%s/capture", sanitizeUUID(transactionUUID))
req, err := s.client.newRequest("POST", path, nil)
if err != nil {
return nil, err
}
var dst InvoiceCollection
if _, err := s.client.do(ctx, req, &dst); err != nil {
return nil, err
}
return &dst, nil
}
func (s *purchasesImpl) Cancel(ctx context.Context, transactionUUID string) (*InvoiceCollection, error) {
path := fmt.Sprintf("/purchases/%s/cancel", sanitizeUUID(transactionUUID))
req, err := s.client.newRequest("POST", path, nil)
if err != nil {
return nil, err
}
var dst InvoiceCollection
if _, err := s.client.do(ctx, req, &dst); err != nil {
return nil, err
}
return &dst, nil
}