GuidesLaravel
Accept KHQR payments in Laravel
To accept KHQR in Laravel, call Dalo's POST /v1/payments with Laravel's Http client when the customer clicks Pay, redirect them to the returned checkout_url, and mark the order paid when the signed payment.completed webhook arrives. It is three routes and two small controllers. The code below works on Laravel 10, 11 and 12.
1. Keys and config
Create a store at dalodesk.com/app, then an API key (tick Test key to start with dk_test_) and a webhook endpoint pointing at https://your-site.com/webhooks/dalo. Put both secrets in .env:
.env
DALO_API_KEY=dk_test_...
DALO_WEBHOOK_SECRET=whsec_...
// config/services.php
'dalo' => [
'key' => env('DALO_API_KEY'),
'webhook_secret' => env('DALO_WEBHOOK_SECRET'),
],
The key stays on the server. Never put it in Blade, JavaScript or a mobile app.
2. Routes
// routes/web.php
use App\Http\Controllers\CheckoutController;
use App\Http\Controllers\DaloWebhookController;
Route::post('/checkout/{order}', [CheckoutController::class, 'pay'])->name('checkout.pay');
Route::get('/checkout/return', [CheckoutController::class, 'return'])->name('checkout.return');
Route::post('/webhooks/dalo', DaloWebhookController::class);
Dalo cannot send a CSRF token, so exclude the webhook route:
// bootstrap/app.php (Laravel 11 and later)
->withMiddleware(function (Middleware $middleware) {
$middleware->validateCsrfTokens(except: ['webhooks/dalo']);
})
On Laravel 10, add 'webhooks/dalo' to $except in app/Http/Middleware/VerifyCsrfToken.php instead.
3. Create the payment and redirect
The Idempotency-Key makes a double click or a retry return the same payment instead of a second QR. Add one to payment_attempt on the order when a QR expires and the customer tries again.
<?php
// app/Http/Controllers/CheckoutController.php
namespace App\Http\Controllers;
use App\Models\Order;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
class CheckoutController extends Controller
{
public function pay(Order $order)
{
$response = Http::withToken(config('services.dalo.key'))
->withHeaders(['Idempotency-Key' => "order_{$order->id}_{$order->payment_attempt}"])
->timeout(20)
->post('https://dalodesk.com/v1/payments', [
'amount' => number_format($order->total, 2, '.', ''),
'reference_id' => (string) $order->id,
'success_url' => route('checkout.return'),
]);
if ($response->failed()) {
return back()->withErrors(['payment' => 'The payment could not start. Please try again.']);
}
$order->update(['dalo_payment_id' => $response['id']]);
return redirect()->away($response['checkout_url']);
}
public function return(Request $request)
{
$id = (string) $request->query('payment_id', '');
abort_unless(preg_match('/^pay_[\w-]{4,64}$/', $id), 404);
$payment = Http::withToken(config('services.dalo.key'))
->get("https://dalodesk.com/v1/payments/{$id}")
->throw()
->json();
return view('checkout.return', ['paid' => $payment['status'] === 'paid', 'status' => $payment['status']]);
}
}
The return page reads payment_id and asks Dalo for the real status. The ?status=paid in the URL is only a hint: anyone can type it.
4. Verify the webhook and fulfil
Read the raw body with $request->getContent() and check the signature before you trust anything in it. Only payment.completed with livemode: true means money arrived.
<?php
// app/Http/Controllers/DaloWebhookController.php
namespace App\Http\Controllers;
use App\Models\Order;
use Illuminate\Http\Request;
class DaloWebhookController extends Controller
{
public function __invoke(Request $request)
{
$raw = $request->getContent(); // the exact bytes Dalo signed
$header = (string) $request->header('X-KHQR-Signature', '');
if (! preg_match('/^t=(\d+),v1=([a-f0-9]{64})$/', $header, $m) || abs(time() - (int) $m[1]) > 300) {
abort(401);
}
$expected = hash_hmac('sha256', $m[1] . '.' . $raw, config('services.dalo.webhook_secret'));
if (! hash_equals($expected, $m[2])) {
abort(401);
}
$event = json_decode($raw, true);
$payment = $event['data']['payment'];
if ($event['type'] === 'payment.completed' && $event['livemode'] === true && empty($payment['test'])) {
$order = Order::find($payment['reference_id']);
if ($order && $order->status !== 'paid' && $payment['amount'] === number_format($order->total, 2, '.', '')) {
$order->update(['status' => 'paid']); // queue emails and shipping from here
}
}
return response()->noContent();
}
}
Answer within 8 seconds; send emails and shipping from a queued job. Dalo retries failed deliveries (5 s, 30 s, 2 min, 10 min, 1 h) with the same X-KHQR-Delivery id, so the handler must be safe to run twice. The status !== 'paid' check above does that.
5. Test, then go live
With the dk_test_ key, open a checkout and press Simulate paid, or call POST /v1/payments/{id}/simulate. Your webhook receives a real signed event with livemode: false: the code above acknowledges it and ships nothing. To test the paid path, temporarily allow livemode === false on your machine. When it works, verify the store in the dashboard and swap the key for a dk_live_ one. Nothing else changes.
Questions
Is there a Composer package for Dalo?
You do not need one: the API is plain JSON over HTTPS and Laravel's Http client covers it in a few lines, as shown above.
Why does my webhook return 419?
Laravel's CSRF check is rejecting it. Exclude webhooks/dalo from CSRF verification (bootstrap/app.php on Laravel 11+, VerifyCsrfToken on Laravel 10).
Why does the signature never match?
The signature covers the exact raw body. Use $request->getContent(), not $request->all() or json_encode of the parsed input, and use the whsec_ secret of the same endpoint.
Can I show the QR on my own Blade page instead of redirecting?
Yes. Put qr_image_url in an img tag and poll GET /v1/payments/{id} from your server every 2 to 3 seconds until the status is paid, expired, failed or cancelled.
Get a test key in two minutes.
Sign in with your email, create a store in test mode and copy a dk_test_ key. No merchant application, no paperwork. Free up to 50 sales a month.
More guidesKHQR payment APINode.jsNext.jsDjangoTelegram botTest modeCheck payment statusFlask and FastAPIStatic websiteChoosing an API