dayhaysoos / use-shopping-cart

Shopping cart state and logic for Stripe
MIT License
907 stars 118 forks source link

redirectToCheckout is not redirecting. #333

Closed yagasta closed 1 year ago

yagasta commented 1 year ago

brave_kAy0OZ9fVy

backend

app.post("/checkout", async (req, res) => {
  try {
    const cartItems = req.body;
    const sanityData = await sanityClient.fetch(
      '*[_type in ["games", "gears"]]{"name":title, "id": _id, price, "poster": poster.asset->url, currency }'
    );

    let line_items = validateCartItems(sanityData, cartItems);

    const params = {
      submit_type: "pay",
      mode: "payment",
      payment_method_types: ["card"],
      billing_address_collection: "auto",
      line_items,
      success_url: `http://127.0.0.1:5173/`,
      cancel_url: `http://127.0.0.1:5173/`,
    };

    const session = await stripe.checkout.sessions.create(params);
    res.status(200).json(session);
  } catch (error) {
    res.status(500).json({
      message: "While communicating with Stripe, we encountered an error.",
      error: error.message,
    });
  }
});

I was able to solve it by replacing redirectToCheckout with window.location = response.url. Is there any downside to doing it this way?

yagasta commented 1 year ago

Ahh... I forgot change the successUrl in CartProvider component.