65536) { throw new Problem('payload_too_large', 413); } if (strlen($signature) > 4096 || substr_count($signature, 't=') !== 1 || !preg_match('/(?:^|,)t=([0-9]{1,12})(?:,|$)/D', $signature, $match) || abs(time() - (int) $match[1]) > 300) { throw new Problem('invalid_signature', 400); } try { Webhook::constructEvent($raw, $signature, $secret, 300); } catch (\UnexpectedValueException $error) { throw new Problem('invalid_payload', 400); } catch (Throwable $error) { throw new Problem('invalid_signature', 400); } try { $event = json_decode($raw, true, 16, JSON_THROW_ON_ERROR); } catch (Throwable $error) { throw new Problem('invalid_payload', 400); } if (!is_array($event) || !is_string($event['id'] ?? null) || !preg_match('/^[a-zA-Z0-9_]{1,160}$/D', $event['id']) || !is_string($event['type'] ?? null) || !is_string($event['data']['object']['id'] ?? null) || !preg_match('/^[a-zA-Z0-9_]{1,160}$/D', $event['data']['object']['id'])) { throw new Problem('invalid_payload', 400); } SandboxGuard::object($event); if (!in_array($event['type'], ['checkout.session.completed', 'checkout.session.async_payment_succeeded', 'checkout.session.async_payment_failed', 'checkout.session.expired'], true)) { throw new Problem('unsupported_event', 400); } return ['id' => $event['id'], 'type' => $event['type'], 'session_id' => $event['data']['object']['id']]; } }