React/Node: Integration error encountered when subscribing using Trial_period_days
P粉378264633
P粉378264633 2024-04-04 20:30:57
0
1
333

I have defined a product in Stripe that I want to set up as a 7 day free trial and then charge the customer (unless they cancel beforehand)

Using a custom Nodejs function via Firebase - I get the following error:

Missing value for stripe.confirmCardPayment intent secret: value should be a client_secret string

My front-end functions:

const createSubscription = async () => {
    setProcessing(true);
    setWait('PLEASE WAIT');
    try {
      
      // create a payment method
      const paymentMethod = await stripe?.createPaymentMethod({
        type: "card",
        card: elements?.getElement(CardElement),
        billing_details: {
          name,
          email,
        },
      });

      // call the backend to create subscription
      const response = await fetch("https://XXX.cloudfunctions.net/tplannersubscription", {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
        body: JSON.stringify({
          paymentMethod: paymentMethod?.paymentMethod?.id,
          name,
          lname,
          email,
          priceId
        }),
      }).then((res) => res.json());
      
      const confirmPayment = await stripe?.confirmCardPayment(
        response.clientSecret
      );
};

and my node function:

cors(request, response, async () => {
        // create a stripe customer
        try {
            if (request.method != "POST") return response.status(400);
            const { name, email, paymentMethod } = request.body;
            // Create a customer
            const customer = await stripe.customers.create({
              email,
              name,
              payment_method: paymentMethod,
              invoice_settings: { default_payment_method: paymentMethod },
            });

            // const priceId = request.priceId;

            // Create a subscription
            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",
                },
                payment_behavior: "default_incomplete",
                trial_period_days: 7,
                expand: ["latest_invoice.payment_intent"],
              });
            
              // Send back the client secret for payment
            response.json({
              message: "Subscription successfully initiated",
              clientSecret: subscription.latest_invoice.payment_intent.client_secret,
              subID:subscription.id
            });
          } catch (err) {
            console.error(err);
            response.status(500).json({ message: "Internal server error" });
          }
   });

If I take out the Trial_period_days then it works fine, but if I include it it returns the error as mentioned above.

P粉378264633
P粉378264633

reply all(1)
P粉497463473

When you create a trial subscription, a PaymentIntent is not created because no payment is currently required.

Instead, SetupIntent can be used to collect payment method details. You can extend pending_setup_intent [0] when creating a subscription and pass confirmCardSetup一个>.

[0] https://stripe.com/docs/api/subscription/object#subscription_object-pending_setup_intent

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!