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