How To Use PayUMoney Example In Android

PayU Money is the Online Payment Gateway to accept Payments Online. PayU Money is India's most comprehensive payment gateway with credit cards, debit cards and net banking.Go live with all payment options fastest in India.

1. MainActivity.java

package com.tutorialsee.payu;

import com.payu.india.Extras.PayUChecksum;
import com.payu.india.Model.PaymentParams;
import com.payu.india.Model.PayuConfig;
import com.payu.india.Model.PayuHashes;
import com.payu.india.Model.PostData;
import com.payu.india.Payu.PayuConstants;
import com.payu.india.Payu.PayuErrors;
import com.tutorialsee.payu.R;
import com.tutorialsee.payu.payment.PayUBaseActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class MainActivity extends Activity  {
int merchantIndex = 0;
int env = PayuConstants.PRODUCTION_ENV;
int couponcode = 0;
String offerKeys[] = { "test123@6622", "offer_test@ffer_t5172","offerfranklin@6636" };
String merchantKey = "mWQMDU";
String merchantSalt = "1nAwq4Sw";
String mandatoryKeys[] = { PayuConstants.KEY, PayuConstants.AMOUNT,
PayuConstants.PRODUCT_INFO, PayuConstants.FIRST_NAME,
PayuConstants.EMAIL, PayuConstants.TXNID, PayuConstants.SURL,
PayuConstants.FURL, PayuConstants.USER_CREDENTIALS,
PayuConstants.UDF1, PayuConstants.UDF2, PayuConstants.UDF3,
PayuConstants.UDF4, PayuConstants.UDF5, PayuConstants.ENV };
String mandatoryValues[] = { merchantKey, "1.0", "myproduct", "firstname",
"me@itsmeonly.com", "" + System.currentTimeMillis(),
"https://payu.herokuapp.com/success",
"https://payu.herokuapp.com/failure",
merchantKey + ":payutest@payu.in", "udf1", "udf2", "udf3", "udf4",
"udf5", "" + env };
String inputData = "";
static String msgsss = "",error = "",_d_url = "https://play.google.com/store/apps/details?id=com.cms.kilol",versioncode = "", msgnew = "", someissues = "";
String version = "";
private PayUChecksum checksum;
private PostData postData;
private String key;
private String salt;
private String var1;
private PaymentParams mPaymentParams;
private PayuConfig payuConfig;
private String cardBin;
static String pid = "", qty = "2", size = "35";
TextView  confirmation;
Context context;
private Intent intent;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
confirmation = (TextView) findViewById(R.id.order);
confirmation.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
navigateToBaseActivity();
}

});

}


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
/* if (requestCode == PayuConstants.PAYU_REQUEST_CODE) {
if (data != null) {
Log.v("PayuResult", data.getStringExtra("result"));
String str = data.getStringExtra("result");
str = str.replace("&", "&");

if (str.contains("status=success")) {
String strArray[] = str.split("&");
String strArray1[] = strArray[5].split("=");
PrefernceSettings.setFBNAME(strArray1[1]);
System.out.println(strArray1[1]);
String gift = PrefernceSettings.getGiftMail();
PayuMoneySecond();
}

} else {
Toast.makeText(getActivity(), "Could not receive data", Toast.LENGTH_LONG).show();
}
}*/
}

public void navigateToBaseActivity() {

intent = new Intent(getApplicationContext(), PayUBaseActivity.class);
mPaymentParams = new PaymentParams();
payuConfig = new PayuConfig();

mPaymentParams.setKey(merchantKey);
key = merchantKey;
// txt_totals.getText()+""
mPaymentParams.setAmount("3533");

mPaymentParams.setProductInfo(size + "_" + qty + "_" + pid);

mPaymentParams.setFirstName("sandeep khandelwal");

mPaymentParams.setEmail("tutorialsee9@gmail.com");

mPaymentParams.setTxnId("" + System.currentTimeMillis());

mPaymentParams.setSurl("https://payu.herokuapp.com/success");

mPaymentParams.setFurl("https://payu.herokuapp.com/failure");

mPaymentParams.setUdf1("");

mPaymentParams.setUdf2("");

mPaymentParams.setUdf3("");

mPaymentParams.setUdf4("");

mPaymentParams.setUdf5("");
mPaymentParams.setUserCredentials(merchantKey + ":payutest@payu.in");
var1 = merchantKey + ":payutest@payu.in";
mPaymentParams.setOfferKey("");
intent.putExtra(PayuConstants.SALT, merchantSalt);
salt = merchantSalt;
String environment = "0";
payuConfig.setEnvironment(0);
cardBin = "";

generateHashFromSDK(mPaymentParams, merchantSalt);

}


public void launchSdkUI(PayuHashes payuHashes) {

intent.putExtra(PayuConstants.PAYU_CONFIG, payuConfig);
intent.putExtra(PayuConstants.PAYMENT_PARAMS, mPaymentParams);
intent.putExtra(PayuConstants.PAYU_HASHES, payuHashes);
intent.putExtra(PayuConstants.SALT, salt);
startActivityForResult(intent, PayuConstants.PAYU_REQUEST_CODE);

}

