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

Raj*_*jan 2 javascript node.js stripe-payments

我正在使用 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',
});
Run Code Online (Sandbox Code Playgroud)

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

som*_*oma 6

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

我建议监听 webhookcheckout.session.completed并执行以下操作:

// 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,
    },
});
Run Code Online (Sandbox Code Playgroud)

您可以通过阅读Stripe 文档中的此页面来了解更多信息。