|
<?php |
|
|
|
// These are required environment variables! |
|
define('KEYGEN_PRODUCT_TOKEN', getenv('KEYGEN_PRODUCT_TOKEN')); |
|
define('KEYGEN_ACCOUNT_ID', getenv('KEYGEN_ACCOUNT_ID')); |
|
define('KEYGEN_POLICY_ID', getenv('KEYGEN_POLICY_ID')); |
|
|
|
// Since this page should only be accessed after a successful order redirect, |
|
// we should verify that the passed order is valid -- this helps defend |
|
// against somebody accessing this page directly to generate free |
|
// license keys. |
|
// |
|
// NOTE: You should use this to check if the given order ID is valid before |
|
// continuing onto the license generation! E.g. if you're using 2Checkout, |
|
// utilize the 2Checkout API to validate the order ID. |
|
if (!isset($_GET['order'])) { |
|
http_response_code(400); |
|
|
|
echo "Order ID is required for generating new licenses\n"; |
|
|
|
exit(1); |
|
} |
|
|
|
function create_license() { |
|
$ch = curl_init( |
|
sprintf("https://api.keygen.sh/v1/accounts/%s/licenses", KEYGEN_ACCOUNT_ID) |
|
); |
|
|
|
$body = json_encode([ |
|
'data' => [ |
|
'type' => 'license', |
|
'attributes' => [ |
|
// NOTE: Hash the order ID to ensure duplicate licenses cannot be generated |
|
'key' => hash('sha256', $_GET['order']), |
|
// Store order ID in the license's metadata |
|
'metadata' => [ |
|
'orderId' => $_GET['order'] |
|
] |
|
], |
|
'relationships' => [ |
|
'policy' => [ |
|
'data' => ['type' => 'policy', 'id' => KEYGEN_POLICY_ID] |
|
] |
|
] |
|
] |
|
]); |
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
|
curl_setopt($ch, CURLOPT_POST, 1); |
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $body); |
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [ |
|
sprintf('Authorization: Bearer %s', KEYGEN_PRODUCT_TOKEN), |
|
'Content-Type: application/json', |
|
'Accept: application/json' |
|
]); |
|
|
|
$res = curl_exec($ch); |
|
$license = json_decode($res); |
|
|
|
// Check if we received an error from Keygen |
|
if (isset($license->errors)) { |
|
http_response_code(500); |
|
|
|
$messages = join(', ', array_map(function($err) { return "{$err->title}: {$err->detail}"; }, $license->errors)); |
|
|
|
echo "Keygen license creation error: $messages\n"; |
|
|
|
exit(1); |
|
} |
|
|
|
return $license->data; |
|
} |
|
|
|
function create_activation_token($license) { |
|
$ch = curl_init( |
|
sprintf("https://api.keygen.sh/v1/accounts/%s/licenses/%s/tokens", KEYGEN_ACCOUNT_ID, $license->id) |
|
); |
|
|
|
$body = json_encode([ |
|
'data' => [ |
|
'type' => 'token', |
|
'attributes' => [ |
|
'maxActivations' => 3, |
|
'maxDeactivations' => 2 |
|
] |
|
] |
|
]); |
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
|
curl_setopt($ch, CURLOPT_POST, 1); |
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $body); |
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [ |
|
sprintf('Authorization: Bearer %s', KEYGEN_PRODUCT_TOKEN), |
|
'Content-Type: application/json', |
|
'Accept: application/json' |
|
]); |
|
|
|
$res = curl_exec($ch); |
|
$token = json_decode($res); |
|
|
|
// Check if we received an error from Keygen |
|
if (isset($token->errors)) { |
|
http_response_code(500); |
|
|
|
$messages = join(', ', array_map(function($err) { return "{$err->title}: {$err->detail}"; }, $token->errors)); |
|
|
|
echo "Keygen activation token creation error: $messages\n"; |
|
|
|
exit(1); |
|
} |
|
|
|
return $token->data; |
|
} |
|
|
|
// Create the license and activation token |
|
$license = create_license(); |
|
$token = create_activation_token($license); |
|
|
|
// TODO: Generate distribution download link. Then email link, license key and |
|
// activation token to the customer. |
|
echo "Keygen: created license {$license->id} and activation token {$token->id}\n"; |
|
echo " License key: {$license->attributes->key}\n"; |
|
echo " Activation token: {$token->attributes->token}\n"; |
|
|
|
http_response_code(202); |