【发布时间】:2020-11-28 14:56:10
【问题描述】:
我正在尝试模仿 Stripe 在他们的 Github 中的内容,以通过我的网站创建一个新的订阅会话。当我单击触发事件处理程序的按钮时,我在 Google Chrome 的控制台中收到以下错误:
script.js:3 POST http://127.0.0.1:3000/create-checkout-session net::ERR_CONNECTION_REFUSED createCheckoutSession@script.js:3 (匿名)@script.js:39
和
Uncaught (in promise) TypeError: Failed to fetch
Promise.then(异步)
(匿名)@script.js:39
VS Code 中的终端向我显示此错误(显示部分消息):
{ 错误:缺少必需的参数:line_items[0][currency].
我假设 script.js 文件中的某些内容不正确,但我不确定是什么?这是该文件中的代码:
// Create a Checkout Session with the selected plan ID
var createCheckoutSession = function (priceId) {
return fetch('/create-checkout-session', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
priceId: priceId,
}),
}).then(function (result) {
return result.json();
});
};
// Handle any errors returned from Checkout
var handleResult = function (result) {
if (result.error) {
var displayError = document.getElementById('error-message');
displayError.textContent = result.error.message;
}
};
/* Get your Stripe publishable key to initialize Stripe.js */
fetch('/setup')
.then(function (result) {
return result.json();
})
.then(function (json) {
var publishableKey = json.publishableKey;
var basicPriceId = json.basicPrice;
var stripe = Stripe(publishableKey);
// Setup event handler to create a Checkout Session when button is clicked
document
.getElementById('basic-plan-btn')
.addEventListener('click', function (evt) {
console.log('This is working');
createCheckoutSession(basicPriceId).then(function (data) {
// Call Stripe.js method to redirect to the new Checkout page
stripe
.redirectToCheckout({
sessionId: data.sessionId,
})
.then(handleResult);
});
});
});
如有任何帮助,我们将不胜感激!
【问题讨论】:
标签: javascript node.js stripe-payments