ducky/subscriptions

Paddy 2015-06-16 Parent:56a2bef197cd Child:b240b6123548

2:61c4ce5850da Go to Latest

ducky/subscriptions/memstore.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
paddy@0 1 package subscriptions
paddy@0 2
paddy@0 3 import (
paddy@0 4 "sync"
paddy@0 5 )
paddy@0 6
paddy@0 7 // Memstore is an in-memory version of our datastores, useful
paddy@0 8 // for testing. It should not be used in production.
paddy@0 9 type Memstore struct {
paddy@0 10 subscriptions map[string]Subscription
paddy@0 11 subscriptionLock sync.RWMutex
paddy@0 12 }
paddy@0 13
paddy@0 14 // NewMemstore returns a pointer to a Memstore object, ready
paddy@0 15 // to be used as a datastore.
paddy@0 16 func NewMemstore() *Memstore {
paddy@0 17 return &Memstore{
paddy@0 18 subscriptions: map[string]Subscription{},
paddy@0 19 }
paddy@0 20 }
paddy@0 21
paddy@0 22 func (m *Memstore) reset() error {
paddy@0 23 m.subscriptionLock.Lock()
paddy@0 24 defer m.subscriptionLock.Unlock()
paddy@0 25
paddy@0 26 m.subscriptions = map[string]Subscription{}
paddy@0 27 return nil
paddy@0 28 }