Cancel Stripe Subscription using API

Test this app for free
95
import os
import uvicorn
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Item(BaseModel):
    type: str

@app.post("/webhook")
async def read_item(item: Item):
    #pulls Stripe event data
    if item.type == 'customer.subscription.deleted':
        print(item.json())
    return item

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8080)
Get full code

Created: | Last Updated:

This app will react to a Stripe API for subscription cancellation and immediately print the data received from the webhook. It's a good starting point to hook up additional functionality to then create some event in a database or create a notification or for example disable access for a customer.

Introduction to the Cancel Stripe Subscription using API Template

Welcome to the step-by-step guide on how to use the "Cancel Stripe Subscription using API" template on Lazy. This template is designed to help you set up a webhook listener that reacts to Stripe subscription cancellation events. When a subscription is canceled in Stripe, this app will capture the event data and print it out, which can be a starting point for further actions such as updating a database, sending notifications, or revoking customer access.

Clicking Start with this Template

To begin using this template, simply click on the "Start with this Template" button on the Lazy platform. This will pre-populate the code in the Lazy Builder interface, so you won't need to copy, paste, or delete any code.

Test: Pressing the Test Button

Once you have the template loaded, press the "Test" button to start the deployment of the app. The Lazy CLI will launch, and the app will be deployed without you having to worry about installing libraries or setting up the environment.

Using the App

After pressing the "Test" button and deploying the app, Lazy will provide you with a dedicated server link. This link is where the Stripe webhook should send events to. Additionally, since the app uses FastAPI, you will also receive a link to the FastAPI documentation, which includes interactive API documentation.

To use the app, you'll need to set up a webhook in Stripe to point to the server link provided by Lazy. Here's how to do that:

1. Log in to your Stripe dashboard. 2. Navigate to the "Developers" section and click on "Webhooks". 3. Click on "Add endpoint" and enter the server link provided by Lazy. 4. Select the "customer.subscription.deleted" event. 5. Click "Add endpoint" to save the webhook.

Now, whenever a customer subscription is deleted in Stripe, the event data will be sent to your app, and the app will print the data.

Integrating the App

After setting up the webhook in Stripe, you may want to integrate the app further into your service or frontend. Depending on your needs, you might:

- Add functionality to update a database with the cancellation event. - Send a notification to an administrator or the customer. - Disable access for the customer in your system.

For these integrations, you may need to modify the code to perform additional actions based on the event data received. If you need to add this functionality, consider the following sample code as a starting point:

@app.post("/webhook") async def read_item(item: Item):     # Pulls Stripe event data     if item.type == 'customer.subscription.deleted':         # Here you can add your custom integration logic         # For example, update a database, send a notification, etc.         print(item.json())     return item

Remember, any modifications to the code can be done directly within the Lazy Builder interface.

By following these steps, you should now have a functional webhook listener for Stripe subscription cancellations, ready to be integrated with your additional business logic.

Technologies

Stripe Stripe
Python App Templates for Scraping, Machine Learning, Data Science and More Python App Templates for Scraping, Machine Learning, Data Science and More