# Example: plain PHP

> Add 'Pay with crypto' to a PHP shop with intents-php: a checkout endpoint, a webhook endpoint and one HTML page. No framework, no Composer.

For custom PHP shops, and the shape WHMCS and PrestaShop modules follow. It uses
[intents-php](/docs/platform/server-sdk#php-intents-php), one file you copy next to your code.
**Ship the order on the `invoice.paid` webhook**, never on the pop-up's `onPaid`. This is
[`examples/checkout-php`](https://github.com/devluke24/intentsfi/tree/main/examples/checkout-php),
run in CI against a test key.

Copy [`intents.php`](https://github.com/devluke24/intentsfi/blob/main/packages/php/intents.php)
into the same folder, and set the environment:

```bash
# A test key while you build: dashboard.intents.fi → API keys → Test mode.
INTENTS_SECRET_KEY=ik_test_...
# The whsec_… shown when you added the webhook endpoint (Webhooks → Add an endpoint).
INTENTS_WEBHOOK_SECRET=whsec_...
```

## 1. Create the invoice

`checkout.php`

```php
<?php
// 1. "Pay with crypto" + Place order: create the invoice for this order. POST {"orderId": "…"}
require __DIR__ . '/intents.php';
require __DIR__ . '/orders.php';

$order = load_orders()[json_decode(file_get_contents('php://input'), true)['orderId'] ?? ''] ?? null;
header('Content-Type: application/json');
if ($order === null) { http_response_code(404); exit('{"error":"unknown order"}'); }

// The amount comes from your records; `reference` makes a repeat return the same invoice.
$intents = new \IntentsFi\Intents(getenv('INTENTS_SECRET_KEY'));
$invoice = $intents->createInvoice(['amount' => $order['amount'], 'reference' => $order['id']]);
echo json_encode(['invoiceId' => $invoice['id'], 'clientSecret' => $invoice['clientSecret']]);
```

## 2. Ship on the webhook

`webhook.php`

```php
<?php
// 3. The only place an order is marked paid: a verified invoice.paid webhook.
require __DIR__ . '/intents.php';
require __DIR__ . '/orders.php';

try {
    $event = \IntentsFi\Intents::verifyWebhook(
        file_get_contents('php://input'), // the raw body
        $_SERVER,                         // reads HTTP_X_INTENTS_SIGNATURE
        (string) getenv('INTENTS_WEBHOOK_SECRET')
    );
} catch (\IntentsFi\WebhookSignatureError $e) {
    http_response_code(400);
    exit('bad signature');
}
// "Send test event" samples were never paid: acknowledge them, ship nothing.
if ($event['type'] === 'invoice.paid' && !\IntentsFi\Intents::isTestEvent($event)) {
    mark_paid((string) $event['data']['reference']); // idempotent: events can arrive twice
}
http_response_code(204);
```

`orders.php` stands in for your database:

```php
<?php
// Your order store. Here: a JSON file. markPaid must be safe to call twice for one order.
function orders_file(): string { return getenv('ORDERS_FILE') ?: __DIR__ . '/orders.json'; }
function load_orders(): array {
    return is_file(orders_file()) ? json_decode(file_get_contents(orders_file()), true)
        : ['order_1001' => ['id' => 'order_1001', 'amount' => '25.00', 'status' => 'pending']];
}
function mark_paid(string $id): void {
    $orders = load_orders();
    if (!isset($orders[$id]) || $orders[$id]['status'] === 'paid') return; // already shipped
    $orders[$id]['status'] = 'paid'; // …and ship it
    file_put_contents(orders_file(), json_encode($orders), LOCK_EX);
}
```

## 3. The page

`index.html`

```html
<!doctype html>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Pay with crypto: PHP example</title>
<link rel="stylesheet" href="https://pay.intents.fi/v1/option.css">
<script src="https://pay.intents.fi/v1/checkout.js"></script>

<!-- 2. Your checkout: "Pay with crypto" beside your other methods. -->
<main style="max-width: 420px; margin: 48px auto; display: grid; gap: 8px">
  <h1>$25.00</h1>
  <label><input type="radio" name="payment-method" value="card" checked> Card</label>
  <label class="ifpm-Option ifpm-Option--light">
    <input class="ifpm-Radio" type="radio" name="payment-method" value="crypto">
    <span class="ifpm-Text">
      <span class="ifpm-Label">Pay with crypto</span>
      <span class="ifpm-Subtitle">Any token, any network</span>
    </span>
    <span class="ifpm-Icons" aria-hidden="true"><img src="https://pay.intents.fi/v1/brand/token-marks.svg" width="80" height="20" alt=""><span class="ifpm-More">+1,000</span></span>
  </label>
  <button id="place" type="button">Place order</button>
</main>

<script>
  document.getElementById("place").addEventListener("click", async () => {
    if (document.querySelector('[name="payment-method"]:checked').value !== "crypto") return; // your card flow
    const res = await fetch("/checkout.php", {
      method: "POST",
      headers: { "content-type": "application/json" },
      body: JSON.stringify({ orderId: "order_1001" }),
    });
    const { invoiceId, clientSecret } = await res.json();
    // onPaid only updates this page; the order ships from the webhook.
    IntentsCheckout.open({ invoiceId, clientSecret, onPaid: () => location.assign("/?paid=1") });
  });
</script>
```

## Try it

1. In the dashboard, switch to **Test mode**, create a secret key, and add a webhook endpoint for
   `invoice.paid` pointing at `webhook.php` (a tunnel's URL for a local server).
2. `php -S localhost:3700`, open the page, pick **Pay with crypto** and pay in the pop-up. Test
   mode moves no money.
3. **Send test event** delivers a sample the endpoint acknowledges and ignores; **Resend** on the
   real delivery leaves the order paid once.

---

Source: https://intents.fi/docs/checkout/examples/php · The whole corpus: https://intents.fi/docs/llms-full.txt
