English 中文(简体)
ReactJS + Stripe: trial_period_days not functioning
原标题:

trial_period_days: 7 does not seem to be recognised in my node function. Any idea why this may be the case? Am I missing a variable? When posting to this it causes an integration error...

const subscription = await stripe.subscriptions.create({
                customer: customer.id,
                items: [{ price: "price_1KZ3nTGxUje7SlyIDUfIXkr3" }],
                payment_settings: {
                  payment_method_options: {
                    card: {
                      request_three_d_secure: "any",
                    },
                  },
                  payment_method_types: ["card"],
                  save_default_payment_method: "on_subscription",
                },
                trial_period_days: 7,
                expand: ["latest_invoice.payment_intent"],
              });
问题回答

When you create a Subscription with a trial or a 100% coupon, there’s no Invoice to pay immediately. Therefore, there’s no PaymentIntent, and no client_secret. That’s why your PaymentIntent confirmation failed. Instead, the Subscription creates a SetupIntent to collect a Payment Method to use in the future. You can access the client secret of the SetupIntent via pending_setup_intent.client_secret property (you will need to expand pending_setup_intent property when creating the Subscription). Then, you will need to confirm SetupIntent on the frontend. To better understand how SetupIntents work you can follow this guide: https://stripe.com/docs/payments/save-and-reuse?platform=web.

Paul, I was just running into this same issue, because I was coming from C#, and it was just a regular field in the object to create the session. In TypeScript though, looks like the field is hidden under "subscription_data" in the session create options. So something like this should work:

let checkoutSession = await stripe.checkout.sessions.create({
    customer: customer.id, 
    success_url:"https://mycompany.com", 
    line_items: [{price: priceId}], 
    allow_promotion_codes: true,
    mode: "subscription",
    subscription_data: {trial_period_days: 7} //this is the field.
});

Hope this helps.

Also here is the link to the API docs for more info: https://stripe.com/docs/api/checkout/sessions/create.





相关问题
How to use one react app into another react app?

I have two react apps, parent app and child app. and child app have an hash router. I have build the child app using npm run build, It creates build folder. That build folder moved into inside the ...

how to get selected value in Ant Design

I want to print the selected value, and below is my option list: const vesselName = [ { value: 0 , label: ALBIDDA , }, { value: 1 , label: ALRUMEILA , }, { value: 2 ,...

How to add a <br> tag in reactjs between two strings?

I am using react. I want to add a line break <br> between strings No results and Please try another search term. . I have tried No results.<br>Please try another search term. but ...

热门标签