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'
- }
Step 2 - Initialize the key id
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
- checkout.setKeyID("rzp_test_LdI1ob5rGXZDF6");
-
-
- }
We will see the full code later on.
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.
- try {
- JSONObject orderRequest = new JSONObject();
- orderRequest.put("amount", 12000);
- orderRequest.put("currency", "INR");
- orderRequest.put("receipt", "order_rcptid_11");
-
- Order order = razorpay.Orders.create(orderRequest);
- } catch (RazorpayException e) {
-
- System.out.println(e.getMessage());
- }
In Response you will get order id like below
- {
- "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
- }
The above steps are optional we will see the payment checkout without order id.
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
- <?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
- 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());
-
-
- buyButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
-
- startPayment();
-
- }
- });
- }
-
-
- public void startPayment() {
-
-
-
- final Activity activity = this;
-
- final Checkout co = new Checkout();
- co.setKeyID("rzp_test_LdI1ob5rGXZDF6");
- try {
-
-
-
-
-
- int finalAmount = 120*100;
-
- JSONObject options = new JSONObject();
- options.put("name", "RazorPayTestApp");
- options.put("description", "Test Payment");
-
-
- options.put("currency", "INR");
- options.put("amount", String.valueOf(finalAmount));
-
-
-
- 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();
- }
- }
Here we are starting chekout activity on a button click see startPayment() method on a button click.
Step 5 - Handle success and error events
- public class MainActivity extends AppCompatActivity implements PaymentResultWithDataListener {
- @Override
- public void onPaymentSuccess(String razorpayPaymentID) {
-
-
-
- }
-
- @Override
- public void onPaymentError(int code, String response) {
-
-
-
- }
- }
You can see the full code above to get better idea of what we are doing in success and error listeners.
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
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.