PHP code example of snlbaral / khalti-api

1. Go to this page and download the library: Download snlbaral/khalti-api 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/ */

    

snlbaral / khalti-api example snippets


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);


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: "[email protected]"
            //     idx: "QA3rsGoGgtQHKGDfvrL9NU"
            //     mobile: "[email protected]"
            //     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;

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