PHP code example of pralhadstha / omnipay-khalti

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

    

pralhadstha / omnipay-khalti example snippets


    use Omnipay\Omnipay;
    use Exception;

    $gateway = Omnipay::create('Khalti_Khalti');

    $gateway->setSecret('secret_key_provided_by_khalti');
    $gateway->setTestMode(true);

    try {
        $response = $gateway->purchase([
            'amount' =>  10000, // Rs. 100 in paisa
            'purchaseOrderId' => 'SH-100',
            'purchaseOrderName' => "Basmati Rice 500gm",
            'websiteUrl' =>  'https://merchant.com/',
            'returnUrl' => 'https://merchant.com/payment/1/complete',
        ])->send();

        if ($response->isRedirect()) {
            $response->redirect();
        }
    } catch (Exception $e) {
        return $e->getMessage();
    }

    $gateway = Omnipay::create('Khalti_Khalti');

    $gateway->setSecret('secret_key_provided_by_khalti');
    $gateway->setTestMode(true);

    $payload = $_GET;

    try {
        $response = $gateway->fetchTransaction([
            'paymentId' => $payload['pidx']
        ])->send();

        if ($response->isSuccessful()) {
            // Verified
        } else {
            // Unverified
        }
    } catch (Exception $e) {
        return $e->getMessage();
    }