Integrar con PHP
Instala y configura el SDK de Commet para PHP.
Instalar
composer require commet/commet-phpConfigurar
COMMET_API_KEY=ck_sandbox_xxx<?php
require_once __DIR__ . '/vendor/autoload.php';
use Commet\Commet;
$commet = new Commet(
apiKey: $_ENV['COMMET_API_KEY'],
);Crear cliente y suscribir
customers->create es idempotente — si ya existe un cliente con el mismo id, devuelve el registro existente.
$commet->customers->create(
email: 'user@example.com',
id: 'user_123',
);
$subscription = $commet->subscriptions->create(
customerId: 'user_123',
planCode: 'pro',
);
$checkoutUrl = $subscription->data['checkout_url'];El cliente es redirigido al checkout para completar el pago.
Verificar acceso
$sub = $commet->subscriptions->get('user_123');
$status = $sub->data['status'];
$access = $commet->features->check(code: 'custom_branding', customerId: 'user_123');
$allowed = $access->data['allowed'];Trackear uso
$commet->usage->track(
customerId: 'user_123',
feature: 'api_calls',
value: 1,
);El uso se agrega y se cobra al final del período.
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]);Relacionado
¿Cómo está esta guía?