# PayRam webhook signature verification in Laravel

> Laravel’s api routes have no CSRF check, which suits callbacks. This guide gives a tested controller.

- Canonical URL: https://payramhost.com/en/docs/webhook-laravel/
- Other languages: 中文: https://payramhost.com/docs/webhook-laravel/
- Published: 2026-11-11 · Last updated: 2026-11-29
- Site: PayRamHost (independently operated; not affiliated with PayRam)

> PayRamHost hosted merchant onboarding has been paused since 29 November 2026. Where this guide mentions PayRamHost, it describes the situation before the pause; the PayRam usage it explains still applies.

> **The short version:** a PayRam webhook signature is an HMAC-SHA256 of the **raw request body**, keyed with your **project API key**, sent in the header `X-Payram-Signature: sha256=<hex>`. In Laravel the key is to get **the raw bytes untouched by the framework** and compute the signature over them. Below is a complete, tested version.

## 1. Before you start

- Laravel 11 or later (tested on Laravel 13) and PHP 8.2 or later.
- Put the project API key in `PAYRAM_API_KEY` in `.env` and read it in `config/services.php`.
- Register an HTTPS callback address for the project in the PayRam dashboard and set it to active; see the [webhook guide](https://payramhost.com/en/docs/webhook-guide/#setup).
- With several projects, verify each with its own API key; see [multiple projects](https://payramhost.com/en/docs/multi-project-setup/).

## 2. The complete code

File: `app/Http/Controllers/PayramWebhookController.php`

```
<?php
// app/Http/Controllers/PayramWebhookController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PayramWebhookController extends Controller
{
    public function handle(Request $request)
    {
        // 原始请求体。不要用 $request->all() 或 $request->json() 重新组装后再算签名
        $raw = $request->getContent();

        $expected = 'sha256=' . hash_hmac('sha256', $raw, config('services.payram.api_key'));
        $given = (string) $request->header('X-Payram-Signature', '');

        if (! hash_equals($expected, $given)) { // 恒定时间比较
            return response('invalid signature', 401);
        }

        $event = json_decode($raw, true);

        // 以 reference_id + status 做幂等：同一条通知可能重复到达。
        // 尽快返回 2xx，耗时的事放进队列：ProcessPayramEvent::dispatch($event);

        return response('ok', 200);
    }
}
```

The code in this guide was tested locally before publishing: test cases cover a valid signature, a tampered body, re-serialised JSON, a missing signature, a short signature, a wrong key and more, and we confirmed that the openssl command below produces the same signature as the code.

## 3. Setup and routing

Three pieces of configuration. If your project has no `routes/api.php` yet (Laravel 11 and later do not ship one), run `php artisan install:api` first:

```
// config/services.php
'payram' => [
    'api_key' => env('PAYRAM_API_KEY'),
],

// routes/api.php
use App\Http\Controllers\PayramWebhookController;

Route::post('/payram/webhook', [PayramWebhookController::class, 'handle']);

# .env
PAYRAM_API_KEY=your project API key

# callback address: https://your-domain/api/payram/webhook
```

## 4. Why it is written this way

- **`$request->getContent()` is the raw body.** Do not rebuild it with `$request->all()` or `$request->json()` and compute the signature over that.
- **Compare with `hash_equals`.** It compares in constant time; never compare signatures with `==` or `===`.
- **Put the route in `routes/api.php`.** API routes skip CSRF verification and do not depend on a session. In `routes/web.php` the request would get a 419 for lack of a CSRF token.
- **Use `config()`, not `env()` in code.** After `php artisan config:cache`, calling `env()` outside config files returns `null`, and the signature will never match.
- **Put slow work on a queue.** In the controller only verify and enqueue, then return 200 immediately.

## 5. Testing locally

The command below computes the correct signature with your key and sends a simulated callback to your local server. It should return `200`; change any one character in `BODY` and send it again, and it should return `401`. Use the key of a separate test project, not a production one.

```
export PAYRAM_API_KEY='your test project API key'
BODY='{"reference_id":"a1b2c3d4e5","invoice_id":"INV-0090","status":"FILLED"}'
SIG="sha256=$(printf '%s' "$BODY" | openssl dgst -sha256 -hmac "$PAYRAM_API_KEY" -hex | sed 's/^.* //')"
curl -i -X POST http://localhost:8000/api/payram/webhook \
  -H "Content-Type: application/json" \
  -H "X-Payram-Signature: $SIG" \
  -d "$BODY"
```

## 6. What to add before going live

- **Idempotency.** The same notification can arrive twice. Use `reference_id` plus `status` as a unique key and answer 200 straight away if it was already handled.
- **Ship only after receipt.** Ship only on `FILLED` or `OVER_FILLED`; see [payment statuses](https://payramhost.com/en/docs/payment-statuses/).
- **Return 2xx first, then process.** PayRam retries requests answered with 400 or above, or with no response within 60 seconds; the rules are in the [webhook guide](https://payramhost.com/en/docs/webhook-guide/#retry).
- **Use the status endpoint as a safety net.** If a notification is ever missed, reconcile with the [status endpoint](https://payramhost.com/en/docs/create-payment-api/).
- **Run it once with a real 1 US dollar payment.** See the [go-live checklist](https://payramhost.com/en/docs/go-live-checklist/).

## 7. FAQ

### Why do I get 419 Page Expired?

The route is in `routes/web.php` and is caught by CSRF verification. Move it to `routes/api.php`. If it has to stay in web, add the path to the CSRF exceptions (`validateCsrfTokens(except: [...])` in `bootstrap/app.php`); we did not test that approach.

### The signature is still wrong after I changed `.env`.

Run `php artisan config:clear`. Once the configuration is cached, new environment variables are not picked up.

### How do I handle duplicate notifications?

Keep a log table with a unique index on `reference_id` and `status`, insert before handling, and if the insert fails it was already handled, so return 200. See the [webhook guide](https://payramhost.com/en/docs/webhook-guide/#idempotent).

## Related reading

- [Webhook guide](https://payramhost.com/en/docs/webhook-guide.md): How to receive PayRam webhooks: register an endpoint, verify X-Payram-Signature, understand retry rules and payment states, handle duplicates, with Node.js and Python examples.
- [Payment statuses](https://payramhost.com/en/docs/payment-statuses.md): What PayRam's five payment statuses mean, how to handle underpaid (PARTIALLY_FILLED), overpaid (OVER_FILLED) and expired (CANCELLED) payments, and how to keep your order status in sync using webhooks and the status endpoint.
- [Create payments via API](https://payramhost.com/en/docs/create-payment-api.md): Add crypto payments to your own site with the PayRam API: create a payment and get a payment link, send the customer to the payment page, confirm receipt with webhooks and the status endpoint, and reconcile by order number. With curl, Node.js and Python examples.
- [Go-live checklist](https://payramhost.com/en/docs/go-live-checklist.md): PayRam mainnet has no sandbox, so test with small real amounts before launch. This checklist covers, in order: checking the configuration, one payment per chain and coin, verifying sweeping, under- and overpayment and expiry, webhooks, refunds and payouts, the card channel, and where to look when something fails.

## Sources

This guide is based on the PayRam official documentation below, checked on 2026-11-12. PayRamHost is an independently operated service and is not affiliated with PayRam. If anything differs, the official documentation prevails.

- [Webhook](https://docs.payram.com/api-integration/payments-api/webhook)
- [Payment Status](https://docs.payram.com/api-integration/payments-api/payment-status)
- [Payout Webhooks](https://docs.payram.com/api-integration/payouts-apis/payout-webhooks)
