如何在 Stripe Element JS 上要求电子邮件

How to require email on Stripe Element JS

我正在使用条纹元素创建付款意向,但是我确实需要从客户那里获取电子邮件和姓名以在购买后授予对产品的访问权限,这是我的代码:

async function initialize() {
  const { clientSecret } = await fetch("../create.php", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ items }),
  }).then((r) => r.json());

  elements = stripe.elements({ clientSecret });

  const paymentElement = elements.create("payment", {
    fields: {
      billingDetails: {
        name: 'auto',
        email: 'auto',
      }
    }
  });
  paymentElement.mount("#payment-element");
}

付款元素已创建并获取 CC 信息,选项设置为自动或关闭,文档中没有选项来设置必填字段,我如何仍能获取有关付款意向的信息?

遗憾的是,目前无法将支付元素本身配置为需要电子邮件等详细信息。

目前获得此功能的最佳方法可能是禁用支付元素中的那些字段,创建您的电子邮件字段,并在允许提交表单之前检查它是否已填写。然后您可以将电子邮件传递给 payment_method_data.billing_details 参数[1]

const {error} = await stripe.confirmPayment({
  //`Elements` instance that was used to create the Payment Element
  elements,
  confirmParams: {
    return_url: 'https://my-site.com/order/123/complete',
    payment_method_data: {
      billing_details: {
        email: 'test@example.com',
      }
    }
  },
});

[1] https://stripe.com/docs/js/payment_intents/confirm_payment#confirm_payment_intent-options-confirmParams-payment_method_data-billing_details