#rk0660-react-cart
1 messages · Page 1 of 1 (latest)
Hello. This isn't really a Stripe issue. Have you attempted to create a handler for your form submission which will make the fetch request?
There's an example here: https://github.com/stripe-samples/accept-a-payment/blob/main/custom-payment-flow/client/react-cra/src/Card.js
Hi thanks - I did put a onSubmit={handleSubmit} on my form and got rid of the action attribute, then in the handleSubmit function I sent a post fetch request to the endpoint
I did it last night but the stripe checkout page never loaded
This was my code yesterday
I'm trying to pass in custom data to my node.js backend when the user wants to checkout, but clicking my button doesn't take me to the checkout page.
const Cart = () => {
const cart = useSelector(state => state.cart);
const handleStripe = async (e) => {
e.preventDefault();
const { url } = (await fetch("http://localhost:5000/api/stripe/checkout", {
method: "POST",
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
products: cart.products,
}),
})).json()
window.location.href(url)
}
return (
<Container>
<Form onSubmit={handleStripe>
<SummaryButton> CHECKOUT NOW</SummaryButton>
</Form>
</Summary>
</Bottom>
</Wrapper>
<Footer />
</Container>
)
}
@wintry atlas
How are you handling the redirect?
Sorry what do you mean by redirect?
That’s all the code I had in my front end, in my back end I followed the stripe docs example of router.post(‘/checkout’, etc)
Can you share your backend code?
To send users to the Checkout page (via the url field)
Sure, I will be home in 15 minutes I’ll immediately copy my backend code here
My guess is you had a server redirect (res.redirect(url)) when you used the <form> action. That won't work with fetch, so you'll need to implement a client-side redirect, either via redirectToCheckout from Stripe.js or with window.location.href()
const { url } = await (await fetch("http://localhost:5000/api/stripe/checkout", {
method: "POST",
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
products: cart.products,
}),
})).json()
window.location.href(url)
Here is my backend code
router.post("/checkout", async (req, res) => {
const session = await stripe.checkout.sessions.create({
line_items: [
{
price_data: {
currency: 'usd',
product_data: {
name: 'T-shirt',
},
unit_amount: 2000,
},
quantity: 1,
},
{
price_data: {
currency: 'usd',
product_data: {
name: 'Mug',
},
unit_amount: 500,
},
quantity: 1,
}
],
mode: 'payment',
success_url: 'http://localhost:3000/',
cancel_url: 'http://localhost:3000/cart',
});
res.redirect(303, session.url);
});
module.exports = router;
Thanks I will give that a try right now!
So it's a bit better, the problem is the page just reloads now when I click my button. I'll update my frontend and backend in the code above
the problem is the page just reloads now when I click my button.
that usually means you don't callevent.preventDefault()in the event handler function
though seems like you have that
Oh no that's not it, I have it
Oh its a cors issue
I have require cors and app.use(cors()); in my backend, do i somehow need it in frontend too?
I think the issue is maybe with what ynnoj shared, try window.location.replace(url) instead
Nope, it still gives
Access to fetch at 'https://checkout.stripe.com/pay/cs_test_b1uW0PTCbSMtLCdWTWIM0SfIFQWXZUsn0bjxGxxiIDiNuTpJtYgqypyoUH#fidkdWxOYHwnPyd1blpxYHZxWjA0TlBjPHJMN2NTNXZnNVZyNmp9a0phczVibWFmRG80ajJjYHVrMnY3MEtfV1xiaTxTb1RvMD1cSTJyXzR3U05fdEw8ZlMzNzNyT3xnPExKN0duUnZsZl1xNTVjbmJoa0BQVScpJ2N3amhWYHdzYHcnP3F3cGApJ2lkfGpwcVF8dWAnPydocGlxbFpscWBoJyknYGtkZ2lgVWlkZmBtamlhYHd2Jz9xd3BgeCUl' (redirected from 'http://localhost:5000/api/stripe/checkout') from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
Should I make my headers headers: {'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*'},
yeah this is what ynnoj said
its' because you return res.redirect(303, session.url); on the backend
which you can't do
Ohh sorry I didn't see that part I just followed the stripe docs for that I think
it should be something like res.json({url:session.url})
yeah but the docs assume you have a simple form action
Ohhh ok, what you two suggested got it to work. Thank you very much! Out of curiousity, I saw there is a new way of doing checkout with 'elements'? Is that something easy to implement without many changes or
is it a whole different implementation