我正在尝试使用Stripe结帐。我生成了会话,然后将其显示在仪表板上。然后,我尝试在具有硬编码ID的html文件上运行directocheckout。但是错误Uncaught IntegrationError: stripe.redirectToCheckout: Invalid value for sessionId. You specified 'pi_1FrFbeBNGS...sCWFtQr'.
我试图定义一个json对象并使用session.id,但仍然无法正常工作。怎么了?
这是我的server.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)
});
这是我的index.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
);您需要将server.js中生成的session.id
发送/设置/渲染到HTML页面。
“您的html文件不包含会话ID-这是付款意向ID。您需要提供正确的ID才能使用此功能” @floatingLomas
这帮助了我。仪表板上的ID是付款意向ID。我试图从会话对象中打印出ID,然后将其放在directToPayment()中,并且可以正常工作!!