Saturday, November 2, 2019

How to Integrate Razorpay Payment Gateway in Android

Razorpay is one of the most widely used payment gateways in India for Android applications.

It supports:

  • UPI payments
  • Credit cards
  • Debit cards
  • Net banking
  • Wallet payments

Razorpay integration is fast, developer-friendly, and requires minimal code for basic payment implementation.

In this tutorial, we will learn how to integrate Razorpay Payment Gateway in Android using the official Razorpay Android SDK.


Features of Razorpay Android SDK

  • Fast payment integration
  • Multiple payment methods
  • UPI support
  • Secure payment processing
  • Custom branding support
  • Production-ready checkout UI

Requirements

  • Minimum SDK 19 or higher
  • Android Studio
  • Internet connection
  • Razorpay account

Step 1 — Create New Android Project

Open Android Studio and create a new Android project.

  1. Click File → New Project
  2. Select Empty Activity
  3. Choose Java or Kotlin
  4. Finish project setup

Step 2 — Add Razorpay Dependency

Open build.gradle and add:


dependencies {

    implementation 'com.razorpay:checkout:1.6.33'
}

Then sync the Gradle project.


Step 3 — Add Permissions and API Key

Open AndroidManifest.xml.

Add Internet permission:


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

Add Razorpay API Key

Inside the <application> tag:


<meta-data
    android:name="com.razorpay.ApiKey"
    android:value="YOUR_RAZORPAY_KEY"/>

Replace:


YOUR_RAZORPAY_KEY

with your actual Razorpay API key.


Step 4 — Create activity_checkout.xml

Create layout file inside:


res/layout/activity_checkout.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:padding="16dp">

    <EditText
        android:id="@+id/editTextAmount"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter Amount"
        android:inputType="numberDecimal"/>

    <Button
        android:id="@+id/buttonPay"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Pay Now"
        android:layout_marginTop="16dp"/>

</LinearLayout>

Step 5 — Create CheckoutActivity.java

Create:


CheckoutActivity.java

package com.example.razorpayexample;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.razorpay.Checkout;
import com.razorpay.PaymentResultListener;

import org.json.JSONObject;

public class CheckoutActivity
        extends AppCompatActivity
        implements PaymentResultListener {

    private EditText editTextAmount;

    private Button buttonPay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_checkout);

        editTextAmount =
                findViewById(R.id.editTextAmount);

        buttonPay =
                findViewById(R.id.buttonPay);

        buttonPay.setOnClickListener(
                new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {

                        if (editTextAmount.getText()
                                .toString()
                                .trim()
                                .isEmpty()) {

                            Toast.makeText(
                                    CheckoutActivity.this,
                                    "Enter payment amount",
                                    Toast.LENGTH_SHORT
                            ).show();

                            return;
                        }

                        startPayment();
                    }
                });
    }

    private void startPayment() {

        final Activity activity = this;

        final Checkout checkout =
                new Checkout();

        try {

            JSONObject options =
                    new JSONObject();

            options.put(
                    "name",
                    "CodeChain Dev"
            );

            options.put(
                    "description",
                    "Demo Payment"
            );

            options.put(
                    "currency",
                    "INR"
            );

            String payment =
                    editTextAmount
                            .getText()
                            .toString();

            double total =
                    Double.parseDouble(payment);

            total = total * 100;

            options.put("amount", total);

            JSONObject prefill =
                    new JSONObject();

            prefill.put(
                    "email",
                    "example@gmail.com"
            );

            prefill.put(
                    "contact",
                    "9999999999"
            );

            options.put(
                    "prefill",
                    prefill
            );

            checkout.open(activity, options);

        } catch (Exception e) {

            Toast.makeText(
                    activity,
                    "Payment Error: "
                            + e.getMessage(),
                    Toast.LENGTH_SHORT
            ).show();

            e.printStackTrace();
        }
    }

    @Override
    public void onPaymentSuccess(
            String razorpayPaymentId
    ) {

        Toast.makeText(
                this,
                "Payment Successful",
                Toast.LENGTH_SHORT
        ).show();
    }

    @Override
    public void onPaymentError(
            int code,
            String response
    ) {

        Toast.makeText(
                this,
                "Payment Failed",
                Toast.LENGTH_SHORT
        ).show();
    }
}

How Razorpay Payment Flow Works

The workflow is:

  1. User enters amount
  2. User clicks Pay button
  3. Razorpay Checkout opens
  4. User completes payment
  5. Payment callback returns success or failure

Understanding Razorpay Amount System

Razorpay expects payment amount in:


Paise

Example:

INR Paise
1 INR 100
500 INR 50000

That is why:


total = total * 100;

Common Mistakes Developers Make

1. Using Wrong API Key

Always use:

  • Test key for development
  • Live key for production

2. Forgetting Internet Permission

Without Internet permission, Razorpay Checkout cannot connect to servers.


3. Trusting Frontend Success Only

Frontend payment success is NOT enough for production applications.


Important Production Security Recommendations

Production applications should ALWAYS:

  • Verify payments on backend
  • Validate Razorpay signature
  • Store transaction logs securely
  • Prevent duplicate transactions
  • Use HTTPS APIs

Modern Android Improvements

Modern Android applications can improve this integration using:

  • Kotlin
  • MVVM Architecture
  • Jetpack Compose
  • Coroutines
  • Retrofit API integration
  • Secure backend validation

Razorpay vs UPI Intent Integration

Razorpay UPI Intent
Supports multiple payment methods Mainly UPI only
Professional checkout UI Basic app switching
Backend payment ecosystem Lightweight integration

FAQ

Does Razorpay support UPI?

Yes. Razorpay supports UPI payments along with cards, wallets, and net banking.

Can I test payments without real money?

Yes. Razorpay provides test mode with test credentials.

Is backend verification necessary?

Yes. Production systems must verify payment signatures securely on backend.


Conclusion

Razorpay provides one of the simplest ways to integrate secure payment functionality into Android applications.

Its SDK reduces development complexity while supporting multiple payment methods through a professional checkout experience.

For production applications, developers should combine frontend payment flow with secure backend verification systems.


About the Author

Salil Jha is a Full Stack and Mobile Developer specializing in Android, React Native, fintech systems, scalable SaaS platforms, and payment integrations.

CodeChain Dev — Build Modern Products. Solve Real Problems.

No comments:

Post a Comment