stripe / stripe-terminal-react-native

React Native SDK for Stripe Terminal
https://stripe.com/docs/terminal/payments/setup-integration?terminal-sdk-platform=react-native
MIT License
104 stars 50 forks source link

Reinitialize Stripe #369

Closed mvincent7891 closed 2 years ago

mvincent7891 commented 2 years ago

It is sometimes necessary in development as well as in production to switch the location of a device connected to a Stripe Terminal. This is mostly useful in development, but could be relevant in our production app as well for instance when training staff across locations.

When this happens within the context of the app, I would like to re-initialize Stripe with the new tokenProvider, which requests a connection token for the new location. So far, I'm unable to figure out a way to handle the location change without completely restarting the app.

A great solution would just be a reinitialize() callback returned from useStripeTerminal(), or at least an automatic reinitialization if a change is detected for tokenProvider.

An alternative would just be to refetch the connection token if tokenProvider changes.

jdivock-stripe commented 2 years ago

Hey @mvincent7891 thanks for reaching out. If you look at the dev-app in our repo we have that set up to switch merchants on the fly for our own internal testing. While there are a number of patterns in that app you'd never want to deploy to production it does outline how to go about it. I think the key piece you're missing is a call to clearCachedCredentials when switching to a new merchant. You can see our call and surrounding logic here. Let me know how that works out for you!

mvincent7891 commented 2 years ago

That's perfect, will try that and report back. Thanks @jdivock-stripe!

mvincent7891 commented 2 years ago

Works great!

zameschua commented 1 month ago

Hey @jdivock-stripe, sorry for commenting on such an old issue.

In the code example above, why is it calling clearCachedCredentials() right after initialize()?

The usage is pretty different from steps is described in the docs https://docs.stripe.com/terminal/references/api/js-sdk#clear-cached-credentials