Skip to main content

Razorpay Payment gateway integration in android application.

    

    Razorpay payment gateway requires less time and minimum code.







Creating New Project:

1. In Android Studio, create a new project by navigating to File ⇒ New Project and fill all the required details. When it prompts to select a default activity, select Blank Activity and proceed.

Notes:- Minsdk should be equal to or greater than 19

2. Open build.gradle and add razorpay support by adding compile 'com.razorpay:checkout:1.2.0' under dependencies section.

dependencies  {

implementation 'com.razorpay:checkout:1.5.8'

}


3. Now open AndroidManifest.xml and add this singleton class in <application> tag using android:name property to execute the class automatically whenever app launches. Also add INTERNET permission as we are going to make network calls.

You need to add your Razorpay API key to AndroidManifest.xml. You also need to add the RECEIVE_SMS permission.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.razorpay">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".CheckoutActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

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

</manifest>


4. Create activity file name like this CheckutActivity.java

package com.example.razorpay;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
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 Button buttonConfirmOrder;
    private EditText editTextPayment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_checkout);

        findViews();
        listeners();
    }

    public void findViews() {
        buttonConfirmOrder = (Button) findViewById(R.id.buttonConfirmOrder);
        editTextPayment = (EditText) findViewById(R.id.editTextPayment);
    }

    public void listeners() {


        buttonConfirmOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(editTextPayment.getText().toString().equals(""))
                {
                    Toast.makeText(CheckoutActivity.this, "Please fill payment", Toast.LENGTH_LONG).show();
                    return;
                }
                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();

        try {
            JSONObject options = new JSONObject();
            options.put("name", "Razorpay Corp");
            options.put("description", "Demoing Charges");
            //You can omit the image option to fetch the image from dashboard
            options.put("image", "https://rzp-mobile.s3.amazonaws.com/images/rzp.png");
            options.put("currency", "INR");

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

            double total = Double.parseDouble(payment);
            total = total * 100;
            options.put("amount", total);

            JSONObject preFill = new JSONObject();
            preFill.put("email", "salil@gnail.com");
            preFill.put("contact", "99999999");

            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) {
        Toast.makeText(this, "Payment successfully done! " + razorpayPaymentID, Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onPaymentError(int code, String response) {
        try {
            Toast.makeText(this, "Payment error please try again", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            Log.e("OnPaymentError", "Exception in onPaymentError", e);
        }
    }
}


5. Create xml file in res/layout folder activity_checkout

<LinearLayout 
    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"
    android:orientation="vertical"
    android:weightSum="10">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="9"
        android:fillViewport="true">

        <LinearLayout
            android:id="@+id/linearLayoutCheckout"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="9"
            android:background="#ffffff"
            android:gravity="center"
            android:orientation="vertical"
            android:weightSum="10">

            <EditText
                android:id="@+id/editTextPayment"
                android:layout_width="80dp"
                android:layout_height="50dp"
                android:background="#e9e9e9"
                android:padding="3dp"
                android:text="20.00"
                android:textSize="25sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Type Payment what you are paying?" />
        </LinearLayout>
    </ScrollView>

    <LinearLayout
        android:id="@+id/linearLayoutButtonPlaceOrder"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_gravity="bottom"
        android:layout_weight="1"
        android:background="#FFFFFF"
        android:orientation="vertical">

        <Button
            android:id="@+id/buttonConfirmOrder"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="2dp"
            android:background="#007200"
            android:text="Confirm Order"
            android:textColor="#ffffff"
            android:visibility="visible" />
    </LinearLayout>
</LinearLayout>




Comments

Popular posts from this blog

Why Ethereum Smart Contracts Make It Hard to Get Payments

  The Unique Role of Smart Contracts in Ethereum One of Ethereum’s standout features is its ability to host diverse applications on its blockchain using smart contracts. However, these smart contracts, while powerful, sometimes complicate simple tasks. For instance, determining the amount of ETH deposited into a specific Ethereum address can be surprisingly complex. This is because you cannot understand the actions of a smart contract without executing it. Synchronizing Blockchain Internals and Externals Smart contracts operate exclusively within the blockchain's data, reading and writing information stored on-chain. This limitation does not prevent the creation of valuable applications, such as multi-signature wallets or tokens like ERC-20 and ERC-721, which rely solely on on-chain data. However, most practical applications also require interaction with off-chain systems. Take cryptocurrency exchanges, for example. Exchanges facilitate converting ETH into fiat currency or vice ver...

Room + ViewModel + LiveData + RecyclerView (MVVM)

Part 1 - Introduction Part 2 – Entity Part 3 – DAO & RoomDatabase Part 4 – Repository Part 5 – ViewModel Part 6 – RecyclerView + Adapter Part 7 – Add Note Activity Part 8 – Swipe to Delete Part 9 – OnItemClickListener & Update Functionality Part 10 – ListAdapter                     Part 1 - Introduction In this tutorial we will build a note taking app, using the Android Architecture Component libraries (Room, ViewModel, LiveData and LifeCycle), a RecyclerView and Java. The data will be stored in an SQLite database and supports insert, read, update and delete operations. For this we will follow the official recommendations from the “Guide to App Architecture” (link below). In part 1 we will learn what the Architecture Components are, how they work and why we need them. We will learn about the problems that arise from the Activity and Fragment lifecycle, configuration changes and bloated, tightly coupled cl...

DodgeInsetEdges

The layout_dodgeInsetEdges together with the layout_insetEdge attribute, to move views within a CoordinatorLayout out of the way of other views. This behavior is the default behavior for FloatingActionButtons and Snackbars , but we will also apply it to views like normal buttons and bottom sheets. In Mainactivity.java add the code below stated:- import android.support.design.widget.BottomSheetBehavior; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity {     private BottomSheetBehavior bottomSheetBehavior;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activ...