Stripe React Native订阅创建错误 – 该客户没有附加的付款来源或默认付款方式。

huangapple go评论81阅读模式
英文:

stripe react native subscription creation-Error- This customer has no attached payment source or default payment method

问题

我正在尝试在React Native中创建一个订阅支付表单,以下是我的后端代码。当我取消注释trial_period_days: 3时,即使未声明支付方式,它也可以正常工作。但是,如果我注释掉这一行,我会收到错误信息 - "StripeInvalidRequestError:此客户未附加付款来源或默认付款方式。请考虑添加默认付款方式。" 我尝试了以下代码来声明支付方式,但没有成功。我希望新客户也能够使用其国家和操作系统平台支持的所有可用支付方式。

const stripe = require('stripe')(stripeSecretKey);
const express = require('express');
const bodyParser = require('body-parser');
const app = express();

app.use(bodyParser.json());
app.get('/', (req, res) => {
  res.send('Test');
});
app.post('/payment-sheet-subscription', async (req, res) => {
  const { email = `test${Math.floor(Math.random() * 9999) + 1}@domain.com` } =
    req.body;

  const customer = await stripe.customers.create({ email });

  const ephemeralKey = await stripe.ephemeralKeys.create(
    { customer: customer.id },
    { apiVersion: '2020-08-27' },
  );
  const PRICE_ID = 'price_1NQ4EnSBro6pGrG8ZYfdBX5x';
  const subscription = await stripe.subscriptions.create({
    customer: customer.id,
    items: [{ price: PRICE_ID }],
    // trial_period_days: 3,
  });
  const updatedSubscription = await stripe.subscriptions.update(
    subscription.id,
    {
      payment_settings: {
        payment_method_types: ['card'],
      },
    },
  );
  if (typeof updatedSubscription.pending_setup_intent === 'string') {
    const setupIntent = await stripe.setupIntents.retrieve(
      updatedSubscription.pending_setup_intent,
    );

    // if (typeof subscription.pending_setup_intent === 'string') {
    //   const setupIntent = await stripe.setupIntents.retrieve(
    //     subscription.pending_setup_intent,
    //   );

    return res.json({
      setupIntent: setupIntent.client_secret,
      ephemeralKey: ephemeralKey.secret,
      customer: customer.id,
    });
  } else {
    throw new Error(
      'Expected response type string, but received: ' +
        typeof subscription.pending_setup_intent,
    );
  }
});

如果取消注释trial_period_days: 3,则代码可以正常工作,即使未声明付款方式。但是,如果注释掉该行,您会收到一个错误,指出客户未附加付款来源或默认付款方式。您尝试了一些代码来声明付款方式,但没有成功。您希望新的客户也能够使用其国家和操作系统平台支持的所有可用付款方式。

英文:

I am trying to create a subscription paymentsheet in react native, below is my code for backend. When I uncomment trial_period_days: 3, then it works even if payment methods are not declared. But if I comment this line, then I am getting error - "StripeInvalidRequestError: This customer has no attached payment source or default payment method. Please consider adding a default payment method." I tried below code to declare payment methods, but it did not work. I want new customers to also be able to use all the available payment methods that their country and OS platform supports.

const stripe = require('stripe')(stripeSecretKey);
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
app.get('/', (req, res) => {
res.send('Test');
});
app.post('/payment-sheet-subscription', async (req, res) => {
const {email = `test${Math.floor(Math.random() * 9999) + 1}@domain.com`} =
req.body;
const customer = await stripe.customers.create({email});
const ephemeralKey = await stripe.ephemeralKeys.create(
{customer: customer.id},
{apiVersion: '2020-08-27'},
);
const PRICE_ID = 'price_1NQ4EnSBro6pGrG8ZYfdBX5x';
const subscription = await stripe.subscriptions.create({
customer: customer.id,
items: [{price: PRICE_ID}],
// trial_period_days: 3,
});
const updatedSubscription = await stripe.subscriptions.update(
subscription.id,
{
payment_settings: {
payment_method_types: ['card'],
},
},
);
if (typeof updatedSubscription.pending_setup_intent === 'string') {
const setupIntent = await stripe.setupIntents.retrieve(
updatedSubscription.pending_setup_intent,
);
// if (typeof subscription.pending_setup_intent === 'string') {
//   const setupIntent = await stripe.setupIntents.retrieve(
//     subscription.pending_setup_intent,
//   );
return res.json({
setupIntent: setupIntent.client_secret,
ephemeralKey: ephemeralKey.secret,
customer: customer.id,
});
} else {
throw new Error(
'Expected response type string, but received: ' +
typeof subscription.pending_setup_intent,
);
}
});

答案1

得分: 1

The pending_setup_intent is only applicable for non-payment scenarios, such as trials.

在创建需要付款的订阅之前,如果您还没有为客户设置付款方式,您将希望使用 payment_behavior='default_incomplete',如Stripe文档1中所示。使用这个选项,您可以展开 latest_invoice.payment_intent,以获得一个client_secret,用于在您的前端应用程序中完成第一笔付款。这也在文档3中说明。

英文:

The pending_setup_intent is only applicable for non-payment scenarios, such as trials.

When creating a subscription that does require payment before you have a payment method set up for the customer, you'll want to use payment_behavior='default_incomplete', as shown in Stripe's docs here. With this, you can expand the latest_invoice.payment_intent to get a client_secret to use in your front end app to complete the first payment. This is also shown in the docs here.

huangapple
  • 本文由 发表于 2023年7月4日 21:54:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76613373.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定