/****************************** Client hash generation ***********************************/
public void generateHashFromSDK(PaymentParams mPaymentParams, String Salt) {
PayuHashes payuHashes = new PayuHashes();
postData = new PostData();
checksum = null;
checksum = new PayUChecksum();
checksum.setAmount(mPaymentParams.getAmount());
checksum.setKey(mPaymentParams.getKey());
checksum.setTxnid(mPaymentParams.getTxnId());
checksum.setEmail(mPaymentParams.getEmail());
checksum.setSalt(merchantSalt);
checksum.setProductinfo(mPaymentParams.getProductInfo());
checksum.setFirstname(mPaymentParams.getFirstName());
checksum.setUdf1(mPaymentParams.getUdf1());
checksum.setUdf2(mPaymentParams.getUdf2());
checksum.setUdf3(mPaymentParams.getUdf3());
checksum.setUdf4(mPaymentParams.getUdf4());
checksum.setUdf5(mPaymentParams.getUdf5());

postData = checksum.getHash();
if (postData.getCode() == PayuErrors.NO_ERROR) {
Log.v("raj", merchantSalt);
payuHashes.setPaymentHash(postData.getResult());
}
var1 = var1 == null ? PayuConstants.DEFAULT : var1;

if ((postData = calculateHash(key,PayuConstants.PAYMENT_RELATED_DETAILS_FOR_MOBILE_SDK, var1,merchantSalt)) != null
&& postData.getCode() == PayuErrors.NO_ERROR) // Assign post
payuHashes.setPaymentRelatedDetailsForMobileSdkHash(postData.getResult());
Log.v("v", postData.getResult() + "");

// vas
if ((postData = calculateHash(key, PayuConstants.VAS_FOR_MOBILE_SDK,PayuConstants.DEFAULT, merchantSalt)) != null
&& postData.getCode() == PayuErrors.NO_ERROR)
payuHashes.setVasForMobileSdkHash(postData.getResult());

// getIbibocodes
if ((postData = calculateHash(key,
PayuConstants.GET_MERCHANT_IBIBO_CODES, PayuConstants.DEFAULT, merchantSalt)) != null
&& postData.getCode() == PayuErrors.NO_ERROR)
payuHashes.setMerchantIbiboCodesHash(postData.getResult());

if (!var1.contentEquals(PayuConstants.DEFAULT)) {
// get user card
if ((postData = calculateHash(key, PayuConstants.GET_USER_CARDS,
var1, merchantSalt)) != null
&& postData.getCode() == PayuErrors.NO_ERROR) // todo rename
// storedc
// ard
payuHashes.setStoredCardsHash(postData.getResult());
// save user card
if ((postData = calculateHash(key, PayuConstants.SAVE_USER_CARD,
var1, merchantSalt)) != null
&& postData.getCode() == PayuErrors.NO_ERROR)
payuHashes.setSaveCardHash(postData.getResult());
// delete user card
if ((postData = calculateHash(key, PayuConstants.DELETE_USER_CARD,
var1, merchantSalt)) != null
&& postData.getCode() == PayuErrors.NO_ERROR)
payuHashes.setDeleteCardHash(postData.getResult());
// edit user card
if ((postData = calculateHash(key, PayuConstants.EDIT_USER_CARD,
var1, merchantSalt)) != null
&& postData.getCode() == PayuErrors.NO_ERROR)
payuHashes.setEditCardHash(postData.getResult());
}

if (mPaymentParams.getOfferKey() != null) {
postData = calculateHash(key, PayuConstants.OFFER_KEY,
mPaymentParams.getOfferKey(), merchantSalt);
if (postData.getCode() == PayuErrors.NO_ERROR) {
payuHashes.setCheckOfferStatusHash(postData.getResult());
}
}

if (mPaymentParams.getOfferKey() != null
&& (postData = calculateHash(key,
PayuConstants.CHECK_OFFER_STATUS,
mPaymentParams.getOfferKey(), merchantSalt)) != null
&& postData.getCode() == PayuErrors.NO_ERROR) {
payuHashes.setCheckOfferStatusHash(postData.getResult());
}

// we have generated all the hases now lest launch sdk's ui
launchSdkUI(payuHashes);
}

private PostData calculateHash(String key, String command, String var1, String salt) {
checksum = null;
checksum = new PayUChecksum();
checksum.setKey(key);
checksum.setCommand(command);
checksum.setVar1(var1);
checksum.setSalt(salt);
return checksum.getHash();
}


public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
}
}
}

2. activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#BD8C52"
            android:orientation="horizontal" >

            <ImageView
                android:id="@+id/imageVicew1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="8dp"
                android:src="@drawable/menu" />

            <TextView
                android:id="@+id/sdfs"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginLeft="30dp"
                android:gravity="center_vertical"
                android:text="Online Shopping"
                android:textColor="#ffffff"
                android:textSize="18sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/sds"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="All Star Robert Indiana “LOVE” Collection"
                android:textColor="#000000"
                android:gravity="center"
                android:textSize="18sp" />

            <TextView
                android:id="@+id/sdds"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="15.4-inch "
                android:textColor="#000000"
                android:textSize="15sp" />

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="25dp"
                android:src="@drawable/ss" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textColor="#54BBAB"
                android:text="@string/amount"
                android:textAppearance="?android:attr/textAppearanceSmall" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:orientation="horizontal"
                android:padding="15dp" >

                <TextView
                    android:id="@+id/order"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="#A87A48"
                    android:gravity="center"
                    android:padding="10dp"
                    android:text="Proceed to Checkout"
                    android:textColor="#ffffff"
                    android:textSize="13sp" />
            </LinearLayout>

        </LinearLayout>
    </LinearLayout>

</LinearLayout>