Introduction
A payment gateway is an indispensible part of any Android application. There are many payment gateways available in the market, but today we will learn about a very popular payment gateway known as Razorpay. Razorpay Android Standard SDK lets you easily integrate the Razorpay Payment Gateway with your Android application.
Prerequisites
- Create Razorpay account
- Generate test and live keys from dashboard. (Test keys are easy to test in that there will be no deduction of money.)
After creation of Razorpay account go to dashboard and generate a test key for testing purposes like shown in the image below.

The key will look like : rzp_test_LdI1ob5rGXZDF6
Integration Steps
Step 1 - Add Razorpay sdk to your build.gradle file
- repositories {
- mavenCentral()
- }
- dependencies {
- implementation 'com.razorpay:checkout:1.5.16'
- }
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- checkout.setKeyID("rzp_test_LdI1ob5rGXZDF6");
- // ...
- }
Step 3 - Create order on server (Optional)
You can use your backened custom order id. Create an order using Orders API in server. This generates an order_id when the customer places an order on your app. This order_id must be passed by you during checkout.
You can use your backened custom order id. Create an order using Orders API in server. This generates an order_id when the customer places an order on your app. This order_id must be passed by you during checkout.
- try {
- JSONObject orderRequest = new JSONObject();
- orderRequest.put("amount", 12000); // amount in the smallest currency unit
- orderRequest.put("currency", "INR");
- orderRequest.put("receipt", "order_rcptid_11");
- Order order = razorpay.Orders.create(orderRequest);
- } catch (RazorpayException e) {
- // Handle Exception
- System.out.println(e.getMessage());
- }
- {
- "id": "order_DBJOWzybf0sJbb",
- "entity": "order",
- "amount": 50000,
- "amount_paid": 0,
- "amount_due": 50000,
- "currency": "INR",
- "receipt": "rcptid_11",
- "status": "created",
- "attempts": 0,
- "notes": [],
- "created_at": 1566986570
- }
Step 4 - Inititate payment
Firstly we have created some xml to show what we are checking out -- let's see our xml file activity_main.xml
Firstly we have created some xml to show what we are checking out -- let's see our xml file activity_main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity">
- <ImageView
- android:id="@+id/imageview"
- android:layout_width="300dp"
- android:layout_height="300dp"
- android:src="@drawable/ic_tea"
- android:layout_centerInParent="true">
- </ImageView>
- <TextView
- android:id="@+id/textview"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Buy for 120rs"
- android:textSize="18sp"
- android:textStyle="bold"
- android:background="#90EE90"
- android:padding="12dp"
- android:layout_below="@id/imageview"
- android:layout_centerHorizontal="true"/>
- </RelativeLayout>
Now for the corresponding java part, see MainActivity.java
Here we are starting chekout activity on a button click see startPayment() method on a button click.
- package com.example.razorpaytest;
- import androidx.appcompat.app.AppCompatActivity;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.razorpay.Checkout;
- import com.razorpay.PaymentData;
- import com.razorpay.PaymentResultWithDataListener;
- import org.json.JSONObject;
- public class MainActivity extends AppCompatActivity implements PaymentResultWithDataListener {
- TextView buyButton;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- buyButton = findViewById(R.id.textview);
- Checkout.preload(getApplicationContext()); // For faster rendering of payment screen
- buyButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- startPayment();
- }
- });
- }
- public void startPayment() {
- // You need to pass current activity in order to let Razorpay create CheckoutActivity
- final Activity activity = this;
- final Checkout co = new Checkout();
- co.setKeyID("rzp_test_LdI1ob5rGXZDF6");
- try {
- // amount to be in paisa only
- int finalAmount = 120*100; // converting 12p rupees into paisa
- JSONObject options = new JSONObject();
- options.put("name", "RazorPayTestApp");
- options.put("description", "Test Payment");
- //You can omit the image option to fetch the image from dashboard
- // options.put("image", "https://s3.amazonaws.com/rzp-mobile/images/rzp.png");
- options.put("currency", "INR");
- options.put("amount", String.valueOf(finalAmount));
- // options.put("order_id", order_DBJOWzybf0sJbb);//from response of step 3.
- JSONObject preFill = new JSONObject();
- preFill.put("email", "[email protected]");
- preFill.put("contact", "858681XXXX");
- JSONObject notes = new JSONObject();
- notes.put("Product_details","Here are the notes");
- options.put("prefill", preFill);
- options.put("notes",notes);
- co.open(activity, options);
- } catch (Exception e) {
- Toast.makeText(activity, "Error in payment: " + e.getMessage(), Toast.LENGTH_SHORT)
- .show();
- e.printStackTrace();
- }
- }
- @Override
- public void onPaymentSuccess(String id, PaymentData paymentData) {
- Toast.makeText(this, "Payment Success with id : "+id, Toast.LENGTH_SHORT).show();
- }
- @Override
- public void onPaymentError(int i, String response, PaymentData paymentData) {
- Toast.makeText(this, response, Toast.LENGTH_SHORT).show();
- }
- }
Step 5 - Handle success and error events
You can see the full code above to get better idea of what we are doing in success and error listeners.
- public class MainActivity extends AppCompatActivity implements PaymentResultWithDataListener {
- @Override
- public void onPaymentSuccess(String razorpayPaymentID) {
- /**
- * Add your logic here for a successful payment response
- */
- }
- @Override
- public void onPaymentError(int code, String response) {
- /**
- * Add your logic here for a failed payment response
- */
- }
- }
Running application
Now tap on Buy button for 120 rupees.
Now tap on Pay button and you will get confirmation.
Now tap on success -- this screen will not be shown on live payments.
Payment confirmation

Now tap on Buy button for 120 rupees.

Now tap on Pay button and you will get confirmation.

Now tap on success -- this screen will not be shown on live payments.

Payment confirmation

Summary
In this article we have learnt about the integration of Razorpay. This is the easiest way one can integrate the payment sdk. Once you sign up and add your details over it you can capture money from right there.

LIJU DANIELPosted May 29, 2021, 5:46 PM
How to add proguard rules for this razor pay dll