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

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...

Color Picker Dialog(Ambilwarna)

  In this tutorial, we will use a 3rd party library called AmbilWarna to implement a color picker into our app. We will then use the picked color and set the background of our Activity to it. In build.gradle file add these dependency:- implementation 'com.github.yukuku:ambilwarna:2.0.1' In MainActivity.java class file add these lines of codes:- package com.deftskill.colorpickerexample; import androidx.appcompat.app.AppCompatActivity; import androidx.constraintlayout.widget.ConstraintLayout; import androidx.core.content.ContextCompat; import android.os.Bundle; import android.view.View; import android.widget.Button; import yuku.ambilwarna.AmbilWarnaDialog; public class MainActivity extends AppCompatActivity { ConstraintLayout mLayout ; int mDefaultColor ; Button mButton ; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout. activity_main ); mLayout = (ConstraintLayo...

Circular Determinate ProgressBar with Background and Text

we will learn how to create a circular progress bar in Android Studio that displays the current progress value and has a gray background color. I will also show you how you can customize this progress bar further and add things like a color gradient effect. After successful Empty activity creation add these lines of code in the files:- Add a circle.xml file in Drawable folder and add these lines of codes:- <? xml version ="1.0" encoding ="utf-8" ?> < layer-list xmlns: android ="http://schemas.android.com/apk/res/android" > < item > < shape android :shape ="ring" android :thicknessRatio ="16" android :useLevel ="false" > < solid android :color ="#DDD" /> </ shape > </ item > < item > < rotate android :fromDegrees ="270" android :toDegrees ="270...