1. Go to this page and download the library: Download bijayx/nepal-pay-laravel 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/ */
bijayx / nepal-pay-laravel example snippets
use BijayX\NepalPay\Data\ExpectedPayment;
use BijayX\NepalPay\Facades\NepalPay;
$payment = NepalPay::driver('khalti')->initiate([
'amount' => 150000, // Rs 1,500 in paisa
'order_id' => 'ORD-1001-a1', // unique per payment attempt
'order_name' => 'Charging session',
'return_url' => route('payments.return', 'khalti'),
]);
// Persist $payment->reference and $payment->amount on your order, then:
return $payment->redirect(); // GatewayRedirectResponse (Responsable)
$result = NepalPay::driver('esewa')->verify(
$request->query(),
new ExpectedPayment(amount: $order->amount, reference: $order->payment_reference),
);
if ($result->isPaid()) {
// Mark the order paid (idempotently, see below).
}
use BijayX\NepalPay\Contracts\PaymentGateway;
public function __construct(private PaymentGateway $gateway) {}
$table->unsignedBigInteger('amount'); // what the customer owes, in paisa
$table->string('payment_gateway')->nullable();
$table->string('payment_reference')->nullable()->unique();
$table->unsignedBigInteger('payment_amount')->nullable();
$table->string('payment_transaction_id')->nullable();
$table->timestamp('paid_at')->nullable();
namespace App\Http\Controllers;
use App\Models\Order;
use BijayX\NepalPay\Data\ExpectedPayment;
use BijayX\NepalPay\Exceptions\InvalidSignatureException;
use BijayX\NepalPay\Exceptions\NepalPayException;
use BijayX\NepalPay\Exceptions\PaymentMismatchException;
use BijayX\NepalPay\Facades\NepalPay;
use Illuminate\Contracts\Support\Responsable;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
final class PaymentController
{
public function store(Order $order, string $gateway): Responsable|RedirectResponse
{
abort_if($order->paid_at !== null, 409, 'This order is already paid.');
try {
$payment = NepalPay::driver($gateway)->initiate([
'amount' => $order->amount,
// A fresh id for every attempt: both gateways reject reused ids.
'order_id' => $order->id.'-'.Str::lower(Str::random(8)),
'order_name' => "Order #{$order->id}",
'return_url' => route('payments.return', [$gateway, $order]),
'customer' => ['name' => $order->customer_name, 'phone' => $order->customer_phone],
]);
} catch (NepalPayException $e) {
report($e);
return back()->with('error', 'We could not start the payment. Please try again.');
}
// Store what verify() needs BEFORE redirecting.
$order->forceFill([
'payment_gateway' => $gateway,
'payment_reference' => $payment->reference,
'payment_amount' => $payment->amount,
])->save();
return $payment->redirect();
}
public function callback(Request $request, string $gateway, Order $order): RedirectResponse
{
if ($order->payment_gateway !== $gateway || $order->payment_reference === null) {
abort(404);
}
try {
$result = NepalPay::driver($gateway)->verify(
$request->query(),
new ExpectedPayment(amount: $order->payment_amount, reference: $order->payment_reference),
);
} catch (PaymentMismatchException|InvalidSignatureException $e) {
report($e); // possible tampering: investigate, do not fulfil
return to_route('orders.show', $order)->with('error', 'The payment could not be verified.');
} catch (NepalPayException $e) {
report($e); // network/gateway problem: the payment may still complete
return to_route('orders.show', $order)->with('error', 'We could not confirm the payment yet.');
}
if (! $result->isPaid()) {
return to_route('orders.show', $order)->with('error', "Payment {$result->status->value}.");
}
// Callbacks can arrive more than once: lock the row and mark paid only once.
DB::transaction(function () use ($order, $result): void {
$locked = Order::query()->whereKey($order->getKey())->lockForUpdate()->firstOrFail();
if ($locked->paid_at !== null) {
return;
}
$locked->forceFill([
'paid_at' => now(),
'payment_transaction_id' => $result->transactionId,
])->save();
// Dispatch fulfilment here (jobs, emails, ...).
});
return to_route('orders.show', $order)->with('status', 'Payment received. Thank you!');
}
}