PayUmoney Payment Gateway Integration Android Tutorial

Jyotishgher Astrology
By -
0

 

PayUmoney Payment Gateway Integration Android Tutorial

Step By Step Process

  1. Create Account &  Get merchant key & salt key
  2. Upload server side PHP files for checksum HASH
  3. Add dependency in android gradle file
  4. Get checksum hash using retrofit
  5. Start  PayUmoney payment transactions

Add dependency in Android gradle file

Open android studio project gradle file and add these lines. Also you need to use Retrofit library to get security hash from server. NOTE: you can use Volley library or AsyncTask in place of Retrofit.

Retrofit is best for client server communication that’s why I used only retrofit on all android projects. If you don’t know how to use retrofit library, 

implementation 'com.payumoney.core:payumoney-sdk:7.4.4'
implementation 'com.payumoney.sdkui:plug-n-play:1.4.4'

So the complete gradle file will be

apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "blueappsoftware.payu_de"
minSdkVersion 20
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
generatedDensities=[]
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
aaptOptions {
additionalParameters "--no-version-vectors"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.android.support:customtabs:28.0.0'
implementation 'com.android.support:cardview-v7:28.0.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.android.support:animated-vector-drawable:28.0.0'
implementation 'com.payumoney.core:payumoney-sdk:7.4.4'
implementation 'com.payumoney.sdkui:plug-n-play:1.4.4'
// retrofit library
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
// Retrofit API Interceptor for Debuging
// compile 'com.squareup.okhttp3:logging-interceptor:3.8.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1'
implementation 'com.squareup.okhttp3:okhttp:3.4.1'
}

Create two edit text on activity_main.xml file so you can add phone number and amount at run time.

activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Learn with Kamal Bunkar"
android:textStyle="bold"
android:textSize="20dip"
android:layout_marginTop="20dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="10dip"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="www.blueappsoftware.com"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PayUmoney Integrations"
android:textStyle="bold"
android:textSize="24dip"
android:layout_marginTop="20dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="10dip"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:id="@+id/phone"
android:hint="phone Number"
android:inputType="phone"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:id="@+id/amountid"
android:hint="Amount"
android:inputType="number"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/start_transaction"
android:text="Start Payment"
android:onClick="onStartTransaction"
android:layout_gravity="center"
android:textStyle="bold"
android:textSize="16dip"
android:textColor="#000000"/>
</LinearLayout>

MainActivity.java file get phone number and amount from edit text and pass as intent parameter to next activity.

public class MainActivity extends AppCompatActivity {
EditText phone, amount;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
Button btn = (Button) findViewById(R.id.start_transaction);
phone = (EditText) findViewById(R.id.phone);
amount = (EditText) findViewById(R.id.amountid);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, StartPaymentActivity.class);
intent.putExtra("phone", phone.getText().toString());
intent.putExtra("amount", amount.getText().toString());
startActivity(intent);
}
});
}
}

Get checksum hash using Retrofit

Now Create payumoney builder variable and initialize it with value like below. You need to hit the URL of PHP files that you have uploaded on your server. I am using Retrofit Library but you can use volley or AsyncTask. On successful Response you will get security hash from server.

public void getHashkey(){
ServiceWrapper service = new ServiceWrapper(null);
Call<String> call = service.newHashCall(merchantkey, txnid, amount, prodname,
firstname, email);
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
Log.e(TAG, "hash res "+response.body());
String merchantHash= response.body();
if (merchantHash.isEmpty() || merchantHash.equals("")) {
Toast.makeText(StartPaymentActivity.this, "Could not generate hash", Toast.LENGTH_SHORT).show();
Log.e(TAG, "hash empty");
} else {
// mPaymentParams.setMerchantHash(merchantHash);
paymentParam.setMerchantHash(merchantHash);
// Invoke the following function to open the checkout page.
// PayUmoneyFlowManager.startPayUMoneyFlow(paymentParam, StartPaymentActivity.this,-1, true);
PayUmoneyFlowManager.startPayUMoneyFlow(paymentParam, StartPaymentActivity.this, R.style.AppTheme_default, false);
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
Log.e(TAG, "hash error "+ t.toString());
}
});
}

payumoneybuilder variable:-

