PHPackages                             snlbaral/khalti-api - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. snlbaral/khalti-api

ActiveLibrary[API Development](/categories/api)

snlbaral/khalti-api
===================

Khalti API for PHP.

1.0.1(5y ago)34.3k—8.9%PHP

Since May 3Pushed 5y ago1 watchersCompare

[ Source](https://github.com/snlbaral/khalti-api)[ Packagist](https://packagist.org/packages/snlbaral/khalti-api)[ Docs](https://github.com/snlbaral/khalti-api)[ RSS](/packages/snlbaral-khalti-api/feed)WikiDiscussions main Synced 2d ago

READMEChangelog (2)DependenciesVersions (3)Used By (0)

khalti-api-laravel
==================

[](#khalti-api-laravel)

[![Latest Stable Version](https://camo.githubusercontent.com/3202967c63cb3ababf66dd5e5941060a207b9eca53b5212214adbf7a8b4ba14d/68747470733a2f2f706f7365722e707567782e6f72672f736e6c626172616c2f6b68616c74692d6170692f76)](//packagist.org/packages/snlbaral/khalti-api) [![Total Downloads](https://camo.githubusercontent.com/e7b9337191da7d96862f319818e59cc43c35375d2684868c99b52f0bd3f939e4/68747470733a2f2f706f7365722e707567782e6f72672f736e6c626172616c2f6b68616c74692d6170692f646f776e6c6f616473)](//packagist.org/packages/snlbaral/khalti-api) [![Latest Unstable Version](https://camo.githubusercontent.com/e3d0b7cf365f7f1ce07e0aa27cf79a18b808978683673839575df0e594ffdc1b/68747470733a2f2f706f7365722e707567782e6f72672f736e6c626172616c2f6b68616c74692d6170692f762f756e737461626c65)](//packagist.org/packages/snlbaral/khalti-api) [![License](https://camo.githubusercontent.com/7007d4cd4b7080b7a62683821c1c5fa8cad84d4a523c048771b2f8e79a8db7e6/68747470733a2f2f706f7365722e707567782e6f72672f736e6c626172616c2f6b68616c74692d6170692f6c6963656e7365)](//packagist.org/packages/snlbaral/khalti-api)

This is an open source library that allows PHP/Laravel applications to integrate Khalti Payment Gateway.

Requirements
------------

[](#requirements)

Using this library for PHP requires the following:

- \[Composer\]\[composer\] or a manual install of the dependencies mentioned in `composer.json`.

Installation
------------

[](#installation)

The recommended way to install it PHP is to install it using \[Composer\]\[composer\]:

```
composer require snlbaral/khalti-api
```

Quick start
-----------

[](#quick-start)

Create a merchant account in Khalti, Obtain the keys. And use test\_public\_key and test\_secret\_key at first. After the first successful test, you'll get live api keys. **Warning: *Secrets Keys* are similar to passwords or private keys by allowing an application to identify as yours: therefore, *Secret Key* should be kept private.**

### Step 1: create your payment html page

[](#step-1-create-your-payment-html-page)

#### `index.blade.php`

[](#indexbladephp)

```

    Pay with Khalti

        var config = {
            // replace the publicKey with yours
            "publicKey": "test_public_key_YOUR_PUBLIC_KEY",
            "productIdentity": "1234567890", //Product ID
            "productName": "Dragon", //Product Name
            "productUrl": "http://gameofthrones.wikia.com/wiki/Dragons", //Product URL
            "paymentPreference": [
                "KHALTI",
                "EBANKING",
                "MOBILE_BANKING",
                "CONNECT_IPS",
                "SCT",
                ],
            "eventHandler": {
                onSuccess (payload) {
                    // hit merchant api for initiating verfication
                    //console.log(payload);
                    if(payload.status==200) {
                        $.ajaxSetup({
                            headers: {
                                'X-CSRF-Token': '{{csrf_token()}}'
                            }
                        });
                        $.ajax({
                            url: "{{ route('verification') }}", //Your backend route url, replace this with the route you'll be creating later
                            data: payload,
                            method: 'POST',
                            success: function(data) {
                                console.log('Payment is succcessfull');
                                console.log(data);
                            },
                            error: function(err) {
                                console.log(err.response);
                            },
                        });
                    }
                },
                onError (error) {
                    console.log(error);
                },
                onClose () {
                    console.log('widget is closing');
                }
            }
        };

        var checkout = new KhaltiCheckout(config);
        var btn = document.getElementById("payment-button");
        btn.onclick = function () {
            // minimum transaction amount must be 10, i.e 1000 in paisa.
            checkout.show({amount: 3000});
        }

```

### Step 2: create route for payment verification

[](#step-2-create-route-for-payment-verification)

create route for payment verification and use this route in above ajax url

```
Route::post('/verification', [App\Http\Controllers\PaymentController::class, 'verification'])->name('verification');
```

---

Usages
------

[](#usages)

**Init**

```
use Snlbaral\Khalti\Khalti;
$khalti = new Khalti();
```

**Methods**

```
/**
 *
 * @param string $secret your khalti merchant secret key
 * @param string $token your khalti api payment transaction token
 * @param string $idx your khalti api payment transaction idx
 * @param int $amount khalti payment transaction amount
 */

//Payment Verification
$response = $khalti->verifyPayment($secret,$token,$amount);

//List Transactions
$response = $khalti->listTransactions($secret);

//Get Transaction
$response = $khalti->getTransaction($secret,$idx);

//Transaction Status
$response = $khalti->transactionStatus($secret,$token,$amount);
```

---

Example
-------

[](#example)

**PaymentController.php**

```
use Snlbaral\Khalti\Khalti;

public function verification(Request $request) {
    $secret = "test_secret_key_YOUR_SECRET_KEY";
    $token = $reqeust->token;
    $amount = $reqeust->amount;
    $khalti = new Khalti();
    $response = $khalti->verifyPayment($secret,$token,$amount);

    //Response Array from $response
        // status_code: 200
        // data:
            // amount: 3000
            // cashback: 0
            // created_on: "2021-05-03T17:41:16.436643+05:45"
            // fee_amount: 90
            // idx: "pBAKtpzJaQWdfdfsRN7WNtTXpcH"
            // merchant:
            //     email: "user@gmail.com"
            //     idx: "QA3rsGoGgtQHKGDfvrL9NU"
            //     mobile: "user@gmail.com"
            //     name: "Company Name"
            //     reference: null
            //     refunded: false
            //     remarks: ""
            // state:
            //     idx: "DhvMj9hdRufLqkP8ZY4d8gdfdfs"
            //     name: "Completed"
            //     template: "is complete"
            //     token: "WgnXkdfsfCNpGMcoEcojLmxCmM"
            // type:
            //     idx: "LFVwXcpfdfs3wQENxGPZWdELa"
            //     name: "Ebanking payment"
            // user:
            //     email: ""
            //     idx: "xeoLUUnskdfsfsfszikFgGLmKWhH7"
            //     mobile: "NA"
            //     name: "Nepal Clearing House"
    //Response Array Ends

     //Store into Database Here//
     // if($response['status_code']==200) {
	    // $amount = $response['data']['amount'];
	    //.
	    //..
    // }
    // //

    return $response;

}
```

License
-------

[](#license)

This library for PHP is licensed under the [3-Clause BSD License](https://opensource.org/licenses/BSD-3-Clause)

Credits
-------

[](#credits)

This library for PHP is developed and maintained by Sunil Baral.

###  Health Score

30

—

LowBetter than 62% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity25

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

1888d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/75522799?v=4)[Sunil Baral](/maintainers/snlbaral)[@snlbaral](https://github.com/snlbaral)

---

Top Contributors

[![snlbaral](https://avatars.githubusercontent.com/u/75522799?v=4)](https://github.com/snlbaral "snlbaral (11 commits)")

---

Tags

apipaymentkhaltisnlbaral

### Embed Badge

![Health badge](/badges/snlbaral-khalti-api/health.svg)

```
[![Health](https://phpackages.com/badges/snlbaral-khalti-api/health.svg)](https://phpackages.com/packages/snlbaral-khalti-api)
```

###  Alternatives

[everypay/everypay-php

1743.5k](/packages/everypay-everypay-php)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
