Chat
Ask me anything
Ithy Logo

Integrating Stripe Pricing Tables with Custom Checkout in Python

A Comprehensive Guide to Enabling Promo Codes and Free Trials without Breaking Your Existing Setup

stripe checkout integration code on computer screen
PRO
OpenAI o1

Key Takeaways

  • Maintain Your Existing Code: You can offer promo codes and free trials by enhancing your current stripe.checkout.Session.create implementation without switching to Pricing Tables.
  • Custom Checkout with Pricing Tables: It is possible to link to Stripe's checkout page using your own button while leveraging Pricing Tables for subscriptions, promo codes, and free trials.
  • Alternative Solutions: Options like Payment Links and configuring trials at the subscription level provide flexibility without significant code changes.

Introduction

You are currently using stripe.checkout.Session.create with a price line item in your Python application to generate a checkout session URL for your customers. Now, you want to enable promo codes and free trials without breaking your existing code. Additionally, you're interested in linking to the checkout page with your own button rather than embedding Stripe's Pricing Table using their JavaScript.

This comprehensive guide will walk you through the steps to achieve these goals. We'll explore how to modify your existing code, use Stripe Pricing Tables with custom buttons, and discuss alternative approaches that offer promo codes and free trials.


Enhancing Your Existing Code to Support Promo Codes and Free Trials

If you prefer to keep your current setup and avoid integrating Stripe's Pricing Tables, you can enhance your existing stripe.checkout.Session.create implementation to support promo codes and free trials. This approach ensures minimal disruption to your codebase while offering the additional features you desire.

Adding Promo Codes to Your Checkout Sessions

To enable promo codes in your checkout sessions, you need to set the allow_promotion_codes parameter to True. This adds a promo code input field to the Stripe checkout page, allowing customers to apply valid promo codes to their purchase.

Step-by-Step Guide to Enabling Promo Codes

  1. Create Promo Codes in Stripe Dashboard: Before customers can use promo codes, you need to create them in your Stripe Dashboard.
    • Navigate to Products > Coupons.
    • Click on New to create a new coupon with the desired discount parameters.
    • After creating a coupon, generate a corresponding promo code under Promo codes.
  2. Modify Your Checkout Session Creation: Update your stripe.checkout.Session.create call to include allow_promotion_codes=True.
    import stripe
    
    stripe.api_key = "your-secret-key"
    
    session = stripe.checkout.Session.create(
        payment_method_types=["card"],
        line_items=[{
            "price": "price_12345",
            "quantity": 1,
        }],
        mode="subscription",
        allow_promotion_codes=True,
        success_url="https://yourwebsite.com/success",
        cancel_url="https://yourwebsite.com/cancel",
    )
    

With allow_promotion_codes=True, the checkout page will display a field where customers can enter promo codes.

Implementing Free Trials in Your Subscriptions

There are two ways to offer free trials in Stripe:

  1. Configure Free Trials at the Price Level: Set up a trial period when creating or editing a Price in the Stripe Dashboard.
    • Navigate to Products > Prices and select the relevant price.
    • Edit the price and set the Trial period days to the desired length.
  2. Specify Trial Period in Checkout Session: Include the trial_period_days parameter in the subscription_data object during session creation.
    session = stripe.checkout.Session.create(
        payment_method_types=["card"],
        line_items=[{
            "price": "price_12345",
            "quantity": 1,
        }],
        mode="subscription",
        allow_promotion_codes=True,
        subscription_data={
            "trial_period_days": 14,  # Set trial period to 14 days
        },
        success_url="https://yourwebsite.com/success",
        cancel_url="https://yourwebsite.com/cancel",
    )
    

By setting trial_period_days, customers will begin with a free trial and won't be charged until the trial period ends.

Complete Example of Enhanced Checkout Session

import stripe

stripe.api_key = "your-secret-key"

session = stripe.checkout.Session.create(
    payment_method_types=["card"],
    line_items=[{
        "price": "price_12345",
        "quantity": 1,
    }],
    mode="subscription",
    allow_promotion_codes=True,
    subscription_data={
        "trial_period_days": 14,
    },
    success_url="https://yourwebsite.com/success",
    cancel_url="https://yourwebsite.com/cancel",
)

By implementing the above code, you maintain your existing checkout flow while adding support for promo codes and free trials.


Using Stripe Pricing Tables with Custom Buttons

If you're interested in leveraging Stripe's Pricing Tables to manage subscriptions, you might prefer to link to the checkout page with your own button rather than embedding the Pricing Table using Stripe's provided JavaScript.

Understanding Stripe Pricing Tables

Stripe Pricing Tables allow you to display your subscription plans on your website without extensive coding. They support features like promo codes and free trials, which can be managed directly from the Stripe Dashboard.