builder.setAmount(amount) // Payment amount
.setTxnId(txnid) // Transaction ID
.setPhone(phone) // User Phone number
.setProductName(prodname) // Product Name or description
.setFirstName(firstname) // User First name
.setEmail(email) // User Email ID
.setsUrl("https://www.payumoney.com/mobileapp/payumoney/success.php") // Success URL (surl)
.setfUrl("https://www.payumoney.com/mobileapp/payumoney/failure.php") //Failure URL (furl)
.setUdf1("")
.setUdf2("")
.setUdf3("")
.setUdf4("")
.setUdf5("")
.setUdf6("")
.setUdf7("")
.setUdf8("")
.setUdf9("")
.setUdf10("")
.setIsDebug(true) // Integration environment - true (Debug)/ false(Production)
.setKey(merchantkey) // Merchant key
.setMerchantId(merchantId);

Start  PayUmoney payment transactions

Once you get the security checksum hash from server, set it on builder as merchantHash(). its time to start payumooney payment transaction using

PayUmoneyFlowManager.startPayUMoneyFlow(paymentParam, StartPaymentActivity.this, R.style.AppTheme_default, false);

From there onward PayUmoney SDK and plug-n-play SDK will take care of every thing. You will get the status response as “Success” OR “Failure”.  Below is the sample response when your transaction get successful.

// PayUMoneySdk: Success -- {"id":221142,"mode":"CC","status":"success","unmappedstatus":"captured","key":"9y84Mzso","txnid":"223013","transaction_fee":"20.00","amount":"20.00","cardCategory":"domestic","discount":"0.00","addedon":"2018-12-31 09:09:43","productinfo":"a2z shop","firstname":"kamal","email":"kamal.bunkar07@gmail.com","phone":"9144040888","hash":"b22172fcc0ab6dbc0a52925ebb97cca6793328a8dd1e61ef510b9545d9c851600fdbdc985960f803412c49e4faa56968b3e70c67fe62eaed7cecfdb5b3","field1":"562178","field2":"823386","field3":"2061","field4":"MC","field5":"1672274249","field6":"00","field7":"0","field8":"3DS","field9":" Verification of Secure Hash Failed: E700 -- Approved -- Transaction Successful -- Unable to be determined--E000","payment_source":"payu","PG_TYPE":"AXISPG","bank_ref_no":"54578","ibibo_code":"VISA","error_code":"E000","Error_Message":"No Error","name_on_card":"payu","card_no":"401200XXXXXX1112","is_seamless":1,"surl":"https://www.payumoney.com/sandbox/payment/postBackParam.do","furl":"https://www.payumoney.com/sandbox/payment/postBackParam.do"}

Complete code

