ducky/subscriptions

Paddy 2015-06-16 Child:b240b6123548

2:61c4ce5850da Go to Latest

ducky/subscriptions/stripe.go

Use stripe's built-in subscriptions. We're going to use Stripe's built-in subscriptions to manage our subscriptions, which required us to change a lot of stuff. We're now tracking stripe_subscription instead of stripe_customer, and we need to track the plan, status, and if the user is canceling after this month. We also don't need to know when to begin charging them (Stripe will do it), but we should track when their trial starts and ends, when the current pay period they're in starts and ends, when they canceled (if they've canceled), the number of failed charge attempts they've had, and the last time we notified them about billing (To avoid spamming users). We get to delete all the stuff about periods, which is nice. We updated our SubscriptionChange type to match. Notably, there are a lot of non-user modifiable things now, but our Stripe webhook will need to use them to update our database records and keep them in sync. We no longer need to deal with sorting stuff, which is also nice. Our SubscriptionStats have been updated to be... useful? Now we can track how many users we have, and how many of them have failing credit cards, how many are canceling at the end of their current payment period, and how many users are on each plan. We also switched around how the TestUpdateSubscription loops were written, to avoid resetting more than we needed to. Before, we had to call store.reset() after every single change iteration. Now we get to call it only when switching stores. This makes a significant difference in the amount of time it takes to run tests. Finally, we added a test case for retrieving subscription stats. It's minimal, but it works.

History
1 package subscriptions
3 import (
4 "time"
6 "code.secondbit.org/uuid.hg"
8 "github.com/stripe/stripe-go"
9 "github.com/stripe/stripe-go/customer"
10 "github.com/stripe/stripe-go/sub"
11 )
13 type Stripe struct {
14 apiKey string
15 customers customer.Client
16 subscriptions sub.Client
17 }
19 func NewStripe(apiKey string, backend stripe.Backend) Stripe {
20 return Stripe{
21 apiKey: apiKey,
22 customers: customer.Client{
23 B: backend,
24 Key: apiKey,
25 },
26 subscriptions: sub.Client{
27 B: backend,
28 Key: apiKey,
29 },
30 }
31 }
33 func CreateStripeCustomer(token, email string, userID uuid.ID, s Stripe) (*stripe.Customer, error) {
34 customerParams := &stripe.CustomerParams{
35 Desc: "Customer for user " + userID.String(),
36 Email: email,
37 }
38 customerParams.AddMeta("UserID", userID.String())
39 customerParams.SetSource(token)
40 c, err := s.customers.New(customerParams)
41 if err != nil {
42 return nil, err
43 }
44 return c, nil
45 }
47 func CreateStripeSubscription(customer, plan string, userID uuid.ID, s Stripe) (*stripe.Sub, error) {
48 subParams := &stripe.SubParams{
49 Plan: plan,
50 Customer: customer,
51 }
52 subParams.AddMeta("UserID", userID.String())
54 resp, err := s.subscriptions.New(subParams)
55 if err != nil {
56 return nil, err
57 }
58 return resp, nil
59 }
61 func CreateSubscription(token, email string, subscription Subscription, s Stripe, store subscriptionStore) error {
62 // create the subscription in our datastore
63 // this will fail if they already have a subscription, which prevents duplicate/orphaned Stripe customers being created
64 err := store.createSubscription(subscription)
65 if err != nil {
66 return err
67 }
69 // create the customer in Stripe, storing the token for reuse
70 customer, err := CreateStripeCustomer(token, email, subscription.UserID, s)
71 if err != nil {
72 // TODO: delete subscription object
73 return err
74 }
76 // create the subscription in Stripe, storing the ID for tracking and associating purposes
77 stripeSub, err := CreateStripeSubscription(customer.ID, subscription.Plan, subscription.UserID, s)
78 if err != nil {
79 // TODO: delete customer
80 // TODO: delete subscription object
81 return err
82 }
84 // update our subscription in the datastore with the latest information from Stripe
85 change := StripeSubscriptionChange(subscription, *stripeSub)
86 err = store.updateSubscription(subscription.UserID, change)
87 if err != nil {
88 // TODO: log an error, manually retry later?
89 return err
90 }
91 return nil
92 }
94 func StripeSubscriptionChange(orig Subscription, subscription stripe.Sub) SubscriptionChange {
95 var change SubscriptionChange
96 if subscription.Plan != nil && orig.Plan != subscription.Plan.ID {
97 change.Plan = &subscription.Plan.ID
98 }
99 if string(subscription.Status) != orig.Status {
100 status := string(subscription.Status)
101 change.Status = &status
102 }
103 if subscription.EndCancel != orig.Canceling {
104 change.Canceling = &subscription.EndCancel
105 }
106 if !time.Unix(subscription.TrialStart, 0).Equal(orig.TrialStart) {
107 trialStart := time.Unix(subscription.TrialStart, 0)
108 change.TrialStart = &trialStart
109 }
110 if !time.Unix(subscription.TrialEnd, 0).Equal(orig.TrialEnd) {
111 trialEnd := time.Unix(subscription.TrialEnd, 0)
112 change.TrialEnd = &trialEnd
113 }
114 if !time.Unix(subscription.PeriodStart, 0).Equal(orig.PeriodStart) {
115 periodStart := time.Unix(subscription.PeriodStart, 0)
116 change.PeriodStart = &periodStart
117 }
118 if !time.Unix(subscription.PeriodEnd, 0).Equal(orig.PeriodEnd) {
119 periodEnd := time.Unix(subscription.PeriodEnd, 0)
120 change.PeriodEnd = &periodEnd
121 }
122 if !time.Unix(subscription.Canceled, 0).Equal(orig.CanceledAt) {
123 canceledAt := time.Unix(subscription.Canceled, 0)
124 change.CanceledAt = &canceledAt
125 }
126 return change
127 }