gpt4 book ai didi

javascript - 将客户电子邮件传递给 strip 结帐

转载 作者:行者123 更新时间:2023-12-05 01:10:22 30 4
gpt4 key购买 nike

我正在使用在 node 下运行的 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();
});
};

然后在服务器上我 try catch 并转发电子邮件,但我该怎么做呢?

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 的一部分;它是 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`
});

关于javascript - 将客户电子邮件传递给 strip 结帐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64156837/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com