dayhaysoos / use-shopping-cart

Shopping cart state and logic for Stripe
MIT License
919 stars 119 forks source link

redirectToCheckout expects an optional string, some examples outdated #323

Open ciruz opened 1 year ago

ciruz commented 1 year ago

Hey,

redirectToCheckout expects an optional string https://github.com/dayhaysoos/use-shopping-cart/blob/275cb65f3d2fccb15857f4b9278cbbfca11eaf7a/use-shopping-cart/react/index.d.ts#L65

But some examples calls the function with an object: https://github.com/dayhaysoos/use-shopping-cart/blob/275cb65f3d2fccb15857f4b9278cbbfca11eaf7a/examples/vite-react/src/components/price-product.jsx#L24

dayhaysoos commented 1 year ago

Hey @ciruz, I'ma be honest, Stripe has deprecated redirectToCheckout in favor of using a Checkout Object instead:

https://stripe.com/docs/payments/checkout/how-checkout-works.

I think I should work towards removing redirectToCheckout from the library or at least warning people that it's deprecated and providing more updated examples. Thank you for flagging this, going to try and make an effort for this in about 2 weeks.

ciruz commented 1 year ago

Hey @dayhaysoos, thank you for the information. Yeah this sounds like a very good idea.

dayhaysoos commented 1 year ago

@ciruz sweet! Yeah I'm going to make an issue for this and after 2 weeks I'll work towards getting it done and providing more updated examples

milindgoel15 commented 1 year ago
Error:  PropertyTypeError: Invalid value with type "undefined" was received for stripe. Valid type for stripe is "string".

Just to confirm, when using redirectToCheckout in the latest version of useShoppingCart, it will give this error?

dayhaysoos commented 1 year ago

@milindgoel15 can you show me how you have your CartProvider set up?

milindgoel15 commented 1 year ago
let stripeKey: string = process.env.STRIPE_PUBLISHABLE_KEY as string;
                        <CartProvider 
                                 stripe={stripeKey} 
                                 cartMode="checkout-session" 
                                 currency="USD" 
                                 shouldPersist 
                         > 
                                 <style jsx global>{` 
                                         html { 
                                                 font-family: ${inter.style.fontFamily}; 
                                         } 
                                 `}</style> 
                                 <Header /> 
                                 <Component {...pageProps} /> 
                                 <Toaster /> 
                                 <Footer /> 
                         </CartProvider>
dayhaysoos commented 1 year ago

@milindgoel15 How are you using redirectToCheckout?

If you're doing it with an API function, you won't have access to it since the Provider isn't wrapped around the server functions.

I think this week I can spend time on updating to remove redirect to checkout altogether tbh, since it seems to be deprecated

milindgoel15 commented 1 year ago

How are you using redirectToCheckout?

It is inside a custom component here which is imported in the main cart page.

dayhaysoos commented 1 year ago

Yeah it could be possible that it doesn't get returned from the Stripe object anymore. USC uses your Stripe API key to initialize stripe and you get back redirectToCheckout from there.

https://stripe.com/docs/js/deprecated/redirect_to_checkout

I didn't know that it stopped being returned at this point. You're going to have to use a server function of some sort to handle this. I need to update use-shopping-cart docs to reflect these changes, I'm sorry!

milindgoel15 commented 1 year ago

Worked using nextjs router.

router.push(session.url);

ajesuscode commented 11 months ago

Hello. Do you have any updates on this issue? As it seems to be in the docs