Skip to content

Android SDK Integration

himgupta229 edited this page Sep 10, 2019 · 2 revisions

Android Integration

Gradle: compile 'com.payu.india:payu-sdk:4.4.8'

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(“https://payu.herokuapp.com/success”);
      mPaymentParams.setFurl(“https://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);
    
  • 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);
    
  • 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);
        String 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());
    
  • 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

  • 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.6. UPI

Prerequisites for integrating UPI
  • Enable UPI as payment option in merchant panel
  • Merchant Key
  • Merchant salt
  • User Credential

Step1. Fetch Payment option available (Check if UPI as payment option is available for you or not)

1-Initialise PayuConfig with environment to test

2-Initialize MerchantWebService with mentioned data points

MerchantWebService merchantWebService = new MerchantWebService();
merchantWebService.setKey(merchantKey); // Merchant key
merchantWebService.setCommand(PayuConstants.PAYMENT_RELATED_DETAILS_FOR_MOBILE_SDK); // Command for fetching payment related details
merchantWebService.setVar1(userCredential) // User Credential of the merchant
merchantWebService.setHash(paymentRelatedHash) //Hash for fetching payment related details such as payment options

3-Fetch PostData

PostData postData = new MerchantWebServicePostParams(merchantWebService).getMerchantWebServicePostParams();
 if (postData.getCode() == PayuErrors.NO_ERROR) {
   payuConfig.setData(postData.getResult());
 }

Note: If PostData has no error - you are good to go else please check the data point set in merchantWebService

4-Make an object GetPaymentRelatedDetailsTask

 GetPaymentRelatedDetailsTask paymentRelatedDetailsForMobileSdkTask = new GetPaymentRelatedDetailsTask(this);
 //where ,
 //Input param (this) is the instance of class which implements PaymentRelatedDetailsListener
 //‘PaymentRelatedDetailsListener’ is interface with abstract method, which is
 //public void onPaymentRelatedDetailsResponse(PayuResponse payuResponse) 

5-Once you call ‘execute’, the GetPaymentRelatedDetailsTask, ‘onPaymentRelated Details Response’ callback is called

@Override
public void onPaymentRelatedDetailsResponse(PayuResponse payuResponse) {
mPayuResponse = payuResponse;
// Check if UPI as payment option available.
 if(payuResponse.isUpiAvailable()){
 // UPI as payment option is available
 }
}

Step2-Payment with UPI as Payment Option

1-Initialize PaymentParams with mentioned data points

PaymentParams mPaymentParams = new PaymentParams();
mPaymentParams.setKey(merchantKey)
mPaymentParams.setAmount(amount)
mPaymentParams.setProductInfo(productInfo)
mPaymentParams.setFirstName(firstName)
mPaymentParams.setEmail(emailId)
mPaymentParams.setTxnId(transactionID)
mPaymentParams.setSurl(surl) // Success URL
mPaymentParams.setFurl(furl) // Failure URL
mPaymentParams.setUdf1(udf1)
mPaymentParams.setUdf2(udf2)
mPaymentParams.setUdf3(udf3)
mPaymentParams.setUdf4(udf4)
mPaymentParams.setHash(paymentHash); // Payment Hash for making payment
mPaymentParams.setUdf5(udf5)
mPaymentParams.setVpa(virtualAddress) // Virtual address (must for UPI payment)

Note: Validations for virtual address

a.Vpa length should be less than or equal to 50

b.Regex for VPA : value.match(/^([A-Za-z0-9.])+@[A-Za-z0-9]+$/)

2-Fetch PostData

PostData mPostData = new PaymentPostParams(mPaymentParams, PayuConstants.UPI).getPaymentPostParams();
 if (mPostData.getCode() == PayuErrors.NO_ERROR) {
 payuConfig.setData(mPostData.getResult());
 }

Note: If mPostData contain no error you are good to go else check PaymentParams

3-Post data to PayU

webview.postUrl(url, payuConfig.getData().getBytes());
//where, url - PayU Payment URL

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

  • Create an activity for Webview just like for CC

2.2.7. Tez

Prerequisites for integrating Tez
  • Enable Tez as payment option in merchant panel
  • Merchant Key
  • Merchant salt
  • User Credential

Step1. Fetch Payment option available (Check if Tez as payment option is available for you or not)

1-Initialise PayuConfig with environment to test

2-Initialize MerchantWebService with mentioned data points

MerchantWebService merchantWebService = new MerchantWebService();
merchantWebService.setKey(merchantKey); // Merchant key
merchantWebService.setCommand(PayuConstants.PAYMENT_RELATED_DETAILS_FOR_MOBILE_SDK); // Command for fetching payment related details
merchantWebService.setVar1(userCredential) // User Credential of the merchant
merchantWebService.setHash(paymentRelatedHash) //Hash for fetching payment related details such as payment options

