0.1.4 • Published 12 months ago

woocommerce-software-add-on v0.1.4

Weekly downloads
-
License
ISC
Repository
github
Last release
12 months ago

WooCommerce Software Add-on NodeJS module

A simple implementation of WooCommerce's Software Add-on API.

Installation

Install module by running the command:

npm install woocommerce-software-add-on

or from the GitHub repository:

npm install git+https://github.com/cruzjuniel/woocommerce-software-add-on.git

Initialization

The module exports the class WooCommerceSoftwareAddOn. Add this class from the module to your javascript project using:

const { WooCommerceSoftwareAddOn } = require("woocommerce-software-add-on");

Create a new instance of the class by specifying the hostname and product ID:

let licenseManager = new WooCommerceSoftwareAddOn("example.com", "example");

Optionally, an email can also be specified:

let licenseManager = new WooCommerceSoftwareAddOn("example.com", "example", "example@example.com");

Additional option to allow insecure connections is also provided and is disabled by default. This can be used to handle errors from expired SSL certificates.

let licenseManager = new WooCommerceSoftwareAddOn("example.com", "example", "example@example.com", true);

WARNING: It is not recommended to allow insecure connections. Alternatively, the SSL certificate needs to be checked and renewed as necessary.

WooCommerceSoftwareAddOn Methods

These methods are available for use after the class WooCommerceSoftwareAddOn is initialized.

setEmail

This can be used to replace the email used for the API requests.

ArgumentDescription
emailEmail to replace the current default email set during initialization

Return: void

generateKey

This can be used to generate a new keys. This is based on the generate_key API call as documented here.

ArgumentDescription
secret_keySecret product key for use with the API
emailEmail to generate the license key for, default: email set during initialization
order_idOptional order ID to link the license key to, default: null
versionOptional version information of the software license, default: null
key_prefixOptional prefix for generated license keys, default: null
activationsAmount of activations possible per license key, default: 1

Return: Promise<WooCommerceSoftwareResult>

activateLicense

This can be used to activate a single instance of a license key. This is based on the activation API call as documented here.

ArgumentDescription
license_keyLicense key to activate for the software
instancePass to activate existing uses (previously deactivated), default: null, ignored
platformOptional platform information that can be used to identify unique systems, default: null

If platform is not provided (null), the default platform is a string containing information from os.hostname(), os.platform(), os.cpus() and machine ID as generated by node-machine-id

Return: Promise<WooCommerceSoftwareResult>

resetLicense

This can be used to reset/deactivate all activations of a license key. This is based on the activation_reset API call as documented here.

ArgumentDescription
license_keyLicense key to reset all activations

Return: Promise<WooCommerceSoftwareResult>

deactivateLicense

This can be used to deactivate a single activations of a license key. This is based on the deactivation API call as documented here.

ArgumentDescription
license_keyLicense key to deactivate single activation
instancePass to deactivate existing uses (previously activated), default: null, ignored
activation_idPass to only deactivate a single use, default: null, ignored

If both instance and license_key are null (or not provided), this will deactivate the oldest activation of the license key.

Return: Promise<WooCommerceSoftwareResult>

checkLicense

This can be used to check the activation status of a license key. This is based on the check API call as documented here.

ArgumentDescription
license_keyLicense key to validate a single activation
timestampPass to check the timestamp of the activation, default: 0, ignored
platformPass to check the platform used during activation,.default: null, auto-generated info

Return: Promise<WooCommerceSoftwareResult>

getActivations

This can be used to list the existing activations of a license key. This is based on the check API call as documented here.

ArgumentDescription
license_keyLicense key to check for activations

Return: Promise<Array<WooCommerceSoftwareActivations> | null>

Usage Example

Activating License and Checking Activation

const { WooCommerceSoftwareAddOn } = require("woocommerce-software-add-on");

let licenseKey = "example-license-key";
let licenseManager = new WooCommerceSoftwareAddOn("example.com", "example_product_id", "example@example.com");

(async () => {

    // Activate License
    let instance = await licenseManager.activateLicense(licenseKey).then(async (value) => {
        // Initial attempt
        if (value.success) {
            return value.output.instance;
        } else if (value.output.code == 103) {
            // Deactivate oldest activation
            await licenseManager.deactivateLicense(licenseKey);
            // Retry activation
            return await licenseManager.activateLicense(licenseKey).then(value => {
                return value.output.instance;
            });
        } else {
            return null;
        }
    });
    // Save 'instance' for License Checking
    console.log(instance);

    // Check if the license key is activated with the saved instance
    let activated = await licenseManager.checkLicense(licenseKey, instance).then(async (value) => {
        return value.success;
    });
    console.log(activated);

})();

