A simple step-by-step guide to help you easily add license key generation, activation and validation to your WordPress plugins.
Start using KeyKit/licenses/check
endpoint, you can periodically check the license's status and take action if anything changes (for example if a license expires).
// === Generate a new License Key For a Product ===
// === Request: ===
<?php
$url = 'https://keykit.io/api/v1/licenses';
$ch = curl_init($url
);
$token = YOUR_KEYKIT_API_TOKEN;
$fields = json_encode(array(
'product_ref'=>
'prod_118830f35b2c0779ccac'
));
$authorization = "Authorization: Bearer ".$token;
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
$authorization));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($ch);
curl_close($ch);
# === Response: ===
echo $result;
{
"data": {
"id": "25",
"type": "license_key",
"attributes": {
"key": "a9e48217-203c-4c8a-b5cb-933fd4c95e06",
"status": "pending",
"activated_at": null,
"expired_at": null,
"expired": false
}
}
}
// Get The License Key:
echo $result["data"]["attributes"]["key"];
"a9e48217-203c-4c8a-b5cb-933fd4c95e06"
<?php
// This is your KeyKit Product Ref, used for license key activation/validation.
// This should match a Product Ref in your dashboard for the product you want to create licenses for:
define( 'KEYKIT_PRODUCT_REF', 'prod_118830f35b2c0779ccac' );
// Get the domain for the site activating the license key.
$domain = wp_parse_url(get_bloginfo('url'));
// Get the license key being submitted.
$license_key = sanitize_text_field($_POST['license_key']);
// Set the product this license key is being activated for
$product_ref = KEYKIT_PRODUCT_REF;
// Set the KeyKit Api url
$api_url = 'https://keykit.io/licenses/activate';
$attributes = array(
'key' => $license_key,
'product_ref' => $product_ref,
'domain' => $domain['host']
);
// Make the request to KeyKit and get the response
$request = wp_remote_post($api_url, $attributes);
$response = json_decode(wp_remote_retrieve_body($request));
// Check if the activation was successfully
if ($response['valid']) {
// Success! The license key was successfully activated
//Save the license key
update_option('keykit_license_key', $response->license_key);
// Activate your plugin!
} else {
// The license key activation was unsuccessful.
echo $response;
}
// Get the domain.
$domain = wp_parse_url(get_bloginfo('url'));
// Get the license key.
$license_key = get_option('keykit_license_key');
// Get the Product Reference Key
$product_ref = KEYKIT_PRODUCT_REF;
// Set the KeyKit Api url
$api_url = 'https://keykit.io/licenses/check';
$attributes = array(
'key' => $license_key,
'product_ref' => $product_ref,
'domain' => $domain['host']
);
// Make the request to KeyKit and get the response
$request = wp_remote_post($api_url, $attributes);
$response = json_decode(wp_remote_retrieve_body($request));
// Check if the license key is valid
if ($response['valid']) {
// Success! The license key is valid
// Do something!
} else {
// The license key is NOT valid.
// Do something else!
echo $response;
}