khalti_checkout_flutter 1.0.0-dev.4 copy "khalti_checkout_flutter: ^1.0.0-dev.4" to clipboard
khalti_checkout_flutter: ^1.0.0-dev.4 copied to clipboard

An official Flutter plugin for Khalti Payment Gateway, with all the necessary interface that make it easy to integrate with your app.

Khalti Payment Gateway

Khalti Payment Gateway for Flutter

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


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 #

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.
      returnUrl: Uri.parse('https://your_return_url'),
      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
  }
}

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.
      returnUrl: Uri.parse('https://your_return_url'),
      environment: Environment.prod,
    );
    
    • 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.
    • returnUrl: Merchant's URL where the user must be redirected after the payment is (un)successfully made.
    • 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.
      • status: A string representing the status of the payment.
      • payload: An instance of PaymentPayload. It contains general informations such as pidx, amount and transactionId 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.status);
      print(paymentResult.payload.pidx);
      print(paymentResult.payload.amount);
      print(paymentResult.payload.transactionId);
    }
    
  • 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.
      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.
      paymentLookupfailure,
    
      /// An unknown event.
      unknown
    }
    

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

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`.

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`.

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

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

(To integrate Khalti to your business and other online platforms.)

Contact Our Merchant Support

Contact Our Technical Team

(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.
2
likes
140
pub points
74%
popularity

Publisher

verified publisherkhalti.com

An official Flutter plugin for Khalti Payment Gateway, with all the necessary interface that make it easy to integrate with your app.

Homepage
Repository (GitHub)
View/report issues

Documentation

Documentation
API reference

License

BSD-3-Clause (LICENSE)

Dependencies

equatable, flutter, flutter_inappwebview, http, internet_connection_checker_plus, khalti_checkout_core, meta, url_launcher

More

Packages that depend on khalti_checkout_flutter