3-Fetch PostData

PostData postData = new MerchantWebServicePostParams(merchantWebService).getMerchantWebServicePostParams();
 if (postData.getCode() == PayuErrors.NO_ERROR) {
   payuConfig.setData(postData.getResult());
 }

Note: If PostData has no error - you are good to go else please check the data point set in merchantWebService

4-Make an object GetPaymentRelatedDetailsTask

 GetPaymentRelatedDetailsTask paymentRelatedDetailsForMobileSdkTask = new GetPaymentRelatedDetailsTask(this);
 //where ,
 //Input param (this) is the instance of class which implements PaymentRelatedDetailsListener
 //‘PaymentRelatedDetailsListener’ is interface with abstract method, which is
 //public void onPaymentRelatedDetailsResponse(PayuResponse payuResponse) 

5-Once you call ‘execute’, the GetPaymentRelatedDetailsTask, ‘onPaymentRelated Details Response’ callback is called

@Override
public void onPaymentRelatedDetailsResponse(PayuResponse payuResponse) {
mPayuResponse = payuResponse;
// Check if Tez as payment option available.
 if(payuResponse.isGoogleTezAvailable()){
 // Tez as payment option is available
 }
}

Step2-Payment with Tez as Payment Option

1-Initialize PaymentParams with mentioned data points

PaymentParams mPaymentParams = new PaymentParams();
mPaymentParams.setKey(merchantKey)
mPaymentParams.setAmount(amount)
mPaymentParams.setProductInfo(productInfo)
mPaymentParams.setFirstName(firstName)
mPaymentParams.setEmail(emailId)
mPaymentParams.setTxnId(transactionID)
mPaymentParams.setSurl(surl) // Success URL
mPaymentParams.setFurl(furl) // Failure URL
mPaymentParams.setUdf1(udf1)
mPaymentParams.setUdf2(udf2)
mPaymentParams.setUdf3(udf3)
mPaymentParams.setUdf4(udf4)
mPaymentParams.setHash(paymentHash); // Payment Hash for making payment
mPaymentParams.setUdf5(udf5)
mPaymentParams.setVpa(virtualAddress) // Tez Virtual address (Optional if set address will be auto populated on payment screen)

2-Fetch PostData

PostData mPostData = new PaymentPostParams(mPaymentParams, PayuConstants.TEZ).getPaymentPostParams();
 if (mPostData.getCode() == PayuErrors.NO_ERROR) {
 payuConfig.setData(mPostData.getResult());
 }

Note: If mPostData contain no error you are good to go else check PaymentParams

3-Post data to PayU

webview.postUrl(url, payuConfig.getData().getBytes());
//where, url - PayU Payment URL

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

  • Create an activity for Webview just like for CC

PhonePe Intent Integration changes for PayUbiz Mobile SDK

1- Payment Related Details

PhonePe Intent is returned as payment option, when it is enabled from merchant panel, in onPaymentRelatedDetailsResponse(PayuResponse payuResponse)

1.1) Check Availability of PhonePe Intent - (Optional)

   Method - isPhonePeIntentAvailable()

   Return: true – PhonePe Intent is enabled

   false – PhonePe Intent is disable

   Example: payuResponse.isPhonePeIntentAvailable()

1.2) Get Post Data for PhonePe Intent

Create PaymentPostParams object with the above data(similar to other payment options) of the type PostData

   PostData mPostData = new PaymentPostParams(mPaymentParams,PayuConstants.PHONEPE_INTENT).getPaymentPostParams();

LazyPay Integration changes for PayUbiz Mobile SDK

1- Payment Related Details

LazyPay is returned as payment option, when it is enabled from merchant panel, in onPaymentRelatedDetailsResponse(PayuResponse payuResponse)

1.1) Check Availability of LazyPay - (Optional)

Method - isLazyPayAvailable()

Return: true – LazyPay is enabled

false – LazyPay is disable

Example: payuResponse. isLazyPayAvailable()

1.2) payuResponse.getLazyPay(): Return LazyPay list which contain description about lazy pay.

2- Payment Params for LazyPay

2.1) Notify Url – Callback URL of merchant where a notification of transaction status will be sent on completion of transaction. It should be HTTPS.

  • Get all the required parameters

  • Create an object of PaymentParams

PaymentParams mPaymentParams = new PaymentParams(); // set other payment params in mPaymentParams

mPaymentParams.setNotifyURL(mPaymentParams.getSurl());

2.2) Get Post Data for LazyPay

Create PaymentPostParams object with the above data of the type PostData

PostData mPostData = new PaymentPostParams(mPaymentParams, PayuConstants.LAZYPAY).getPaymentPostParams();

IMPORTANT: For Risk analysis and cover please take following permission from user:

  1. <uses-permission android:name="android.permission.READ_PHONE_STATE" />
Clone this wiki locally