Stripe Checkout Single Subscription Sample

repository·main·Indexed 21 days ago

https://github.com/stripe-samples/checkout-single-subscription

A sample implementation demonstrating how to use Stripe Checkout and Stripe Billing to set up a single subscription payment flow. This sample supports multiple server-side languages, including Node, Go, .NET, PHP, and Java, and includes instructions for configuring Stripe Tax and local webhooks using the Stripe CLI.

Tokens
9.8K
Snippets
36
Records
59
Agent score
74%

What's inside checkout-single-subscription

  1. Enable Stripe Tax in the Go server

    main

    To automatically calculate sales tax during checkout, you must modify the server.go file:

    1. Locate the commented-out AutomaticTax parameter in the CheckoutSession creation logic.
    2. Uncomment the following line:
    AutomaticTax: &stripe.CheckoutSessionAutomaticTaxParams{Enabled: stripe.Bool(true)},

    Prerequisites:

    • You must have already set up Stripe Tax.
    • Your products and prices must be updated with appropriate tax behavior and optionally tax codes.
    // Uncomment this line in server.go to enable automatic tax calculation
    AutomaticTax: &stripe.CheckoutSessionAutomaticTaxParams{Enabled: stripe.Bool(true)},
  2. Clone and configure the checkout-single-subscription sample

    main

    You can clone and configure this sample using either the Stripe CLI (recommended) or manually via Git.

    Using the Stripe CLI

    If you have the Stripe CLI installed, run:

    ./stripe samples create checkout-single-subscription

    The CLI will guide you through selecting your server/client languages and configuring your .env file with your Stripe API keys.

    Manual Cloning

    1. Clone the repository:
    git clone https://github.com/stripe-samples/checkout-single-subscription
    1. Copy the .env.example file to a new .env file in your chosen server directory (e.g., server/node/.env):
    cp .env.example server/node/.env
    1. Populate the .env file with your Stripe API keys:
    STRIPE_PUBLISHABLE_KEY=<replace-with-your-publishable-key>
    STRIPE_SECRET_KEY=<replace-with-your-secret-key>
    ./stripe samples create checkout-single-subscription
  3. Run the Go implementation of Checkout single subscription

    main

    To run the Go server implementation, follow these steps from the server/go directory:

    1. Configure Environment Variables: Ensure a .env file exists in the server directory. If it doesn't, copy .env.example from the project root to the server directory and populate it with your Stripe credentials and configuration.
    2. Install Dependencies: Use Go modules to fetch and vendor dependencies.
    3. Start the Server: Run the application using go run.

    The application will be available at http://localhost:4242.

    # 1. Setup .env (manual step)
    # 2. Install dependencies
    go mod tidy
    go mod vendor
    
    # 3. Run the application
    go run server.go
  4. Enable Stripe Tax in the PHP Checkout session

    main

    To automatically calculate sales tax during the checkout process, you must modify the create-checkout-session.php file.

    1. Locate the automatic_tax configuration in public/create-checkout-session.php.
    2. Uncomment the following line:
      'automatic_tax' => ['enabled' => true],

    Prerequisites for Stripe Tax:

    • You must have completed the Stripe Tax setup.
    • Your Stripe Products and Prices must be updated with the correct tax behavior and, optionally, tax codes.
    // In public/create-checkout-session.php
    'automatic_tax' => ['enabled' => true],
  5. Setup and run the Python server

    main

    To run the Python implementation of the Checkout single subscription sample, follow these steps to set up a virtual environment, install dependencies, and launch the Flask application.

    1. Create and activate a virtual environment

    MacOS / Unix:

    python3 -m venv env
    source env/bin/activate

    Windows (PowerShell):

    python3 -m venv env
    .\env\Scripts\activate.bat

    2. Install dependencies

    pip install -r requirements.txt

    3. Export environment variables and run

    MacOS / Unix:

    export FLASK_APP=server.py
    python3 -m flask run --port=4242

    Windows (PowerShell):

    $env:FLASK_APP="server.py"
    python3 -m flask run --port=4242

    4. Access the demo

    Open your browser and navigate to http://localhost:4242.

  6. Configure Stripe Tax for Products and Prices

    main

    To use Stripe Tax, ensure you have set up Stripe Tax in your Stripe Dashboard first.

    1. Create a Product with a Tax Code

    Submit a tax code (e.g., txcd_10000000 for 'General - Electronically Supplied Services') when creating the product:

    ./stripe products create \
      -d "name=Premium" \
      -d "description=Premium plan" \
      -d "tax_code=txcd_10000000"

    2. Create a Price with Tax Behavior

    When creating the price, specify the tax_behavior. Common values are inclusive or exclusive.

    ./stripe prices create \
      -d "unit_amount=1800" \
      -d "currency=usd" \
      -d "tax_behavior=exclusive" \
      -d "recurring[interval]=month" \
      -d "product=<INSERT_ID_FROM_PRODUCT_STEP>"
    ./stripe products create \
      -d "name=Premium" \
      -d "description=Premium plan" \
      -d "tax_code=txcd_10000000"
  7. Enable Stripe Tax in the Python server

    main

    To automatically calculate sales tax during checkout, you must enable Stripe Tax in the server.py file and ensure your Stripe account is configured correctly.

    1. Modify Code: In server.py, locate and uncomment the following line within the Checkout session creation logic:
      automatic_tax={'enabled': True},
    2. Stripe Configuration: Ensure you have completed the Stripe Tax setup.
    3. Product/Price Setup: Your products and prices must be updated with the appropriate tax behavior and, optionally, tax codes as described in the Stripe documentation.
    # In server.py
    automatic_tax={'enabled': True},
  8. Setup and run the PHP Checkout sample

    main

    To run this PHP implementation of a single subscription checkout locally, follow these steps:

    1. Install dependencies: Use Composer to install the required PHP packages.
    2. Configure environment variables: Copy the .env.example file from the project root to server/php/.env and populate it with your Stripe API keys and your specific Basic and Pro price IDs.
    3. Start the local server: Navigate to the public directory and start the PHP built-in web server on port 4242.
    4. Access the application: Open your browser and navigate to http://localhost:4242.
    # 1. Install dependencies
    composer install
    
    # 2. Configure environment
    cp .env.example server/php/.env
    
    # 3. Run the server
    cd public
    php -S localhost:4242