javascriptxmlhttprequestelectronquery-stringeasy-digital-downloads

Difference between Electron-based XMLHttpRequest and browser-based URL with query string?


I'm working on an Electron app and trying to integrate the Easy Digital Downloads Software Licensing WordPress plugin. I haven't done much with HTTP communication in Electron/Javascript so this may be a naive question.

The problem: I am able to get a license activation response from my EDD server and while there is no specific error, for some reason a license is not activated. The odd thing is that if I use a URL and query string in a browser with the same data, the plugin responds as expected: I can activate, deactivate and check the status of a license.

So EDD seems to be working and there are no errors with Electron. But something is missing. Initially I was using the net Electron module but after this issue came up, I switched to using the example script from EDD (below) which uses XMLHttpRequest. With that I get the following response back:

{"success":true,"license":"valid","item_id":539,"item_name":"My Awesome App","license_limit":1,"site_count":0,"expires":"2020-12-19 23:59:59","activations_left":1,"checksum":"f2d66c6844b37d1fa931b813c408", "payment_id":248,"customer_name":"Marvin Gardens","customer_email":"marvin@home.com","price_id":false}

Which is fine except that "activations_left":1 never changes and it should given "license_limit":1. So something is wrong.

On the other hand, if I use a URL with a query string in a browser, the "activations_left" is decremented and license activation only works once (as it should). For example, this works:

http://YOURSITE.com/?edd_action=activate_license&item_id=8&license=cc22c1ec86304b36883440e2e84cddff&url=http://licensedsite.com

My Question: is there some fundamental difference between these two methods? Is there something I need to add to my XMLHttpRequest? I have a support ticket open with EDD but I need to keep moving with this. And sorry to be so long-winded!


UPDATE:
@aw04 suggested I try using GET – just tried that and I "get" the same response as before: no error but also no activation.

Could there be some property which should (or shouldn't) be in the Electron request which is (or isn't) in a browser request by default?

 xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function () {

    if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
      console.log('xhttp.responseText', xhttp.responseText);
    }
  }

  var url = "http://YOURSITE.com/?edd_action=activate_license&item_id=8&license=cc22c1ec86304b36883440e2e84cddff"
  xhttp.open("GET", url);
  xhttp.send();

var xhttp = new XMLHttpRequest();
var postUrl = 'http://<domain.com>/edd-sl/';

xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
        console.log(xhttp.responseText);
    }   
}

var data = {
    edd_action: 'check_license', 
    license:    '<license key>',
    item_name:  encodeURIComponent('<item name>'),
};

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);

Solution

  • Based on some help from Easy Digital Downloads support folks, this is resolved.

    The issue had to do with a property in their Software Licensing plugin setup: "Do not check URL". I hadn't enabled that with the result that my API call from Electron failed and the one using a browser succeeded because the browser was adding headers that Electron was not.

    After enabling "Do not check URL", calls from within Electron work. I guess there is also an option to pass in a URL, but since I am using EDD for licensing desktop software, that didn't seem like a needed option.

    Anyway, hope this helps someone.