stripe-single-subscription-ec2

A Python Flask backend and vanilla JS frontend implementing Stripe Checkout for single subscriptions, configured for EC2 deployment.

https://github.com/davidbmar/stripe-single-subscription-ec2  ·  public  ·  shipped

What it is

This project is a reference implementation of a subscription payment flow using Stripe Checkout. It consists of a lightweight Flask server that manages API keys and retrieves checkout session details, paired with a static client-side interface that dynamically loads price IDs and handles post-payment success display. It includes build scripts to validate environment configuration before deployment.

Features

Quickstart

python3 -m venv myenv
source myenv/bin/activate
cp env.example .env
# Edit .env to set STRIPE_SECRET_KEY, STRIPE_PUBLISHABLE_KEY, BASIC_PRICE_ID, PRO_PRICE_ID
python3 server/build.py
pip install flask stripe python-dotenv
python3 server/server.py

Architecture

flowchart TD
    Client[Browser Client] -->|GET /| Server[Flask Server]
    Client -->|GET /config| Server
    Client -->|GET /checkout-session| Server
    Server -->|Retrieve Session| StripeAPI[Stripe API]
    Server -->|Read Env| DotEnv[(.env File)]
    BuildScript[build.py] -->|Validate| DotEnv

How it's built

The backend is built with Python 3.8+ using Flask for HTTP routing and the `stripe` Python library for API interactions. Environment variables are managed via `python-dotenv`. The frontend uses vanilla JavaScript (ES6+) with `fetch` API calls to communicate with the backend. Configuration validation is handled by a custom Python script (`build.py`).

How it runs

sequenceDiagram
    participant Browser
    participant FlaskServer
    participant StripeAPI
    
    Browser->>FlaskServer: GET /
    FlaskServer->>Browser: Render index.html
    
    Browser->>FlaskServer: GET /config
    FlaskServer->>Browser: Return {publishableKey, basicPrice, proPrice}
    
    Note over Browser: User initiates checkout (client-side Stripe.js)
    Browser->>StripeAPI: Create Checkout Session
    StripeAPI->>Browser: Return Session ID & Redirect URL
    
    Browser->>Browser: Redirect to Success Page
    Browser->>FlaskServer: GET /checkout-session?sessionId=...
    FlaskServer->>StripeAPI: Retrieve Session Details
    StripeAPI->>FlaskServer: Return Session Object
    FlaskServer->>Browser: Return Session JSON

How to apply & reuse

Use this as a starting point for integrating Stripe Subscriptions into a Python web application. It demonstrates how to securely expose publishable keys and price IDs to the client while keeping secret keys on the server. It is specifically structured for deployment on AWS EC2, including instructions for installing the Stripe CLI on Linux (ARM/x86).

At a glance

CapabilitiesSubscription managementPayment processingEnvironment validationStatic file serving
ComponentsFlask Web ServerVanilla JS FrontendStripe CLI IntegrationBuild Validation Script
TechPython 3.8+FlaskStripe APIJavaScriptHTML/CSS
Depends onflaskstripepython-dotenvstripe-cli
Integrates withStripe CheckoutAWS EC2
PatternsServer-Client ConfigurationEnvironment Variable InjectionAPI Proxy
Reuse tagsstripe-integrationpython-flasksubscription-boilerplateec2-deployment

⚠ Needs attention