Payment via the Chrome Web Store Free Trial for Extension

I’m trying to convert the Google Chrome extension, which is published on the Chrome Web Store, to a free trial using their new licensing API — however, the Google docs for Google are painfully confusing for me. See: https://developer.chrome.com/webstore/check_for_payment

Also, does it look like OpenID 2.0 is deprecated? https://developers.google.com/accounts/docs/OpenID2

Is there any code to install a free trial and check the user for compliance with the licensing API? I have many users, and I do not want to spoil and force them to hit the payment wall - they should be free. I cannot find anyone else on the net who did this to look at their code and understand.

Ideally, my extension should be fully functional for 7 days, and then expire a free trial and require payment from the user.

I appreciate any help here!

+4
source share
2 answers

I found a great resource at https://github.com/GoogleChrome/chrome-app-samples/tree/master/samples/one-time-payment

, , . , Google API . , , , . getLicense. , . . - .

function getLicense() {
  var CWS_LICENSE_API_URL = 'https://www.googleapis.com/chromewebstore/v1.1/userlicenses/';
  xhrWithAuth('GET', CWS_LICENSE_API_URL + chrome.runtime.id, true, onLicenseFetched);
}

function onLicenseFetched(error, status, response) {
  function extensionIconSettings(badgeColorObject, badgeText, extensionTitle ){
    chrome.browserAction.setBadgeBackgroundColor(badgeColorObject);
    chrome.browserAction.setBadgeText({text:badgeText});
    chrome.browserAction.setTitle({ title: extensionTitle });
  }
  var licenseStatus = "";
  if (status === 200 && response) {
    response = JSON.parse(response);
    licenseStatus = parseLicense(response);
  } else {
    console.log("FAILED to get license. Free trial granted.");
    licenseStatus = "unknown";
  }
  if(licenseStatus){
    if(licenseStatus === "Full"){
      window.localStorage.setItem('ChromeGuardislicensed', 'true');
      extensionIconSettings({color:[0, 0, 0, 0]}, "", "appname is enabled.");
    }else if(licenseStatus === "None"){
      //chrome.browserAction.setIcon({path: icon}); to disabled - grayed out?
      extensionIconSettings({color:[255, 0, 0, 230]}, "?", "appnameis disabled.");
      //redirect to a page about paying as well?
    }else if(licenseStatus === "Free"){
      window.localStorage.setItem('appnameislicensed', 'true');
      extensionIconSettings({color:[255, 0, 0, 0]}, "", window.localStorage.getItem('daysLeftInappnameTrial') + " days left in free trial.");
    }else if(licenseStatus === "unknown"){
      //this does mean that if they don't approve the permissions,
      //it works free forever. This might not be ideal
      //however, if the licensing server isn't working, I would prefer it to work.
      window.localStorage.setItem('appnameislicensed', 'true');
      extensionIconSettings({color:[200, 200, 0, 100]}, "?", "appnameis enabled, but was unable to check license status.");
    }
  }
  window.localStorage.setItem('appnameLicenseCheckComplete', 'true');
}

/*****************************************************************************
* Parse the license and determine if the user should get a free trial
*  - if license.accessLevel == "FULL", they've paid for the app
*  - if license.accessLevel == "FREE_TRIAL" they haven't paid
*    - If they've used the app for less than TRIAL_PERIOD_DAYS days, free trial
*    - Otherwise, the free trial has expired 
*****************************************************************************/

function parseLicense(license) {
  var TRIAL_PERIOD_DAYS = 1;
  var licenseStatusText;
  var licenceStatus;
  if (license.result && license.accessLevel == "FULL") {
    console.log("Fully paid & properly licensed.");
    LicenseStatus = "Full";
  } else if (license.result && license.accessLevel == "FREE_TRIAL") {
    var daysAgoLicenseIssued = Date.now() - parseInt(license.createdTime, 10);
    daysAgoLicenseIssued = daysAgoLicenseIssued / 1000 / 60 / 60 / 24;
    if (daysAgoLicenseIssued <= TRIAL_PERIOD_DAYS) {
      window.localStorage.setItem('daysLeftInCGTrial', TRIAL_PERIOD_DAYS - daysAgoLicenseIssued);
      console.log("Free trial, still within trial period");
      LicenseStatus = "Free";
    } else {
      console.log("Free trial, trial period expired.");
      LicenseStatus = "None";
      //open a page telling them it is not working since they didn't pay?
    }
  } else {
    console.log("No license ever issued.");
    LicenseStatus = "None";
    //open a page telling them it is not working since they didn't pay?
  }
  return LicenseStatus;
}

/*****************************************************************************
* Helper method for making authenticated requests
*****************************************************************************/

// Helper Util for making authenticated XHRs
function xhrWithAuth(method, url, interactive, callback) {
  console.log(url);
  var retry = true;
  var access_token;
  getToken();

  function getToken() {
    console.log("Calling chrome.identity.getAuthToken", interactive);
    chrome.identity.getAuthToken({ interactive: interactive }, function(token) {
      if (chrome.runtime.lastError) {
        callback(chrome.runtime.lastError);
        return;
      }
      console.log("chrome.identity.getAuthToken returned a token", token);
      access_token = token;
      requestStart();
    });
  }

  function requestStart() {
    console.log("Starting authenticated XHR...");
    var xhr = new XMLHttpRequest();
    xhr.open(method, url);
    xhr.setRequestHeader('Authorization', 'Bearer ' + access_token);
    xhr.onreadystatechange = function (oEvent) { 
      if (xhr.readyState === 4) {  
        if (xhr.status === 401 && retry) {
          retry = false;
          chrome.identity.removeCachedAuthToken({ 'token': access_token },
                                                getToken);
        } else if(xhr.status === 200){
          console.log("Authenticated XHR completed.");
          callback(null, xhr.status, xhr.response);
        }
        }else{
          console.log("Error - " + xhr.statusText);  
        }
      }
    try {
      xhr.send();
    } catch(e) {
      console.log("Error in xhr - " + e);
    }
  }
}
+6

All Articles