This repository was archived by the owner on Nov 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsubscription.go
More file actions
113 lines (108 loc) · 3.59 KB
/
subscription.go
File metadata and controls
113 lines (108 loc) · 3.59 KB
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
package stripe
import (
"encoding/json"
"net/url"
"strconv"
)
type Subscription struct {
Status string `json:"status"` // "trialing"/"active"/"past_due"/"canceled"/"unpaid"
Object string `json:"object"` // Should always be "subscription"
PeriodStart int64 `json:"current_period_start"`
PeriodEnd int64 `json:"current_period_end"`
CancelAtPeriodEnd bool `json:"cancel_at_period_end"`
CanceledAt int64 `json:"canceled_at"`
EndedAt int64 `json:"ended_at"`
Start int64 `json:"start"`
TrialStart int64 `json:"trial_start"`
TrialEnd int64 `json:"trial_end"`
Plan *Plan `json:"plan"`
CustomerID string `json:"customer"` // The Customer's ID
Error *RawError `json:"error"`
}
// Values assigns the applicable properties of *subscription to the appropriate keys
// in *values. This makes constructing an HTTP request around a Subscription simpler.
func (subscription *Subscription) Values(values *url.Values) error {
if subscription == nil {
// TODO: throw an error
}
if subscription.Plan == nil {
// TODO: throw an error
}
if subscription.Plan.ID == "" {
// TODO: throw an error
}
values.Set("plan", subscription.Plan.ID)
if subscription.TrialEnd > 0 {
values.Set("trial_end", strconv.FormatInt(subscription.TrialEnd, 10))
}
return nil
}
// Subscribe updates the customer's plan. The customer will be billed monthly according to the new plan.
//
// *subscription is the only required argument. *subscription.Plan.ID and *subscription.CustomerID must be set.
//
// If couponID is non-empty, it will be used as the ID of a coupon to apply to the customer.
//
// If prorate is true, the customer will be prorated to make up for the price changes
//
// If chargeable is non-nil, it will be attached to the customer. Can be either a token or a credit card.
func (stripe *Stripe) Subscribe(subscription *Subscription, couponID string, prorate bool, chargeable Chargeable) (resp *Subscription, err error) {
values := make(url.Values)
if subscription.CustomerID == "" {
// TODO: throw an error
}
err = subscription.Values(&values)
if err != nil {
return nil, err
}
if couponID != "" {
values.Set("coupon", couponID)
}
if prorate != true {
values.Set("prorate", "false")
}
if chargeable != nil {
chargeable.ChargeValues(&values)
}
data := values.Encode()
r, err := stripe.request("POST", "customers/"+subscription.CustomerID+"/subscription", data)
if err != nil {
return nil, err
}
err = json.Unmarshal(r, &resp)
if err != nil {
return nil, err
}
if resp.Error != nil {
// TODO: Throw an error
}
return
}
// Unsubscribe cancels the subscription of the customer whose ID matches customerID.
//
// If at_period_end is true, the subscription will remain active until the end of the period, at which point
// it will be cancelled and not renewed. Otherwise, the subscription will be cancelled immediately.
//
// Any pending invoice items will still be charged for at the end of the period unless they are manually deleted.
func (stripe *Stripe) Unsubscribe(customerID string, at_period_end bool) (resp *Subscription, err error) {
values := make(url.Values)
if at_period_end {
values.Set("at_period_end", "true")
}
params := values.Encode()
if params != "" {
params = "?" + params
}
r, err := stripe.request("DELETE", "customers/"+customerID+"/subscription"+params, "")
if err != nil {
return nil, err
}
err = json.Unmarshal(r, &resp)
if err != nil {
return nil, err
}
if resp.Error != nil {
// TODO: Throw an error
}
return
}