Skip to content

Khalti Android SDK

Khalti Payment Gateway SDK for Andriod with default payment interface, works out of the box without having to add any additional user interface.


Khalti Payment Gateway

Maven Central Khalti Docs BSD-3 License GitHub issues Website Follow Khalti in Facebook Follow Khalti in Instagram Follow Khalti in Twitter Subscribe Youtube Channel

Installation

Read the introduction here.

Getting Started

Integrating Khalti Payment Gateway requires merchant account. You can always create one easily from here.

Tip

A merchant account is required for integration.

Access Information

For Sandbox Access

Signup from here as a merchant.

Please use 987654 as login OTP for sandbox env.

For Production Access

Please visit here

Test Credentials for sandbox environment

Test Khalti ID for 9800000000 9800000001 9800000002 9800000003 9800000004 9800000005

Test MPIN 1111

Test OTP 987654

Read the steps to integrate Khalti Payment Gateway in details here.

Requirements

  • Android 5.0 and above
  • AndroidX
  • Android Studio 3 and above

Configuration

Add checkout-android to your build.gradle dependencies

implementation "com.khalti:checkout-android:$latest_version"

Also add the following lines inside the android block of your build.gradle file

compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
   }

Fetching pidx

Please go through the Initiating a Payment request to learn how to request the pidx

Setup

Building Config

Create an instance of KhaltiPayConfig with publicKey, pidx, returnUrl, environment as parameters.

val config = KhaltiPayConfig(  
    publicKey = "<your_public_key>",  
    pidx = "<your_pidx>",  
    returnUrl = Uri.parse("your_return_url"),  
    environment = Environment.TEST  
)

Note : environment has 2 options; Environment.prod & Environment.Test

Building Khalti

Create an instance of Khalti, using init function, with the above config as parameter along with the callbacks onPaymentResult, onMessage and onReturn. Here, onReturn is optional and can be skipped.

Khalti.init(  
    LocalContext.current,  // context
    config,  
    onPaymentResult = { paymentResult, khalti ->  
         // your implementation here     
    },  
    onMessage = { payload, khalti ->  
         // your implementation here     
    },  
    onReturn = { khalti ->  
         // your implementation here     
    }  
)

Note : Make sure the context you passed to Khalti can be used to open an activity.

Callbacks
Callback Description
onPaymentResult(result: PaymentResult, khalti: Khalti) Invoked on completion of payment. Inside this callback you'll have access to PaymentResult object and Khalti object

After completion of payment process, khalti will internally trigger the verification API. It's result is then propagated through the onPaymentResult callback. You'll receive an object of PaymentResult.
onMessage(payload: OnMessagePayload, khalti: Khalti) Invoked on failures and exceptions at any point of time. Inside this callback you'll have access to OnMessagePayload and Khalti object

OnMessagePalyload contains onMessageEvent that dictates what type of event triggered the callback. It also contains a flag needsPaymentConfirmation which if true indicates that you must verify the status of the payment. It can be done through the Khalti object passed to this callback. Use khalti.verify().
onReturn(khalti: Khalti) This is an optional callback that is invoked when return_url's page is successfully loaded. Inside this callback you'll have access to Khalti object

Schema

PaymentResult {
    status: String,
    payload: PaymentPayload,
}
PaymentPayload {
    pidx: String,
    totalAmount: Long,
    status: String,
    transactionId: String,
    fee: Long,
    refunded: Boolean
}
OnMessagePayload {
    event: OnMessageEvent,
    message: String,
    throwable: Throwable,
    code: Number,
    needsPaymentConfirmation: Boolean
}
OnMessageEvent {
    BackPressed, ReturnUrlLoadFailure, NetworkFailure, PaymentLookUpFailure, Unknown
}

Public functions in Khalti

Function Description
init(Context, KhaltiPayConfig, OnPaymentResult, OnMessage, OnReturn) Creates an instance of Khalti. Use this function to create an object of Khalti

val khalti = Khalti.init()
open() Opens the payment page. After creating an object of Khalti. Use the said object to open the payment page.

khalti.open()
verify() Looks up of the payment status. Khalti sdk internally verifies the status of the payment, but if required the status lookup can be triggered again. The result is propagated through the callbacks passed during init(). Use this function to confirm the payment status if and when needsPaymentConfirmation is true in OnMessagePayload.

khalti.verify()
close() Closes the payment page. Khalti does not close the payment page and it's sole responsibility lies on the merchant. Use this function to close the payment page when required.

khalti.close()

Sample Implementations

Method 1: With onReturn

Initialize the Khalti Object

val khalti = Khalti.init(  
    LocalContext.current,  
    KhaltiPayConfig(  
        publicKey = "<public_key>",  
        pidx = "<pidx>",  
        returnUrl = Uri.parse("<return_url>"),  
        environment = Environment.TEST // Or Environment.Prod for production  
    ),  
    onPaymentResult = { paymentResult, khalti ->  
        // Your implementation  
    },  
    onMessage = { payload, khalti ->  
        // Your implementation  
    },  
    onReturn = { khalti ->  
        // Your implementation  
    } 
)
Method 2: Without onReturn

Initialize the Khalti Object

val khalti = Khalti.init(  
    LocalContext.current,  
    KhaltiPayConfig(  
        publicKey = "<public_key>",  
        pidx = "<pidx>",  
        returnUrl = Uri.parse("<return_url>"),  
        environment = Environment.TEST // Or Environment.Prod for production  
    ),  
    onPaymentResult = { paymentResult, khalti ->  
        // Your implementation  
    },  
    onMessage = { payload, khalti ->  
        // Your implementation  
    } 
)

Use khalti.open(), khalti.verify(), khalti.close() wherever appropriate.

Check out the source for Khalti checkout on Github.

Changelog