# Stripe

Offering subscription plans with Stripe is very easy with Ready SaaS because all the hardest parts have been already implemented for you.

You just need to follow a few steps to generate a Stripe API key and send Stripe webhooks to your Django app.

## Stripe Setup

Follow these steps to setup Stripe subscription plans within your Ready SaaS project.

### 1. Create a Stripe account

Go to <https://stripe.com/> and create an account

### 2. Create a Stripe Product Object

Navigate over to the Product Catalog section and create a new product.

<figure><img src="https://1908511726-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FT1c9BPzeUyd2bkOioywo%2Fuploads%2FxEOmftyJgDyEEmeK4GIG%2Fimage.png?alt=media&#x26;token=b8f3010c-3fc6-4666-8b67-f38bd0a7cbff" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1908511726-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FT1c9BPzeUyd2bkOioywo%2Fuploads%2FzGoLAKW63p5eYJFRh9mM%2Fimage.png?alt=media&#x26;token=aa3ac40d-7c24-459e-9459-5967d29db19b" alt=""><figcaption></figcaption></figure>

#### Complete all the required fields to create your subscription product:

* name
* description
* price
* type: for subscriptions use the **recurring** type

<figure><img src="https://1908511726-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FT1c9BPzeUyd2bkOioywo%2Fuploads%2Fe5PRA9X4fNMXbhgEikwa%2Fimage.png?alt=media&#x26;token=d05c6ee4-8051-424b-921a-9487adce2a61" alt=""><figcaption></figcaption></figure>

#### Save the price\_id for later:

<figure><img src="https://1908511726-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FT1c9BPzeUyd2bkOioywo%2Fuploads%2FMH7ITCppM2D6LjoweOeU%2Fimage.png?alt=media&#x26;token=2c741209-0d84-47f2-8159-1036f30a8a77" alt=""><figcaption></figcaption></figure>

### 3. Setup API Keys

To connect Stripe to the Ready SaaS project you need set your Stripe API key

Type "API keys" in the Stripe Search bar and select **Developers > API Keys**

<figure><img src="https://1908511726-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FT1c9BPzeUyd2bkOioywo%2Fuploads%2FKvddTwnoUUS1RbxYWnU7%2Fimage.png?alt=media&#x26;token=f8347a1f-0401-446f-bae1-3cb880094ba4" alt=""><figcaption></figcaption></figure>

In that screen you will be able to extract the Secret Key

<figure><img src="https://1908511726-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FT1c9BPzeUyd2bkOioywo%2Fuploads%2F2DR0gFFP2s1hE7lfRk5K%2Fimage.png?alt=media&#x26;token=8e9bccae-e278-4361-b200-ea8858e7ed59" alt=""><figcaption></figcaption></figure>

Now you can add that key as an environment variable in your Ready SaaS project:

* For your local instance of Ready SaaS, add it as `STRIPE_SECRET_KEY` within your `.env` file
* For your production instance, add it as an environment variable with the name `STRIPE_SECRET_KEY`

### 4. Create a Subscription Plan in Ready SaaS

Navigate to the admin dashboard and create a Plan object

<figure><img src="https://1908511726-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FT1c9BPzeUyd2bkOioywo%2Fuploads%2FFym8XcUuPtMElsn0Wtuv%2Fimage.png?alt=media&#x26;token=e1f7cf6b-f5c8-43ca-84d8-ea48cb00174b" alt=""><figcaption></figcaption></figure>

Define all the attributes of your plan and make sure to paste the `price_id` from [step 2](#id-2.-create-a-stripe-product-object) as the `external plan id` so that the plan is linked to the Stripe product you created.

<figure><img src="https://1908511726-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FT1c9BPzeUyd2bkOioywo%2Fuploads%2FXoP6rVGsdl1WN3hA4b3O%2Fimage.png?alt=media&#x26;token=4c6a9778-a9fb-438a-a81b-1dc0b1b9983f" alt=""><figcaption></figcaption></figure>

### 5. Webhooks

Ready SaaS uses Stripe webhooks In order for your app to know when a payment has been made. As mentioned in the [Payments & Subscriptions section](https://readysaas.gitbook.io/docs/guides/payments-and-subscriptions), the subscription management system follows the following lifecycle by reacting to Stripe's webhooks:&#x20;

* On Subscription Activated:

  Automatically provisions the subscription for the user, granting them access to the subscribed content or features.
* On Payment Received:

  The system continues to provision the subscription and updates the subscription's end date to reflect the new billing cycle.
* On Payment Failed:

  Marks the subscription as past due and triggers a notification to the customer to update their payment information.

To enable this functionality, you need to specify the webhook url within your Stripe account.

#### Webhooks Setup

Search for webhooks in your Stripe dashboard and click on **Developers > Webhooks**

<figure><img src="https://1908511726-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FT1c9BPzeUyd2bkOioywo%2Fuploads%2FSpF7NfZxYaNX4Y7psK8U%2Fimage.png?alt=media&#x26;token=fc137308-d43e-4e99-9f61-2942101d6267" alt=""><figcaption></figcaption></figure>

Click on **Add an endpoint.** (if you're testing locally, feel free to use their local testing option)

<figure><img src="https://1908511726-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FT1c9BPzeUyd2bkOioywo%2Fuploads%2FX4eWqwDep2Fn3f1TI3SM%2Fimage.png?alt=media&#x26;token=48356a70-89b5-4e0a-920e-b6023dbd4220" alt=""><figcaption></figcaption></figure>

Make sure you point the webhook to your Ready SaaS endpoint. By default the endpoint lives in `/stripe/webhook` but you can change it to any other path.

<figure><img src="https://1908511726-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FT1c9BPzeUyd2bkOioywo%2Fuploads%2FYk14CdjBRrxD2tCKmQzu%2Fimage.png?alt=media&#x26;token=1f64f582-3c5d-4e56-a94d-fa0a1557ce6a" alt=""><figcaption></figcaption></figure>

Next, make sure you include the required events:

<figure><img src="https://1908511726-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FT1c9BPzeUyd2bkOioywo%2Fuploads%2FukGvXQiug6ZRiVz648HD%2Fimage.png?alt=media&#x26;token=b2cf9531-f1f2-4761-adca-895340465f4a" alt=""><figcaption></figcaption></figure>

The following are the required events:

<figure><img src="https://1908511726-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FT1c9BPzeUyd2bkOioywo%2Fuploads%2F0EYTZrfKhXICNa3b52Q3%2Fimage.png?alt=media&#x26;token=fdd3a6c8-c8a3-48c2-af0d-d41eb272b393" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1908511726-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FT1c9BPzeUyd2bkOioywo%2Fuploads%2FwUQB2egHhwEFdnCI7Msu%2Fimage.png?alt=media&#x26;token=6fe034b0-1f42-4fa1-a136-1d005bb4ff0f" alt=""><figcaption></figcaption></figure>

***Yay! You're ready to accept subscription payments with Stripe*** :tada:
