-
Notifications
You must be signed in to change notification settings - Fork 461
/
sub.go
152 lines (135 loc) · 6.91 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
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
const (
SubscriptionStatusActive SubscriptionStatus = "active"
SubscriptionStatusAll SubscriptionStatus = "all"
SubscriptionStatusCanceled SubscriptionStatus = "canceled"
SubscriptionStatusPastDue SubscriptionStatus = "past_due"
SubscriptionStatusTrialing SubscriptionStatus = "trialing"
SubscriptionStatusUnpaid SubscriptionStatus = "unpaid"
)
// SubscriptionBilling is the type of billing method for this subscription's invoices.
type SubscriptionBilling string
const (
SubscriptionBillingChargeAutomatically SubscriptionBilling = "charge_automatically"
SubscriptionBillingSendInvoice SubscriptionBilling = "send_invoice"
)
// 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"`
Billing *string `form:"billing"`
BillingCycleAnchor *int64 `form:"billing_cycle_anchor"`
BillingCycleAnchorNow *bool `form:"-"` // See custom AppendTo
BillingCycleAnchorUnchanged *bool `form:"-"` // See custom AppendTo
CancelAtPeriodEnd *bool `form:"cancel_at_period_end"`
Card *CardParams `form:"card"`
Coupon *string `form:"coupon"`
Customer *string `form:"customer"`
DaysUntilDue *int64 `form:"days_until_due"`
Items []*SubscriptionItemsParams `form:"items,indexed"`
OnBehalfOf *string `form:"on_behalf_of"`
Plan *string `form:"plan"`
Prorate *bool `form:"prorate"`
ProrationDate *int64 `form:"proration_date"`
Quantity *int64 `form:"quantity"`
Source *string `form:"source"`
TaxPercent *float64 `form:"tax_percent"`
TrialEnd *int64 `form:"trial_end"`
TrialEndNow *bool `form:"-"` // See custom AppendTo
TrialFromPlan *bool `form:"trial_from_plan"`
TrialPeriodDays *int64 `form:"trial_period_days"`
}
// 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:"*"`
AtPeriodEnd *bool `form:"at_period_end"`
}
// 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 p.BillingCycleAnchorNow != nil {
body.Add(form.FormatKey(append(keyParts, "billing_cycle_anchor")), "now")
}
if p.BillingCycleAnchorUnchanged != nil {
body.Add(form.FormatKey(append(keyParts, "billing_cycle_anchor")), "unchanged")
}
if p.TrialEndNow != nil {
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:"*"`
ClearUsage *bool `form:"clear_usage"`
Deleted *bool `form:"deleted"`
ID *string `form:"id"`
Plan *string `form:"plan"`
Quantity *int64 `form:"quantity"`
}
// 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:"*"`
Billing string `form:"billing"`
Created int64 `form:"created"`
CreatedRange *RangeQueryParams `form:"created"`
Customer string `form:"customer"`
Plan string `form:"plan"`
Status string `form:"status"`
}
// 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"`
Billing SubscriptionBilling `json:"billing"`
BillingCycleAnchor int64 `json:"billing_cycle_anchor"`
CanceledAt int64 `json:"canceled_at"`
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"`
Discount *Discount `json:"discount"`
CancelAtPeriodEnd bool `json:"cancel_at_period_end"`
EndedAt int64 `json:"ended_at"`
ID string `json:"id"`
Items *SubscriptionItemList `json:"items"`
Metadata map[string]string `json:"metadata"`
Plan *Plan `json:"plan"`
Quantity int64 `json:"quantity"`
Start int64 `json:"start"`
Status SubscriptionStatus `json:"status"`
TaxPercent float64 `json:"tax_percent"`
TrialEnd int64 `json:"trial_end"`
TrialStart int64 `json:"trial_start"`
}
// 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 {
type sub Subscription
var ss sub
err := json.Unmarshal(data, &ss)
if err == nil {
*s = Subscription(ss)
} else {
// the id is surrounded by "\" characters, so strip them
s.ID = string(data[1 : len(data)-1])
}
return nil
}