React Native Razorpay
Overview
In this article, we will learn how to add Razorpay payment gateway to our React Native app. Razorpay is a popular and secure payment gateway in India that supports various payment methods. We will use the react-native-razorpay package, which lets us use the Razorpay checkout UI and handle a payment event in our app. Continue reading the article to learn more about the payment checkout process and the package methods.
Introduction to react-native-razorpay Package
react-native-razorpay is a package that provides an easy way to integrate Razorpay payment gateway in React Native apps. It supports both iOS and Android platforms and exposes the native Razorpay SDKs to JavaScript.
react-native-razorpay allows you to open a checkout form in your app and collect payment details from your customers. It also supports various features such as custom UI, pre-filled fields, callback events, etc.
Prerequisites
To use react-native-razorpay in your React Native project, you need to have the following:
- A Razorpay account with an API key and a secret key. You can sign up for a free account at https://razorpay.com/ and get your keys from the dashboard.
- A React Native project with React Native version 0.60 or higher. You can create a new project using the command:
To create and set up a new Razorpay account API key and a secret key, you need to do the following steps:
- Go to https://razorpay.com/ and sign up for a free account with your email or Google account.
- Log into your dashboard with your credentials and select the mode (Test or Live) for which you want to generate the API key.
- Click on Settings and then click on API Keys.
- Click on Generate Test Key or Generate Live Key depending on your mode.
- The Client Id and Secret Key will appear. Copy these keys and click on OK.
- Now you can use these keys in your app to integrate with Razorpay payment gateway.
- You can find more details about how to generate API keys on Razorpay in their official documentation: https://razorpay.com/docs/payment-gateway/webhooks/
Required Installations
To install react-native-razorpay in your React Native project, you can use either npm or yarn:
If you are using Expo, you cannot use react-native-razorpay as it requires native modules. You need to eject your Expo project or use Expo Custom Development Client.
Important Linkings
To use react-native-razorpay in your app, you need to do some additional steps depending on your platform and use case.
iOS
For iOS, you need to do the following steps:
- Add the following line to your Podfile:
- Run the following command to install the pods:
- Add the following line to your Info.plist file:
- Add the following code to your AppDelegate.m file:
Android
For Android, you need to do the following steps:
- Add the following line to your android/app/build.gradle file:
- Add the following line to your android/settings.gradle file:
- Add the following line to your MainApplication.java file:
Custom UI
If you want to use a custom UI for your checkout form, you need to do the following steps:
- Create a custom layout file in your android/app/src/main/res/layout folder. For example, you can create a file named razorpay_custom_layout.xml with the following content:
- Add the following line to your android/app/src/main/res/values/strings.xml file:
Add the following line to your Info.plist file:
Setting Up the Payment Gateway With React Native App
To set up the React Native Razorpay payment gateway with your app, you need to do the following steps:
- Import react-native-razorpay in your app component:
- Create a function that opens the checkout form and handles the payment events:
In this function we will:
- Create an options object with payment details such as amount, currency, description, etc.
- Open the checkout form with the options object
- Handle payment success event
- Handle payment error event
- Handle external wallet selection
Call the function when you want to initiate the payment:
In the above code, we used a single method called open() that takes an options object as a parameter and opens the Razorpay React Native checkout UI in a webview. The options object contains various parameters that we can customize according to our needs. Some of the important parameters are:
- key: This is the API key that we get from our Razorpay account. We need to use the test key for testing purposes and the live key for production purposes.
- amount: This is the amount that we want to charge from the user in paise. For example, if we want to charge Rs. 100, we need to pass 10000 as the value.
- currency: This is the currency code that we want to use for the payment. For example, INR for Indian Rupee, USD for US Dollar, etc.
- name: This is the name of our app or business that will be displayed on the checkout UI.
- description: This is an optional description that we can provide for the payment.
- order_id: This is an optional parameter that we can use to link the payment to an order. An order is a Razorpay entity that represents a payment request with a fixed amount and currency. We can create an order using the React Native Razorpay API and pass its id as the value of this parameter. This will ensure that the payment amount and currency are consistent with the order and also enable us to track the payment status using the order id.
- prefill: This is an optional object that we can use to prefill some fields on the checkout UI such as email, contact, method, etc. This can improve the user experience and reduce the chances of errors.
- theme: This is an optional object that we can use to customize the appearance of the checkout UI such as color, image, etc.
The open method returns a promise that resolves with a response object if the payment is successful or rejects with an error object if the payment fails or is cancelled by the user. The response object contains some useful information about the payment such as id, order_id, signature, etc. The error object contains some details about the error such as code, description, source, etc.
We can also use the onExternalWalletSelection callback to handle the case when the user selects an external wallet such as Paytm, PhonePe, etc. This callback takes two parameters: walletName and paymentData. The walletName is the name of the wallet that the user selected and the paymentData is an object that contains some information about the payment such as id, order_id, signature, etc. We can use this callback as follows:
FAQs
Here are some frequently asked questions about adding Razorpay React Native payment gateway to your app.
Q. Can I use React Native Razorpay with Expo? A. No, react-native-razorpay requires native modules and cannot be used directly with Expo. You need to either eject your Expo project or use Expo Custom Development Client.
Q. How can I customize the UI of the Razorpay checkout form? A. To use a custom UI for the checkout form, you need to create a custom layout file in your Android project and add a corresponding line in your Android code. The layout file allows you to design and include your desired UI elements for the checkout form.
Q. How can I handle external wallet selection with Razorpay React Native? A. Razorpay provides the onExternalWalletSelection callback function that allows you to handle cases where the user selects an external wallet during the payment process, such as Paytm or PhonePe.
Conclusion
- The react-native-razorpay package supports both iOS and Android platforms and provides access to the native Razorpay SDKs.
- Before integrating React Native Razorpay, developers need to have a Razorpay account with API and secret keys, which can be obtained from the Razorpay dashboard.
- The article also explains how to customize the UI of the payment form and provides code examples for initiating payments and handling payment events.
- The open() method is used to open the checkout form and handle payment success, error, and external wallet selection events.
- The package returns a promise that resolves with a response object for successful payments or rejects with an error object for failed or cancelled payments.
- The onExternalWalletSelection callback can be used to handle cases where the user selects an external wallet for payment.