Mobile SDK for Android

Integrating PayHere with your Android Mobile App is super easy with our PayHere Mobile SDK for Android. 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 Mobile 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 Mobile 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:0.1.5'
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.5' // 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
InitRequest req = new InitRequest();
req.setMerchantId("1210251"); // Your Merchant ID
req.setMerchantSecret("bhasha2"); // Your Merchant secret
req.setAmount(1000.00); // Amount which the customer should pay
req.setCurrency("LKR"); // Currency
req.setOrderId("ItemNo12345"); // Unique ID for your payment transaction
req.setItemsDescription("Door bell wireless");  // Item title or Order/Invoice number
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");
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));

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);
        }
    }


Mobile SDK for Android - PayHere Knowledge Base

Still need help? Get in touch!
Last updated on 11th Jun 2018