【发布时间】:2020-10-22 18:24:53
【问题描述】:
我已经设置了一个节点服务器来管理订阅,现在我正在尝试添加到 Stripe 客户门户的连接(这样我就可以在创建后将订阅管理外包给他们)。
说明书说:
创建一个供用户点击的按钮:
<form method="POST" action="/create_customer_portal_session">
<button type="submit">Manage billing</button>
</form>
然后为此添加端点:
// Set your secret key. Remember to switch to your live secret key in production!
// See your keys here: https://dashboard.stripe.com/account/apikeys
const stripe = require('stripe')('sk_test_yvBluFKhx8Xg1vjZVoulY8NO003U0HnRoA');
var session = await stripe.billingPortal.sessions.create({
customer: 'cus_IETxeMQvgvY05a',
return_url: 'https://example.com/account',
});
所以我采用了当前的服务器实现并尝试对其进行更新以包含此事件:
/* Get your Stripe publishable key to initialize Stripe.js */
fetch("https://***.com:4343/setup")
.then(function(result) {
return result.json();
})
.then(function(json) {
var publicKey = json.publicKey;
var basicPlanId = json.basicPlan;
var proPlanId = json.proPlan;
var stripe = Stripe(publicKey);
// Setup event handler to create a Checkout Session when button is clicked
document
.getElementById("basic-plan-btn")
.addEventListener("click", function(evt) {
customerEmail=document.querySelector('[name="username"]').value;
createCheckoutSession(basicPlanId).then(function(data) {
// Call Stripe.js method to redirect to the new Checkout page
stripe
.redirectToCheckout({
sessionId: data.sessionId,
})
.then(handleResult);
});
});
// Setup event handler to create a Checkout Session when button is clicked
document
.getElementById("pro-plan-btn")
.addEventListener("click", function(evt) {
customerEmail=document.querySelector('[name="username"]').value;
createCheckoutSession(proPlanId,customerEmail).then(function(data) {
// Call Stripe.js method to redirect to the new Checkout page
stripe
.redirectToCheckout({
sessionId: data.sessionId
})
.then(handleResult);
});
});
//PART ADDED TO CALL THE USER PORTAL
//Setup event handler to create a Portal Sesssion when button is clicked
document
.getElementById("portal")
.addEventListener("click",function(evt){
var customerId = 'cus_IETxeMQvgvY05a';
stripe.billingPortal.sessions.create({
customer: 'cus_IETxeMQvgvY05a',
return_url: 'https://***.com/account',
});
});
});
但是当我尝试单击按钮时,我发现 Stripe.BillinPortal 未定义。
其他两个请求工作顺利:对于每个请求,我都有一个这样定义的函数:
var createCheckoutSession = function(planId,customerEmail) {
return fetch("https://***.com:4343/create-checkout-session", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
planId: planId,
customerEmail: customerEmail
})
}).then(function(result) {
return result.json();
});
};
节点服务器上对应的处理程序是这样的:
app.post("/create-checkout-session", async (req, res) => {
const domainURL = process.env.DOMAIN;
const { planId,customerEmail } = req.body;
const session = await stripe.checkout.sessions.create({
payment_method_types: ["card"],
customer_email: customerEmail,
line_items: [{price: planId, quantity: 1}],
subscription_data: {
//items: [{ plan: planId }]
trial_period_days: 15
},
metadata: {'planId': planId},
success_url: `${domainURL}/success.html?session_id={CHECKOUT_SESSION_ID}`,
cancel_url: `${domainURL}/canceled.html` ,
mode: 'subscription',
});
res.send({
sessionId: session.id
});
});
我不知道如何对客户门户做同样的事情。
【问题讨论】:
标签: javascript node.js stripe-payments