Stripe订阅续订陷入“未完成”状态,无法自动续订或扣款

问题描述 投票:0回答:1

我正在使用 Stripe 处理应用程序中的订阅。初始订阅创建工作完美,但我面临自动续订的问题。

当包月续费时,状态变为“未完成”,且不会自动扣款。这会导致订阅无法续订,并且用户最终处于不完整状态。


const createProduct = async (productName) => {
    const PRODUCT_TYPE = 'service'

    const product = await stripe.products.create({
        name: productName,
        type: PRODUCT_TYPE,
    });
    return product?.id
}

const createCustomer = async (email) => {
    const customer = await stripe.customers.create({
        email: email,
    })
    return customer.id
}

const createPrice = async (productId, amount, duration) => {
    const CURRENCY = "usd"
    const price = await stripe.prices.create({
        product: productId,
        unit_amount: Number(amount) * 100,
        currency: CURRENCY,
        recurring: { interval: 'day' }
    })

    return price?.id
}

const productId = await createProduct(productName)
    const customerId = customer_id ? customer_id : await createCustomer(email)
    const metaData = {
      duration,
      user_id,
      productId,
      amount,
      productName,
      ...req?.body
}

const subscription = await stripe.subscriptions.create({
  customer: customerId,
  items: [{ price: await createPrice(productId, amount, duration) }],
  payment_behavior: 'default_incomplete',
  expand: ['latest_invoice.payment_intent'],
  metadata: metaData,
});

我使用 payment_behavior: 'default_incomplete' 进行初始订阅创建,效果很好。但是,当订阅续订时,它会处于不完整状态,并且不会处理任何付款。

javascript node.js stripe-payments payment-gateway stripes
1个回答
0
投票

订阅需要设置默认付款方式以收取未来付款的费用。您的集成中出现的问题可能是因为付款方式未设置为订阅的默认付款方式,无法使用可用的付款方式进行收费。

在创建订阅时的代码中,缺少

payment_settings
来保存订阅的默认付款方式:

const subscription = await stripe.subscriptions.create({
  customer: customerId,
  items: [{
    price: priceId,
  }],
  payment_behavior: 'default_incomplete',

  // `payment_settings` should be added to save the payment method 
  // as the default of the subscription
  payment_settings: { save_default_payment_method: 'on_subscription' },

  expand: ['latest_invoice.payment_intent'],
});

参考:https://docs.stripe.com/billing/subscriptions/build-subscriptions?platform=web&ui=elements#create-subscription

© www.soinside.com 2019 - 2024. All rights reserved.