PayU money Payment Gateway Integration

Nikhil Mandlik
3 min readDec 26, 2020

Overview

Transaction Flow

Prerequisite

  1. Merchant Key & Merchant Salt

2. Merchant ID

  • Go To Your Profile
  • General Details Section > Copy Merchant ID

3. Hosted PHP file for generating Hash

Copy the following PHP File and upload it on your server<?php $key = $_POST["key"]; $txnid = $_POST["txnid"]; $amount = $_POST["amount"]; //Please use the amount value from database $productinfo = $_POST["productInfo"]; 
$firstname = $_POST["firstName"];
$email = $_POST["email"];
$udf1 = $_POST["udf1"];
$udf2 = $_POST["udf2"];
$udf3 = $_POST["udf3"];
$udf4 = $_POST["udf4"];
$udf5 = $_POST["udf5"];
$salt = "Your Merchant Salt"; //Please change the value with the live salt for production environment
//hash sequence
$hashSeq = "$key|$txnid|$amount|$productinfo|$firstname|$email|$udf1|$udf2|$udf3|$udf4|$udf5||||||$salt";
$hash = hash("sha512", $hashSeq);
error_log("all posted variables:" . print_r($_POST, true));
echo $hash;

Lets Start

  1. Include Following Dependencies
//PayUMoney 
implementation 'com.payumoney.sdkui:plug-n-play:1.6.1'
//Volley
implementation 'com.android.volley:volley:1.1.1'

2. Setting up Your Manifest File

//Add Internet Permission  
<uses-permission android:name="android.permission.INTERNET"/>
//add following line in application tag of manifest file android:usesCleartextTraffic="true"

3. Creating Constants (create separate Java Class for storing your PayU Money Credentials)

public class Constants {

//your server url containing hash genration php file
public static final String GET_HASH = "Your Server Url";

//success and failure url , you can copy the same
public static final String SURL = "https://www.payumoney.com/mobileapp/payumoney/success.php";
public static final String FURL = "https://www.payumoney.com/mobileapp/payumoney/failure.php";

//Your Merchant Account Information
public static final String MERCHANT_KEY = "Your Merchant Key";
public static final String MERCHANT_ID = "Your Merchant ID";

//For Testing set Debug = True, and False for Production
public static final boolean DEBUG = false;

}

4. Creating Style for payment activity

<style name="PayUMoney" parent="PayumoneyAppTheme">
<item name="colorPrimary">?attr/colorPrimary</item>
<item name="colorPrimaryDark">?attr/colorPrimaryDark</item>
<item name="colorAccent">@android:color/holo_orange_dark</item>
<item name="colorButtonNormal">@android:color/holo_orange_dark</item>
<item name="alertDialogTheme">@style/AlertDialogStylePayUMoney</item>
<item name="actionMenuTextColor">@color/white</item>
</style>

<style name="AlertDialogStylePayUMoney" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorAccent">?attr/colorPrimary</item>
<item name="android:textColorPrimary">@color/payumoney_text_color</item>
</style>

5. create PayUmoneySdkInitializer.PaymentParam.Builder Object

private void launchPaymentFlow() {
PayUmoneyConfig payUmoneyConfig = PayUmoneyConfig.getInstance();
payUmoneyConfig.setPayUmoneyActivityTitle("PayU Money Integration Demo");
payUmoneyConfig.setDoneButtonText(" Pay ");
PayUmoneySdkInitializer.PaymentParam.Builder builder = new PayUmoneySdkInitializer.PaymentParam.Builder();
builder.setAmount(amount) //your amount
.setTxnId(System.currentTimeMillis() + "") //unique transaction ID
.setPhone(contact)
.setProductName("Testing PayU Money") //you can set anything you want
.setFirstName(fname)
.setEmail(email)
.setsUrl(Constants.SURL)
.setfUrl(Constants.FURL)
.setUdf1("")
.setUdf2("")
.setUdf3("")
.setUdf4("")
.setUdf5("")
.setUdf6("")
.setUdf7("")
.setUdf8("")
.setUdf9("")
.setUdf10("")
.setIsDebug(Constants.DEBUG)
.setKey(Constants.MERCHANT_KEY)
.setMerchantId(Constants.MERCHANT_ID);
try {
PayUmoneySdkInitializer.PaymentParam mPaymentParams = builder.build();
getHashFromServer(mPaymentParams);
} catch (Exception e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}

6. Requesting Hash From Server and Starting Payment Activity

private void getHashFromServer(final PayUmoneySdkInitializer.PaymentParam mPaymentParams) {
Toast.makeText(MainActivity.this,"Getting Hash From Server ",Toast.LENGTH_SHORT).show();
String url = Constants.GET_HASH;
StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
hashFromServer = response;
Log.i(TAG, "onResponse: response "+response);
Toast.makeText(MainActivity.this,"Hash From server "+hashFromServer,Toast.LENGTH_SHORT).show();
if (hashFromServer.isEmpty() || hashFromServer.equals("")) {
Toast.makeText(MainActivity.this, "Could not generate hash", Toast.LENGTH_SHORT).show();
} else {
mPaymentParams.setMerchantHash(hashFromServer);
PayUmoneyFlowManager.startPayUMoneyFlow(mPaymentParams, MainActivity.this, R.style.PayUMoney, true);
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i(TAG, "onErrorResponse: "+error.getMessage());
}
}) {
@Override
protected Map<String, String> getParams() {
Log.i(TAG, "getParams: "+mPaymentParams.getParams());
return mPaymentParams.getParams();
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}
};
Volley.newRequestQueue(this).add(request);
}

7. Handling Transaction Response in OnActivityResult Method

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Result Code is -1 send from Payumoney activity
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PayUmoneyFlowManager.REQUEST_CODE_PAYMENT && resultCode == RESULT_OK && data != null) {
TransactionResponse transactionResponse = data.getParcelableExtra(PayUmoneyFlowManager.INTENT_EXTRA_TRANSACTION_RESPONSE);
ResultModel resultModel = data.getParcelableExtra(PayUmoneyFlowManager.ARG_RESULT);
// Check which object is non-null
if (transactionResponse != null && transactionResponse.getPayuResponse() != null) {
if (transactionResponse.getTransactionStatus().equals(TransactionResponse.TransactionStatus.SUCCESSFUL)) {
//Success Transaction
} else {
//Failure Transaction
}
} else if (resultModel != null && resultModel.getError() != null) {
Log.d(TAG, "Error response : " + resultModel.getError().getTransactionResponse());
} else {
Log.d(TAG, "Both objects are null!");
}
}
}

Download Source Code

Clikc Here

--

--