I would like to create trial period to be used with stripe checkout session:
session = stripe.checkout.Session.create(
customer=customer.stripe_id,
payment_method_types=['card'],
line_items=[{
'price': "price_1HjynjHdAhQwSUAK",
'quantity': 1,
'tax_rates': ["txr_1Hkntg4yXtzmX", ],
},
mode='payment',
allow_promotion_codes=True,
success_url=request.build_absolute_uri(reverse('thanks')) + '?session_id=CHECKOUT_SESSION_ID}',
cancel_url=request.build_absolute_uri(reverse('index_payment')),
)
in the tripe.Subscription.create looks like we just need to add trial_end=1605387163, but it doesnt work in checkout session. I cant seem to find the way to do it even though I am pretty sure it is doable as displayed in this demo:
I appreciate if someone can help.
If you'd like to add a trial period for a subscription, you can do so by using subscription_data to add a trial_period_days number. This number needs to be an integer and at least equal to 1. Overall, a subscription with a trial period of 2 weeks would be written this way: const session = await stripe.
Checkout Sessions expire 24 hours after creation. After creating a Checkout Session, redirect your customer to the URL returned in the response.
The Checkout Session provides a URL that redirects customers to a Stripe-hosted payment page. Customers enter their payment details on the payment page and complete the transaction. After the transaction, a webhook fulfills the order using the checkout. session.
Stripe Checkout lets you sign up customers for a free trial of a subscription service without collecting their payment details. At the end of the trial period you specify, use Stripe to configure a reminder email to collect a customer’s payment details.
At the end of the trial period you specify, use Stripe to configure a reminder email to collect a customer’s payment details. A subscription_data dictionary with a trial_period_days field set to the length (in days) you want your free trial to be.
When the trial ends, a new billing cycle starts for the customer. Once the trial period is up, Stripe generates an invoice and sends an invoice.created event notification. Approximately an hour later, Stripe attempts to charge that invoice.
Trial periods are normally applied at the start of a subscription, but you can also use a trial period on an existing subscription to change the subscription’s billing cycle.
You've got the right idea with trial_end
, it just needs to be a child parameter under subscription_data
.
// other parameters
subscription_data: {
trial_end=1605387163
}
https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-subscription_data-trial_end
Brendan's answer pointed me in the right direction, for PHP the syntax to add the trial_end would be for example:
$session = \Stripe\Checkout\Session::create([
.......
Your Other parameters
.......
'subscription_data' => ['trial_end' => 1624110173],
]);
Where 1624110173 is unix timestamp for the end of the free period. See this Stripe document for general explanation of checkout process and code:
Stripe Documents
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With