// Include Stripe.js in your HTML first (in or just before )
//
// JavaScript to redirect to Stripe Checkout
const stripe = Stripe('pk_test_pk_test_51RVLrEJ34DTeO0UX0i6vh0iUmnKmxa4mgtko74OME0BSbC5Nwg2DPVc5PVvAqqmdrJ4s4Z7oxaOmNbergIPI4inM00kdR3eCKJ'); // Replace with your actual public key
const checkoutButton = document.getElementById('checkout-button');
checkoutButton.addEventListener('click', () => {
fetch('/create-checkout-session', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
// You can pass any necessary data for your backend here
})
})
.then(response => response.json())
.then(session => {
return stripe.redirectToCheckout({ sessionId: session.id });
})
.then(result => {
if (result.error) {
alert(result.error.message);
}
})
.catch(error => {
console.error('Error:', error);
});
});