Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to Integrate Razorpay Payment Gateway in Android?

  • Difficulty Level : Easy
  • Last Updated : 31 Jan, 2021

Many apps nowadays require to have a payment gateway inside their application so that users can do any transactions inside their apps to purchase any product or any service. Many apps use the payment gateway features but the integration of this payment gateway is a difficult task in Android applications. So to make this task simple and easy Razorpay have provided a service with the help of this we can integrate the payment solutions in our app very easily and we can also manage all payment methods in our app. In this article, we will take a look at the implementation of a payment gateway in our Android app. 

What we are going to build in this article? 

We will be building a simple Android application in which we will be displaying an EditText and a button. Inside this screen, we have to add the amount which is to be paid and on clicking the button we will open the Razorpay payment gateway and will make a payment. In this article, we will be adding test credentials for implementing Razorpay in Android. A sample video is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language. 

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.

Step 2: Add dependency of Razor pay library in build.gradle file

Navigate to the Gradle Scripts > build.gradle(Module:app) and add the below dependency in the dependencies section.   

implementation ‘com.razorpay:checkout:1.6.4’

 After adding this dependency sync your project and now we will move towards the XML part. 

Step 3: Adding permissions to the Internet

Navigate to the app > AndroidManifest.xml file and add the below code to it. 

XML




<uses-permission android:name="android.permission.INTERNET" />

Step 4: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <!--EditText text to enter amount-->
    <EditText
        android:id="@+id/idEdtAmount"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_margin="20dp"
        android:hint="Enter amount to be payed"
        android:inputType="number" />
      
    <!--button to make payment-->
    <Button
        android:id="@+id/idBtnPay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/idEdtAmount"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="Pay using RazorPay"
        android:textAllCaps="false" />
  
</RelativeLayout>

Step 5: Generating an API key for using Razorpay

Browser the Razorpay site in Google or you can click on the link here. After clicking on this link you simply have to signup with your email and password and add some basic information such as your phone number. 

Note: Here we are creating a testing credential for using Razor Pay. 

Inside the setting screen, click on Create a new key option your key will be generated. We will be using key ID in our application to test Razor pay. The key-id will start with rzp_test 

Step 6: Working with the MainActivity.java file

Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.

Java




import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
  
import androidx.appcompat.app.AppCompatActivity;
  
import com.razorpay.Checkout;
import com.razorpay.PaymentResultListener;
  
import org.json.JSONException;
import org.json.JSONObject;
  
public class MainActivity extends AppCompatActivity implements PaymentResultListener {
  
    // variables for our 
    // edit text and button.
    private EditText amountEdt;
    private Button payBtn;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
          
        // initializing all our variables.
        amountEdt = findViewById(R.id.idEdtAmount);
        payBtn = findViewById(R.id.idBtnPay);
          
        // adding on click listener to our button.
        payBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // on below line we are getting 
                // amount that is entered by user.
                String samount = amountEdt.getText().toString();
                  
                // rounding off the amount.
                int amount = Math.round(Float.parseFloat(samount) * 100);
                  
                // initialize Razorpay account.
                Checkout checkout = new Checkout();
                  
                // set your id as below
                checkout.setKeyID("Enter your key id here");
                  
                // set image
                checkout.setImage(R.drawable.gfgimage);
                  
                // initialize json object
                JSONObject object = new JSONObject();
                try {
                    // to put name
                    object.put("name", "Geeks for Geeks");
                     
                    // put description
                    object.put("description", "Test payment");
                     
                    // to set theme color
                    object.put("theme.color", "");
                      
                    // put the currency
                    object.put("currency", "INR");
                      
                    // put amount
                    object.put("amount", amount);
                      
                    // put mobile number
                    object.put("prefill.contact", "9284064503");
                      
                    // put email
                    object.put("prefill.email", "chaitanyamunje@gmail.com");
                      
                    // open razorpay to checkout activity
                    checkout.open(MainActivity.this, object);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
  
    @Override
    public void onPaymentSuccess(String s) {
        // this method is called on payment success.
        Toast.makeText(this, "Payment is successful : " + s, Toast.LENGTH_SHORT).show();
    }
  
    @Override
    public void onPaymentError(int i, String s) {
        // on payment failed.
        Toast.makeText(this, "Payment Failed due to error : " + s, Toast.LENGTH_SHORT).show();
    }
}

Now run your app and see the output of the app.

Output:

As we are using test credentials so our payment will not be done. For making your payments live you have to make your application live in the Razorpay console and generate a new key. 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!