Additional Methods

Some methods are also exported so it is usable without the need to create a WooCommerceSoftwareAddOn instance. For these methods, hostname, product id and email are added as required arguments/parameters.

generateKey

ArgumentDescription
hostnameWordpress site hosting the WooCommerce Software Add-on plugin
product_idProduct ID designated for the software license
emailEmail address to validate the check activations of
secret_keySecret product key for use with the API
order_idOptional order ID to link the license key to, default: null
versionOptional version information of the software license, default: null
key_prefixOptional prefix for generated license keys, default: null
activationsAmount of activations possible per license key, default: 1
allowInsecureIndicates whether to allow insecure connections (expired SSL certificates)

Return: Promise<WooCommerceSoftwareResult>

Example

const { generateKey } = require("woocommerce-software-add-on");

(async () => {

    let licenseKey = await generateKey("example.com", "example_product_id", "example@example.com", "examplesecretkey").then(value => {
        if (value.success) {
            return value.output.key;
        } else {
            return null;
        }
    });
    console.log(licenseKey);

})();

checkLicense

ArgumentDescription
hostnameWordpress site hosting the WooCommerce Software Add-on plugin
product_idProduct ID designated for the software license
emailEmail address to validate the check activations of
license_keyLicense key to validate a single activation
timestampPass to check the timestamp of the activation, default: 0, ignored
platformPass to check the platform used during activation,.default: null, auto-generated info
allowInsecureIndicates whether to allow insecure connections (expired SSL certificates)

Return: Promise<WooCommerceSoftwareResult>

Example

const { checkLicense } = require("woocommerce-software-add-on");

(async () => {
    
    let activated = await checkLicense("example.com", "example_product_id",
        "example@example.com", "example-license-key", "example-timestamp").then(value => {
        return (value.success);
    });
    console.log(activated);

})();

Interface Types

WooCommerceSoftwareResult

Most methods/functions from the module can returns a Promise of this type. This interface type is an object with the following keys:

KeyDescription
successIndicates whethere the operation was a success or not
codeHTTP code or API error code
headersHTTP headers response received to the API request
outputActual JSON response to the API request, if API call received a proper API response
errorError that occurred during the API request or as indicated by the API response

WooCommerceSoftwareActivations

The getActivations returns a promise of an array of this type when used. This type is also contained withing the response to checkLicense. This type is an object with the following keys:

KeyDescription
activation_idID associated with the activation
instanceUsually a timestamp of the activation, or as indicated during activation
activation_platformAs indicated during activation
activation_timeActual JSON response to the API request if API call received a proper API response

Known Error Codes

These error codes are found in the plugin source code. For more information about each, please contact the WooCommerce Software Add-on plugin developers or check their documentation.

  • 100: Invalid Request
  • 100: An undisclosed error occurred
  • 100: The email provided is invalid
  • 100: The following required information is missing
  • 101: No matching license key exists
  • 101: Invalid License Key
  • 102: Software has been deactivated
  • 102: The purchase matching this product is not complete
  • 103: Remaining activations is equal to zero
  • 103: Exceeded maximum number of activations
  • 104: Could not activate key
  • 104: No matching instance exists
  • 104: Invalid Instance ID
  • 105: Non matching product_id and secret_key provided
  • 105: Invalid security key
  • 403: Forbidden

Check Request

  • 100: The email provided is invalid
  • 101: No matching license key exists
  • 102: The purchase matching this product is not complete

Activation Reset Request

  • 100: An undisclosed error occurred
  • 100: The email provided is invalid
  • 101: No matching license key exists

Activation Request

  • 100: The email provided is invalid
  • 101: No matching license key exists
  • 102: The purchase matching this product is not complete
  • 103: Remaining activations is equal to zero
  • 104: Could not activate key

Deactivation Request

  • 100: The email provided is invalid
  • 101: No matching license key exists
  • 104: No matching instance exists

Generate Key Request

  • 105: Non matching product_id and secret_key provided
  • 105: Invalid security key