如何在结帐流程中将卡设置为默认付款方式

How can I make the card as the default payment method during checkout flow

我正在使用 Stripe checkout 来收集银行卡详细信息和客户地址,如下所示

 const session = await stripe.checkout.sessions.create({
  payment_method_types: ['card'],
  mode: 'setup',
  customer: req.subscription.customerId,
  client_reference_id: email,
  metadata: {'plan': 'basic'},
  billing_address_collection: 'required',
  success_url: req.protocol + '://' + req.get('host') + '/payment/middle?'+queryParams,
  cancel_url: req.protocol + '://' + req.get('host') + '/payment/failure',
});

如何在结帐流程中将银行卡设置为默认付款方式?

结帐会话会自动将新的付款方式附加到客户。如果您想随后将此付款方式设置为订阅付款的默认付款方式,您需要手动更新客户对象的 invoice_settings.default_payment_method 属性。

我建议收听 checkout.session.completed webhook 并执行此操作:

// The checkout session object sent by the webhook
const session = event.data.object;

// Retrieve the associated setup intent (we need it to get the payment_method just after)
const setupIntent = await stripe.setupIntents.retrieve(
    session.setup_intent
);
  
// Update the default payment method for the customer
const customer = await stripe.customers.update(session.customer, {
    invoice_settings: {
      default_payment_method: setupIntent.payment_method,
    },
});

您可以通过阅读 Stripe 文档中的 this page 了解更多信息。