• Hello.

    Would it be possible to add a simple Key validating example?
    Just so i could copy/phaste and edit a few valuables if needed.
    I am not a big coder, but thats the only thing i would need to buy the plugin!

    I’m crossing my fingers!
    – David

Viewing 1 replies (of 1 total)
  • Plugin Contributor 10Quality

    (@10quality)

    Hi David, thanks for the awesome review.

    Sure, we’ll be glad to help you! In which coding language are you looking to implement the license key validation?

    This can be implemented on any coding language.

    If its PHP, you can use our PHP library (php5 version recommended):
    https://github.com/10quality/license-keys-php-client/tree/php5

    This package provides a complete documentation here: https://github.com/10quality/license-keys-php-client/wiki

    If you are looking to implement this on an existing plugin or theme of yours, then maybe you can do something like this (on functions.php or plugin.php):

    require_once '[path-to-package-folder]/src/LicenseRequest.php';
    require_once '[path-to-package-folder]/src/Client.php';
    require_once '[path-to-package-folder]/src/Api.php';
    
    use LicenseKeys\Utility\Api;
    use LicenseKeys\Utility\Client;
    use LicenseKeys\Utility\LicenseRequest;
    
    define( 'MY_PRODUCT_LICENSE_OPTION', '_my_product_license' );
    
    // Activate product
    function my_product_activate( $license_key ) {
        $response = Api::activate(
            Client::instance(), // Client instance
            function() use( $license_key ) {
                return LicenseRequest::create(
                    'https://your-domain.com/wp-admin/admin-ajax.php', // API's base url
                    'YOUR-STORE', // API's store code
                    'SKU', // Related product SKU
                    $license_key ,
                    LicenseRequest::DAILY_FREQUENCY
                );
            },
            function( $license ) {
               // Here we save the license string into wordpress
               update_option( MY_PRODUCT_LICENSE_OPTION, $license, true );
            }
        );
        return $response->error === false;
    }
    
    // Validate product
    function my_product_is_valid() {
        return = Api::validate(
            Client::instance(), // Client instance
            function() {
                return new LicenseRequest( get_option( MY_PRODUCT_LICENSE_OPTION ) );
            },
            function( $license ) {
               // We update license string
               update_option( MY_PRODUCT_LICENSE_OPTION, $license );
            }
        );
    }
    
    // Deactivate product
    function my_product_deactivate() {
        $response = Api::deactivate(
            Client::instance(), // Client instance
            function() {
                return new LicenseRequest( get_option( MY_PRODUCT_LICENSE_OPTION ) );
            },
            function( $license ) {
               // We clear license string
               if ($license === null)
                   update_option( MY_PRODUCT_LICENSE_OPTION, null );
            }
        );
        return $response->error === false;
    }

    Make sure to any instace of “MY_PRODUCT” with the name of your product (don’t use spaces or any weird character aside from underscore “_” or dashes “-“).

    Once those functions are in place you can use them as follows (anywhere in your project):

    – To activate the product

    
    // To activate the product
    if ( my_product_activate( '[A CLIENT LICENSE KEY]' ) ) {
        // Product has been activated
    } else {
        // Product has not been activated. An error occured
        // You will need more coding knowledge to read the response and display
        // the error properly. But this is much simple
    }
    

    – To hide features in your code

    
    if ( my_product_is_valid() ) {
        // Only activated products can reach this code
    }
    

    – To hide features in a template

    
    <div>
        <?php if ( my_product_is_valid() ) : ?>
            <a>My paid featured link</a>
        <?php endif ?>
    </div>
    

    – To deactivate a product

    
    // To activate the product
    if ( my_product_deactivate() ) {
        // Product has been deactivated
    } else {
        // Product has not been deactivated. An error occured
        // You will need more coding knowledge to read the response and display
        // the error properly. But this is much simple
    }
    

    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.

    Best regards.

    • This reply was modified 6 years, 2 months ago by 10Quality.
Viewing 1 replies (of 1 total)
  • The topic ‘Simple key validating’ is closed to new replies.