vaibhav khushalani
vaibhav khushalani

Reputation: 11

Stripe Subscription with 3d authentication failed

I am Implementing Stripe Subscription with 3d Authentication but Subscription got incomplete after successfull verification can anybody help me with this ? Technology--- MERN--

Steps

  1. Created PaymentMethod id from Frontend.
  2. Then at backend api in body get paymentMethod-Id
  3. Created a customer without payment method only basic info then created setupIntent and confirm it to get redirect url
  4. Then waited for response in webhook setup_intend _succeed and get success response then updated customer and created subscription but after everything perfect subscription is incomplete status and giveing invoice hosted url on clicking on that it create subscription

for Frontend passing Payment Method id to backend

 const paymentMethod = await stripe.createPaymentMethod({
        type: "card",
        card: elements.getElement(CardElement),
        billing_details: {
          name: name,
          email: email,
          address: {
            line1: address,
            city,
            state,
            postal_code: zipcode,
            country,
          },
          // phone: "+1 555-555-5555",
        },
      });

Backend

let customer = await stripe.customers.list({ email: email, limit: 1 });


  if (customer.data.length === 0) {
    customer = await stripe.customers.create({
      email,
      name,
      address: {
        line1: billing.address,
        city: billing.city,
        state: billing.state,
        postal_code: billing.zipcode,
        country: billing.country,
      },
 
    });
  } else {
    customer = customer?.data[0];
  }

  const setupIntent = await stripe.setupIntents.create({
    payment_method: paymentMethod,
    customer: customer.id,
     confirm: true,
    usage: "on_session",
    metadata: {
      data: JSON.stringify(req.body),
    },
  });


 const confirmedSetupIntent = await stripe.setupIntents.confirm(
    setupIntent.id,
    {
      return_url: `${CLIENT_BASE_URL}/check-payment-status/${pricePlanId}`,
    }
  );

passing this return url on frontend

On webhook

event setup_intent.succeeded


 const customerList = await stripe.customers.list({
      email: email,
      limit: 1,
    });
    const customer = customerList.data.length > 0 ? customerList.data[0] : null;

    if (customer) {
      await stripe.customers.update(customer.id, {
        name,
        invoice_settings: {
          default_payment_method: paymentMethodId,
        },
      });
    }

    // Check if customer has an active subscription
    const subscriptionList = await stripe.subscriptions.list({
      customer: account.customerId,
      status: "active",
      limit: 1,
    });
  
    let subscription = null;
subscription = await stripe.subscriptions.create({
          customer: account.customerId,
          items: [{ price: pricePlanId }],
          metadata: {
            product_name: plan.name,
            userId: user._id,
            amount: plan.amount,
            credits: plan.credits,
            planId: pricePlanId,
            billingName: billing.name,
            billingEmail: billing.email,
          },
          payment_settings: {
            payment_method_types: ["card"],
            save_default_payment_method: "on_subscription",
          },
          expand: ["latest_invoice.payment_intent"],
          off_session: true,
        });

c

an anybody tell me what is wrong in this code

Upvotes: 0

Views: 627

Answers (1)

codename_duchess
codename_duchess

Reputation: 941

Your flow is not exactly what Stripe recommends in their docs. There shouldn't be any need to directly create a Payment Method or a Setup Intent for a Subscription integration. See these docs for the recommended integration guide.

Upvotes: -1

Related Questions