PHP code example of jaap-tech / laravel-nepali-payment

1. Go to this page and download the library: Download jaap-tech/laravel-nepali-payment 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/ */

    

jaap-tech / laravel-nepali-payment example snippets


return [
    'esewa' => [
        'product_code' => env('ESEWA_PRODUCT_CODE'),
        'secret_key'   => env('ESEWA_SECRET_KEY'),
        'success_url'   => env('ESEWA_SUCCESS_URL'),
        'failure_url'   => env('ESEWA_FAILURE_URL'),
    ],
    'khalti' => [
        'secret_key' => env('KHALTI_SECRET_KEY'),
        'environment' => strtolower(env('KHALTI_ENVIRONMENT', 'test')),
        'success_url'   => env('KHALTI_SUCCESS_URL'),
        'website_url'   => env('KHALTI_WEBSITE_URL'),
    ],
    'connectips' => [
        'merchant_id' => env('CONNECTIPS_MERCHANT_ID'),
        'app_id' => env('CONNECTIPS_APP_ID'),
        'app_name' => env('CONNECTIPS_APP_NAME'),
        'private_key_path' => env('CONNECTIPS_PRIVATE_KEY_PATH'),
        'password' => env('CONNECTIPS_PASSWORD'),
        'environment' => strtolower(env('CONNECTIPS_ENVIRONMENT', 'test')),
    ],
    'database' => [
        'enabled' => env('NEPALI_PAYMENT_DATABASE_ENABLED', false),
    ],
];

use NepaliPayment;

// Initiate eSewa payment
$response = NepaliPayment::esewa()->payment([
    'amount' => 1000,
    'transaction_uuid' => 'unique-id-123', # optional, can be generated automatically
    'success_url' => route('payment.success'), # optional, can be set in config
    'failure_url' => route('payment.failure'), # optional, can be set in config
]);

// Redirect to payment gateway
$response->redirect();

use NepaliPayment;

// Initiate payment with gateway
$response = NepaliPayment::esewa()->payment([
    'amount' => 1000,
    'transaction_uuid' => 'unique-id-123', # optional, can be generated automatically
    'success_url' => route('payment.success'), # optional, can be set in config
    'failure_url' => route('payment.failure'), # optional, can be set in config
]);

// Record verification
$verification = NepaliPayment::esewa()->verify([
    'total_amount' => 1000,
    'transaction_uuid' => 'same-unique-id-123',
]);

// Find payment by reference ID
PaymentTransaction::byReference('ref-123')->first();

// Filter by gateway
PaymentTransaction::byGateway('esewa')->get();

// Filter by status
PaymentTransaction::byStatus('completed')->get();

// Completed payments only
PaymentTransaction::completed()->get();

// Failed payments only
PaymentTransaction::failed()->get();

// Pending/processing payments
PaymentTransaction::pending()->get();

// Filter by payable type and ID
PaymentTransaction::forPayable('App\Models\User', $userId)->get();

$payment = Payment::find($id);

// Check status
$payment->isCompleted();
$payment->isFailed();
$payment->isPending();

// Update status
$payment->markAsProcessing();
$payment->markAsCompleted();
$payment->markAsFailed('Reason for failure');
$payment->markAsCancelled();

// Relationships
$payment->payable;        // The associated model (User, Order, etc)

// Check if database is enabled
nepali_payment_enabled();

// Find payments
nepali_payment_find('ref-123');                    // by reference

// Query payments
nepali_payment_get_by_status('completed')->paginate();
nepali_payment_get_by_gateway('khalti')->latest()->get();

use JaapTech\NepaliPayment\Events\{
    PaymentInitiatedEvent,
    PaymentProcessingEvent,
    PaymentCompletedEvent,
    PaymentFailedEvent,
};

// Listen to events in EventServiceProvider
protected $listen = [
    PaymentCompletedEvent::class => [
        \App\Listeners\SendPaymentConfirmation::class,
        \App\Listeners\UpdateUserBalance::class,
    ],
    PaymentFailedEvent::class => [
        \App\Listeners\NotifyPaymentFailure::class,
    ],
];

// Or listen inline
Event::listen(PaymentCompletedEvent::class, function ($event) {
    // $event->payment
});

// PaymentController.php


namespace App\Http\Controllers;

use App\Models\Order;
use NepaliPayment;
use JaapTech\NepaliPayment\Models\PaymentTransaction;

class PaymentController extends Controller
{
    public function create(Order $order)
    {
        // Initiate payment with gateway
        $response = NepaliPayment::esewa()->payment([
            'amount' => $order->total,
            'transaction_uuid' => $payment->reference_id,
            'success_url' => route('payment.verify', $payment->id),
            'failure_url' => route('payment.failed', $payment->id),
        ]);

        return $response->redirect();
    }

    public function verify(PaymentTransaction $payment)
    {
        // Get gateway response
        $verification = NepaliPayment::esewa()->verify([
            'total_amount' => $payment->amount,
            'transaction_uuid' => $payment->reference_id,
        ]);

        if ($verification->isSuccess()) {
            NepaliPayment::completePayment(
                $payment,
                gatewayTransactionId: $payment->reference_id,
                responseData: $verification->toArray()
            );

            return redirect()->route('order.show', $payment->payable)
                ->with('success', 'Payment completed successfully!');
        } else {
            NepaliPayment::failPayment($payment, 'Gateway verification failed');

            return redirect()->route('payment.create', $payment->payable)
                ->with('error', 'Payment verification failed');
        }
    }

    public function failed(PaymentTransaction $payment)
    {
        NepaliPayment::failPayment($payment, 'User cancelled payment');
        
        return redirect()->route('payment.create', $payment->payable)
            ->with('error', 'Payment was cancelled');
    }
}
bash
php artisan vendor:publish --tag=nepali-payment-config
bash
# 1. Publish migrations
php artisan vendor:publish --tag=nepali-payment-migrations

# 2. Run migrations
php artisan migrate

# 3. Enable in .env
NEPALI_PAYMENT_DATABASE_ENABLED=true
bash
php artisan nepali-payment:check