I was trying to use Stripe checkout. I generated session and then it showed up on the dashboard. I then tried to run directocheckout on an html file with hardcoded id. But errorUncaught IntegrationError: stripe.redirectToCheckout: Invalid value for sessionId. You specified 'pi_1FrFbeBNGS...sCWFtQr'.
I tried to define an json object and use session.id but still doesn't work. What is wrong?
Here is my 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)
});
here is my 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>
the full error
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
It looks like you're passing your id
and not the session.id
(though I can't see where you would be getting the session
from in your case here); you'll need to send/set/render that session.id
produced in server.js to your html page.
"Your html file does not contain a Session ID - that's a Payment Intent ID. You need to include the correct ID to make this work" @floatingLomas
this helped me. The id on the dashboard is payment intent ID. I tried to print out the ID from the session object and put it in the directToPayment() and it worked!!!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With