PHP code example of asdh / laravel-khalti

1. Go to this page and download the library: Download asdh/laravel-khalti library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

asdh / laravel-khalti example snippets




return [
    /**
     * The public key that you receive from khalti
     */
    'public_key' => env('KHALTI_PUBLIC_KEY'),

    /**
     * The secret key that you receive from khalti
     */
    'secret_key' => env('KHALTI_SECRET_KEY'),

    /**
     * The url that is used to verify khalti payment
     */
    'verification_url' => env('KHALTI_VEFIRICATION_URL', 'https://khalti.com/api/v2/payment/verify/')
];



namespace App\Http\Controllers;

use AsDh\Khalti;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class KhaltiVerificationController extends Controller
{
    public function verify(Request $request, Khalti $khalti)
    {
        $khalti->withToken($request->token)
            ->withAmount((int) $request->amount)
            ->verify();

        if($khalti->hasError()) {
            $errorMessage = $khalti->errorMessage();

            // perform your action
            return $errorMessage;
        }

        // The payment is verified
        // perform your action
        return $khalti->response();
    }
}



namespace App\Http\Controllers;

use AsDh\Khalti;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class KhaltiVerificationController extends Controller
{
    public function verify(Request $request, Khalti $khalti)
    {
        $khalti->withToken($request->token)
            ->withAmount((int) $request->amount)
            ->verify();

        if($khalti->isVerified()) {
            $response = $khalti->response();

            // perform your action
            return $response;
        }

        // The payment is not verified
        // perform your action
        return $khalti->errorMessage();
    }
}

$khalti->statusCode();
sh
php artisan vendor:publish --provider="AsDh\KhaltiServiceProvider" --tag="khalti"