Last updated:
0 purchases
khalti checkout flutter
Khalti Payment Gateway for Flutter
Khalti is a payment gateway, digital wallet and API provider system for various online services for Nepal.
With this package, you can accepts payments from:
Khalti users.
eBanking users of our partner banks.
Mobile banking users of our mobile banking partner banks.
SCT/VISA card holders.
connectIPS users.
Using Khalti Payment Gateway, you do not need to integrate with individual banks.
Introduction
Getting Started
Supported Platforms
Setup
Android
iOS
Web
Launching Payment Interface
Generating the pidx
Khalti Initialization
Loading payment interface
Payment verification API
Closing the Khalti payment gateway page
Example
Contributing
Support
License
Introduction #
Read the introduction here.
Getting Started #
Integrating Khalti Payment Gateway requires merchant account.
You can always create one easily from here.
Read the steps to integrate Khalti Payment Gateway in details here.
Supported Platforms #
Android
iOS
Web
Desktop (macOS, Linux, Windows)
✔️
✔️
❌
❌
Setup #
Detailed setup for each platform.
Android #
No configuration is required for android.
iOS #
No configuration is required for iOS.
Web #
Since, the SDK uses flutter_inappwebview internally to load the payment gateway, check its doc for the necessary web setup.
Launching Payment Interface #
Generating the pidx #
A unique product identifier pidx must be generated via a server side POST request before being able to proceed. Read here for information on how one can generate pidx along with other extra parameters.
Khalti Initialization #
Before being able to launch Khalti payment gateway, it is necessary to initialize Khalti object. The initialization can be done by a static init() method.
It is suggested that the initialization be done inside the initState method of a StatefulWidget.
class KhaltiSDKDemo extends StatefulWidget {
const KhaltiSDKDemo({super.key});
@override
State<KhaltiSDKDemo> createState() => _KhaltiSDKDemoState();
}
class _KhaltiSDKDemoState extends State<KhaltiSDKDemo> {
late final Future<Khalti> khalti;
@override
void initState() {
super.initState();
final payConfig = KhaltiPayConfig(
publicKey: '__live_public_key__', // Merchant's public key
pidx: pidx, // This should be generated via a server side POST request.
environment: Environment.prod,
);
khalti = Khalti.init(
enableDebugging: true,
payConfig: payConfig,
onPaymentResult: (paymentResult, khalti) {
log(paymentResult.toString());
},
onMessage: (
khalti, {
description,
statusCode,
event,
needsPaymentConfirmation,
}) async {
log(
'Description: $description, Status Code: $statusCode, Event: $event, NeedsPaymentConfirmation: $needsPaymentConfirmation',
);
},
onReturn: () => log('Successfully redirected to return_url.'),
);
}
@override
Widget build(BuildContext context) {
// Rest of the code
}
}
copied to clipboard
The static init() method takes in a few arguments:
enableDebugging: A boolean argument that is when set to true can be used to view network logs in the debug console. It is set to false by default.
payConfig: An instance of KhaltiPayConfig(). It is used to set few config data. The instance of KhaltiPayConfig takes in few arguments:
final payConfig = KhaltiPayConfig(
publicKey: '__live_public_key__', // Merchant's public key
pidx: pidx, // This should be generated via a server side POST request.
environment: Environment.prod,
);
copied to clipboard
publicKey: Merchant's live or test public key provided by Khalti.
pidx: Unique product identifier received after initiating the payment via a server-side POST request.
environment: An enum that determines whether test API or production API should be invoked. Can be either Environment.prod or Environment.test. Set to Environment.prod by default.
onPaymentResult: A callback function that is triggered if the payment is successfully made and redirected to merchant's return URL. The callback takes in two arguments.
paymentResult: An instance of PaymentResult class. It provides some informations about the payment after it is successfully made. Following data is provided by this instance.
payload: An instance of PaymentPayload. It contains general informations such as pidx, totalAmount, transactionId, status, fee, refunded, purchaseOrderId, purchaseOrderName and extraMerchantParams regarding the payment made.
khalti: An instance of Khalti. Can be used to invoke any methods provided by this instance.
onPaymentResult(paymentResult, khalti) {
print(paymentResult.payload?.status);
print(paymentResult.payload?.pidx);
print(paymentResult.payload?.totalAmount);
print(paymentResult.payload?.transactionId);
}
copied to clipboard
onReturn: A callback function that gets triggered when the return_url is successfully loaded.
onMessage: A callback function that is triggered to convey any message. It gets triggered when any issue is encountered or when any general message is to be conveyed. In case of error, this callback provides error informations such as error description and status code. It also provides information about why the error occured via KhaltiEvent enum. This enum consists of:
enum KhaltiEvent {
/// Event for when khalti payment page is disposed.
kpgDisposed,
/// Event for when return url fails to load from the API.
returnUrlLoadFailure,
/// Event for when there's an exception when making a network call.
networkFailure,
/// Event for when there's a HTTP failure when making a network call for verifying payment status.
paymentLookupfailure,
/// An unknown event.
unknown
}
copied to clipboard
Additionally, it also provides a bool needsPaymentConfirmation which needs to be checked. If it is true, developers should invoke payment confirmation API that is provided by the SDK to confirm the status of their payment. The callback also provides the instance of Khalti.
onMessage: (
khalti, {
description,
statusCode,
event,
needsPaymentConfirmation,
}) {
log('Description: $description, Status Code: $statusCode, Event: $event, NeedsPaymentConfirmation: $needsPaymentConfirmation');
}
copied to clipboard
Loading payment interface #
The SDK internally uses flutter_inappwebview to load the khalti payment gateway. SDK users should invoke open() method provided by the SDK to push a new page in their route.
khalti.open(context); // Assuming that khalti is a variable that holds the instance of `Khalti`.
copied to clipboard
Payment verification API #
A payment verification API that confirms the status of the payment made, is already called by the SDK internally. However, if the users receive needsPaymentConfirmation as true in onMessage callback, they are supposed to called the payment verification API manually to be sure about the payment status. It can be done with a method provided by the Khalti instance.
await khalti.verify(); // Assuming that khalti is a variable that holds the instance of `Khalti`.
copied to clipboard
The instance of Khalti is provided in the onPaymentResult and onMessage callback which can be used to invoke any public functions provide by Khalti class.
Closing the Khalti payment gateway page #
After payment is successfully made, developers can opt to pop the page that was pushed in their route. The SDK provides a close() method.
khalti.close(context);
copied to clipboard
Example #
For a more detailed example, check the examples directory inside khalti_checkout_flutter package.
Contributing #
Contributions are always welcome. Also, if you have any confusion, please feel free to create an issue.
Support #
For Queries, feel free to call us at:
Contact Our Merchant Team
Mobile (Viber / Whatsapp): 9801165567, 9801165538
Email: [email protected]
(To integrate Khalti to your business and other online platforms.)
Contact Our Merchant Support
Mobile (Viber / Whatsapp): 9801165565, 9801856383, 9801856451
Email: [email protected]
Contact Our Technical Team
Mobile (Viber / Whatsapp): 9843007232
Email / Skype: [email protected]
(For payment gateway integration support.)
License #
Copyright (c) 2024 The Khalti Authors. All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of Sparrow Pay Pvt. Ltd. nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
copied to clipboard
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.