Lel*_*eta 5 javascript email checkout node.js stripe-payments
我正在使用在节点下运行的 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);
});
});
Run Code Online (Sandbox Code Playgroud)
这里的电子邮件直接在代码中只是为了测试它。在 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();
});
};
Run Code Online (Sandbox Code Playgroud)
然后在服务器上我尝试捕获并转发电子邮件,但我该怎么做呢?
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
});
});
Run Code Online (Sandbox Code Playgroud)
我还尝试使用以下方法将电子邮件直接传递到服务器:
subscription_data: { items: [{ plan: planId, customer_email: 'a.b@gmail.com' }] },
Run Code Online (Sandbox Code Playgroud)
但这不会填充结帐页面中的字段
我如何解决它?
cee*_*yoz 11
它不是 的一部分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`
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7840 次 |
| 最近记录: |