Created
May 10, 2019 10:47
-
-
Save adnan-kamili/e62d931891f9e586c7b91cee89097bb3 to your computer and use it in GitHub Desktop.
Cryptlex- FastSpring integration script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// access token must have following permissions (scope): license:write, user:read, user:write | |
$PERSONAL_ACCESS_TOKEN = "YOUR_PERSONAL_ACCESS_TOKEN"; | |
function PostRequest($url, $body) | |
{ | |
$headers = array( | |
"Authorization: Bearer " . $GLOBALS['PERSONAL_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); | |
return json_decode($response); | |
} | |
function GetRequest($url) | |
{ | |
$headers = array("Authorization: Bearer ".$GLOBALS['PERSONAL_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); | |
return json_decode($response); | |
} | |
try { | |
$base_url = "https://api.cryptlex.com/v3"; | |
$product_id = "YOUR_PRODUCT_ID"; | |
// creating user is optional | |
$user = NULL; | |
$name_parts = explode(" ", $name); | |
$first_name = $name_parts[0]; | |
if(count($name_parts) == 1) { | |
$last_name = " "; | |
} else { | |
$last_name = $name_parts[1]; | |
} | |
$query['email'] = $email; | |
$users = GetRequest($base_url."/users?".http_build_query($query)); | |
if(count($users) == 0) { | |
$user_body["email"] = $email; | |
$user_body["firstName"] = $first_name; | |
$user_body["lastName"] = $last_name; | |
$user_body["company"] = $company; | |
// generate a random 8 character password | |
$user_body["password"] = substr(md5(uniqid()), 0, 8); | |
$user_body["role"] = "user"; | |
$user = PostRequest($base_url."/users",$user_body); | |
} else { | |
$user = $users[0]; | |
} | |
// creating license | |
$license_body["productId"] = $product_id; | |
$license_body["userId"] = $user->id; | |
$license_body["allowedActivations"] = (int) $quantity; | |
$metadata["key"] = $reference; | |
$metadata["value"] = "3"; | |
$metadata["visible"] = false; | |
$license_body["metadata"] = array($metadata); | |
$license = PostRequest($base_url."/licenses", $license_body); | |
echo $license->key; | |
} | |
catch (Exception $e) { | |
echo 'error: ' . $e->getMessage(); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment