Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

trial period in Checkout session in Stripe

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: enter image description here

I appreciate if someone can help.

like image 306
Nabat Farsi Avatar asked Nov 07 '20 21:11

Nabat Farsi


People also ask

How do I add a trial period in Stripe?

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.

How long does Stripe Checkout session last?

Checkout Sessions expire 24 hours after creation. After creating a Checkout Session, redirect your customer to the URL returned in the response.

What is Checkout session in Stripe?

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.

What is stripe checkout?

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.

How do I configure a free trial period in stripe?

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.

What happens when a stripe trial ends?

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.

What is a trial period and how do I use it?

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.


2 Answers

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

like image 97
Brendan Moore Avatar answered Jan 04 '23 09:01

Brendan Moore


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

like image 24
Lew Perren Avatar answered Jan 04 '23 09:01

Lew Perren