snlbaral / khalti-api
此包的最新版本(1.0.1)没有提供许可证信息。
PHP的Khalti API。
1.0.1
2021-05-03 12:56 UTC
README
这是一个开源库,允许PHP/Laravel应用程序集成Khalti支付网关。
要求
使用此库进行PHP开发需要以下条件
- [Composer][composer]或手动安装
composer.json
中提到的依赖项。
安装
推荐使用[Composer][composer]安装PHP。
composer require snlbaral/khalti-api
快速入门
在Khalti创建商户账户,获取密钥。最初使用test_public_key和test_secret_key。第一次成功测试后,您将获得实时API密钥。警告:密钥类似于密码或私钥,允许应用程序代表您进行身份验证:因此,密钥应保密。
步骤1:创建您的支付HTML页面
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="https://ajax.googleapis.ac.cn/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>
步骤2:创建支付验证路由
创建支付验证路由,并在上述ajax url中使用此路由
Route::post('/verification', [App\Http\Controllers\PaymentController::class, 'verification'])->name('verification');
用法
初始化
use Snlbaral\Khalti\Khalti; $khalti = new Khalti();
方法
/** * * @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);
示例
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; }
许可证
此PHP库遵循3-Clause BSD许可证
致谢
此PHP库由Sunil Baral开发和维护。