Update subscription_creator to use the new strategy.
When creating subscriptions through the client, detect when the returned error
is saying the account already has a subscription, or the subscription already
exists in stripe.
Add an UpdateSubscription function that will update a subscription through the
API.
Update our subscription_creator listener to listen for profile creation and
login verification messages. This mainly involved fixing the constants (the
system, model, and topic) that the listener for profile creation was listening
for. It also meant adding a new updateMessageHandler that listens for login
verification, tries to create a subscription that has the user ID and email of
the login that was verified, and if a subscription already exists, updates it
instead to use the email address that was just verified. This will ensure that
users get their receipts automatically emailed to them by Stripe.
4 commonAPI "code.secondbit.org/api.hg"
5 "code.secondbit.org/auth.hg"
7 "code.secondbit.org/ducky/subscriptions.hg"
8 "code.secondbit.org/ducky/subscriptions.hg/api"
11 func (c *Client) CreateSubscription(change subscriptions.SubscriptionChange) (subscriptions.Subscription, error) {
12 resp, err := c.Post("/subscriptions/", change, auth.Scopes{api.ScopeSubscription, api.ScopeSubscriptionAdmin}.Strings(), change.UserID)
14 hErr, ok := err.(httpErrors)
16 for _, e := range hErr {
17 if e.Slug == commonAPI.RequestErrConflict &&
18 e.Field == "/user_id" {
19 return subscriptions.Subscription{}, subscriptions.ErrSubscriptionAlreadyExists
20 } else if e.Slug == commonAPI.RequestErrConflict &&
21 e.Field == "/stripe_token" {
22 return subscriptions.Subscription{}, subscriptions.ErrStripeSubscriptionAlreadyExists
26 return subscriptions.Subscription{}, err
28 if len(resp.Subscriptions) < 1 {
29 return subscriptions.Subscription{}, subscriptions.ErrSubscriptionNotFound
31 return resp.Subscriptions[0], nil
34 func (c *Client) UpdateSubscription(change subscriptions.SubscriptionChange) (subscriptions.Subscription, error) {
35 resp, err := c.Patch("/subscriptions/"+change.UserID.String(), change, auth.Scopes{api.ScopeSubscription, api.ScopeSubscriptionAdmin}.Strings(), change.UserID)
37 return subscriptions.Subscription{}, err
39 if len(resp.Subscriptions) < 1 {
40 return subscriptions.Subscription{}, subscriptions.ErrSubscriptionNotFound
42 return resp.Subscriptions[0], nil