Payments
How Cascade handles payments and billing.
Payment provider of choice
Cascade uses Lemon Squeezy as the merchant of record. Lemon Squeezy is a payment gateway that allows you to accept payments from your customers. It is a secure and reliable payment gateway that supports multiple payment methods. It also handles all tax related things for you.
Setup on Lemon Squeezy side
Make sure you are doing all of the below in test mode. You can switch between test and live in the Lemon Squeezy dashboard.
To start accepting even test payments, you need to set up a new store.
Next step is to create some test products. You can do that here.
Let’s start with a simple product. You can create a product by clicking on the New Product
button.
Choose Subscription and select $9.99 as the price for collection every month.
Now let’s creat two variants of this Product, we will call it Yearly
& Monthly
and set the price to 9 & 100 respectively.
Save your product and variants, you should have deftault monthly subscription and yearly subscription now!
Go to stores page and copy the store ID.
Go to api keys page and create an API key. Copy it as well.
Name your API key corresponding to your store so you could rotate it later if needed.
Setup on Cascade side
Now that you have your store ID and API key, you can set up Cascade to use Lemon Squeezy as the payment provider.
Inside of .env
file add the following:
We are using local tunnel for webhook URL, you should replace cascade with your app name and generate correct connection in the next step.
Visit the Lemon Squeezy setup page on Cascade dashboard at http://localhost:3000/ls-setup
Make sure you are signed in and your user has SUPER_ADMIN
role.
Run npx localtunnel --port 3000 --subdomain cascade
with replacing cascade with your domain. This will make sure we are able to receive webhooks from Lemon Squeezy locally.
You can now click two buttons to create a webhook and create plans in our database that correspond to Lemon Squeezy subsriptions.
Create webhook
button will create a webhook on Lemon Squeezy side, so we can receive events. This is just a nice utility to setup webhooks without going to Lemon Squeezy dashboard.Sync plans
button will create plans in our database that correspond to Lemon Squeezy subscriptions. This is needed so we can match Lemon Squeezy events to our plans.
Working with payment utilities
To make sure we incentivise our users to subscribe to paid plans we sometimes need to block them from using our app. We can do this by using payment utilities.
useGuardedSpendCredits
hook will check if the user has enough credits to spend on the given feature. You need to ensure that you Prisma schema has a field for each feature you want to block.
Guarded usage retuns an object with useful fields for working with paywalled features:
For example, if you want to block button clicks(a basic example exists here), you need to add a buttonClicks
field to your Plan
schema & reflect it in your FeatureUsage
schema.
When you create plans automatically with syncPlansFromLemonsqueezyVariants
using this guide you need to
manually input the limits for the features in your Database.
To properly track feature usage you need to use a FeatureUsage
model in database. This model will be used to track the usage of each feature for each user. Extent it when needed.
It is really easy to paywall any feature you want and show a dialog to the user to upgrade their plan.