NodeJS 带3d身份验证的条带订阅失败

lb3vh1jj  于 2023-06-05  发布在  Node.js
关注(0)|答案(1)|浏览(191)

我正在使用3D身份验证实施Stripe订阅,但成功验证后订阅不完整,有人可以帮助我吗?技术--- MERN--
步骤
1.已从前端创建PaymentMethod ID。
1.然后在body中的后端API获取paymentMethod-Id
1.创建了一个没有付款方式的客户,只有基本信息,然后创建setupIntent并确认它以获取重定向URL
1.然后在webhook setup_intend _succeed中等待响应并获得成功响应,然后更新客户并创建订阅,但在所有完美的订阅都不完整状态后,单击它创建订阅并提供发票托管URL

用于前端将Payment Method id传递给后端

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",
        },
      });

后台

let customer = await stripe.customers.list({电子邮件:电子邮件,限制:});

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}`,
    }
  );

在前端传递此返回url

webhook上

事件setup_intent. successfully

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

谁能告诉我这段代码有什么问题

oxalkeyp

oxalkeyp1#

你的流程并不完全是Stripe在他们的文档中推荐的。不需要为订阅集成直接创建付款方式或设置意图。有关推荐的集成指南,请参阅these docs

相关问题