Software Licensing API - Example using JavaScript

This document is a subset of the Software Licensing API document which lists all features available via the API. The following is example code showcasing how to implement those features with JavaScript.

Request

// Handling a SoftwAre licensing request without jQuery in pure JavaScript
var xhttp = new XMLHttpRequest();

// The url to the site running Easy Digital Downloads w/ Software Licensing
var postUrl = 'http://<domain.com>/edd-sl/';

xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
        var slData = JSON.parse(xhttp.responseText);
        handleSoftwareLicensingResponse( slData );
    }   
}

var data = {
    edd_action: 'check_license', // Valid actions are activate_license, deactivate_license, get_version, check_license
    license:    '<license key>',
    item_name:  encodeURIComponent('<item name>'),
    url: 'domain.com' // If you Disable URL Checking, you do not need this entry
};

xhttp.open("POST", postUrl, true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.setRequestHeader("Access-Control-Allow-Origin", "http://local.dev");

var values = '';
for (var key in data){
    values += key + '=' + data[ key ] + '&';
}
values = values.substring(0, values.length - 1);

xhttp.send(values);


function handleSoftwareLicensingResponse( slData ) {
    if ( slData.success == true ) {
        console.log(slData);
        // Software Licensing has a valid response to parse
    } else {
        // Invalid request was made to software licensing
    }
}

Response

A response to the above query would be a JSON object that would look something like this:

{
	"license": "valid",
	"item_name": "EDD Product name",
	"expires": "2014-10-23 00:00:00",
	"payment_id": 54224,
	"customer_name": "John Doe",
	"customer_email": "john@sample.com"
}