我正在尝试使用他们的新许可API将我在Chrome网上应用店中发布的Google Chrome扩展程序转换为免费试用版 - 但Google的文档对我来说非常困惑。请参阅:https://developer.chrome.com/webstore/check_for_payment
此外,OpenID 2.0似乎已被弃用? https://developers.google.com/accounts/docs/OpenID2
是否有某种插入代码来设置免费试用版并根据Licensing API检查用户?我有很多用户,我不想搞砸并迫使他们打到支付墙 - 他们应该免费获得。我无法在网上找到其他人来查看他们的代码并理解。
理想情况下,我的分机应在7天内完全正常运行,然后免费试用期满,并要求用户付款。
我感谢你们的任何帮助!
答案 0 :(得分:7)
我在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);
}
}
}