Note. This functionality is only available for the "Professional" version of the boilerplate.
Step 1. Create an account on Stripe, complete the email and account verification
Step 2. On the left panel in the dashboard switch the "Viewing test data" switcher on
Step 3. From the central part, copy publishable key and secret key and paste them into your environment variables:
Note. Don't forget to add these variables to the config.py in the root folder. For different configuration it's higly recommended to use different values. For example you can add the following values in the DevelopmentConfig class:
STRIPE_PUBLISHABLE_KEY = os.environ.get('STRIPE_TEST_PUBLISHABLE_KEY')
STRIPE_SECRET_KEY = os.environ.get('STRIPE_TEST_SECRET_KEY')
and into ProductionConfig:
STRIPE_PUBLISHABLE_KEY = os.environ.get('STRIPE_PROD_PUBLISHABLE_KEY')
STRIPE_SECRET_KEY = os.environ.get('STRIPE_PROD_SECRET_KEY')
Step 4. Create plans and add meta fields to each of them:
Step 5. Install Stripe API:
pip install stripe
pip freeze > requirements.txt
When user signs up, you can give him/her to do it in 2 ways:
If the first case the url for registration will be /auth/register. If you require selecting a plan you would pass it as a parameter /auth/register/?plan=plan_code.
You can specify almost any workflow with your environmental variables.
Default plan code: if no plan is specified (using URL) the default plan code will be assigned (if it should be assigned).
DEFAULT_PLAN_CODE = 'free' // Default plan code is 'free'
Plan should be selected by user. So, you provide it in URL (as described before). If you don't or user somehow skips it, the exception will be thrown and user will see the error message.
REQUIRE_SELECT_PLAN = False // True means you allow to not to select a plan.
Require entering payment method if user selects a free plan. So, whenever user selects it (on registration or later) he/she will be required to enter the payment informatioln.
REQUIRE_PM_FOR_FREE_PLAN = True
Require entering payment method for plans having a valid trial period. If user selects such a plan, or it's assigned on registration, he/she will be required to enter the payment information.
REQUIRE_PM_FOR_TRIAL = True // If False, the payment information can be entered later.
Require entering payment method if a default plan selected or assigned.
REQUIRE_PM_FOR_DEFAULT_PLAN = False
Require entering payment information even if no plan is selected or assigned.
REQUIRE_PM_FOR_NO_PLAN = False
For example, how can we implement the following scenario?
Use the following values for env variables for this scenario (other variables don't matter for this scenario):
REQUIRE_SELECT_PLAN = True
REQUIRE_PM_FOR_FREE_PLAN = False
REQUIRE_PM_FOR_TRIAL = False
To be able to update the payment information in the real time, webhooks should be established. On the Stripe interface, go Developers --> Webhooks --> Endpoints --> Add endpoint. Add 2 endpoints for the following events:
As for URL, it depends on the environment you currently use. If it's local, you have to install tools like Ngrok (https://ngrok.com/) and use the following formats for URLs (URL is the same for both endpoints):
where XXX is your ngrok id (you will see it in the command prompt).
If you are running your website on the Internet and it's alive, use the following format for URL: