stripe / stripe-js

Loading wrapper for Stripe.js
https://stripe.com/docs/js
MIT License
633 stars 157 forks source link

[BUG]: StripeEmbeddedCheckout.destroy cannot safely be called multiple times and no way to check a destroyed property #685

Open davidbielik opened 3 days ago

davidbielik commented 3 days ago

What happened?

@stripe/stripe-js": "^3.0.3

My integration has two logical places where I destroy an embedded checkout instance (if a user goes to another page, or if a user clicks to go back to pricing plans and hide the embedded checkout).

I call instance.destroy() and create new instances which works as expected, but calling destroy twice results in an error message.

It would be great if you allowed calling destroy on an already destroyed instance. If not possible for some reason, it would be helpful to have access to the embeddedCheckout.isDestroyed property in the typescript definitions somehow.

I was able to find a workaround (not ideal passing more state around) but would have saved time if this worked more seamlessly.

const instance = await stripe?.initEmbeddedCheckout({clientSecret});
instance?.destroy();
instance?.destroy(); // throws an error 
{
    "message": "This Embedded Checkout instance has been destroyed and can't be used anymore. Please create a new one.",
    "name": "IntegrationError"
}

Environment

No response

Reproduction

No response

brendanm-stripe commented 1 day ago

Hmm I can see how that would be convenient for your flow, though for how I've pictured your integration tracking the embedded Checkout state I think makes sense. Does it also work to wrap your call to destroy in a try/catch?

davidbielik commented 1 day ago

Thanks, yes that works. Just wanted to pass along the (minor) feedback, as this was an area that caused confusion. I assumed I could safely call instance?.destroy() any time I wanted to call initEmbeddedCheckout.