Skip to content

Commit

Permalink
Added docs on how to deal with cancellations in the stripe api
Browse files Browse the repository at this point in the history
  • Loading branch information
mpscholten committed Jan 13, 2022
1 parent f57f892 commit 663324a
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions Guide/stripe.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,47 @@ To test it out [install the Stripe CLI](https://stripe.com/docs/stripe-cli). Run

On the first start this command will print out a webhook secret key (starting with `whsec_`). Copy the key and open `start`. Inside the `start` script set the `export STRIPE_WEBHOOK_SECRET_KEY=""` line to this key. After this restart your app.

### Handling Unsubscribes

To automatically deal with customers that unsubscribe, add the following handler to `Web/Controller/StripeWebhook.hs`:

```haskell
on CustomerSubscriptionUpdated { subscriptionId, cancelAtPeriodEnd, currentPeriodEnd } = do
maybeSubscription <- query @Subscription
|> filterWhere (#stripeSubscriptionId, subscriptionId)
|> fetchOneOrNothing
case maybeSubscription of
Just subscription -> do
subscription
|> set #endsAt (if cancelAtPeriodEnd
then currentPeriodEnd
else Nothing)
|> updateRecord
pure ()
Nothing -> pure ()

on CustomerSubscriptionDeleted { subscriptionId } = do
maybeSubscription <- query @Subscription
|> filterWhere (#stripeSubscriptionId, subscriptionId)
|> fetchOneOrNothing
case maybeSubscription of
Just subscription -> do
now <- getCurrentTime
subscription
|> set #endsAt now
|> set #isActive False
|> updateRecord

user <- fetch (get #userId subscription)
user
|> set #planId Nothing
|> set #subscriptionId Nothing
|> updateRecord
Nothing -> pure ()
````

This will set the `endsAt` date on the subscription, will mark the subscription as not active anymore and then sets the `planId` of the user to `null`.

## Billing Portal

To integrate the Stripe Billing Portal, add an action like this to your app:
Expand Down

2 comments on commit 663324a

@stephenbenedict
Copy link

@stephenbenedict stephenbenedict commented on 663324a Jan 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This worked great except for 3 changes I had to make:

  1. Change the ends_at field in subscriptions table to type TIMESTAMP WITH TIME ZONE. In the IHP Stripe guide for creating the subscriptions table[1], it instructs to make the ends_at field of type date. However, getCurrentTime is of time UTCTime so |> set #endsAt now does not work out of the box.

  2. @Subscription had to be changed to @Web.Controller.Prelude.Subscription to avoid ambiguity.

  3. Add pure () after

user
    |> set #planId Nothing
    |> set #subscriptionId Nothing
    |> updateRecord

[1] https://ihp.digitallyinduced.com/Guide/stripe.html#subscriptions-table

@mpscholten
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Fixed this via 5f07ff0

Please sign in to comment.