簡體   English   中英

將客戶電子郵件傳遞給條帶結帳

[英]Pass customer email to stripe checkout

我正在使用在節點下運行的 Stripe 訂閱。

我想創建一個預先填寫電子郵件地址的新結帳。 所以我試着在客戶端做:

// 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' }] },

但這不會填充結帳頁面中的字段

我如何解決它?

它不是subscription_data的一部分; 它是自己的名為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` 
  });

使用“customer_email”參數,如下所示,這是Node.js中的示例:

const session = await stripe.checkout.sessions.create({
    customer_email: 'example@gmail.com', // Here
    line_items=[
        {
            'price_data': {
                'currency': 'jpy',
                'unit_amount': 1000,
                'product_data': {
                    'name': 'Honey',
                    'description': 'Good honey',
                },
            },
            'quantity': 1,
        },
    ],
    mode: 'payment',
    success_url: 'https://example.com/success',
    cancel_url: 'https://example.com/cancel',
});

並且因為您使用“customer_email”參數,所以電子郵件字段是只讀的,如下所示:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM