条带有问题:未捕获的集成错误:条带.重定向到签出:会话 ID 的值无效。您指定了'pi_1Fr...9sCWFtQr'



我试图使用Stripe checkout。我生成了会话,然后它显示在仪表板上。然后,我尝试在具有硬编码ID的html文件上运行directocheckout。但是错误Uncaught IntegrationError: stripe.redirectToCheckout: Invalid value for sessionId. You specified 'pi_1FrFbeBNGS...sCWFtQr'.

我尝试定义一个 json 对象并使用 session.id 但仍然不起作用。怎么了?

这是我的服务器.js

const express = require("express");
const app = express();
const { resolve } = require("path");
// Copy the .env.example in the root into a .env file in this folder
const env = require("dotenv").config({ path: "./.env" });
const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);
(async () => {
session = await stripe.checkout.sessions.create({
payment_method_types: ['card'],
line_items: [{
name: 'T-shirt',
description: 'Comfortable cotton t-shirt',
images: ['https://example.com/t-shirt.png'],
amount: 500,
currency: 'usd',
quantity: 1,
}],
success_url: 'https://example.com/success?session_id={CHECKOUT_SESSION_ID}',
cancel_url: 'https://example.com/cancel',
});
console.log(session.id);
return session;
})().then(function(session){
console.log(session.id)
});

这是我的索引.html

<!DOCTYPE html>
<script src="https://js.stripe.com/v3/"></script>
<body>
direct to payment
</body>
<script>
var stripe = Stripe('pk_test_XhC9cMRDNNqdkBVtHwzgYTQa00ov5gDmmN');
id = 'pi_1FrFEYBNGSiXwN3Wx3FBbawa';
stripe.redirectToCheckout({
// Make the id field from the Checkout Session creation API response
// available to this file, so you can provide it as parameter here
// instead of the {{CHECKOUT_SESSION_ID}} placeholder.
sessionId: session.id
}).then(function (result) {
// If `redirectToCheckout` fails due to a browser or network
// error, display the localized error message to your customer
// using `result.error.message`.
if (result.error) {
var displayError = document.getElementById("error-message");
displayError.textContent = result.error.message;
}
});
</script>
</html>

完整的错误

Uncaught IntegrationError: stripe.redirectToCheckout: Invalid value for sessionId. You specified 'pi_1FrFbeBNGSiXwN3W9sCWFtQr'.
at new t (https://js.stripe.com/v3/:1:10765)
at Eu (https://js.stripe.com/v3/:1:136437)
at wu (https://js.stripe.com/v3/:1:137398)
at Ou (https://js.stripe.com/v3/:1:138478)
at Pu (https://js.stripe.com/v3/:1:138592)
at e.redirectToCheckout (https://js.stripe.com/v3/:1:139007)
at file:///Users/MattMachine/sdpp_stripe/client.html:62:16
t @ (index):1
Eu @ (index):1
wu @ (index):1
Ou @ (index):1
Pu @ (index):1
(anonymous) @ (index):1
(anonymous) @ client.html:62

看起来您正在传递您的id而不是session.id(尽管我看不出您在这里的情况中会从哪里获得session(; 您需要发送/设置/渲染在服务器中生成的session.id.js到您的 HTML 页面。

"您的 html 文件不包含会话 ID - 这是一个付款意向 ID。您需要包含正确的 ID 才能完成此操作"@floatingLomas

这对我有帮助。仪表板上的 ID 是付款意向 ID。我尝试从会话对象中打印出 ID 并将其放入 directToPayment(( 中,它起作用了!!

相关内容

最新更新