How to Integrate PayKun Payment Gateway in an Android Application

Why is the payment gateway integration in Android important?

The global pandemic has played a significant role in drifting the economy towards digital payment adoption. Mobile application commerce is in the driver’s seat alongside the digital payments that have been gaining control.

payment gateway integration in Android

India being a mobile-first country has made it kind of mandatory for the eCommerce businesses to also adopt m-commerce (mobile commerce). This is when the business and other institutes would have a sheer need for an ideal payment gateway integration in Android apps. Due to the ready PayKun Android SDKs, PayKun android integration is smooth and the easiest.

PayKun Payment Gateway Integration in Android

The merchants can collect secure and simple online payments from their domestic and international customers through the PayKun android integration to their apps.

They can provide multiple payment modes including debit cards, credit cards, wallets, net banking, QR code, UPI, and even credit card EMI. PayKun provides a smooth and sleek checkout experience to the customers helping in reducing customer dropouts.

You need to get your registered PayKun Merchant Account activated to process the payments in live mode through your android app.

Check out this video for a stepwise visual guide on PayKun integration with Android:

[You may get the demo app from here – Paykun Demo Android App]

1.  Installing PayKun Android SDK in your app

We have distributed our SDK via Maven Central Repository. You can add our latest PayKun SDK directly to your build.gradle file in the dependency section using the below-given line:

Our latest PayKun SDK can be added directly to your build.gradle file in the dependency section with-

implementation ‘com.paykun.sdk:paykun-androidx-sdk:1.1.17’

2.  Implementing SDK into your Android APP

Login to the PayKun Dashboard using Live mode

Get your Merchant ID and Access Token from there

Pass the below details to SDK –

merchant id, access token, customer name, customer email, customer phone, product name, order no, and amount to SDK.

Note: order number needs to be unique; the duplicate order number will result in the invalid request

  • Paykun SDK is hosted in maven Central so first confirm that you have ‘mavenCentral()’ in your repositories section.
allprojects {
    repositories {
        mavenCentral()
    }
}
  • Performing transaction
// Initiate Transaction
public void createPayment()
{
	// Required data to be provided
	String merchantIdLive="<Marcent Id>";
	String accessTokenLive="<Mobile Access Token>";
	String productName="<Product Name>";
	String orderId= String.valueOf(System.currentTimeMillis()); // You can change this based on your requirement
	String amount="<Amount>";
	String customerName="<Customer Name>";
	String customerPhone="<Customer Mobile No>";
	String customerEmail="<Customer Email Id>";
	
	// You can customize which payment method should be provided
	// Here is the example for payment method customization, This is optional.
	
	// Create Map for payment methods
	HashMap<PaymentTypes, PaymentMethods> payment_methods = new HashMap<>();
	
	// Create payment method object to be added in payment method Map
	PaymentMethods paymentMethod = new PaymentMethods();
	paymentMethod.enable=true; // True if you want to enable this method or else false, default is true
	paymentMethod.priority=1; // Set priority for payment method order at checkout page
	paymentMethod.set_as_prefered=true; // If you want this payment method to show in prefered payment method then set it to true
	
	// Add payment method into our Map
	payment_methods.put(PaymentTypes.UPI, paymentMethod);
	
	// Example for netbanking
	paymentMethod = new PaymentMethods();
	paymentMethod.enable=true;
	paymentMethod.priority=2;
	paymentMethod.set_as_prefered=true;
	paymentMethod.sub_methods.add(new Sub_Methods(SubPaymentTypes.SBIN,1));
	paymentMethod.sub_methods.add(new Sub_Methods(SubPaymentTypes.HDFC,1));
	payment_methods.put(PaymentTypes.NB, paymentMethod);
	
	// Example for wallet
	paymentMethod = new PaymentMethods();
	paymentMethod.enable=false;
	paymentMethod.priority=3;
	paymentMethod.set_as_prefered=false;
	payment_methods.put(PaymentTypes.WA, paymentMethod);
	
	// Example for card payment
	paymentMethod = new PaymentMethods();
	paymentMethod.enable=true;
	paymentMethod.priority=3;
	paymentMethod.set_as_prefered=true;
	payment_methods.put(PaymentTypes.DCCC, paymentMethod);
	
	// Example for UPI Qr
	paymentMethod = new PaymentMethods();
	paymentMethod.enable=true;
	paymentMethod.priority=3;
	paymentMethod.set_as_prefered=true;
	payment_methods.put(PaymentTypes.UPIQRCODE, paymentMethod);
	
	// Example for EMI
	paymentMethod = new PaymentMethods();
	paymentMethod.enable=true;
	paymentMethod.priority=3;
	paymentMethod.set_as_prefered=true;
	payment_methods.put(PaymentTypes.EMI, paymentMethod);
	
	// Now, Create object for paykun transaction
	PaykunTransaction paykunTransaction=new PaykunTransaction(merchantIdLive,accessTokenLive,true);
	
	try {
		// Set all request data
		paykunTransaction.setCurrency("INR");
		paykunTransaction.setCustomer_name(customerName);
		paykunTransaction.setCustomer_email(customerEmail);
		paykunTransaction.setCustomer_phone(customerPhone);
		paykunTransaction.setProduct_name(productName);
		paykunTransaction.setOrder_no(orderId);
		paykunTransaction.setAmount(amount);
		paykunTransaction.setLive(true); // Currently only live transactions is supported so keep this as true
		
		// Optionally you can customize color and merchant logo for checkout page
		paykunTransaction.setTheme_color("<COLOR CODE>");
		paykunTransaction.setTheme_logo("<LOGO URL>");
		
		// Set the payment methods Map object that we have prepared above, this is optional
		paykunTransaction.setPayment_methods(payment_methods);
		
		new PaykunApiCall.Builder(PaymentActivity.this).sendJsonObject(paykunTransaction);
	} catch (Exception e) {
		e.printStackTrace();
	}
}
  • Handling transaction callback (Response)
  • You will get the result in PaymentActivity class after sending your data to Paykun.
  • Handling Payment callback, This Activity/Fragment Must implement PaykunResponseListener interface
  • This interface provides callback methods ‘onPaymentSuccess’ and ‘onPaymentError’
