Laravel Khalti
View Package
Overview: Laravel Khalti streamlines the integration of Khalti payment gateway into Laravel applications. With easy installation, configuration, and usage, developers can seamlessly incorporate Khalti's payment functionality, enhancing the payment experience for users.
Installation: Install the package via Composer:
composer require dipesh79/laravel-khalti
Set environment variables in your .env file:
KHALTI_SECRET_KEY="Khalti Secret Id" KHALTI_ENV="Sandbox" #Sandbox or Live KHALTI_WEBSITE="https://yourwebsite.com" KHALTI_CALLBACK="https://yourwebsite.com/return_url"
Publish the vendor file:
php artisan vendor:publish --provider="Dipesh79\LaravelKhalti\KhaltiServiceProvider"
Usage:
- Redirect the user to the payment page from your controller:
<?php public function khaltiPayment() { $khalti = new LaravelKhalti(); $amount = 123; // In Paisa $order_id = 251264889; // Your Unique Order Id $order_name = "Order Name"; $payment_response = $khalti->khaltiCheckout($amount, $order_id, $order_name); $pidx = $payment_response['pidx']; // Store this to your db for future reference. $url = $payment_response['url']; return redirect($url); }
- Handle the callback function:
<?php public function callBackFunction(Request $request) { if ($request->pidx) { $payment = Payment::where('your_pidx_id', $request->pidx)->first(); $payment->status = "Success"; $payment->save(); } else { // Payment Failed } }
- Check the status of payment:
<?php public function checkStatus($pidx) { $khalti = new LaravelKhalti(); $status = $khalti->checkStatus($pidx); // The status will look like this // Now you can update your payment status as per need. }
License: MIT
Author: @Dipesh79
Support: For support, email dipeshkhanal79[at]gmail[dot]com.
This post is licensed under CC BY
4.0 by the
author.