Creating a Pricing Table in the Stripe Dashboard

  1. Navigate to Products > Pricing tables in your Stripe Dashboard.
  2. Click on Create pricing table.
  3. Add your products and configure their prices.
  4. Customize the layout and appearance as desired.
  5. Enable features like promo codes and free trials in the settings.
  6. Save your pricing table to get the unique Pricing Table ID.

Linking to Checkout with Your Own Button

While Stripe encourages embedding the Pricing Table using their JavaScript component, you can link to the checkout page using your own button by generating a Checkout Session that corresponds to your Pricing Table.

Method 1: Using a Predefined Checkout URL

You can create a direct link to the Pricing Table's checkout page and use it in your custom button.

<a href="https://checkout.stripe.com/purchase/pricing_table_id" target="_blank" class="btn">Subscribe Now</a>

Replace pricing_table_id with your actual Pricing Table ID.

Method 2: Creating a Checkout Session Programmatically

Create a Checkout Session in your backend and use the session URL in your custom button.

import stripe

stripe.api_key = "your-secret-key"

session = stripe.checkout.Session.create(
    success_url="https://yourwebsite.com/success",
    cancel_url="https://yourwebsite.com/cancel",
    mode="subscription",
    payment_method_types=["card"],
    line_items=[],
    pricing_table={"pricing_table": "prctbl_1234567890"},
)

checkout_url = session.url

In your frontend, use the generated checkout_url in your button:

<a href="{{ checkout_url }}" target="_blank" class="btn">Subscribe Now</a>

This approach allows you to maintain control over the user interface while utilizing the features of Stripe Pricing Tables.

Considerations When Using Custom Buttons

  • Promo Codes and Free Trials: Ensure that these are enabled in your Pricing Table settings so that they are available during checkout.
  • Security: Always generate Checkout Sessions securely on your server to avoid exposing secret keys.
  • Updates to Pricing: Changes to products or prices in your Pricing Table reflect automatically, reducing maintenance overhead.

Alternatives to Pricing Tables for Promo Codes and Free Trials

If neither of the above solutions fits your needs, there are alternative approaches to offer promo codes and free trials without significant changes to your existing code.

Using Payment Links

Stripe Payment Links allow you to create shareable links to a checkout page configured with your products. They support promo codes and free trials and can be used without embedding any JavaScript.

Creating a Payment Link

  1. In the Stripe Dashboard, navigate to Payments > Payment links.
  2. Click on Create payment link.
  3. Add the product or subscription you want to sell.
  4. Enable promo codes and set up free trials in the advanced options.
  5. Create the link and copy the URL.

You can use this URL in your custom button:

<a href="https://buy.stripe.com/link_id" target="_blank" class="btn">Subscribe Now</a>

Configuring Trials at the Subscription Level

You can directly set up free trials when creating subscriptions, either in your backend code or via the Stripe Dashboard.

Setting Trial Period in Subscription Creation

subscription = stripe.Subscription.create(
    customer="cus_1234567890",
    items=[{"price": "price_12345"}],
    trial_period_days=14,
)

This method allows you to start a subscription with a free trial without changing your checkout session code.

Applying Coupons to Subscriptions

You can apply coupons directly to subscriptions, giving customers discounts without requiring them to enter a promo code.

Creating and Applying Coupons

  1. Create a Coupon:
    coupon = stripe.Coupon.create(
        percent_off=20,
        duration="once",
    )
    
  2. Create a Promotion Code from the Coupon:
    promo_code = stripe.PromotionCode.create(
        coupon=coupon.id,
        code="WELCOME20",
    )
    
  3. Apply the Coupon to the Subscription:
    subscription = stripe.Subscription.create(
        customer="cus_1234567890",
        items=[{"price": "price_12345"}],
        discounts=[{"coupon": coupon.id}],
    )
    

This approach allows you to control discounts programmatically.


Recap and Conclusion

To summarize, you have several options to enable promo codes and free trials without breaking your existing code:

  • Enhancing Existing Code: Modify your stripe.checkout.Session.create implementation to include allow_promotion_codes=True and set up free trials via trial_period_days.
  • Using Pricing Tables with Custom Buttons: Create Pricing Tables in the Stripe Dashboard and link to them using your own buttons by generating Checkout Sessions programmatically.
  • Alternative Approaches: Utilize Payment Links or configure promotions and trials at the subscription level to offer additional features without significant code changes.

By choosing the method that best fits your application's architecture, you can provide a seamless checkout experience to your customers while leveraging the powerful features offered by Stripe.


References

  • Stripe API Reference – Create a Checkout Session
  • Stripe Documentation – Pricing Table
  • Stripe Documentation – Adding Discounts
  • Stripe Documentation – Subscription Trials
  • Stripe Documentation – Coupons and Promotion Codes

Last updated January 24, 2025
Ask Ithy AI
Download Article
Delete Article