最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Pass customer email to stripe checkout - Stack Overflow

programmeradmin0浏览0评论

I'm using Stripe Subscription running under node.

I want to create a new checkout prefilling the email address. So I tried to do in the client:

// 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);
    });
  });

The email here is directly in the code just to test it. In createCheckoutSession I added the customerEmail:

var createCheckoutSession = function(planId) {
  return fetch(":4343/create-checkout-session", {
    method: "POST",
    headers: {
      "Content-Type": "application/json"
    },
    body: JSON.stringify({
      planId: planId,
      customerEmail: '[email protected]'
    })
  }).then(function(result) {
    return result.json();
  });
};

Then on the server I try to catch and forward the email but how can I do it?

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 
  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
  });
});

I also tried to pass the email directly to the server using:

subscription_data: { items: [{ plan: planId, customer_email: '[email protected]' }] },

But this doesn't populate the field in the checkout page

How do I fix it?

I'm using Stripe Subscription running under node.

I want to create a new checkout prefilling the email address. So I tried to do in the client:

// 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);
    });
  });

The email here is directly in the code just to test it. In createCheckoutSession I added the 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: '[email protected]'
    })
  }).then(function(result) {
    return result.json();
  });
};

Then on the server I try to catch and forward the email but how can I do it?

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
  });
});

I also tried to pass the email directly to the server using:

subscription_data: { items: [{ plan: planId, customer_email: '[email protected]' }] },

But this doesn't populate the field in the checkout page

How do I fix it?

Share Improve this question edited Jul 12, 2022 at 21:18 Super Kai - Kazuya Ito 1 asked Oct 1, 2020 at 13:52 Lelio FaietaLelio Faieta 6,6889 gold badges47 silver badges84 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 14

It is not part of subscription_data; it is its own field titled customer_email.

  const session = await stripe.checkout.sessions.create({
    payment_method_types: ["card"],
    // THIS LINE, HERE:
    customer_email: '[email protected]',
    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` 
  });

Use "customer_email" parameter as shown below and this is the example in Node.js:

const session = await stripe.checkout.sessions.create({
    customer_email: '[email protected]', // 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',
});

And because you use "customer_email" parameter so the email field is readonly as shown below:

发布评论

评论列表(0)

  1. 暂无评论