implement payment gateway in android :

 

How to Integrate Razorpay Payment Gateway in Android :



Razorpay  provided a service with the help of this we can integrate the payment solutions in our app very easily and we can also manage all payment methods in our app. In this article, we will take a look at the implementation of a payment gateway in our Android app.. 

Step by Step Implementation

Step 1: Add dependency of Razor pay library in build.gradle file:

Note-put latest version dependency ,we can check Razorpay doc:

checkout this Link : https://razorpay.com/docs/payments/payment-gateway/android-integration/standard/
implementation'com.razorpay:checkout:1.6.20'
Step 2: Adding permissions to the Internet : 
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.like.exampleofpaymentgetway">

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:exported="true"
android:theme="@style/Theme.ExampleOfPaymentGetWay">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

</manifest>
Step 3: Working with the activity_main.xml file :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context=".MainActivity">

<!--EditText text to enter amount-->
<ImageView
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_marginTop="12dp"
android:src="@drawable/compare"
android:scaleType="fitXY">

</ImageView>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:boxStrokeColor="@color/colorPrimary"
app:boxStrokeWidthFocused="2dp"
app:endIconMode="clear_text"
app:endIconTint="@color/colorPrimary"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginTop="12dp"
app:hintTextColor="@color/black"
app:startIconTint="@color/colorBTN"
android:layout_gravity="center"
app:startIconDrawable="@drawable/rupee_24"
android:hint="Enter amount to be payed"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
>

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/idEdtAmount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="@color/purple_700"
android:drawableTint="@color/btnbackground"
android:inputType="number">

</com.google.android.material.textfield.TextInputEditText>

</com.google.android.material.textfield.TextInputLayout>


<!--button to make payment-->
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/idBtnPay"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="@id/idEdtAmount"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:layout_marginLeft="9dp"
android:padding="8dp"
android:background="@color/btnbackgroundd"
android:backgroundTint="@color/btnbackgroundd"
android:layout_gravity="center"
android:text="Pay using RazorPay"
android:textAllCaps="false" />

</LinearLayout>
Step 4: Generating an API key for using Razorpay :
Browser the Razorpay site in Google or you can click on the link https://razorpay.com/. After clicking on this link you simply have to signup with your email and password and add some basic information such as your phone number. 
Note: Here we are creating a testing credential for using Razor Pay.
Inside the setting screen, click on Create a new key option your key will be generated.  we can download that file here ,We will be using key ID in our application to test Razor pay. The key-id will start with rzp_test . 
Step 5: Working with the MainActivity.java file:
package com.like.exampleofpaymentgetway;

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

import androidx.appcompat.app.AppCompatActivity;

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

import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity implements PaymentResultListener {

// variables for our
// edit text and button.
private EditText amountEdt;
private Button payBtn;

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

// initializing all our variables.

amountEdt = findViewById(R.id.idEdtAmount);
payBtn = findViewById(R.id.idBtnPay);

// adding on click listener to our button.
payBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// on below line we are getting amount that is entered by user.

String samount = amountEdt.getText().toString();

// rounding off the amount.
int amount = Math.round(Float.parseFloat(samount) * 100);

// initialize Razorpay account.here checkout is class we create object of that ,
// and clall that class, and put key and image

Checkout checkout = new Checkout();

// set your id as below
checkout.setKeyID("rzp_test_DjmWCNfbOHx5PT");

// set image
checkout.setImage(R.drawable.fb2);
// JSONObject is class we call that class here for put our details ;
// initialize json object

JSONObject object = new JSONObject();
try {
// to put name
object.put("name", "rajesh Bhatt");

// put description
object.put("description", "Test payment");

// to set theme color
object.put("theme.color", "#EF7F1A");

// put the currency
object.put("currency", "INR");

// put amount
object.put("amount", amount);

// put mobile number
object.put("prefill.contact", "1234567823");

// put email
object.put("prefill.email", "rajbhatt@gmail.com");

// open razorpay to checkout activity
checkout.open(MainActivity.this, object);
} catch (JSONException e) {
e.printStackTrace()
;
}
}
})
;
}
/// implement PaymentResultListener interfce above and implement Two Method Given Below:
@Override
public void onPaymentSuccess(String s) {
// this method is called on payment success.
Toast.makeText(this, "Payment is successful : " + s,
Toast.LENGTH_SHORT).show();
}

@Override
public void onPaymentError(int i, String s) {
// on payment failed.
Toast.makeText(this, "Payment Failed due to error : " + s,
Toast.LENGTH_SHORT).show();
}
}

Rajeshbhatt12

My name is Rajesh Bhatt. I am working as a senior android developer . I have created this blog for kotlin ,java and Android Development interview questions etc..

Previous Post Next Post