Make it possible to create a user without payment details.
We want a new subscription flow, in which the system (not the user) is
responsible for creating a subscription when a new profile is created. This is
to prevent issues where a user has an account, but no subscription. This is bad
because the free trial starts ticking from the day the subscription is created,
so we should try to make the subscription and account creation get created as
close to each other as possible.
Our plan is to instead have the authd service fire off an NSQ event when an
auth.Profile is created, which a subscription listener will be listening for.
When that happens, the listener will use the subscription API to create a
subscription. Then the user will update the subscription with their payment info
and the plan they want to use.
To accomplish this, we changed the way things were handled. The
SubscriptionRequest type, along with its Validate method, were removed. Instead,
we get the SubscriptionChange type which handles both the creation of a
subscription and the updating of a subscription.
We also added an endpoint for patching subscriptions, useful for adding the
StripeSubscription or updating the plan. By default, every subscription is
created with a "Pending" plan which has a 31 day free trial. This is so we can
detect users that haven't actually set up their subscription yet, but their free
trial is still timed correctly.
We changed the way we handle scopes, creating actual auth.Scope instances
instead of just declaring an ID for them. This is useful when we have a client,
for example.
With this change, we lose all the validation we had on creating a Subscription,
and we need to rewrite that validation logic. This is because we no longer have
a specific type for "creating a subscription", so we can't just call a validate
method. We should have a helper method validateCreateRequest(change
SubscriptionChange) that will return the API errors we want, so it's easier to
unit test.
We should really be restricting the CreateSubscriptionHandler to
ScopeSubscriptionAdmin, anyways, since Subscriptions should only ever be created
by the system tools or administrators.
We created a PatchSubscriptionHandler that exposes an interface to updating
properties of a Subscription. It allows users to update their own Subscriptions
or requires the ScopeSubscriptionAdmin scope before allowing you to update
another user's Subscription. It, likewise, needs validation still. We also added
the concept of "system-controlled properties" of the SubscriptionChange type,
which only admins or the system tools can update.
We updated our planOptions to distinguish between plans that do and do not need
administrative credentials to be chosen. Our free and pending plans are
available to administrators only.
We updated our StripeChange object to be better organised (separating out the
system and user-controlled properties), and we added a StripeSource and Email
property, so the Stripe part can be better managed, and all our requests can be
made using just this type. This required updating our SubscriptionChange.IsEmpty
helper, which has been updated (along with its tests) and it passes all tests.
To replace our SubscriptionRequest.Validate helper, we created a
ChangingSystemProperties helper (which returns the system-controlled properties
being changed as a slice of JSON pointers, fit for use in error messages) and an
IsAcceptablePlan helper, which returns true if the plan exists and the user has
the authority to select it.
We also updated our stripe helpers to remove the CreateStripeSubscription (we
create one when we create the customer) and create an UpdateStripeSubscription
instead. It does what you'd think it does. We also added some comments to New,
so it at least has some notes about how it's meant to be used and why. Now it
just creates the customer in stripe, then creates a Subscription based on that
customer. We also updated our StripeSubscriptionChange helper to detect when the
StripeSubscription property changed.
6 "code.secondbit.org/uuid.hg"
9 "github.com/secondbit/pan"
12 // GetSQLTableName fulfills the pan.SQLTableNamer interface, allowing
13 // us to manipulate Subscriptions with pan.
14 func (s Subscription) GetSQLTableName() string {
15 return "subscriptions"
18 func (p Postgres) resetSQL() *pan.Query {
19 var subscription Subscription
20 query := pan.New(pan.POSTGRES, "TRUNCATE "+pan.GetTableName(subscription))
21 return query.FlushExpressions(" ")
24 func (p Postgres) Reset() error {
26 _, err := p.Exec(query.String(), query.Args...)
33 func (p Postgres) createSubscriptionSQL(subscription Subscription) *pan.Query {
34 fields, values := pan.GetFields(subscription)
35 query := pan.New(pan.POSTGRES, "INSERT INTO "+pan.GetTableName(subscription))
36 query.Include("(" + pan.QueryList(fields) + ")")
37 query.Include("VALUES")
38 query.Include("("+pan.VariableList(len(values))+")", values...)
39 return query.FlushExpressions(" ")
42 func (p Postgres) CreateSubscription(sub Subscription) error {
43 query := p.createSubscriptionSQL(sub)
44 _, err := p.Exec(query.String(), query.Args...)
45 if e, ok := err.(*pq.Error); ok && e.Constraint == "subscriptions_pkey" {
46 err = ErrSubscriptionAlreadyExists
47 } else if e, ok := err.(*pq.Error); ok && e.Constraint == "subscriptions_stripe_subscription_key" {
48 err = ErrStripeSubscriptionAlreadyExists
53 func (p Postgres) updateSubscriptionSQL(id uuid.ID, change SubscriptionChange) *pan.Query {
54 var subscription Subscription
55 query := pan.New(pan.POSTGRES, "UPDATE "+pan.GetTableName(subscription)+" SET")
56 query.IncludeIfNotNil(pan.GetUnquotedColumn(subscription, "StripeSubscription")+" = ?", change.StripeSubscription)
57 query.IncludeIfNotNil(pan.GetUnquotedColumn(subscription, "Plan")+" = ?", change.Plan)
58 query.IncludeIfNotNil(pan.GetUnquotedColumn(subscription, "Status")+" = ?", change.Status)
59 query.IncludeIfNotNil(pan.GetUnquotedColumn(subscription, "Canceling")+" = ?", change.Canceling)
60 query.IncludeIfNotNil(pan.GetUnquotedColumn(subscription, "TrialStart")+" = ?", change.TrialStart)
61 query.IncludeIfNotNil(pan.GetUnquotedColumn(subscription, "TrialEnd")+" = ?", change.TrialEnd)
62 query.IncludeIfNotNil(pan.GetUnquotedColumn(subscription, "PeriodStart")+" = ?", change.PeriodStart)
63 query.IncludeIfNotNil(pan.GetUnquotedColumn(subscription, "PeriodEnd")+" = ?", change.PeriodEnd)
64 query.IncludeIfNotNil(pan.GetUnquotedColumn(subscription, "CanceledAt")+" = ?", change.CanceledAt)
65 query.IncludeIfNotNil(pan.GetUnquotedColumn(subscription, "FailedChargeAttempts")+" = ?", change.FailedChargeAttempts)
66 query.IncludeIfNotNil(pan.GetUnquotedColumn(subscription, "LastFailedCharge")+" = ?", change.LastFailedCharge)
67 query.IncludeIfNotNil(pan.GetUnquotedColumn(subscription, "LastNotified")+" = ?", change.LastNotified)
68 query.FlushExpressions(", ")
70 query.Include(pan.GetUnquotedColumn(subscription, "UserID")+" = ?", id)
71 return query.FlushExpressions(" ")
74 func (p Postgres) UpdateSubscription(id uuid.ID, change SubscriptionChange) error {
76 return ErrSubscriptionChangeEmpty
79 query := p.updateSubscriptionSQL(id, change)
80 res, err := p.Exec(query.String(), query.Args...)
81 if e, ok := err.(*pq.Error); ok && e.Constraint == "subscriptions_stripe_subscription_key" {
82 return ErrStripeSubscriptionAlreadyExists
83 } else if err != nil {
86 rows, err := res.RowsAffected()
91 return ErrSubscriptionNotFound
96 func (p Postgres) deleteSubscriptionSQL(id uuid.ID) *pan.Query {
97 var subscription Subscription
98 query := pan.New(pan.POSTGRES, "DELETE FROM "+pan.GetTableName(subscription))
100 query.Include(pan.GetUnquotedColumn(subscription, "UserID")+" = ?", id)
101 return query.FlushExpressions(" ")
104 func (p Postgres) DeleteSubscription(id uuid.ID) error {
105 query := p.deleteSubscriptionSQL(id)
106 res, err := p.Exec(query.String(), query.Args...)
110 rows, err := res.RowsAffected()
115 return ErrSubscriptionNotFound
120 func (p Postgres) getSubscriptionsSQL(ids []uuid.ID) *pan.Query {
121 var subscription Subscription
122 fields, _ := pan.GetFields(subscription)
123 intIDs := make([]interface{}, len(ids))
124 for pos, id := range ids {
127 query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(subscription))
129 query.Include(pan.GetUnquotedColumn(subscription, "UserID") + " IN")
130 query.Include("("+pan.VariableList(len(intIDs))+")", intIDs...)
131 return query.FlushExpressions(" ")
134 func (p Postgres) GetSubscriptions(ids []uuid.ID) (map[string]Subscription, error) {
135 results := map[string]Subscription{}
137 return results, ErrNoSubscriptionID
139 query := p.getSubscriptionsSQL(ids)
140 rows, err := p.Query(query.String(), query.Args...)
145 var subscription Subscription
146 err := pan.Unmarshal(rows, &subscription)
150 results[subscription.UserID.String()] = subscription
152 if err := rows.Err(); err != nil {
158 func (p Postgres) getSubscriptionStatsCountSQL() *pan.Query {
159 var subscription Subscription
160 query := pan.New(pan.POSTGRES, "SELECT COUNT(*) FROM")
161 query.Include(pan.GetTableName(subscription))
162 return query.FlushExpressions(" ")
165 func (p Postgres) getSubscriptionStatsCancelingSQL() *pan.Query {
166 var subscription Subscription
167 query := pan.New(pan.POSTGRES, "SELECT COUNT(*) FROM")
168 query.Include(pan.GetTableName(subscription))
170 query.Include(pan.GetUnquotedColumn(subscription, "Canceling")+" = ?", true)
171 return query.FlushExpressions(" ")
174 func (p Postgres) getSubscriptionStatsFailingSQL() *pan.Query {
175 var subscription Subscription
176 query := pan.New(pan.POSTGRES, "SELECT COUNT(*) FROM")
177 query.Include(pan.GetTableName(subscription))
179 statuses := []interface{}{"past_due", "unpaid"}
180 query.Include(pan.GetUnquotedColumn(subscription, "Status")+" IN ("+pan.VariableList(len(statuses))+")", statuses...)
181 return query.FlushExpressions(" ")
184 func (p Postgres) getSubscriptionStatsPlansSQL() *pan.Query {
185 var subscription Subscription
186 fields := []interface{}{pan.GetUnquotedColumn(subscription, "Plan"), "COUNT(*)"}
187 query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM")
188 query.Include(pan.GetTableName(subscription))
189 query.Include("GROUP BY " + pan.GetUnquotedColumn(subscription, "Plan"))
190 return query.FlushExpressions(" ")
193 func (p Postgres) GetSubscriptionStats() (SubscriptionStats, error) {
194 stats := SubscriptionStats{
195 Plans: map[string]int64{},
197 query := p.getSubscriptionStatsCountSQL()
198 err := p.QueryRow(query.String(), query.Args...).Scan(&stats.Number)
200 log.Printf("Error querying for total subscriptions: %+v\n", err)
203 query = p.getSubscriptionStatsCancelingSQL()
204 err = p.QueryRow(query.String(), query.Args...).Scan(&stats.Canceling)
206 log.Printf("Error querying for canceling subscriptions: %+v\n", err)
209 query = p.getSubscriptionStatsFailingSQL()
210 err = p.QueryRow(query.String(), query.Args...).Scan(&stats.Failing)
212 log.Printf("Error querying for failing subscriptions: %+v\n", err)
215 query = p.getSubscriptionStatsPlansSQL()
216 rows, err := p.Query(query.String(), query.Args...)
218 log.Printf("Error querying for plans: %+v\n", err)
224 err := rows.Scan(&plan, &count)
226 log.Printf("Error scanning database row for plans: %+v\n", err)
229 stats.Plans[plan] = count
231 if err := rows.Err(); err != nil {
232 log.Printf("Error querying for plans: %+v\n", err)