Subscription

To use Stripe for subscription payments, you need to create a subscription product in Stripe and then create a subscription plan for that product. You can then use the Stripe API to create a subscription for a customer.

Subscription Plan and User Group Management

In our Django subscription app, we will create a subscription plan model and a user group model. The subscription plan model will store the subscription plan details, such as the plan name, price, and duration. The user group model will store the user group details, such as the group name and permissions.

  • Subscription Activation: Upon subscribing to a plan, a user's group will be updated to reflect the associated plan.

  • Subscription Cancellation: If a user cancels their subscription, their group status will be updated to NULL.

  • Plan Modification: When a user changes their subscription plan, their user group will be adjusted to match the user group of the new plan.

  • Permissions Enforcement: User permissions within your app can be managed based on the user's group.

Stripe product and plan creation

  • Go to the Stripe dashboard and click on the 'Products' tab. (https://dashboard.stripe.com/products)
  • Click on the 'Add product' button and fill in the product details.
  • After creating the product, click on the product and then click on the 'Add pricing plan' button.
  • Fill in the plan details, such as the plan name, price, and duration.
  • After creating the plan, you will see the plan ID in the plan details. You will need this plan ID to create a subscription for a customer.

Django Subscription Plan Model

  • Our Plan model has already been created, comes with this SaaS template.
  • Go to the Django admin and copy and paste the price ID into the Plan model.

Pricing page

  • The pricing page is where users can view the available subscription plans and select a plan to subscribe to.
  • The pricing page will display the available subscription plans and their details, such as the plan name, price, and duration.
  • Users can select a plan and subscribe to it by entering their payment details.

Set up UserGroups

  • Go to the Django admin and create user groups for each subscription plan.
  • For example, if you have a 'Basic' plan, create a user group called 'Basic'.
  • Assign the appropriate permissions to each user group.

Subscription Management

  • The subscription management page is where users can view and manage their subscription details.
  • Users can view their current subscription plan, the next billing date, and the option to cancel or change their subscription plan.
  • Users can also update their payment details on this page.
  • To enable this, you must enable it in your Stripe dashboard: https://dashboard.stripe.com/settings/billing/portal

Stripe environement varaiables

For the subscription app to work, you need to set the following environment variables in your Django app:

  • Go to config/settings/base.py and uncomment the STRIPE_API_KEY, STRIPE_PUBLIC_KEY and STRIPE_ENDPOINT_SECRET environment variables.
  • Set them up in your .env file for local testing.
  • For production, use command
heroku config:set STRIPE_API_KEY=your_secret_key
heroku config:set STRIPE_PUBLIC_KEY=your_public_key
heroku config:set STRIPE_ENDPOINT_SECRET=your_endpoint_secret

Stripe Webhooks

To handle subscription events, such as subscription creation, cancellation, and modification, you need to set up a webhook in your Stripe dashboard.

  • Go to the Stripe dashboard and click on the 'Developers' tab.
  • Click on the 'Webhooks' tab and then click on the 'Add endpoint' button.
  • Fill in the endpoint URL, such as https://interviewdb.com/subscription/webhooks/.
  • For our template, we use those events: checkout.session.completed, customer.subscription.updated, customer.subscription.deleted

Was this page helpful?