snlbaral/khalti-api
Composer 安装命令:
composer require snlbaral/khalti-api
包简介
Khalti API for PHP.
README 文档
README
This is an open source library that allows PHP/Laravel applications to integrate Khalti Payment Gateway.
Requirements
Using this library for PHP requires the following:
- [Composer][composer] or a manual install of the dependencies mentioned in
composer.json.
Installation
The recommended way to install it PHP is to install it using [Composer][composer]:
composer require snlbaral/khalti-api
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
index.blade.php
<html> <head> <script src="https://khalti.s3.ap-south-1.amazonaws.com/KPG/dist/2020.12.17.0.0.0/khalti-checkout.iffe.js"></script> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> </head> <body> <!-- Place this where you need payment button --> <button id="payment-button">Pay with Khalti</button> <!-- Place this where you need payment button --> <!-- Paste this code anywhere in you body tag --> <script> 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}); } </script> <!-- Paste this code anywhere in you body tag --> </body> </html>
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
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
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
This library for PHP is licensed under the 3-Clause BSD License
Credits
This library for PHP is developed and maintained by Sunil Baral.
snlbaral/khalti-api 适用场景与选型建议
snlbaral/khalti-api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.31k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2021 年 05 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「payment」 「khalti」 「snlbaral」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 snlbaral/khalti-api 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 snlbaral/khalti-api 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 snlbaral/khalti-api 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A PSR-7 compatible library for making CRUD API endpoints
Framework-agnostic Khalti PHP SDK for ePayment create/status verification and production-safe backend fulfillment.
Laravel package for integrating Nepali payment gateways like eSewa, Khalti, and ConnectIps.
Easy Khalti ePayment Integration Package for Laravel
Nepali Payment Gateway integration in PHP language. It includes eSewa, Khalti, ConnectIps, etc.
Official PHP SDK for PayBridgeNP — Nepal payment gateway aggregator
统计信息
- 总下载量: 4.31k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2021-05-03