public class PaymentActivity extends AppCompatActivity implements PaykunResponseListener 
{
	@Override
    public void onPaymentSuccess(PaymentMessage message) {
        Log.e("onPaymentSuccess",message.toString());
        Toast.makeText(this,"Your Transaction is Success With Id "+message.getTransactionId(),Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onPaymentError(PaymentMessage message) {
        if(message.getResults().equalsIgnoreCase(PaykunHelper.MESSAGE_CANCELLED)){
            // do your stuff here
            Toast.makeText(this,"Your Transaction is cancelled",Toast.LENGTH_SHORT).show();
        }
        else if(message.getResults().equalsIgnoreCase(PaykunHelper.MESSAGE_FAILED)){
            // do your stuff here
            Toast.makeText(this,"Your Transaction is failed",Toast.LENGTH_SHORT).show();
        }
        else if(message.getResults().equalsIgnoreCase(PaykunHelper.MESSAGE_NETWORK_NOT_AVAILABLE)){
            // do your stuff here
            Toast.makeText(this,"Internet Issue",Toast.LENGTH_SHORT).show();

        }else if(message.getResults().equalsIgnoreCase(PaykunHelper.MESSAGE_SERVER_ISSUE)){
            // do your stuff here
            Toast.makeText(this,"Server issue",Toast.LENGTH_SHORT).show();
        }else if(message.getResults().equalsIgnoreCase(PaykunHelper.MESSAGE_ACCESS_TOKEN_MISSING)){
            // do your stuff here
            Toast.makeText(this,"Access Token missing",Toast.LENGTH_SHORT).show();
        }
        else if(message.getResults().equalsIgnoreCase(PaykunHelper.MESSAGE_MERCHANT_ID_MISSING)){
            // do your stuff here
            Toast.makeText(this,"Merchant Id is missing",Toast.LENGTH_SHORT).show();
        }
        else if(message.getResults().equalsIgnoreCase(PaykunHelper.MESSAGE_INVALID_REQUEST)){
            Toast.makeText(this,"Invalid Request",Toast.LENGTH_SHORT).show();
        }
    }
}

PayKun provides free integration assistance, simply drop an email at [email protected] or contact your account manager. You can view the video tutorial for PayKun Android Integration here

You May Also Like

About the Author: PayKun