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.
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"
15 customers customer.Client
16 subscriptions sub.Client
19 func NewStripe(apiKey string, backend stripe.Backend) Stripe {
22 customers: customer.Client{
26 subscriptions: sub.Client{
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(),
38 customerParams.AddMeta("UserID", userID.String())
39 customerParams.SetSource(token)
40 c, err := s.customers.New(customerParams)
47 func CreateStripeSubscription(customer, plan string, userID uuid.ID, s Stripe) (*stripe.Sub, error) {
48 subParams := &stripe.SubParams{
52 subParams.AddMeta("UserID", userID.String())
54 resp, err := s.subscriptions.New(subParams)
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)
69 // create the customer in Stripe, storing the token for reuse
70 customer, err := CreateStripeCustomer(token, email, subscription.UserID, s)
72 // TODO: delete subscription object
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)
79 // TODO: delete customer
80 // TODO: delete subscription object
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)
88 // TODO: log an error, manually retry later?
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
99 if string(subscription.Status) != orig.Status {
100 status := string(subscription.Status)
101 change.Status = &status
103 if subscription.EndCancel != orig.Canceling {
104 change.Canceling = &subscription.EndCancel
106 if !time.Unix(subscription.TrialStart, 0).Equal(orig.TrialStart) {
107 trialStart := time.Unix(subscription.TrialStart, 0)
108 change.TrialStart = &trialStart
110 if !time.Unix(subscription.TrialEnd, 0).Equal(orig.TrialEnd) {
111 trialEnd := time.Unix(subscription.TrialEnd, 0)
112 change.TrialEnd = &trialEnd
114 if !time.Unix(subscription.PeriodStart, 0).Equal(orig.PeriodStart) {
115 periodStart := time.Unix(subscription.PeriodStart, 0)
116 change.PeriodStart = &periodStart
118 if !time.Unix(subscription.PeriodEnd, 0).Equal(orig.PeriodEnd) {
119 periodEnd := time.Unix(subscription.PeriodEnd, 0)
120 change.PeriodEnd = &periodEnd
122 if !time.Unix(subscription.Canceled, 0).Equal(orig.CanceledAt) {
123 canceledAt := time.Unix(subscription.Canceled, 0)
124 change.CanceledAt = &canceledAt