Using the Freemius API to check if a licence is valid
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 | |
/** | |
* Check to see if our licence is valid | |
* | |
* @param $license | |
* | |
* @return bool | |
*/ | |
function checkLicence( $license ) { | |
define( 'FS__API_SCOPE', 'plugin' ); | |
define( 'FS__API_DEV_ID', 0001 ); | |
define( 'FS__API_PUBLIC_KEY', 'pk_xxxxxxxxxxxxxxxxx' ); | |
define( 'FS__API_SECRET_KEY', 'sk_xxxxxxxxxxxxxxxxx' ); | |
// Init SDK. | |
$api = new Freemius_Api( FS__API_SCOPE, FS__API_DEV_ID, FS__API_PUBLIC_KEY, FS__API_SECRET_KEY ); | |
// Get all products. | |
$result = $api->Api( sprintf( '/licenses/%s.json', $license ) ); | |
if ( property_exists( $result, 'error' ) ) { | |
return false; | |
} else { | |
try { | |
$now = new DateTime(); | |
$expiration = new DateTime( $result->expiration ); | |
$activated = $result->activated; | |
if ( $activated && $expiration >= $now ) { | |
return true; | |
} | |
return false; | |
} catch ( Exception $exception ) { | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment