Android SDK Integration

Android Integration

IMPORTANT: PayU Android SDK version3.0 is deprecated.We are no longer supporting it.

Prerequisites

Before proceeding further with this document make sure that you have read this document

You must already have installed and configured:

  • Java JDK version 1.6 or greater
  • Android SDK
  • A Git client
  • Android Studio / Eclipse
  • All PayU PG Prerequisites (Key, Salt)

Starter Kit

SDK
Sample App (see branches)
Custom Browser
Custom Browser Sample App Without SDK

Integration

1. General Overview

The PayU Sample App essentially consists of three main components:

  • Native app: Merchants will essentially need to replace the sample app with their own app
  • sdkui module: That is the sample UI used by the above sample app. This is a default UI that is available for use by Merchants

    • Non-Seamless: In case you decide to not build your own UI, you are free to use this module
    • Seamless: In case you need to build your own UI, please go through this module carefully to increase your understanding of how to use SDK’s features
  • PayU SDK module: Consists of all the core sdk components and apis

2. Request Generation

2.1. Non-Seamless: Using SDK’s Inbuilt UI

  • Get all the required parameters
  • Create an object of PaymentParams, put all the obtained parameters in it by using its default set methods and setHash to paymentHash

    PaymentParams mPaymentParams = new PaymentParams();
    mPaymentParams.setKey(“gtKFFx”);
    mPaymentParams.setAmount(“15.0”);
    mPaymentParams.setProductInfo(“Tshirt”);
    mPaymentParams.setFirstName(“Guru”);
    mPaymentParams.setEmail(“guru@gmail.com”);
    mPaymentParams.setTxnId(“0123479543689”);
    mPaymentParams.setSurl(“httpss://payu.herokuapp.com/success”);
    mPaymentParams.setFurl(“httpss://payu.herokuapp.com/failure");
    mPaymentParams.setUdf1(“udf1l”);
    mPaymentParams.setUdf2(“udf2”);
    mPaymentParams.setUdf3(“udf3”);
    mPaymentParams.setUdf4(“udf4”);
    mPaymentParams.setUdf5(“udf5”);
    
    mPaymentParams.setHash("your payment hash");
    • You don't need to set udf1-5 in case you are not using them
    • Email and Firstname can be empty strings "" if you don't want to use them
    • For store user card feature
      mPaymentParams.setUserCredentials("your_key:user_id"), you need to pass user_credentials here
    • For offers mPaymentParams.setOfferKey("your_offer_key")
    • For any other payment default param (like phone and others) mPaymentParams.setPhone("your_number")
  • Get the required hashes by using your own server. Create an object of class PayuHashes and set the corresponding hashes using the default set methods provided

    mPaymentParams.setHash(payuHashes.getPaymentHash());
  • Create a new intent pointing to PayUBaseActivity and do the following:

    intent.putExtra(PayuConstants.ENV, PayuConstants.PRODUCTION_ENV);  
    intent.putExtra(PayuConstants.PAYMENT_DEFAULT_PARAMS, mPaymentParams);  
    intent.putExtra(PayuConstants.PAYU_HASHES, payuHashes);
  • To enable one click payment

    // Enable one click with MOBILE as storage option for hash
    intent.putExtra(PayuConstants.STORE_ONE_CLICK_HASH,PayuConstants.STORE_ONE_CLICK_HASH_MOBILE);
    OR
    // Enable once click with MERCHANT SERVER as storage option for hash
    intent.putExtra(PayuConstants.STORE_ONE_CLICK_HASH,PayuConstants.STORE_ONE_CLICK_HASH_SERVER);
    OR
    // DISABLE one click payment
    intent.putExtra(PayuConstants.STORE_ONE_CLICK_HASH,PayuConstants.STORE_ONE_CLICK_HASH_NONE);
  • Start the activity and let PayU SDK take care of the rest
    startActivity(intent);

2.2. Seamless: Using your own UI

2.2.1. Credit / Debit card

  • Create an object of PaymentParams, put all the obtained parameters in it by using its default set methods in the same way as you will create for the non-seamless style and setHash to paymentHash

  • Set the following additional properties to mPaymentParams

    mPaymentParams.setCardNumber(cardNumber);
    mPaymentParams.setCardName(cardName);
    mPaymentParams.setNameOnCard(cardName);
    mPaymentParams.setExpiryMonth(expiryMonth);// MM
    mPaymentParams.setExpiryYear(expiryYear);// YYYY  
    mPaymentParams.setCvv(cvv);
  • To enable one click payment

    // To store one click payment hash pass 1
    mPaymentParams.setEnableOneClickPayment(1);
    // Otherwise pass 0, OR don't call the method at all
    mPaymentParams.setEnableOneClickPayment(0);
  • Create PaymentPostParams object with the above data of the type PostData. Make sure to set the environment (test or prod), create intent and launch the activity (PaymentsActivity if you are using PayU's webview)

    PostData postData = new PaymentPostParams(mPaymentParams, PayuConstants.CC).getPaymentPostParams();
    if (postData.getCode() == PayuErrors.NO_ERROR) {
      // launch webview
      PayuConfig payuConfig = new PayuConfig();
      payuConfig.setEnvironment(PayuConstants.PRODUCTION_ENV);
      payuConfig.setData(postData.getResult());
      Intent intent = new Intent(this,PaymentsActivity.class);
      intent.putExtra(PayuConstants.PAYU_CONFIG,payuConfig);
      startActivityForResult(intent, PayuConstants.PAYU_REQUEST_CODE);
    } else {
      // something went wrong
      Toast.makeText(this,postData.getResult(), Toast.LENGTH_LONG).show();
    }
  • Create an activity for Webview, in case you are not using PayU's webview activity (PaymentsActivity)

    @Override
    protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_payments);
      // initialize
      Bundle bundle =getIntent().getExtras();
      PayuConfig payuConfig =bundle.getParcelable(PayuConstants.PAYU_CONFIG);
      WebView mWebView =(WebView) findViewById(R.id.webview);
      URL url =payuConfig.getEnvironment() == PayuConstants.PRODUCTION_ENV ? PayuConstants.PRODUCTION_PAYMENT_URL :PayuConstants.MOBILE_TEST_PAYMENT_URL; 
      byte[] encodedData = EncodingUtils.getBytes(payuConfig.getData(), "base64");
      mWebView.postUrl(url,encodedData);
      mWebView.getSettings().setSupportMultipleWindows(true);
      mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
      mWebView.getSettings().setJavaScriptEnabled(true);
      mWebView.getSettings().setDomStorageEnabled(true);
      mWebView.setWebChromeClient(new WebChromeClient() {}); 
      mWebView.setWebViewClient(new WebViewClient() {});
    }

    At this point you should be able to see Bank’s 3D Secure page

2.2.2. Netbanking

  • Create an object of PaymentParams, put all the obtained parameters in it by using its default set methods in the same way as you will create for the non-seamless style and setHash to paymentHash

  • Get the bankCode (String) of selected bank from your spinner/list view adapter and add it to the mPaymentParams created above

    mPaymentParams.setBankCode(bankCode);
  • Create PaymentPostParams object of the type PostData

    PostData postData = new PaymentPostParams(mPaymentParams, PayuConstants.NB).getPaymentPostParams();
    if (postData.getCode() == PayuErrors.NO_ERROR){
      // launch webview
      PayuConfig payuConfig = new PayuConfig();
      payuConfig.setEnvironment(PayuConstants.PRODUCTION_ENV);
      payuConfig.setData(postData.getResult());
      Intent intent = new Intent(this,PaymentsActivity.class); 
      intent.putExtra(PayuConstants.PAYU_CONFIG,payuConfig); 
      startActivityForResult(intent, PayuConstants.PAYU_REQUEST_CODE);
    } else {
      // something went wrong
      Toast.makeText(this,postData.getResult(), Toast.LENGTH_LONG).show(); 
    }
  • Create an activity for Webview just like for CC

2.2.3. Cash card

  • Create an object of PaymentParams, put all the obtained parameters in it by using its default set methods in the same way as you will create for the non-seamless style and setHash to paymentHash

  • Get the bankCode (String) of selected cash card from your spinner/list view adapter and add it to the mPaymentParams created above

    mPaymentParams.setBankCode(bankCode);
  • Create PaymentPostParams object of the type PostData

    PostData postData = new PaymentPostParams(mPaymentParams, PayuConstants.CASH).getPaymentPostParams();
    if (postData.getCode() == PayuErrors.NO_ERROR){
      // launch webview
      PayuConfig payuConfig = new PayuConfig();
      payuConfig.setEnvironment(PayuConstants.PRODUCTION_ENV);
      payuConfig.setData(postData.getResult());
      Intent intent = new Intent(this,PaymentsActivity.class); 
      intent.putExtra(PayuConstants.PAYU_CONFIG,payuConfig); 
      startActivityForResult(intent, PayuConstants.PAYU_REQUEST_CODE);
    } else {
      // something went wrong
      Toast.makeText(this,postData.getResult(), Toast.LENGTH_LONG).show(); 
    }
  • Create an activity for Webview just like for CC

2.2.4. Store Cards

  • Create an object of PaymentParams, put all the obtained parameters in it by using its default set methods in the same way as you will create for the non-seamless style and setHash to paymentHash

  • Set the following additional properties to mPaymentParams

    mPaymentParams.setCardToken(cardToken);
    mPaymentParams.setCvv(cvv);
    mPaymentParams.setNameOnCard(cardName);
    mPaymentParams.setExpiryMonth(expiryMonth);// MM
    mPaymentParams.setExpiryYear(expiryYear);// YYYY  
    mPaymentParams.setCardName(storedCard.getCardName());
  • To enable one click payment

    // To store one click payment hash pass 1
    mPaymentParams.setEnableOneClickPayment(1);
    // Otherwise pass 0, OR don't call the method at all
    mPaymentParams.setEnableOneClickPayment(0);
  • Create PaymentPostParams object with the above data of the type PostData. Make sure to set the environment (test or prod), create intent and launch the activity (PaymentsActivity if you are using PayU's webview)

    PostData postData = new PaymentPostParams(mPaymentParams, PayuConstants.CC).getPaymentPostParams();
    if (postData.getCode() == PayuErrors.NO_ERROR) {
      // launch webview
      PayuConfig payuConfig = new PayuConfig();
      payuConfig.setEnvironment(PayuConstants.PRODUCTION_ENV);
      payuConfig.setData(postData.getResult());
      Intent intent = new Intent(this,PaymentsActivity.class);
      intent.putExtra(PayuConstants.PAYU_CONFIG,payuConfig);
      startActivityForResult(intent, PayuConstants.PAYU_REQUEST_CODE);
    } else {
      // something went wrong
      Toast.makeText(this,postData.getResult(), Toast.LENGTH_LONG).show();
    }
  • Create an activity for Webview just like for CC

2.2.4. One Click Payment Cards

  • Create an object of PaymentParams, put all the obtained parameters in it by using its default set methods in the same way as you will create for the non-seamless style and setHash to paymentHash

  • Set the following additional properties to mPaymentParams

    mPaymentParams.setCardToken(cardToken);
    mPaymentParams.setNameOnCard(cardName);
    mPaymentParams.setExpiryMonth(expiryMonth);// MM
    mPaymentParams.setExpiryYear(expiryYear);// YYYY  
    mPaymentParams.setCardName(storedCard.getCardName());
    // make payment using one click payment.
    mPaymentParams.setCardCvvMerchant(merchantHash);

    Where MerchantHash is given by PayU when you store a card as one click payment card.

  • Identifying Stored cards and One click cards.

case 1: merchantHash is stored in Merchant's server. (PayuConstants.STORE_ONE_CLICK_HASH_SERVER)

   ArrayList<StoredCard> storedCards;
   ArrayList<StoredCard> oneClickCards;
   HashMap<String, String> oneClickCardTokens;
   storedCardMap = new PayuUtils().getStoredCard(mPayuResponse.getStoredCards(), oneClickCardTokens);
   storedCards = storedCardMap.get(PayuConstants.STORED_CARD);
   oneClickCards = storedCardMap.get(PayuConstants.ONE_CLICK_CHECKOUT);

case 2: merchantHash is stored in mobile. (PayuConstants.STORE_ONE_CLICK_HASH_MOBILE)

   ArrayList<StoredCard> storedCards;
   ArrayList<StoredCard> oneClickCards;
   HashMap<String, String> oneClickCardTokens;

   storedCardMap = new PayuUtils().getStoredCard(this, mPayuResponse.getStoredCards());
   storedCards = storedCardMap.get(PayuConstants.STORED_CARD);
   oneClickCards = storedCardMap.get(PayuConstants.ONE_CLICK_CHECKOUT);

case 3: merchantHash is not stored. (PayuConstants.STORE_ONE_CLICK_HASH_NONE)

   ArrayList<StoredCard> storedCards;
   storedCards = payuResponse.getStoredCards();

note:

  1. mPayuResponse.getStoredCards() is the list of stored cards(including one click cards) you get from payu server.

  2. HashMap\<String, String> oneClickCardTokens is the hash map of card token and corresponding merchant hash for the card. Which was given by payu after successful transaction.

  3. Enabling one click payment will be done only after success transaction.
  • Create PaymentPostParams object with the above data of the type PostData. Make sure to set the environment (test or prod), create intent and launch the activity (PaymentsActivity if you are using PayU's webview)

    PostData postData = new PaymentPostParams(mPaymentParams, PayuConstants.CC).getPaymentPostParams();
    if (postData.getCode() == PayuErrors.NO_ERROR) {
      // launch webview
      PayuConfig payuConfig = new PayuConfig();
      payuConfig.setEnvironment(PayuConstants.PRODUCTION_ENV);
      payuConfig.setData(postData.getResult());
      Intent intent = new Intent(this,PaymentsActivity.class);
      intent.putExtra(PayuConstants.PAYU_CONFIG,payuConfig);
      startActivityForResult(intent, PayuConstants.PAYU_REQUEST_CODE);
    } else {
      // something went wrong
      Toast.makeText(this,postData.getResult(), Toast.LENGTH_LONG).show();
    }
  • Create an activity for Webview just like for CC

2.2.4. PayUMoney

  • Create an object of PaymentParams, put all the obtained parameters in it by using its default set methods in the same way as you will create for the non-seamless style and setHash to paymentHash

  • Create PaymentPostParams object of the type PostData

    PostData postData = new PaymentPostParams(mPaymentParams, PayuConstants.PAYU_MONEY).getPaymentPostParams();
    if (postData.getCode() == PayuErrors.NO_ERROR){
      // launch webview
      // launch webview
      PayuConfig payuConfig = new PayuConfig();
      payuConfig.setEnvironment(PayuConstants.PRODUCTION_ENV);
      payuConfig.setData(postData.getResult());
      Intent intent = new Intent(this,PaymentsActivity.class);
      intent.putExtra(PayuConstants.PAYU_CONFIG,payuConfig);
      startActivityForResult(intent, PayuConstants.PAYU_REQUEST_CODE);
    } else {
      // something went wrong
      Toast.makeText(this,postData.getResult(), Toast.LENGTH_LONG).show();
    }

2.2.5. One click payment - getting back one click data

  • If using SDK with PayU's CUSTOM BROWSER

    args.putInt(Bank.STORE_ONE_CLICK_HASH, storeOneClickHash);
  • If using ONLY SDK - Add the following Javascriptinterface code where your webview lies

    @JavascriptInterface
                    public void onMerchantHashReceived(final String result) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                switch (storeOneClickHash){
                                    case PayuConstants.STORE_ONE_CLICK_HASH_MOBILE:
                                        try {
                                            JSONObject hashObject = new JSONObject(result);
    
                                            new PayuUtils().storeInSharedPreferences(PaymentsActivity.this, hashObject.getString(PayuConstants.CARD_TOKEN), hashObject.getString(PayuConstants.MERCHANT_HASH));
                                        } catch (JSONException e) {
                                            e.printStackTrace();
    
                                        }
                                        break;
                                    case PayuConstants.STORE_ONE_CLICK_HASH_SERVER:
                                        merchantHash = result;
                                        break;
                                    case PayuConstants.STORE_ONE_CLICK_HASH_NONE:
                                        break;
                                }
                            }
                        });
                    }
    
    @JavascriptInterface
    public void onSuccess(final String result) {
    runOnUiThread(new Runnable() {
    @Override
    public void run() {
    
    Intent intent = new Intent();
    intent.putExtra("result", result);
    if(storeOneClickHash == PayuConstants.STORE_ONE_CLICK_HASH_SERVER && null != merchantHash){
    intent.putExtra(PayuConstants.MERCHANT_HASH, merchantHash);
    }
    setResult(Activity.RESULT_OK, intent);
    finish();
    
    }
    // }
    });
    }

NOTE: Refer this link for surl-furl implementation to understand more why onSuccess JS interface is needed.

  • Once callback has been made by PayU server, if merchant has opted to store this data on their server they need to put the following lines of code in their onActivityResult method

    if(data.hasExtra(PayuConstants.MERCHANT_HASH)){ // we have merchant hash, lets store it merchant server.
                        storeMerchantHash(data.getStringExtra(PayuConstants.MERCHANT_HASH));
                    }

storeMerchantHash(String merchantHash) function should store the merchant hash at merchant's server

Sample Code:

    private void storeMerchantHash(String merchantHashData){
        try {
            JSONObject merchantHashObject = new JSONObject(merchantHashData);
            String merchantHash = merchantHashObject.getString(PayuConstants.MERCHANT_HASH);
            String cardToken  = merchantHashObject.getString(PayuConstants.CARD_TOKEN);
            final String postParams = "merchant_key="+key+"&user_credentials="+var1+"&card_token="+cardToken+"&merchant_hash="+merchantHash;

                new AsyncTask<Void, Void, Void>(){
                    @Override
                    protected Void doInBackground(Void... params) {
                        // code to store the merchant hash params. 
                    }

                }.execute();
        } catch (JSONException e) {
        e.printStackTrace();
        }
    }
  • Create an activity for Webview just like for CC