React/Node:使用 Trial_period_days 进行订阅时遇到集成错误
P粉378264633
P粉378264633 2024-04-04 20:30:57
0
1
314

我在 Stripe 中定义了一个产品,我希望将其设置为 7 天免费试用,然后向客户收费(除非他们事先取消)

通过 Firebase 使用自定义 Nodejs 函数 - 我收到以下错误:

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

我的前端功能:

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

和我的节点功能:

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

如果我取出 Trial_period_days 那么它可以正常工作,但是如果包含它,则会返回如上所述的错误。

P粉378264633
P粉378264633

全部回复(1)
P粉497463473

当您创建试用订阅时,不会创建 PaymentIntent,因为目前不需要付款。

相反,SetupIntent 可用于收集付款方式详细信息。您可以在创建订阅时扩展 pending_setup_intent [0],并通过 confirmCardSetup一个>.

[0] https://stripe.com/docs/api/订阅/对象#subscription_object-pending_setup_intent

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!