forked from stripe/stripe-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sub.go
244 lines (212 loc) · 12.4 KB
/
sub.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package stripe
import (
"encoding/json"
"github.com/stripe/stripe-go/form"
)
// SubscriptionStatus is the list of allowed values for the subscription's status.
type SubscriptionStatus string
// List of values that SubscriptionStatus can take.
const (
SubscriptionStatusActive SubscriptionStatus = "active"
SubscriptionStatusAll SubscriptionStatus = "all"
SubscriptionStatusCanceled SubscriptionStatus = "canceled"
SubscriptionStatusIncomplete SubscriptionStatus = "incomplete"
SubscriptionStatusIncompleteExpired SubscriptionStatus = "incomplete_expired"
SubscriptionStatusPastDue SubscriptionStatus = "past_due"
SubscriptionStatusTrialing SubscriptionStatus = "trialing"
SubscriptionStatusUnpaid SubscriptionStatus = "unpaid"
)
// SubscriptionBilling is the type of collection method for this subscription's invoices.
// This is considered deprecated. Use SubscriptionCollectionMethod instead.
type SubscriptionBilling string
// List of values that SubscriptionBilling can take.
const (
SubscriptionBillingChargeAutomatically SubscriptionBilling = "charge_automatically"
SubscriptionBillingSendInvoice SubscriptionBilling = "send_invoice"
)
// SubscriptionCollectionMethod is the type of collection method for this subscription's invoices.
type SubscriptionCollectionMethod string
// List of values that SubscriptionCollectionMethod can take.
const (
SubscriptionCollectionMethodChargeAutomatically SubscriptionCollectionMethod = "charge_automatically"
SubscriptionCollectionMethodSendInvoice SubscriptionCollectionMethod = "send_invoice"
)
// SubscriptionPaymentBehavior lets you control the behavior of subscription creation in case of
// a failed payment.
type SubscriptionPaymentBehavior string
// List of values that SubscriptionPaymentBehavior can take.
const (
SubscriptionPaymentBehaviorAllowIncomplete SubscriptionPaymentBehavior = "allow_incomplete"
SubscriptionPaymentBehaviorErrorIfIncomplete SubscriptionPaymentBehavior = "error_if_incomplete"
)
// SubscriptionTransferDataParams is the set of parameters allowed for the transfer_data hash.
type SubscriptionTransferDataParams struct {
Destination *string `form:"destination"`
}
// SubscriptionParams is the set of parameters that can be used when creating or updating a subscription.
// For more details see https://stripe.com/docs/api#create_subscription and https://stripe.com/docs/api#update_subscription.
type SubscriptionParams struct {
Params `form:"*"`
ApplicationFeePercent *float64 `form:"application_fee_percent"`
BackdateStartDate *int64 `form:"backdate_start_date"`
BillingCycleAnchor *int64 `form:"billing_cycle_anchor"`
BillingCycleAnchorNow *bool `form:"-"` // See custom AppendTo
BillingCycleAnchorUnchanged *bool `form:"-"` // See custom AppendTo
BillingThresholds *SubscriptionBillingThresholdsParams `form:"billing_thresholds"`
CancelAt *int64 `form:"cancel_at"`
CancelAtPeriodEnd *bool `form:"cancel_at_period_end"`
Card *CardParams `form:"card"`
CollectionMethod *string `form:"collection_method"`
Coupon *string `form:"coupon"`
Customer *string `form:"customer"`
DaysUntilDue *int64 `form:"days_until_due"`
DefaultPaymentMethod *string `form:"default_payment_method"`
DefaultSource *string `form:"default_source"`
DefaultTaxRates []*string `form:"default_tax_rates"`
Items []*SubscriptionItemsParams `form:"items"`
OffSession *bool `form:"off_session"`
OnBehalfOf *string `form:"on_behalf_of"`
PaymentBehavior *string `form:"payment_behavior"`
Plan *string `form:"plan"`
Prorate *bool `form:"prorate"`
ProrationDate *int64 `form:"proration_date"`
Quantity *int64 `form:"quantity"`
TrialEnd *int64 `form:"trial_end"`
TransferData *SubscriptionTransferDataParams `form:"transfer_data"`
TrialEndNow *bool `form:"-"` // See custom AppendTo
TrialFromPlan *bool `form:"trial_from_plan"`
TrialPeriodDays *int64 `form:"trial_period_days"`
// This parameter is deprecated and we recommend that you use CollectionMethod instead.
Billing *string `form:"billing"`
// This parameter is deprecated and we recommend that you use TaxRates instead.
TaxPercent *float64 `form:"tax_percent"`
}
// SubscriptionBillingThresholdsParams is a structure representing the parameters allowed to control
// billing thresholds for a subscription.
type SubscriptionBillingThresholdsParams struct {
AmountGTE *int64 `form:"amount_gte"`
ResetBillingCycleAnchor *bool `form:"reset_billing_cycle_anchor"`
}
// SubscriptionCancelParams is the set of parameters that can be used when canceling a subscription.
// For more details see https://stripe.com/docs/api#cancel_subscription
type SubscriptionCancelParams struct {
Params `form:"*"`
InvoiceNow *bool `form:"invoice_now"`
Prorate *bool `form:"prorate"`
}
// AppendTo implements custom encoding logic for SubscriptionParams so that the special
// "now" value for billing_cycle_anchor and trial_end can be implemented
// (they're otherwise timestamps rather than strings).
func (p *SubscriptionParams) AppendTo(body *form.Values, keyParts []string) {
if BoolValue(p.BillingCycleAnchorNow) {
body.Add(form.FormatKey(append(keyParts, "billing_cycle_anchor")), "now")
}
if BoolValue(p.BillingCycleAnchorUnchanged) {
body.Add(form.FormatKey(append(keyParts, "billing_cycle_anchor")), "unchanged")
}
if BoolValue(p.TrialEndNow) {
body.Add(form.FormatKey(append(keyParts, "trial_end")), "now")
}
}
// SubscriptionItemsParams is the set of parameters that can be used when creating or updating a subscription item on a subscription
// For more details see https://stripe.com/docs/api#create_subscription and https://stripe.com/docs/api#update_subscription.
type SubscriptionItemsParams struct {
Params `form:"*"`
BillingThresholds *SubscriptionItemBillingThresholdsParams `form:"billing_thresholds"`
ClearUsage *bool `form:"clear_usage"`
Deleted *bool `form:"deleted"`
ID *string `form:"id"`
Plan *string `form:"plan"`
Quantity *int64 `form:"quantity"`
TaxRates []*string `form:"tax_rates"`
}
// SubscriptionListParams is the set of parameters that can be used when listing active subscriptions.
// For more details see https://stripe.com/docs/api#list_subscriptions.
type SubscriptionListParams struct {
ListParams `form:"*"`
CollectionMethod *string `form:"collection_method"`
Created int64 `form:"created"`
CreatedRange *RangeQueryParams `form:"created"`
CurrentPeriodEnd *int64 `form:"current_period_end"`
CurrentPeriodEndRange *RangeQueryParams `form:"current_period_end"`
CurrentPeriodStart *int64 `form:"current_period_start"`
CurrentPeriodStartRange *RangeQueryParams `form:"current_period_start"`
Customer string `form:"customer"`
Plan string `form:"plan"`
Status string `form:"status"`
// This parameter is deprecated and we recommend that you use CollectionMethod instead.
Billing *string `form:"billing"`
}
// SubscriptionTransferData represents the information for the transfer_data associated with a subscription.
type SubscriptionTransferData struct {
Destination *Account `json:"destination"`
}
// Subscription is the resource representing a Stripe subscription.
// For more details see https://stripe.com/docs/api#subscriptions.
type Subscription struct {
ApplicationFeePercent float64 `json:"application_fee_percent"`
BillingCycleAnchor int64 `json:"billing_cycle_anchor"`
BillingThresholds *SubscriptionBillingThresholds `json:"billing_thresholds"`
CancelAt int64 `json:"cancel_at"`
CancelAtPeriodEnd bool `json:"cancel_at_period_end"`
CanceledAt int64 `json:"canceled_at"`
CollectionMethod SubscriptionCollectionMethod `json:"collection_method"`
Created int64 `json:"created"`
CurrentPeriodEnd int64 `json:"current_period_end"`
CurrentPeriodStart int64 `json:"current_period_start"`
Customer *Customer `json:"customer"`
DaysUntilDue int64 `json:"days_until_due"`
DefaultPaymentMethod *PaymentMethod `json:"default_payment_method"`
DefaultSource *PaymentSource `json:"default_source"`
DefaultTaxRates []*TaxRate `json:"default_tax_rates"`
Discount *Discount `json:"discount"`
EndedAt int64 `json:"ended_at"`
ID string `json:"id"`
Items *SubscriptionItemList `json:"items"`
LatestInvoice *Invoice `json:"latest_invoice"`
Livemode bool `json:"livemode"`
Metadata map[string]string `json:"metadata"`
Object string `json:"object"`
OnBehalfOf *Account `json:"on_behalf_of"`
PendingSetupIntent *SetupIntent `json:"pending_setup_intent"`
Plan *Plan `json:"plan"`
Quantity int64 `json:"quantity"`
Schedule *SubscriptionSchedule `json:"schedule"`
StartDate int64 `json:"start_date"`
Status SubscriptionStatus `json:"status"`
TransferData *SubscriptionTransferData `json:"transfer_data"`
TrialEnd int64 `json:"trial_end"`
TrialStart int64 `json:"trial_start"`
// This field is deprecated and we recommend that you use CollectionMethod instead.
Billing SubscriptionBilling `json:"billing"`
// This field is deprecated and we recommend that you use StartDate instead.
Start int64 `json:"start"`
// This field is deprecated and we recommend that you use TaxRates instead.
TaxPercent float64 `json:"tax_percent"`
}
// SubscriptionBillingThresholds is a structure representing the billing thresholds for a subscription.
type SubscriptionBillingThresholds struct {
AmountGTE int64 `json:"amount_gte"`
ResetBillingCycleAnchor bool `json:"reset_billing_cycle_anchor"`
}
// SubscriptionList is a list object for subscriptions.
type SubscriptionList struct {
ListMeta
Data []*Subscription `json:"data"`
}
// UnmarshalJSON handles deserialization of a Subscription.
// This custom unmarshaling is needed because the resulting
// property may be an id or the full struct if it was expanded.
func (s *Subscription) UnmarshalJSON(data []byte) error {
if id, ok := ParseID(data); ok {
s.ID = id
return nil
}
type subscription Subscription
var v subscription
if err := json.Unmarshal(data, &v); err != nil {
return err
}
*s = Subscription(v)
return nil
}