createInvoice(['amount' => '25.00', 'currency' => 'USDT', 'network' => 'tron', 'order_id' => '1042']); * header('Location: ' . $invoice['checkout_url']); * * In your webhook endpoint: * * $raw = file_get_contents('php://input'); * $event = HoltPay\Client::verifyWebhook($raw, $_SERVER['HTTP_X_HOLTPAY_SIGNATURE'] ?? '', getenv('HOLTPAY_WEBHOOK_SECRET')); * if ($event['event'] === 'invoice.paid') { // mark order $event['data']['order_id'] as paid } * http_response_code(200); */ namespace HoltPay; class Error extends \Exception { /** @var int */ public $status; /** @var string */ public $errorCode; public function __construct(int $status, string $errorCode, string $message) { parent::__construct($message); $this->status = $status; $this->errorCode = $errorCode; } } class Client { /** @var string */ private $apiKey; /** @var string */ private $baseUrl; /** @var int */ private $timeout; public function __construct(string $apiKey, string $baseUrl, int $timeoutSeconds = 15) { if ($apiKey === '') { throw new \InvalidArgumentException('HoltPay: the API key is required'); } $this->apiKey = $apiKey; $this->baseUrl = rtrim($baseUrl, '/'); $this->timeout = $timeoutSeconds; } /** @return array */ public function request(string $method, string $path, ?array $body = null): array { $ch = curl_init($this->baseUrl . $path); $headers = ['Authorization: Bearer ' . $this->apiKey, 'Accept: application/json']; if ($body !== null) { $headers[] = 'Content-Type: application/json'; curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body)); } curl_setopt_array($ch, [ CURLOPT_CUSTOMREQUEST => $method, CURLOPT_HTTPHEADER => $headers, CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => $this->timeout, CURLOPT_FOLLOWLOCATION => false, ]); $raw = curl_exec($ch); if ($raw === false) { $message = curl_error($ch); curl_close($ch); throw new Error(0, 'network_error', 'HoltPay: ' . $message); } $status = (int) curl_getinfo($ch, CURLINFO_RESPONSE_CODE); curl_close($ch); $json = json_decode($raw, true); if (!is_array($json)) { $json = []; } if ($status < 200 || $status >= 300) { $error = isset($json['error']) && is_array($json['error']) ? $json['error'] : []; throw new Error($status, (string) ($error['code'] ?? 'http_error'), (string) ($error['message'] ?? ('HTTP ' . $status))); } return $json; } /** The networks and currencies this HoltPay site accepts. */ public function networks(): array { return $this->request('GET', '/v1/networks')['data']; } /** * @param array $input amount, currency, network, and optionally order_id, description, metadata, redirect_url, expires_in_minutes */ public function createInvoice(array $input): array { return $this->request('POST', '/v1/invoices', $input); } public function getInvoice(string $id): array { return $this->request('GET', '/v1/invoices/' . rawurlencode($id)); } /** @param array $filters status, order_id, test (bool), limit, before */ public function listInvoices(array $filters = []): array { if (array_key_exists('test', $filters)) { $filters['test'] = $filters['test'] ? '1' : '0'; } $query = $filters ? '?' . http_build_query($filters) : ''; return $this->request('GET', '/v1/invoices' . $query)['data']; } /** Test network only: settle an invoice as if the customer had paid. */ public function simulatePayment(string $id): array { return $this->request('POST', '/v1/invoices/' . rawurlencode($id) . '/simulate-payment', []); } /** * Check a webhook's X-HoltPay-Signature header against the raw body and return the decoded event. * Throws \RuntimeException when the signature is wrong or too old. */ public static function verifyWebhook(string $rawBody, string $header, string $secret, int $toleranceSeconds = 300): array { $parts = []; foreach (explode(',', $header) as $piece) { $kv = explode('=', trim($piece), 2); if (count($kv) === 2) { $parts[$kv[0]] = $kv[1]; } } if (!isset($parts['t'], $parts['v1']) || !ctype_digit($parts['t']) || !preg_match('/^[0-9a-f]{64}$/', $parts['v1'])) { throw new \RuntimeException('HoltPay webhook: malformed signature header'); } if (abs(time() - (int) $parts['t']) > $toleranceSeconds) { throw new \RuntimeException('HoltPay webhook: signature is too old'); } $expected = hash_hmac('sha256', $parts['t'] . '.' . $rawBody, $secret); if (!hash_equals($expected, $parts['v1'])) { throw new \RuntimeException('HoltPay webhook: signature does not match'); } $event = json_decode($rawBody, true); if (!is_array($event)) { throw new \RuntimeException('HoltPay webhook: body is not JSON'); } return $event; } }