Subscriptions in Pegasus have three components which must all be setup in order for them to work correctly.
- Stripe Billing data. This is configured in Stripe.
- Local Stripe models. These are synced automatically from Stripe to your local database, using
- Pegasus metadata. This is configured in
apps/subsriptions/metadata.pyand used to augment the data from Stripe.
The easiest way to setup all three is to follow the guide below.
Complete the following steps in order to setup your first subscription workflow.
- If you haven’t already, setup Pegasus and create an account.
- Setup your billing plans in your Stripe test account. See Stripe’s documentation for help on doing this.
- Update the
settings.pyor in your os environment variables to match the keys from Stripe. See this page to find your API keys.
./manage.py bootstrap_subscriptions. If things are setup correctly, you should see output that includes “Synchronized plan plan_[plan_id]” for each plan you created, and an output starting with
ACTIVE_PRODUCTS =containing the products you just created.
- Paste the
ACTIVE_PRODUCTSoutput from the previous step into
apps/subsriptions/metadata.pyoverriding what is there. Update any other details you want, for example, the “description” and “features” fields.
- Optionally edit the
apps/subsriptions/metadata.pyif you don’t plan to include both monthly and annual offerings.
Now login and click the “Subscription” tab in the navigation. If you’ve set things up correctly you should see a page that looks like this:
If you want to change the contents of the page, just edit the details in
metadata.py. Watch how the page
changes when you make changes.
Webhooks are used to notify your app about events that happen in Stripe, e.g. failed payments. More information can be found in Stripe’s webhook documentation.
Pegasus ships with webhook functionality ready to go, however you are strongly encouraged to test locally using Stripe’s excellent guide.
A few pieces of setup that are required:
- For the webhook URL, it should be https://yourserver.com/stripe/webhook/. The trailing slash is required.
If using the Stripe CLI you can use
stripe listen --forward-to localhost:8000/stripe/webhook/
- Make sure to set
settings.pyor environment. This value can be found when configuring your webhook endpoint in the Stripe dashboard, or read from the console output in the Stripe CLI.
Once webhooks are properly setup, all the underlying Stripe data will be automatically synced from Stripe with no additional setup on your part.
Custom Webhook Handling¶
You may want to do more than just update the underlying Stripe objects when processing webhooks, for example, notifying a customer or admin of a failed payment.
Pegasus ships with an example of executing custom logic from a webhook in
This basic example will mail your project admins when a Subscription is canceled.
More details on custom webhooks can be found in the dj-stripe documentation.
Good luck collecting and managing your first subscription payments!
Pegasus ships with a demo page with a few feature-gating examples, which are available from a new Pegasus installation under the “Subscription Demo” tab.
- Changing content on a page based on the user/team’s subscription.
- Restricting access to an entire page based on the user/team’s subscription.
- Showing subscription details like plan, payment details, and renewal date.
For additional questions on feature-gating don’t hesitate to get in touch!