---
title: Accept crypto in PHP
description: A dependency-free PHP integration: build the HMAC string with hash_hmac, post an order with cURL, and verify the webhook with hash_equals before fulfilling.
url: https://gaapay.com/integrations/php
updated: 2026-12-04
language: en
type: guide
---

# Accept crypto in PHP

> PHP needs no Composer package: `hash_hmac`, `hash_equals` and cURL cover signing, constant-time comparison and transport. Read `php://input` for the raw body; do not use `$_POST`.

Most PHP payment integrations fail on one line: reading the body after PHP has already parsed it. GaaPay signs the raw body, so `file_get_contents("php://input")` is the only correct source.

## 1 · Create an order

```php
<?php
$path = "/api/v1/orders";
$raw  = json_encode([
  "amount" => number_format($amount, 2, ".", ""),
  "merchant_order_ref" => $ref,
], JSON_UNESCAPED_SLASHES);

$ts    = (string) time();
$nonce = bin2hex(random_bytes(16));
$payload = $ts."\n".$nonce."\nPOST\n".$path."\n".$raw;
$sig = hash_hmac("sha256", $payload, getenv("GAAPAY_SECRET"));

$ch = curl_init("https://api.gaapay.com".$path);
curl_setopt_array($ch, [
  CURLOPT_POST => true,
  CURLOPT_POSTFIELDS => $raw,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_HTTPHEADER => [
    "content-type: application/json",
    "x-gaapay-app: ".getenv("GAAPAY_APP"),
    "x-gaapay-timestamp: ".$ts,
    "x-gaapay-nonce: ".$nonce,
    "x-gaapay-signature: ".$sig,
  ],
]);
$result = json_decode(curl_exec($ch), true);
header("Location: ".$result["checkout_url"]);
```

## 2 · Verify the IPN

```php
<?php
$raw = file_get_contents("php://input");
$ts  = $_SERVER["HTTP_X_GAAPAY_TIMESTAMP"] ?? "";
$sig = $_SERVER["HTTP_X_GAAPAY_SIGNATURE"] ?? "";

$expected = "v1=".hash_hmac("sha256", $ts.".".$raw, getenv("GAAPAY_WEBHOOK_SECRET"));
if (!hash_equals($expected, $sig)) { http_response_code(401); exit; }
if (abs(time() - (int) $ts) > 300) { http_response_code(401); exit; }

$event = json_decode($raw, true);
if ($event["type"] === "payment.succeeded") {
  fulfil($event["data"]["order_id"]);
}
http_response_code(200);
```

> **Do not trust $_POST:** — PHP parses `application/json` differently depending on SAPI; `php://input` is stable. If you use a framework, grab the raw body before its middleware touches it.

## PHP pitfalls

- `json_encode` escapes slashes by default; use `JSON_UNESCAPED_SLASHES` and sign exactly what you send.
- `hash_equals` before any comparison — `==` leaks timing and accepts type juggling.
- Do not `urldecode` or re-encode the body; sign the bytes.
- Behind a proxy, confirm `HTTP_X_GAAPAY_*` headers are forwarded; some hosts strip unknown `x-` headers.

## PHP questions

### Is there a Laravel package?

No official package. The two snippets above fit a controller and a route; a package would only hide the two lines that matter.

### How do I avoid duplicate delivery?

Store `x-gaapay-event-id` with a unique index. If the insert conflicts, return 200 and skip the work.

### Can I run this on shared hosting?

Yes, if outbound HTTPS and `hash_hmac` are available (both are standard). The webhook route must be reachable from the internet.

[Open the sandbox](/console) — **Test the signature flow**: The sandbox is the default. Create an app, create an order, watch a webhook arrive — then flip one environment variable.

## Related

- https://gaapay.com/docs/auth
- https://gaapay.com/docs/verify
- https://gaapay.com/integrations/node
- https://gaapay.com/docs/errors
