npm package: @saltcorn/stripe
Description: Subscriptions with Stripe
Author: Tom Nielsen
Latest version: 0.2.2

# stripe

Billing with Stripe

This plugin allows you to use Stripe to link subscription payments to an elevated user role, such that subscribers are automatically given access to some resources that are for paying users only. Payments are handled by Stripe and you do not have to design a checkout form or workflow yourself; you only link your Stripe account and then payments will be taken on the Stripe checkout webpage.

1. Create a Stripe account and make sure you have access to the dashboard
2. You should have at least one product with one recurring price in Stripe. Products and prices are created in the Stripe dashboard
3. Also set up the customer portal in Stripe (https://dashboard.stripe.com/test/settings/billing/portal). You should fill in the "default redirect link" to the URL of a page on your website.
4. Make sure you have configured the base URL. To do this, settings-> about application-> site identity and enter your domain name or IP address starting with the http or https protocol. For instance, http://56.67.36.11 or https://example.com. Click save at the bottom of the form
5. Install the stripe plugin in Saltcorn in the plugin store (settings-> plugins)
6. In the settings for the stripe plugin, enter the publishable key and the secret key. You can leave the web hooks signing secret blank for now.
7. Create a new user role which is the role you would like paying users to have. This role should have access to some pages and views that ordinary users cannot access. The role ID should be lower than 8. 7 is a good role ID to start with.
8. Create a stripe_webhook API action in Saltcorn. To do this, go to your settings-> events-> actions menu, and create a new trigger (add trigger). The name should be a short name with no spaces (for instance, ”stripehook”, the action should be stripe_webhook, and When should be set to “API call”. Once you have saved the new trigger, in the table of triggers the API call URL should be shown. For instance, if your base URL is http://example.com and the action name is stripehook then the URL for this action will be http://example.com/api/action/stripehook
9. Go to the Stripe dashboard. Under developers-> webhooks click the “+ Add endpoint” button. Enter your action URL under endpoint URL. Select these events to send: invoice.payment_failed, checkout.session.completed, invoice.paid. Once you have saved the web hook, you will see the details page for this web hook. In the box labelled Signing secret, click the “click to reveal” button. Copy the secret that appears and go back to your stripe plugin settings in the Saltcorn plugin store and enter the webhook's signing secret.
10. You now need to create some views in Saltcorn. Start by creating a view of each of these two view templates: “Stripe success view” and ” Stripe customer portal“. If you are asked to pick a table, you can pick any table (for instance, users). The Stripe views will ignore their assigned tables (in a future version of Saltcorn, you will not be asked to pick tables). Neither of these two views have any configurations.
11. You now need to create two pages that users will be directed to if they succeed with a subscription payment, and if they do not proceed with the subscription payment, respectively. We call these the success and the cancel pages, but you can give them whichever name you like. It is best if you embed the view of the Stripe success view template on the success page. This is not necessary, but will ensure that the user’s role is elevated as soon as their subscription payment is completed
12. Now create the subscription view. This will show a button that will direct a user to payment for one of your product prices. If you have more than one product or one price that will be available to the user, you need to create a view for each of the prices. This subscription view does have a configuration page. Here you will have to select your price ID (if none are available, then something has gone wrong in the configuration, probably in the keys). You must also choose the role that the user will assume on successful subscription, and the pages they are directed to on successful and cancelled subscriptions, respectively.
13. You should create a link to the customer portal view somewhere it is accessible to the user. displaying this view will immediately redirect the user to their customer portal on the Stripe website.

To test your setup, use test keys and one of the Stripe test cards (for instance, credit card number, 5555555555554444, any future expiry date and 3 digit CVC number) to test a subscription with a new user.