Android SDK

Integrating PayHere with your Android Mobile App is super easy with our native PayHere Android SDK. You just have to include that in your Android app project & call its methods to initiate a payment & fetch the payment status just after the payment. The beauty is, this Android SDK allows you to accept a payment within your app, without redirecting your app user to the web browser.

Please refer the below steps to integrate PayHere Android SDK to your Android App.

1. Include PayHere Mobile SDK to your App

Add maven URL to main build.gradle file

 maven {
            url  "https://dl.bintray.com/payhere/payhere-android-payment"
       }

Example build.gradle after adding the line

allprojects {
    repositories {
        google()
        jcenter()
        maven {
            url  "https://dl.bintray.com/payhere/payhere-android-payment"
        }
    }
}


2. Add SDK dependency

Add PayHere Mobile SDK dependency to the necessary module with appcompat and gson libraries. In most cases .\app\build.gradle file

implementation 'lk.payhere.sdk:android-payment-sdk:2.0.24'  // New PayHere 2.0 SDK!
implementation 'com.android.support:appcompat-v7:25.4.0' // ignore if you have already added
implementation 'com.google.code.gson:gson:2.8.0' // ignore if you have already added

Example build.gradle after adding the line

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'lk.payhere.sdk:android-payment-sdk:0.1.6' // payhere payment sdk
    implementation 'com.google.code.gson:gson:2.8.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}


3. Add Internet Permission to AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />


4. Initiate a Payment Request to PayHere Payment Gateway

a. One-time Payment Request

InitRequest req = new InitRequest();
req.setMerchantId("1210XXX");       // Your Merchant PayHere ID
req.setMerchantSecret("ugknesnvn"); // Your Merchant secret (Add your app at Settings > Domains & Credentials, to get this))
req.setCurrency("LKR");             // Currency code LKR/USD/GBP/EUR/AUD
req.setAmount(1000.00);             // Final Amount to be charged
req.setOrderId("230000123");        // Unique Reference ID
req.setItemsDescription("Door bell wireless");  // Item description title 
req.setCustom1("This is the custom message 1");
req.setCustom2("This is the custom message 2");
req.getCustomer().setFirstName("Saman");
req.getCustomer().setLastName("Perera");
req.getCustomer().setEmail("samanp@gmail.com");
req.getCustomer().setPhone("+94771234567");
req.getCustomer().getAddress().setAddress("No.1, Galle Road");
req.getCustomer().getAddress().setCity("Colombo");
req.getCustomer().getAddress().setCountry("Sri Lanka");

//Optional Params
req.getCustomer().getDeliveryAddress().setAddress("No.2, Kandy Road");
req.getCustomer().getDeliveryAddress().setCity("Kadawatha");
req.getCustomer().getDeliveryAddress().setCountry("Sri Lanka");
req.getItems().add(new Item(null, "Door bell wireless", 1, 1000.0));

Intent intent = new Intent(this, PHMainActivity.class);
intent.putExtra(PHConstants.INTENT_EXTRA_DATA, req);
PHConfigs.setBaseUrl(PHConfigs.SANDBOX_URL);
startActivityForResult(intent, PAYHERE_REQUEST); //unique request ID like private final static int PAYHERE_REQUEST = 11010;

b. Recurring Payment Request

InitRequest req = new InitRequest();

req.setMerchantId("1210XXX");       // Your Merchant ID  
req.setMerchantSecret("ugknesnvn"); // Your Merchant Secret (Add your app at Settings > Domains & Credentials, to get this) 
req.setCurrency("LKR");             // Currency code LKR/USD/GBP/EUR/AUD
req.setAmount(250.00);              // Final Amount to be charged
req.setRecurrence("1 Month");       // Recurrence of the Subscription
req.setDuration("Forever");         // Duration of the Subscription
req.setOrderId("230000124");        // Unique Reference ID
req.setCustom1("This is the custom message 1");
req.setCustom2("This is the custom message 2");
req.getCustomer().setFirstName("Saman");
req.getCustomer().setLastName("Perera");
req.getCustomer().setEmail("sampanp@gmail.com");
req.getCustomer().setPhone("+947771234567");
req.getCustomer().getAddress().setAddress("No.1, Galle Road");
req.getCustomer().getAddress().setCity("Colombo");
req.getCustomer().getAddress().setCountry("Sri Lanka");
req.setItemsDescription("PRO Plan Subscription");

// Optional Param
req.setStartupFee(0);               // +/- Adjustment to the fist charge 

Intent intent = new Intent(this, PHMainActivity.class);
intent.putExtra(PHConstants.INTENT_EXTRA_DATA, req);
PHConfigs.setBaseUrl(PHConfigs.SANDBOX_URL);
startActivityForResult(intent, PAYHERE_REQUEST); //unique request ID like private final static int PAYHERE_REQUEST = 11010;

c. Preapproval Request

InitPreapprovalRequest req = new InitPreapprovalRequest();

req.setMerchantId("1210XXX");       // Your Merchant ID 
req.setMerchantSecret("ugknesnvn"); // Your Merchant Secret (Add your app at Settings > Domains & Credentials, to get this)
req.setOrderId("230000125");        // Unique Reference ID
req.setCurrency("LKR");             // Currency code of future payments
req.setItemsDescription("1 Greeting Card");
req.setCustom1("This is the custom 1 message");
req.setCustom2("This is the custom 2 message");
req.getCustomer().setFirstName("Saman");
req.getCustomer().setLastName("Perera");
req.getCustomer().setEmail("samanp@email.com");
req.getCustomer().setPhone("+947771234567");
req.getCustomer().getAddress().setAddress("No.01, Galle Road,");
req.getCustomer().getAddress().setCity("Colombo");
req.getCustomer().getAddress().setCountry("Sri Lanka");

Intent intent = new Intent(this, PHMainActivity.class);
intent.putExtra(PHConstants.INTENT_EXTRA_DATA, req);
PHConfigs.setBaseUrl(PHConfigs.SANDBOX_URL);
startActivityForResult(intent, PAYHERE_REQUEST); //unique request ID like private final static int PAYHERE_REQUEST = 11010;


5. Fetch the Payment Status

Example code

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //TODO process response
        if (requestCode == PAYHERE_REQUEST && data != null && data.hasExtra(PHConstants.INTENT_EXTRA_RESULT)) {
            PHResponse<StatusResponse> response = (PHResponse<StatusResponse>) data.getSerializableExtra(PHConstants.INTENT_EXTRA_RESULT);
            String msg;
            if (response.isSuccess()) {
                msg = "Activity result:" + response.getData().toString();
                Log.d(TAG, msg);
            } else {
                msg = "Result:" + response.toString();
                Log.d(TAG, msg);
            }
            textView.setText(msg);
        }
    }


PayHere Android SDK - PayHere Knowledge Base

Still need help? Get in touch!
Last updated on 24th Jul 2020