stripe.checkout.Session.create
implementation without switching to Pricing Tables.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.
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.
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.
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.
There are two ways to offer free trials in Stripe:
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.
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.
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.
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.
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.
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.
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.
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.
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.
You can use this URL in your custom button:
<a href="https://buy.stripe.com/link_id" target="_blank" class="btn">Subscribe Now</a>
You can directly set up free trials when creating subscriptions, either in your backend code or via the Stripe Dashboard.
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.
You can apply coupons directly to subscriptions, giving customers discounts without requiring them to enter a promo code.
coupon = stripe.Coupon.create(
percent_off=20,
duration="once",
)
promo_code = stripe.PromotionCode.create(
coupon=coupon.id,
code="WELCOME20",
)
subscription = stripe.Subscription.create(
customer="cus_1234567890",
items=[{"price": "price_12345"}],
discounts=[{"coupon": coupon.id}],
)
This approach allows you to control discounts programmatically.
To summarize, you have several options to enable promo codes and free trials without breaking your existing code:
stripe.checkout.Session.create
implementation to include allow_promotion_codes=True
and set up free trials via trial_period_days
.
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.