About the Stripe React Native migration guide
If you have ever integrated payments with React Native, you may have encountered the popular community library “tipsi-stripe“. This project has been unmaintained for several years and the README now suggests migrating to the official Stripe React Native project.
To better support developers looking to migrate from “tipsi-stripe” to stripe-react-native, we’ve prepared The Stripe React Native Migration Guide to walk you through the core concepts and integration steps.
This article is the first part of the guide in which we go through the process of migrating from tipsi-stripe to stripe-react-native.
The guide is divided into five parts in which you'll learn about the following aspects of the migration process:
- Part 1 - How to migrate from Tipsi Stripe to the Stripe React Native SDK
- Part 2 - How to configure Stripe React Native
- Part 3 - A detailed description of Stripe React Native migration function by function (you are here)
- Part 4 - How not to break your app after migrating from Tipsi Stripe to Stripe React Native
- Part 5 - A collection of the most common questions related to Stripe React Native
Introduction
This article is part 3 of our ongoing series on migrating from Tipsi-Stripe to stripe-react-native. Make sure to read the previous articles on understanding stripe react native and configuring the stripe-react-native library.
Are you migrating your app’s payments logic from tipsi-stripe to stripe-react-native? In this article, we cover how to migrate each payment type and function you might use. As you could see in the previous posts, the initial setup of stripe-react-native is simple and intuitive. Today, we will focus on each payment function in tipsi-stripe and describe how to migrate it to stripe-react-native.
Card components
One of the essential features of using Stripe is collecting the card details in order to process a payment. Tipsi-stripe has a PaymentCardTextField component and onParamsChange prop that is used to collect card details. Collected card details have to be stored in a state in order to use them in card payment.
With stripe-react-native there is no need to collect card data manually. The library does that for you. It makes it both easy to use and much more secure. Just render CardField every time you want to use card-related payment.
Before (tipsi-stripe):
After (stripe-react-native):
In the above code examples, the placeholder information is now a single object called placeholder. In addition, basic component styles can be modified using single cardStyle prop instead of multiple ones (cursorColor, placeholderColor, etc.).
Another difference with the CardField component is the set of methods on its ref. Tipsi has blur, focus, isFocused and iOS-only setParams. First two are exactly the same in stripe-react-native. However, in order to know if card input is focused, you should use the onFocus prop and handle this information declaratively instead of calling an imperative isFocused function. Setting params are not available in stripe-react-native but we believe that since this is not supported by both platforms there is no need for that in React Native applications. This decision together with onCardChange prop which sends only the last four digits instead of the whole card number improves the security of the CardField component.
NOTE: If your app requires access to the entire card number it is still possible with stripe-react-native. You can set dangerouslyGetFullCardDetails to true in order to have a full card number returned in the onCardChange handler. Keep in mind this is not recommended since your customers' information must be protected at all costs.
Accepting a payment
With PaymentIntent API there are some significant differences between libraries. The biggest one is that in stripe-react-native for PCI compliance reasons all of the sensitive data is sent to the specific methods under the hood and you don’t need to hand it over on your own. This means that in order to proceed with a Card payment, you have to collect the data using either CardField or CardForm component provided by stripe-react-native library. In this way, the personal data is secure, as it is kept confidential from developers.
Create a payment method
In both libraries for creating a payment method you can use createPaymentMethod function. If it comes to card payments main difference was described above - instead of passing card details now you just have to set the type param to "Card" and be sure that CardField component is rendered on your screen.
Before (tipsi-stripe):
After (stripe-react-native):
In both libraries provide billing details using the billingDetails field which contains address, email, name, and phone. See the api reference for more details.
NOTE: If you are using token to provide card details to createPaymenMethod you can still do this. The only difference is the way of generating it. More details you will find later in this article.
Confirm payment intent (for manual payment flows)
If your payment requires confirmation, you have to call a specific function in order to launch an activity where the user can authenticate the payment. For manual payment flow, there is an authenticatePaymentIntent method in tipsi-stripe and the equivalent in stripe-react-native is handleCardAction.
Before (tipsi-stripe):
After (stripe-react-native):
In the above example, you can see that clientSecret is now a separate and the first argument of the function. This new implementation, just as in Tipsi, returns a Promise with payment intent details. In Tipsi, you could access there intent id and its status, stripe-react-native returns an error and the whole PaymentIntent object.
NOTE: It is worth mentioning that in error scenarios stripe-react-native always resolves promise and returns the error instead of rejecting it.
Confirm payment intent (for automatic payment flows)
For automatic confirmation flows, you just need to change the way you use confirmPaymentIntent method. In stripe-react-native function name was changed to confirmPayment but the main difference is that instead of passing client secret and payment details in a single object you need to pass them as two separate function arguments.
As with other payment methods, now there is no need to pass card details manually. You just need to specify the payment type and everything else will be collected automatically.
Before (tipsi-stripe):
After (stripe-react-native):
See confirmPayment for more details about possible configuration and different payment types.
Future payments
In tipsi-stripe to set up recurring payments or to create one-time payments with a final amount determined later, there is a confirmSetupIntent method. stripe-react-native has the same function but with a slightly changed API. The differences here are almost the same as with confirmPaymentIntent function. Now you pass client secret as a first function argument and there is no need for card details.
Before (tipsi-stripe):
After (stripe-react-native):
Create a token
Stripe provides a way to convert information collected by card components into a single-use token that you safely pass to your server to use in an API call.
Tipsi has a createTokenWithCard function for that. It differs from the new createToken only in the way how to pass card data and the fact that it doesn't require to manually pass card details.
Before (tipsi-stripe):
After (stripe-react-native):
Remember that as with other card related functions, to use createToken function you have to render CardField component .
NOTE: tipsi-stripe provides also a way to create a bank account token using createTokenWithBankAccount function. This is not yet supported by stripe-react-native but should be ready the upcoming release. This functionality will be added as a part of createToken function.
ApplePay
deviceSupportsNativePay
Before displaying Apple Pay as a payment option in your app, first determine if the user’s device supports Apple Pay and that they have a card added to their wallet. In order to this stripe-react-native provides isApplePaySupported boolean value from useApplePay hook.
Before (tipsi-stripe):
After (stripe-react-native):
NOTE: If your application doesn't use functional components, as an alternative you can import isApplePaySupported method directly from the library.
paymentRequestWithNativePay
Requesting payment in Tipsi can be done with paymentRequestWithNativePay function. It takes a list of items and an options object with shipping details and other properties. After migration, you should use presentApplePay function and pass all needed details as a single configuration object. Please refer to the documentation for all available properties.
Before (tipsi-stripe):
After (stripe-react-native):
Remember that the last item in items array needs to reflect the total value.
completeNativePayRequest
To process payment in Tipsi after requesting it, it is required to call completeNativePayRequest function. After migration there is a confirmApplePayPayment function for that. This and every other ApplePay related function you can get from useApplePay hook or import directly from the library.
Before (tipsi-stripe):
After (stripe-react-native):
GooglePay
As against to tipsi-stripe, stripe-react-native provides a separate API for GooglePay. In Tipsi, you could use the same functions as for ApplePay (see above) but with stripe-react-native there is a need to (first) initialize GooglePay and then (second) present it using presentGooglePay function.
Before (tipsi-stripe):
After (stripe-react-native):
After migrating, you can also create a payment method instead of confirming the payment. For that createGooglePayPaymentMethod can be used. Please refer to the documentation for more details.
CardForm
In Tipsi there is a paymentRequestWithCardForm function that opens the Add Card view to accept a payment. Unfortunately, this is not supported in stripe-react-native. In order to collect card details please use CardField component or confirmPaymentSheetPayment functionality for a unify Payment UI experience.
Example
So looks like migration is not that scary anymore and it is quite doable, right? Show your work - as they say. Fortunately, I have an example and a proof for you.
There's an example PR with a Tipsi to stripe-react-native migration. This is a migration of a project with:
- Card payment
- Apple Pay usage
- Google Pay usage

Summary
These migration steps described above show these two libraries are noticeable and require you to refactor your app’s code. But remember, after you start the migration process you will see that the differences are not too big. You should be able to migrate almost everything quickly.
In the upcoming articles, we will be covering the testing part and answering most common questions. Thanks for reading and stay tuned!
Resources:
- SDK reference
- Repository
- Official documentation
- Callstack React Native articles
You can find all services offered by our React Native development company in the Services section.