Skip to content

Instantly share code, notes, and snippets.

@ezekg
Last active March 28, 2023 13:46
Show Gist options
  • Save ezekg/fe8696b43dd9262d4f44788ce102518e to your computer and use it in GitHub Desktop.
Save ezekg/fe8696b43dd9262d4f44788ce102518e to your computer and use it in GitHub Desktop.
<?php
// These are required!
$product_token = getenv('KEYGEN_PRODUCT_TOKEN');
$account_id = getenv('KEYGEN_ACCOUNT_ID');
// Retrieve the request's body and parse it as JSON
$body = @file_get_contents('php://input');
$event_json = json_decode($body);
// Retrieve from the Keygen API to assert the event exists
// TODO: replace this with signature verification
$ch = curl_init("https://api.keygen.sh/v1/accounts/{$account_id}/webhook-events/{$event_json->data->id}");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: Bearer $product_token",
'Accept: application/json'
]);
$json = curl_exec($ch);
$event = json_decode($json);
// Check if we received an error from Keygen
if (isset($event->errors)) {
http_response_code(500);
$messages = join(', ', array_map(function($err) { return "{$err->title}: {$err->detail}"; }, $event->errors));
echo "Error: $messages\n";
exit(1);
}
switch ($event->data->attributes->event) {
case 'license.created':
$license = json_decode($event->data->attributes->payload);
// Do something after a license is created…
var_dump($license);
break;
case 'license.suspended':
break;
case 'license.renewed':
break;
case 'license.expired':
break;
}
http_response_code(202);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment