forked from stripe/stripe-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sub.go
100 lines (86 loc) · 2.79 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
package stripe
import "encoding/json"
// SubStatus is the list of allowed values for the subscription's status.
// Allowed values are "trialing", "active", "past_due", "canceled", "unpaid".
type SubStatus string
const (
Trialing SubStatus = "trialing"
Active SubStatus = "active"
PastDue SubStatus = "past_due"
Canceled SubStatus = "canceled"
Unpaid SubStatus = "unpaid"
)
// SubParams 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 SubParams struct {
Params
Customer, Plan string
Coupon, Token string
TrialEnd int64
Card *CardParams
Quantity uint64
FeePercent float64
NoProrate, EndCancel bool
}
// SubListParams 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 SubListParams struct {
ListParams
Customer string
}
// Sub is the resource representing a Stripe subscription.
// For more details see https://stripe.com/docs/api#subscriptions.
type Sub struct {
Id string `json:"id"`
EndCancel bool `json:"cancel_at_period_end"`
Customer *Customer `json:"customer"`
Plan *Plan `json:"plan"`
Quantity uint64 `json:"quantity"`
Status SubStatus `json:"status"`
FeePercent float64 `json:"application_fee_percent"`
Canceled int64 `json:"canceled_at"`
PeriodEnd int64 `json:"current_period_end"`
PeriodStart int64 `json:"current_period_start"`
Discount *Discount `json:"discount"`
Ended int64 `json:"ended_at"`
Meta map[string]string `json:"metadata"`
TrialEnd int64 `json:"trial_end"`
TrialStart int64 `json:"trial_start"`
}
// SubList is a list object for subscriptions.
type SubList struct {
ListMeta
Values []*Sub `json:"data"`
}
// SubIter is a iterator for list responses.
type SubIter struct {
Iter *Iter
}
// Next returns the next value in the list.
func (i *SubIter) Next() (*Sub, error) {
s, err := i.Iter.Next()
if err != nil {
return nil, err
}
return s.(*Sub), err
}
// Stop returns true if there are no more iterations to be performed.
func (i *SubIter) Stop() bool {
return i.Iter.Stop()
}
// Meta returns the list metadata.
func (i *SubIter) Meta() *ListMeta {
return i.Iter.Meta()
}
func (s *Sub) UnmarshalJSON(data []byte) error {
type sub Sub
var ss sub
err := json.Unmarshal(data, &ss)
if err == nil {
*s = Sub(ss)
} else {
// the id is surrounded by escaped \, so ignore those
s.Id = string(data[1 : len(data)-1])
}
return nil
}