-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgenerate.php
78 lines (60 loc) · 2.2 KB
/
generate.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
// These are required!
$product_token = getenv('KEYGEN_PRODUCT_TOKEN');
$account_id = getenv('KEYGEN_ACCOUNT_ID');
$policy_id = getenv('KEYGEN_POLICY_ID');
// Since this page should only be accessed after a successful order,
// we should verify that the passed order is valid -- this helps defend
// against somebody accessing this page directly to generate free
// license keys.
$order_id = $_GET['order'];
if (!isset($order_id)) {
http_response_code(400);
echo "Order ID is required for generating new licenses";
exit(1);
}
// TODO: Verify the order actually happened, and that this request is
// coming from our payment provider.
// Create a new license key.
$url = "https://api.keygen.sh/v1/accounts/{$account_id}/licenses";
$ch = curl_init($url);
// Generate a short license key that can be easily input by-hand.
function generate_license_key($length = 16, $chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") {
return join('-', str_split(substr(str_shuffle($chars), 0, $length), 4));
}
$key = generate_license_key();
$body = json_encode([
'data' => [
'type' => 'licenses',
'attributes' => ['key' => $key],
'relationships' => [
'policy' => [
'data' => ['type' => 'policies', 'id' => $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, [
"Authorization: Bearer $product_token",
'Content-Type: application/json',
'Accept: application/json'
]);
$json = curl_exec($ch);
$res = json_decode($json);
// Check if we received an error from Keygen.
if (isset($res->errors)) {
http_response_code(500);
$messages = join(', ', array_map(function($err) { return $err->detail; }, $res->errors));
echo "Error: $messages\n";
exit(1);
}
// All is good -- license was successfully created. Here, you may
// want to adjust the response below to meet your payment provider's
// requirements, i.e. plaintext vs JSON response, status code, etc.
// You may also want to email the user a copy of their license key,
// so that they can move onto the activation step.
http_response_code(200);
echo "{$res->data->attributes->key}";