Tutorial: Quick Script - 10quality/license-keys-php-client GitHub Wiki
Small tutorial that will explain how to integrate the client with your PHP product. Make sure to the install the client before the step 1.
NOTE: This script uses "WP Ajax" as API handler. To change the handler read this article.
Let's start by referencing the client classes at the top of our script:
use LicenseKeys\Utility\Api;
use LicenseKeys\Utility\Client;
use LicenseKeys\Utility\LicenseRequest;
Your product must handled the LICENSE STRING, which is the license key data needed for the client to check the authenticity and validity of the license key whenever the client is used for activation, validation or deactivation. By handling, your product must be capable of creating, updating, reading and deleting the license string with ease.
You will need to code 3 methods inside your product, one for creating, one for updating, another for reading and a last one for deleting. Optionally, you can add a layer of security by encrypting the license key string during this process.
If you are using this client on a wordpress product (such as a paid plugin or a theme), you can use the core Wordpress function update_option()
to create, update and delete the license string and user the function get_option()
to read it.
For sake of this tutorial, we will create functions update_option()
and get_option()
to mimic Wordpress functionality:
/**
* Saves and updates the license string.
* @param string $key The key used as filename
* @param string $license License string.
*/
function update_option( $key, $license ) {
$filename = $key . '.key';
// Delete if license is null
if ( $license === null && is_file( $filename ) ) {
return unlink( $filename );
}
// Open or create and save
$file = fopen( $filename, 'w' ) or die( 'Unable to open or create file: '.$filename );
fwrite( $file , $license );
fclose( $file );
}
/**
* Returns license string found.
* @param string $key The key used as filename
*/
function get_option( $key ) {
$filename = $key . '.key';
// If no file return null
if ( ! is_file( $filename ) ) return;
// Read and return
$file = fopen( $filename, 'r' ) or die( 'Unable to open file: ' . $filename );
$license = fread( $file, filesize( $filename ) );
fclose( $file );
return $license;
}
For tutorial purposes, we are going to pretend that our product's name is photomeme
. We are going to keep this as reference moving on.
We will be defining a constant that will hold the key name where the license key will be stored:
define( 'PHOTOMEME_OPTION_KEY', '_photomeme_string' );
Now, lets code inside the product the methods that will handle the activation, validation and deactivation endpoints.
The activate method will make the activation request and will try to activate your product based on a license key provided by your customer.
Inside this method is where you will define the API configuration (the url where the api is located, the store code and sku).
For tutorial purposes, we are going to pretend that our API is hosted at https://tutorials.net/wp-admin/admin-ajax.php
, the store code is fake007
and the SKU, for our product "PHOTO MEME", is PHOTOMEME
. We are going to setup the client to only do API validations on daily bases (read more).
/**
* Method used to activate the product. Returns true or false.
* @param string $license_key A customer license key.
* @return bool
*/
function photomeme_activate( $license_key ) {
$response = Api::activate(
Client::instance(),
function() use( $license_key ) {
return LicenseRequest::create(
'https://tutorials.net/wp-admin/admin-ajax.php',
'fake007',
'PHOTOMEME',
$license_key ,
LicenseRequest::DAILY_FREQUENCY
);
},
function( $license ) {
update_option( PHOTOMEME_OPTION_KEY, $license, true );
}
);
return $response->error === false;
}
If the activation is successful, the license string is saved using update_option()
and a true
flag is returned.
The validate method will make a validation request to the API or will self validate the license string based on the frequency set during activation.
The license string is updated every time there is a response from the API, overriding any change done locally where the product is located.
/**
* Returns true or false.
* @return bool
*/
function photomeme_is_valid() {
return Api::validate(
Client::instance(),
function() {
return new LicenseRequest( get_option( PHOTOMEME_OPTION_KEY ) );
},
function( $license ) {
update_option( PHOTOMEME_OPTION_KEY, $license );
}
);
}
NOTE: The example above will call your API's validate endpoint, performing a cross-server validation. If you wish to do a soft validation (not against your API, just validate the activated license string) use this function code instead:
/**
* Returns true or false.
* @return bool
*/
function photomeme_is_valid() {
return Api::softValidate(
function() {
return new LicenseRequest( get_option( PHOTOMEME_OPTION_KEY ) );
}
);
}
/**
* Returns true or false.
* @return bool
*/
function photomeme_deactivate() {
$response = Api::deactivate(
Client::instance(),
function() {
return new LicenseRequest( get_option( PHOTOMEME_OPTION_KEY) );
},
function( $license ) {
if ($license === null)
update_option( PHOTOMEME_OPTION_KEY, null );
}
);
return $response->error === false;
}
With all the code above in place, now you have a small script that will cover all the functionality needed to integrate your product and the API.
You can check the following GIST that contains the full script as reference: https://gist.github.com/amostajo/b0bd8794e4fe996ec8e3f6ee7d7e8799
Use the function photomeme_activate()
we created to activate your product. For tutorial purposes, lets pretend a customer is activating with key the007fakekey-666
.
if ( photomeme_activate( 'the007fakekey-666' ) ) {
// Product has been activated
} else {
// Product has not been activated.
}
Use the function photomeme_is_valid()
to check if the product is valid or not. You can call this function as much times as you want, and can be used functionality-wise or inside html templates.
For example, use it to enable features:
if ( photomeme_is_valid() ) {
// Enable feature
}
For example, use it to display a button:
<div>
<?php if ( photomeme_is_valid() ) : ?>
<a>Featured button</a>
<?php endif ?>
</div>
Use the function photomeme_deactivate()
we created to deactivate your product.
if ( photomeme_deactivate() ) {
// Product has been deactivated
} else {
// Product has not been deactivated.
}
You will need to check if there is a license saved in the system to determine whether or not to display an activation form or a deactivation button.
To do this, first, you will need to retrieve the license string:
$license = get_option( PHOTOMEME_OPTION_KEY );
If license string is empty, then product has not been activated, else there is activation information in the license.
if ( empty( $license ) ) {
// DISPLAY: ACTIVATION FORM HERE
} else {
// Decode the license
$license = json_decode( $license );
// DISPLAY: ACTIVATION INFORMATION
echo $license->activation_id; // Activation id
echo date( get_option( 'date_format' ), $license->activation_id ); // Activation date
if ( $license->expire ) {
echo $license->expire_date;
}
// DISPLAY: DEACTIVATION BUTTON HERE
}
When the $license
is decoded, an object will be generated with the data returned by the API's response.
Hope this helps. If you are not working with PHP, you can use the API using normal HTTP requests. Just let us know what are you using and I can provide you a code snippet, but the logic is the same, you need to understand those 3 states of the license key ACTIVATION, VALIDATION and DEACTIVATION.