Integrate with PHP
Install and configure the Commet PHP SDK.
Install
composer require commet/commet-phpConfigure
COMMET_API_KEY=ck_sandbox_xxx<?php
require_once __DIR__ . '/vendor/autoload.php';
use Commet\Commet;
$commet = new Commet(
apiKey: $_ENV['COMMET_API_KEY'],
environment: 'sandbox',
);Create Customer and Subscribe
customers->create is idempotent — if a customer with the same external_id exists, it returns the existing record.
$commet->customers->create(
email: 'user@example.com',
externalId: 'user_123',
);
$subscription = $commet->subscriptions->create(
externalId: 'user_123',
planCode: 'pro',
);
$checkoutUrl = $subscription->data['checkout_url'];The customer is redirected to checkout to complete payment.
Check Access
$sub = $commet->subscriptions->get(externalId: 'user_123');
$status = $sub->data['status'];
$access = $commet->features->check(code: 'custom_branding', externalId: 'user_123');
$allowed = $access->data['allowed'];Track Usage
$commet->usage->track(
externalId: 'user_123',
feature: 'api_calls',
value: 1,
);Usage is aggregated and billed at end of period.
Webhooks
<?php
use Commet\Webhooks;
$webhooks = new Webhooks();
$payload = $webhooks->verifyAndParse(
rawBody: file_get_contents('php://input'),
signature: $_SERVER['HTTP_X_COMMET_SIGNATURE'] ?? '',
secret: $_ENV['COMMET_WEBHOOK_SECRET'],
);
if ($payload === null) {
http_response_code(401);
echo json_encode(['error' => 'Invalid signature']);
exit;
}
match ($payload['event']) {
'subscription.activated' => handleActivated($payload),
'subscription.canceled' => handleCanceled($payload),
default => null,
};
echo json_encode(['ok' => true]);Related
How is this guide?