【发布时间】: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' }] },
但这不会填充结帐页面中的字段
我该如何解决?
【问题讨论】: