【问题标题】:Pass customer email to stripe checkout将客户电子邮件传递给条带结帐
【发布时间】:2021-01-17 06:17:57
【问题描述】:

我正在使用在节点下运行的 Stripe Subscription。

我想创建一个预先填写电子邮件地址的新结帐。所以我试着在客户端做:

// Setup event handler to create a Checkout Session when button is clicked
document
  .getElementById("basic-plan-btn")
  .addEventListener("click", function(evt) {
    createCheckoutSession(basicPlanId).then(function(data) {
      // Call Stripe.js method to redirect to the new Checkout page
      stripe
        .redirectToCheckout({
              sessionId: data.sessionId,
        })
        .then(handleResult);
    });
  });

这里的邮件直接在代码中只是为了测试一下。 在 createCheckoutSession 中,我添加了 customerEmail:

var createCheckoutSession = function(planId) {
  return fetch("https://example.com:4343/create-checkout-session", {
    method: "POST",
    headers: {
      "Content-Type": "application/json"
    },
    body: JSON.stringify({
      planId: planId,
      customerEmail: 'mario.rossi@gmail.com'
    })
  }).then(function(result) {
    return result.json();
  });
};

然后在服务器上我尝试捕获并转发电子邮件,但我该怎么做呢?

app.post("/create-checkout-session", async (req, res) => {
  const domainURL = process.env.DOMAIN;
  const { planId } = req.body;

  // Create new Checkout Session for the order
  // Other optional params include:
  // [billing_address_collection] - to display billing address details on the page
  // [customer] - if you have an existing Stripe Customer ID
  // [customer_email] - lets you prefill the email input in the form
  // For full details see https://stripe.com/docs/api/checkout/sessions/create
  const session = await stripe.checkout.sessions.create({
    payment_method_types: ["card"],
    subscription_data: { items: [{ plan: planId }] },
    // ?session_id={CHECKOUT_SESSION_ID} means the redirect will have the session ID set as a query param
    success_url: `${domainURL}/success.html?session_id={CHECKOUT_SESSION_ID}`,
    cancel_url: `${domainURL}/canceled.html` 
  });

  res.send({
    sessionId: session.id
  });
});

我还尝试使用以下方式将电子邮件直接传递到服务器:

subscription_data: { items: [{ plan: planId, customer_email: 'a.b@gmail.com' }] },

但这不会填充结帐页面中的字段

我该如何解决?

【问题讨论】:

    标签: node.js stripe-payments


    【解决方案1】:

    它不是subscription_data 的一部分;是its own field titled customer_email

      const session = await stripe.checkout.sessions.create({
        payment_method_types: ["card"],
        // THIS LINE, HERE:
        customer_email: 'a.b@gmail.com',
        subscription_data: { items: [{ plan: planId }] },
        // ?session_id={CHECKOUT_SESSION_ID} means the redirect will have the session ID set as a query param
        success_url: `${domainURL}/success.html?session_id={CHECKOUT_SESSION_ID}`,
        cancel_url: `${domainURL}/canceled.html` 
      });
    

    【讨论】:

    • 知道了!如何从我的 createCheckoutSession 中获取它?我想将它从客户端传递到服务器
    • @LelioFaieta 与您当前在const { planId } = req.body; 中获取计划 ID 的方式相同。我不是 Python 人,但我猜想const { planId, customerEmail } = req.body;?
    • 所以它将是 const { customerEmail } = req.body?抱歉,我还在学习节点 :-)
    • 所以req.body 包含来自您的fetch 呼叫的计划 ID 和客户电子邮件值。我也不是 Node 人,哎呀,所以我不确定这里的语法。
    • 而@ceejayoz 在结帐会话创建请求的顶层使用customer_email 是完全正确的。
    猜你喜欢
    • 2021-12-23
    • 2021-03-18
    • 2016-12-25
    • 1970-01-01
    • 1970-01-01
    • 2017-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多