Stripe Checkout One-Time Payments Samples

repository·main·Indexed 21 days ago

https://github.com/stripe-samples/checkout-one-time-payments

Implementation samples for integrating Stripe Checkout for one-time payments. Includes backend implementations in Node, Python, Go, .NET Core, Java, and PHP, with client options for Vanilla JavaScript and React. The samples demonstrate how to configure Price IDs, handle webhooks via the Stripe CLI, and enable Stripe Tax for automatic sales tax calculation.

Tokens
9.7K
Snippets
38
Records
50
Agent score
76%

What's inside checkout-one-time-payments

  1. Configure the Python server environment

    main

    To run the Python implementation, you must set several environment variables in a .env file. The server uses python-dotenv to load these values.

    Required environment variables:

    • STRIPE_SECRET_KEY: Your Stripe secret API key.
    • STRIPE_PUBLISHABLE_KEY: Your Stripe publishable API key.
    • PRICE: A valid Stripe Price ID (e.g., price_123...).
    • DOMAIN: The base URL of your application (e.g., http://localhost:4242).
    • STATIC_DIR: The directory containing your static assets and templates.

    Optional environment variables:

    • STRIPE_WEBHOOK_SECRET: The signing secret used to verify webhook signatures.
    STRIPE_SECRET_KEY=sk_test_...
    STRIPE_PUBLISHABLE_KEY=pk_test_...
    PRICE=price_...
    DOMAIN=http://localhost:4242
    STATIC_DIR=public
    STRIPE_WEBHOOK_SECRET=whsec_...
  2. Run the Checkout single product Express server

    main

    This sample is an Express server implementation for checking out a single product.

    Requirements

    • Node.js v10 or higher
    • A configured .env file in the root directory.

    Setup and Execution

    1. Configure Environment Variables: Ensure your .env file contains a valid PRICE variable set to a Price ID from your Stripe account.
    2. Install Dependencies: Run npm install to install the necessary Node modules.
    3. Start the Server: Run npm start to launch the application.

    Accessing the Demo

    • HTML Client: Navigate to http://localhost:4242.
    • React Client: Navigate to http://localhost:3000.
    npm install
    npm start
  3. Set up the Python server for Checkout single product

    main

    To run this sample, ensure you have Python 3 installed and a configured .env file in the root directory. You must set the PRICE environment variable to a valid Price ID from your Stripe account.

    1. Configure Environment Variables

    Open your .env file and set the PRICE variable:

    PRICE=price_1Hh1ZeCZ6qsJgndJaX9fauRl

    Note: Do not use placeholder IDs like price_12345; use a real ID from your Stripe Dashboard or via the Stripe CLI.

    2. Create a Virtual Environment

    MacOS / Unix:

    python3 -m venv env
    source env/bin/activate

    Windows (PowerShell):

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

    3. Install Dependencies

    pip install -r requirements.txt

    4. Run the Flask Application

    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

    5. Access the Demo

    • For the HTML client: http://localhost:4242
    • For the React client: http://localhost:3000
    # MacOS / Unix
    export FLASK_APP=server.py
    python3 -m flask run --port=4242
  4. Enable Stripe Tax in the PHP sample

    main

    To automatically calculate sales tax during checkout, you must enable the automatic_tax option in the create-checkout-session.php file.

    1. Open public/create-checkout-session.php.
    2. Locate and uncomment the following line:
      'automatic_tax' => ['enabled' => true],
    3. Prerequisites: Ensure you have already set up Stripe Tax and that your products and prices are configured with the appropriate tax behavior and tax codes.
    // In public/create-checkout-session.php
    'automatic_tax' => ['enabled' => true],
  5. Requirements for the Java Checkout sample

    main

    To run the Java implementation of the single product checkout sample, you need the following installed and configured:

    • Maven: For building the project.
    • Java: The runtime environment.
    • Configured .env file: A .env file located in the root directory (refer to the main repository README for setup) containing necessary Stripe credentials.
  6. Set up the Ruby Sinatra implementation

    main

    This sample is a Sinatra implementation for checking out a single product.

    Requirements

    • Ruby v2.4.5+
    • A configured .env file containing a valid Stripe PRICE ID.

    Installation and Execution

    1. Configure Environment Variables: Ensure your .env file has a PRICE variable set to a real Price ID from your Stripe account:

      PRICE=price_1Hh1ZeCZ6qsJgndJaX9fauRl

      Note: Using a placeholder like price_12345 will cause the sample to fail.

    2. Install Dependencies:

      bundle install
    3. Run the Server:

      ruby server.rb
    4. Access the Demo:

      • For the HTML client: http://localhost:4242
      • For the React client: http://localhost:3000
    bundle install
    ruby server.rb
  7. Enable Stripe Tax in the Go server

    main

    To automatically calculate sales tax during checkout, you must manually enable it in the source code:

    1. Open server/go/server.go.
    2. Locate the commented-out AutomaticTax parameter.
    3. Uncomment the following line:
    AutomaticTax: &stripe.CheckoutSessionAutomaticTaxParams{Enabled: stripe.Bool(true)},

    Prerequisites for Stripe Tax:

    • You must have completed the Stripe Tax setup.
    • Your products and prices must be updated with the correct tax behavior and, optionally, tax codes.
    // AutomaticTax: &stripe.CheckoutSessionAutomaticTaxParams{Enabled: stripe.Bool(true)},
  8. Enable Stripe Tax in the Java sample

    main

    To automatically calculate sales tax during checkout, you must modify the source code and ensure your Stripe account is configured for Tax.

    1. Modify Server.java: Locate and uncomment the following line in src/main/java/com/stripe/sample/Server.java:

      // .setAutomaticTax(SessionCreateParams.AutomaticTax.builder().setEnabled(true).build())
    2. Stripe Configuration: Ensure you have completed the Stripe Tax setup and that your products and prices are updated with the appropriate tax behavior and tax codes.

    .setAutomaticTax(SessionCreateParams.AutomaticTax.builder().setEnabled(true).build())
  9. Clone and configure the checkout-one-time-payments sample

    main

    You can set up this sample using either the Stripe CLI (recommended) or by manually cloning the repository.

    Using the Stripe CLI

    This is the fastest method. The CLI will guide you through selecting your integration type, server/client languages, and configuring your .env file with your Stripe API keys.

    stripe samples create checkout-one-time-payments

    Manual Installation

    1. Clone the repository:
    git clone https://github.com/stripe-samples/checkout-one-time-payments
    1. Create a .env file by copying the example from your chosen server directory:
    cp .env.example server/node/.env
    1. Populate the .env file with your Stripe API keys from the Stripe developer dashboard:
    STRIPE_PUBLISHABLE_KEY=<replace-with-your-publishable-key>
    STRIPE_SECRET_KEY=<replace-with-your-secret-key>
  10. Enable Stripe Tax in the Python server

    main

    To automatically calculate sales tax during checkout, you must modify server.py and ensure your Stripe account is configured for Tax.

    1. Update Code: In server.py, locate and uncomment the following line:
    automatic_tax={'enabled': True},
    1. Prerequisites:
    • You must have completed the Stripe Tax setup.
    • Your products and prices must be updated with tax behavior and, optionally, tax codes.
    # In server.py
    automatic_tax={'enabled': True},
  11. Configure Stripe Tax for products and prices

    main

    To use Stripe Tax with this sample, follow these steps:

    1. Ensure Stripe Tax is set up in your Stripe Dashboard.
    2. Create a product with a specific tax code (e.g., txcd_10000000 for 'General - Electronically Supplied Services'):
    stripe products create \
      --name="demo" \
      --tax-code="txcd_10000000"
    1. Create a price associated with that product, specifying the tax-behavior (either inclusive or exclusive):
    stripe prices create \
      --unit-amount=500 \
      --currency=usd \
      --tax-behavior=exclusive \
      --product=<INSERT_PRODUCT_ID>
    stripe products create \
      --name="demo" \
      --tax-code="txcd_10000000"
    
    stripe prices create \
      --unit-amount=500 \
      --currency=usd \
      --tax-behavior=exclusive \
      --product=<INSERT_ID, like prod_ABC123>