-
Notifications
You must be signed in to change notification settings - Fork 4
/
CryptlexApi.php
114 lines (98 loc) · 3.3 KB
/
CryptlexApi.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
class CryptlexApi
{
private static $access_token;
// Leave it as such unless you're using the self-hosted version of Cryptlex.
private static $base_path = "https://api.cryptlex.com/v3";
public static function SetAccessToken($access_token)
{
self::$access_token = $access_token;
}
public static function CreateUser($body)
{
$api_url = self::$base_path . "/users";
// creating new user...
$user = self::PostRequest($api_url, $body);
return $user;
}
public static function GetUser($email)
{
$api_url = self::$base_path . "/users";
$query['email'] = $email;
// check whether user exists
$users = self::GetRequest($api_url."?".http_build_query($query));
if (count($users)) {
// user already exists!
return $users[0];
}
// user not found
return NULL;
}
public static function CreateLicense($body)
{
$api_url = self::$base_path . "/licenses";
// creating license...
$license = self::PostRequest($api_url, $body);
return $license;
}
public static function RenewLicense($productId, $metadataKey, $metadataValue)
{
$api_url = self::$base_path . "/licenses";
// fetching existing license...
$licenses = self::GetRequest($api_url."?productId=".$productId."&metadataKey=".$metadataKey."&metadataValue=".$metadataValue);
if (count($licenses) == 0) {
throw new Exception("License does not exist!");
}
$license = $licenses[0];
// renewing existing license...
$renewedLicense = self::PostRequest($api_url."/".$license->id."/renew", null);
return $renewedLicense;
}
private static function GetRequest($url)
{
if (!self::$access_token)
{
throw new Exception("You must set the access token.");
}
$headers = array("Authorization: Bearer ".self::$access_token, "Content-Type: application/json");
$request = curl_init($url);
curl_setopt($request, CURLOPT_HEADER, 0);
curl_setopt($request, CURLOPT_ENCODING, "");
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($request, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($request, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($request);
$info = curl_getinfo($request);
if($info["http_code"] != 200)
{
throw new Exception($response);
}
curl_close($request);
//var_dump($response);
return json_decode($response);
}
private static function PostRequest($url, $body)
{
if (!self::$access_token)
{
throw new Exception("You must set the access token.");
}
$headers = array("Authorization: Bearer ".self::$access_token, "Content-Type: application/json");
$request = curl_init($url);
curl_setopt($request, CURLOPT_HEADER, 0);
curl_setopt($request, CURLOPT_ENCODING, "");
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($request, CURLOPT_POSTFIELDS, json_encode($body));
curl_setopt($request, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($request, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($request);
$info = curl_getinfo($request);
if($info["http_code"] != 200 && $info["http_code"] != 201)
{
throw new Exception($response);
}
curl_close($request);
//var_dump($response);
return json_decode($response);;
}
}