package blueappsoftware.payu_de;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.payumoney.core.PayUmoneySdkInitializer;
import com.payumoney.core.entity.TransactionResponse;
import com.payumoney.sdkui.ui.utils.PayUmoneyFlowManager;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class StartPaymentActivity extends AppCompatActivity {
PayUmoneySdkInitializer.PaymentParam.Builder builder = new PayUmoneySdkInitializer.PaymentParam.Builder();
//declare paymentParam object
PayUmoneySdkInitializer.PaymentParam paymentParam = null;
String TAG ="mainActivity", txnid ="txt12346", amount ="20", phone ="9144040888",
prodname ="BlueApp Course", firstname ="kamal", email ="kamal.bunkar07@gmail.com",
merchantId ="5884494", merchantkey="WnWkb6be"; // first test key only
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_startpayment);
Intent intent = getIntent();
phone = intent.getExtras().getString("phone");
amount = intent.getExtras().getString("amount");
startpay();
}
public void startpay(){
builder.setAmount(amount) // Payment amount
.setTxnId(txnid) // Transaction ID
.setPhone(phone) // User Phone number
.setProductName(prodname) // Product Name or description
.setFirstName(firstname) // User First name
.setEmail(email) // User Email ID
.setsUrl("https://www.payumoney.com/mobileapp/payumoney/success.php") // Success URL (surl)
.setfUrl("https://www.payumoney.com/mobileapp/payumoney/failure.php") //Failure URL (furl)
.setUdf1("")
.setUdf2("")
.setUdf3("")
.setUdf4("")
.setUdf5("")
.setUdf6("")
.setUdf7("")
.setUdf8("")
.setUdf9("")
.setUdf10("")
.setIsDebug(true) // Integration environment - true (Debug)/ false(Production)
.setKey(merchantkey) // Merchant key
.setMerchantId(merchantId);
try {
paymentParam = builder.build();
// generateHashFromServer(paymentParam );
getHashkey();
} catch (Exception e) {
Log.e(TAG, " error s "+e.toString());
}
}
public void getHashkey(){
ServiceWrapper service = new ServiceWrapper(null);
Call<String> call = service.newHashCall(merchantkey, txnid, amount, prodname,
firstname, email);
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
Log.e(TAG, "hash res "+response.body());
String merchantHash= response.body();
if (merchantHash.isEmpty() || merchantHash.equals("")) {
Toast.makeText(StartPaymentActivity.this, "Could not generate hash", Toast.LENGTH_SHORT).show();
Log.e(TAG, "hash empty");
} else {
// mPaymentParams.setMerchantHash(merchantHash);
paymentParam.setMerchantHash(merchantHash);
// Invoke the following function to open the checkout page.
// PayUmoneyFlowManager.startPayUMoneyFlow(paymentParam, StartPaymentActivity.this,-1, true);
PayUmoneyFlowManager.startPayUMoneyFlow(paymentParam, StartPaymentActivity.this, R.style.AppTheme_default, false);
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
Log.e(TAG, "hash error "+ t.toString());
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// PayUMoneySdk: Success -- payuResponse{"id":225642,"mode":"CC","status":"success","unmappedstatus":"captured","key":"9yrcMzso","txnid":"223013","transaction_fee":"20.00","amount":"20.00","cardCategory":"domestic","discount":"0.00","addedon":"2018-12-31 09:09:43","productinfo":"a2z shop","firstname":"kamal","email":"kamal.bunkar07@gmail.com","phone":"9144040888","hash":"b22172fcc0ab6dbc0a52925ebbd0297cca6793328a8dd1e61ef510b9545d9c851600fdbdc985960f803412c49e4faa56968b3e70c67fe62eaed7cecacdfdb5b3","field1":"562178","field2":"823386","field3":"2061","field4":"MC","field5":"167227964249","field6":"00","field7":"0","field8":"3DS","field9":" Verification of Secure Hash Failed: E700 -- Approved -- Transaction Successful -- Unable to be determined--E000","payment_source":"payu","PG_TYPE":"AXISPG","bank_ref_no":"562178","ibibo_code":"VISA","error_code":"E000","Error_Message":"No Error","name_on_card":"payu","card_no":"401200XXXXXX1112","is_seamless":1,"surl":"https://www.payumoney.com/sandbox/payment/postBackParam.do","furl":"https://www.payumoney.com/sandbox/payment/postBackParam.do"}
// Result Code is -1 send from Payumoney activity
Log.e("StartPaymentActivity", "request code " + requestCode + " resultcode " + resultCode);
if (requestCode == PayUmoneyFlowManager.REQUEST_CODE_PAYMENT && resultCode == RESULT_OK && data != null) {
TransactionResponse transactionResponse = data.getParcelableExtra( PayUmoneyFlowManager.INTENT_EXTRA_TRANSACTION_RESPONSE );
if (transactionResponse != null && transactionResponse.getPayuResponse() != null) {
if(transactionResponse.getTransactionStatus().equals( TransactionResponse.TransactionStatus.SUCCESSFUL )){
//Success Transaction
} else{
//Failure Transaction
}
// Response from Payumoney
String payuResponse = transactionResponse.getPayuResponse();
// Response from SURl and FURL
String merchantResponse = transactionResponse.getTransactionDetails();
Log.e(TAG, "tran "+payuResponse+"---"+ merchantResponse);
} /* else if (resultModel != null && resultModel.getError() != null) {
Log.d(TAG, "Error response : " + resultModel.getError().getTransactionResponse());
} else {
Log.d(TAG, "Both objects are null!");
}*/
}
}
}

Run the Code

Run this code in real device or in emulator. You test payment transaction you need test cart details. Test cart details is already given in Merchant Dashboard. Here I am also giving test cart details. Following screen you will see on your phone.

Cart number – 5123456789012346      expiry – 05/20            CVV – 123

payumoney integration in android    payumoney test card   payumoney integration

Post a Comment

0Comments

Post a Comment (0)