# PHP helper (intents-php)

> intents-php: createInvoice and verifyWebhook for PHP in one file, no framework, PHP 7.4+. For WHMCS, PrestaShop and custom PHP shops.

```php
require __DIR__ . '/intents.php';

$intents = new \IntentsFi\Intents(getenv('INTENTS_SECRET_KEY'));
$invoice = $intents->createInvoice(['amount' => '42.00', 'reference' => $orderId]);
```

One file, no framework, no Composer needed: copy
[`intents.php`](https://github.com/devluke24/intentsfi/blob/main/packages/php/intents.php) next to
your code, or `composer require intents-fi/intents-php`. PHP 7.4 or later; `createInvoice` needs
the `curl` extension. The WooCommerce, WHMCS and PrestaShop plugins check signatures with the same
code.

**Ship the order only on a verified `invoice.paid` webhook.** The customer landing on your
thank-you page, or the pop-up saying it is done, is for their screen.

## createInvoice

```php
$intents = new \IntentsFi\Intents(getenv('INTENTS_SECRET_KEY')); // ik_test_… while you build
$invoice = $intents->createInvoice([
    'amount'     => '42.00',               // USD, from your own records
    'reference'  => $orderId,              // required: a repeat returns the same invoice
    'successUrl' => 'https://shop.example/thanks',
]);
// $invoice['id'], $invoice['url'] (hosted page), $invoice['clientSecret'] (for the pop-up)
```

The API origin defaults to `INTENTS_API_URL`, else `https://api.intents.fi`; pass it as the second
constructor argument to override. Errors throw `\IntentsFi\ApiError`, with the API's code in
`->errorCode` (`INVALID_REQUEST`, `UNAUTHORIZED`, …) and the HTTP status in `->status`.

`getInvoice($id)` returns the same shape a webhook's `data` carries.

## verifyWebhook

```php
try {
    $event = \IntentsFi\Intents::verifyWebhook(
        file_get_contents('php://input'),   // the raw body, never a re-encoded one
        $_SERVER,                           // reads HTTP_X_INTENTS_SIGNATURE
        getenv('INTENTS_WEBHOOK_SECRET')
    );
} catch (\IntentsFi\WebhookSignatureError $e) {
    http_response_code(400);
    exit;
}

if ($event['type'] === 'invoice.paid' && !\IntentsFi\Intents::isTestEvent($event)) {
    mark_paid($event['data']['reference']);   // must be safe to run twice
}
http_response_code(204);
```

`$headers` may be `$_SERVER`, `getallheaders()`, any header array in any letter case, or the
header value itself. It checks the HMAC and that the timestamp is within five minutes (the fourth
argument changes the window). `WebhookSignatureError->reason` is one of `missing_secret`,
`missing_signature`, `malformed_signature`, `timestamp_out_of_tolerance`, `signature_mismatch` or
`invalid_payload`, the same values as the [TypeScript helper](/docs/platform/server-sdk).

| Helper | True when |
|---|---|
| `Intents::isFinal($event)` | the invoice will not change again: paid, expired or canceled |
| `Intents::isTestEvent($event)` | it came from **Send test event**: acknowledge it, never ship on it |

## A complete example

[Example: plain PHP](/docs/checkout/examples/php): a checkout endpoint, a webhook endpoint and one
page, run in CI against a test key. [Verify and test webhooks](/docs/platform/verify-webhooks)
covers duplicates, ordering and testing your receiver.

---

Source: https://intents.fi/docs/platform/server-sdk-php · The whole corpus: https://intents.fi/docs/llms-full.txt
