2
votes

I'm working on stripe integration with react node.js

While creating a subscription without trial, I have no issues, and all use-cases are much simple.

But, I'm trying to create free trial without collecting credit card details, later when trial expired, I would like to collect credit card details

Server side: Creating Trial Subscription without payment method:

  const subscription = await stripe.subscriptions.create({
    customer: customer.id,
    items: [{ price: priceId }],
    trial_end: expiredAt.unix(),
    expand: ['latest_invoice.payment_intent'], 
  });

ClientSide: Later when trial expired, I would like to collect credit card details(paymentMethod), check if user action is required due to failures or 3ds and update the subscription by updating the customer's default payment_method:

  const updateCustomerDefaultPaymentMethod = await stripe.customers.update(
    customerId,
    {
      invoice_settings: {
        default_payment_method: req.body.paymentMethodId,
      },
    }
  );

How can I update the subscription to perform paymentIntent or charge the user, and return to client same subscription object with status 'in_complete' same as when creating subscription without trial_period? Currently when running my code, I keep getting status='active' because the first invoice is in status='paid' with price of '0$'.

1

1 Answers

2
votes

In the case of a free trial, creating a subscription won't result in an initial payment, and Stripe will instead create a SetupIntent under the hood for you automatically. This SetupIntent is meant to be used when collecting the customer's payment method. So at any point during the trial, the customer would need to return to your app to enter their credit card details, which you would then save using the SetupIntent:

Once the payment method is setup and saved to the customer (as the default payment method for invoices) it will be used to pay for the subscription's first invoice when the trial ends. The first payment after a trial is considered an off-session payment (last FAQ), so if that payment fails Stripe will attempt to recollect the payment using the usual smart retries and dunning functionality.

In a nutshell, you need to use the SetupIntent that's created (and attached to the subscription) to save the user's payment information.