admin管理员组

文章数量:1415664

I'm trying to convert my Google Chrome Extension, which is published in the Chrome Web Store, into a Free Trial using their new Licensing API - however Googles docs for this are excruciatingly confusing to me. See:

Furthermore, it seems that OpenID 2.0 is deprecated?

Is there some sort of drop-in code to setup a Free Trial and check the user against the Licensing API? I have a lot of users and I don't want to mess up and force them to hit a payment wall- they should be grandfathered in for free. I can't find anyone else online who has done this to look at their code and understand.

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

I appreciate any help here!

I'm trying to convert my Google Chrome Extension, which is published in the Chrome Web Store, into a Free Trial using their new Licensing API - however Googles docs for this are excruciatingly confusing to me. See: https://developer.chrome./webstore/check_for_payment

Furthermore, it seems that OpenID 2.0 is deprecated? https://developers.google./accounts/docs/OpenID2

Is there some sort of drop-in code to setup a Free Trial and check the user against the Licensing API? I have a lot of users and I don't want to mess up and force them to hit a payment wall- they should be grandfathered in for free. I can't find anyone else online who has done this to look at their code and understand.

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

I appreciate any help here!

Share Improve this question edited Sep 7, 2014 at 6:29 Catalyst asked Sep 7, 2014 at 5:45 CatalystCatalyst 1374 silver badges9 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

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

I edited it a bit to add more error trapping as I moved it into my app. I had a lot of times where I had to go to different Google API things though and edit them. If you have trouble with this code, I will try to look up what I changed and let you know. You can copy and paste this into the console window of the background page and execute it with getLicense. If you copy your key into the manifest, you can do it with a local extension and don't have to wait an hour with every change. I highly remend that. The key is the one from the developer dashboard -more info.

function getLicense() {
  var CWS_LICENSE_API_URL = 'https://www.googleapis./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 pleted.");
          callback(null, xhr.status, xhr.response);
        }
        }else{
          console.log("Error - " + xhr.statusText);  
        }
      }
    try {
      xhr.send();
    } catch(e) {
      console.log("Error in xhr - " + e);
    }
  }
}

本文标签: javascriptChrome Web Store Payments Free trial for ExtensionStack Overflow