Android

Razorpay Payment Gateway Integration Android Tutorial

Razorpay Payment Gateway Integration.

  • In this Razorpay android tutorial, I will show you both live mode payment and test mode payment.
  • Razor pay payment gateway integration in the android app is very easy.
  • Razor pay accepts international payments.
  • Users can pay via credit card, debit card, internet banking, UPI payment, and from wallets.
  • The most important point is the commission rate.
  • As compared to other payment gateway commission rate on each transaction is less than others. So let’s start with Razorpay payment gateway integration in the android app.

 

Razorpay Payment Gateway Integration Step:-
  1. Add a dependency in the android app project.
  2. Meta-data key in the android manifest file
  3. Add Razor pay payment gateway in the activity.
  4. Razorpay test card details.

 

1)Add a dependency in the android app project.
  • I hope you have created a new account on the razor pay dashboard. if not please create Razor pay Account. By default TEST MODE is active, you can see it on top of the page. By clicking on the test mode button you can change it to LIVE MODE.
    //Add the dependency in android razorpay sdk
    implementation 'com.razorpay:checkout:1.6.6'

     

2)Add Meta-data:

  • Once you add the dependency, add the meta-data key in the android manifest file. Meta-data key is used to identify the merchant details. add the below code in the android manifest file and change the key with your account key. Please NOTE the meta-data tag should be inside the Application tag.

 

  • How to generate API KEY ?” On the left side menu bar, the last option is Setting. Inside setting there is multiple option available Configuration, Webhooks and API Keys. Generate Key to generate a key for the selected mode. The Key Id and Key Secret appear on the popup box. Please download the keys and keep them secret.

Android Manifest File:-

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.thecodehubs.razorpay">
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">l̥
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
         <!-- Add your API key here -->
        <meta-data
            android:name="com.razorpay.ApiKey"
            android:value="Add your API key here"/>
    </application>
</manifest>

3)Add Razorpay payment
  • I am using a simple layout file with an edit text box. You can set any amount in the edit text box to test the payment in run time. Add one button to start the payment process. Below the code layout file which I am using in this Razorpay android tutorial.

activity_payment.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#007C9E"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:orientation="vertical"
        android:gravity="center"
        >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="110dp"
            >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="80dp"
                android:layout_alignParentBottom="true"
                android:background="#E2E2E2"
                android:gravity="center"
                >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingTop="35dp"
                    android:paddingBottom="0dp"
                    android:textSize="12sp"
                    android:text="Order #RZP42"
                    />

            </LinearLayout>

            <ImageView
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:layout_centerHorizontal="true"
                android:background="#FFFFFF"
                android:src="@drawable/rzp"
                />

        </RelativeLayout>

        <LinearLayout
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:paddingTop="15dp"
            android:background="#f2f2f2"
            android:gravity="center"
            >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:paddingTop="0dp"
                android:paddingBottom="5dp"
                android:textSize="20sp"
                android:text="Razorpay T-Shirt"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:paddingTop="0dp"
                android:paddingBottom="0dp"
                android:textSize="12sp"
                android:text="INR 1.00"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:paddingTop="10dp"
                android:paddingBottom="0dp"
                android:textSize="12sp"
                android:textStyle="italic"
                android:layout_marginStart="16dp"
                android:layout_marginEnd="16dp"
                android:textColor="@android:color/holo_red_dark"
                android:gravity="center_horizontal"
                android:text="Transaction is for testing purpose and your deducted amount will get refunded in 3-5 business days "
                />

            <TextView
                android:id="@+id/txt_privacy_policy"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:paddingTop="10dp"
                android:paddingBottom="0dp"
                android:textSize="12sp"
                android:textStyle="italic"
                android:layout_marginStart="16dp"
                android:layout_marginEnd="16dp"
                android:textColor="@android:color/holo_red_dark"
                android:gravity="center_horizontal"
                android:text="Privacy Policy"
                />

            <Button
                android:id="@+id/btn_pay"
                android:layout_width="wrap_content"
                android:layout_height="36dp"
                android:layout_marginTop="20dp"
                android:layout_marginBottom="20dp"
                android:layout_gravity="center_horizontal"
                android:paddingRight="20dp"
                android:paddingLeft="20dp"
                android:background="@drawable/green_button"
                android:textSize="14sp"
                android:textColor="#fcfcfc"
                android:text="Purchase"
                />

        </LinearLayout>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:paddingTop="10dp"
            android:paddingBottom="10dp"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            android:background="@drawable/secured_by_bg"
            android:textSize="12sp"
            android:textColor="#a1ddee"
            android:textStyle="italic"
            android:text="Secure Payments by Razorpay"
            />

    </LinearLayout>
</RelativeLayout>

 

Now on PaymentActivity.java  implement payment result listener and override the methods. To start the razor pay payment, you need to create a JSON object which contains all the information about the payer and receiver like name, description, amount, email, contact, etc.

package com.thecodehubs.razorpay.java;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.razorpay.Checkout;
import com.razorpay.PaymentResultListener;
import com.razorpay.sampleapp.R;

import org.json.JSONObject;

public class PaymentActivity extends Activity implements PaymentResultListener {
    private static final String TAG = PaymentActivity.class.getSimpleName();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_payment);

        /*
         To ensure faster loading of the Checkout form,
          call this method as early as possible in your checkout flow.
         */        Checkout.preload(getApplicationContext());

        // Payment button created by you in XML layout
        Button button = (Button) findViewById(R.id.btn_pay);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startPayment();
            }
        });

        TextView privacyPolicy = (TextView) findViewById(R.id.txt_privacy_policy);

        privacyPolicy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent httpIntent = new Intent(Intent.ACTION_VIEW);
                httpIntent.setData(Uri.parse("https://razorpay.com/sample-application/"));
                startActivity(httpIntent);
            }
        });
    }

    public void startPayment() {
        final Activity activity = this;

        final Checkout co = new Checkout();

        try {
            JSONObject options = new JSONObject();
            options.put("name", "Razorpay Corp");
            options.put("description", "Demoing Charges");
            options.put("send_sms_hash",true);
            options.put("allow_rotation", true);
            //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", "100");

            JSONObject preFill = new JSONObject();
            preFill.put("email", "Your Registration Email Id");
            preFill.put("contact", "Your Registration Contact Number");
            options.put("prefill", preFill);
            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 razorpayPaymentID) {
        try {

            Toast.makeText(this, "Payment Successful: " + razorpayPaymentID, Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            Log.e(TAG, "Exception in onPaymentSuccess", e);
        }
    }


    @Override
    public void onPaymentError(int code, String response) {
        try {
            Toast.makeText(this, "Payment failed: " + code + " " + response, Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            Log.e(TAG, "Exception in onPaymentError", e);
        }
    }
}
  • Now you can run your project.

Congratulations!!! you have developed your Razorpay Payment Gateway Integration. in Android Studio and now just keep following the rest of the tutorial step by step to become a great Android Developer. All the very best.

Thank You For Visiting Us.

 

 

 

 

 

Keyur Vaghani

Android Developer

Recent Posts

Testing hk

Testing

1 year ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

1 year ago

Operation

Testing

1 year ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

1 year ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

1 year ago

TETS NEW

test

2 years ago