Skip to content

Instantly share code, notes, and snippets.

@JEverhart383
Created June 4, 2020 13:56
Show Gist options
  • Save JEverhart383/77f2cf996a2355a74ce31f9561894f84 to your computer and use it in GitHub Desktop.
Save JEverhart383/77f2cf996a2355a74ce31f9561894f84 to your computer and use it in GitHub Desktop.
var GumroadLicenseValidator = /** @class */ (function () {
function GumroadLicenseValidator() {
}
GumroadLicenseValidator.isLicenseValid = function () {
var licenseDetails = GumroadLicenseValidator.getLicenseDetails();
if (licenseDetails) {
var gumroadResponse = JSON.parse(licenseDetails);
if (gumroadResponse.success === true
&& gumroadResponse.refunded === false
&& gumroadResponse.disputed === false
&& gumroadResponse.chargebacked === false) {
if (GumroadLicenseValidator.INCREMENT_USES_COUNT && gumroadResponse.uses > GumroadLicenseValidator.MAX_NUMBER_OF_USES) {
return false;
}
if (GumroadLicenseValidator.IS_SUBSCRIPTION
&& gumroadResponse.subscription_cancelled_at !== null
&& gumroadResponse.subscription_failed_at !== null) {
return false;
}
return true;
}
else {
return false;
}
}
return false;
};
GumroadLicenseValidator.validateLicense = function (licenseKey) {
var response = UrlFetchApp.fetch("" + this.URL, {
method: 'post',
payload: {
product_permalink: this.PRODUCT_PERMALINK,
license_key: licenseKey
},
muteHttpExceptions: true
});
var license = response.getContentText();
GumroadLicenseValidator.setLicenseInProps(license);
return license;
};
GumroadLicenseValidator.setLicenseInProps = function (license) {
var properties = { 'gumroadLicense': license };
GumroadLicenseValidator.PROPERTY_STORE.setProperties(properties);
};
GumroadLicenseValidator.getLicenseDetails = function () {
return GumroadLicenseValidator.PROPERTY_STORE.getProperty('gumroadLicense');
};
GumroadLicenseValidator.deleteLicenseFromProps = function () {
GumroadLicenseValidator.PROPERTY_STORE.deleteProperty('gumroadLicense');
};
GumroadLicenseValidator.showLicenseKeyPrompt = function () {
var ui = GumroadLicenseValidator.DOCUMENT_CONTAINER.getUi(); // Same variations.
var result = ui.prompt('Activate License', 'Please enter your the license from your Gumroad transaction:', ui.ButtonSet.OK_CANCEL);
// Process the user's response.
var button = result.getSelectedButton();
var licenseKey = result.getResponseText();
if (button == ui.Button.OK) {
//Here we will validate
var licenseResponse = GumroadLicenseValidator.validateLicense(licenseKey);
// User clicked "OK".
ui.alert('Your license details are: ' + licenseResponse + '.');
}
else if (button == ui.Button.CANCEL) {
// User clicked "Cancel".
ui.alert('Certain features of this product won\'t work until you activate your license.');
}
else if (button == ui.Button.CLOSE) {
// User clicked X in the title bar.
ui.alert('Certain features of this product won\'t work until you activate your license.');
}
};
GumroadLicenseValidator.showProps = function () {
var ui = GumroadLicenseValidator.DOCUMENT_CONTAINER.getUi(); // Same variations.
var license = GumroadLicenseValidator.getLicenseDetails();
ui.alert(license || 'Props are empty');
};
GumroadLicenseValidator.showBuyModal = function () {
var ui = GumroadLicenseValidator.DOCUMENT_CONTAINER.getUi(); // Same variations.
var htmlOutput = HtmlService
.createHtmlOutputFromFile('index')
.setWidth(450)
.setHeight(700);
ui.showModalDialog(htmlOutput, 'Buy Now');
};
GumroadLicenseValidator.URL = 'https://api.gumroad.com/v2/licenses/verify';
/**
* CONFIGURE THESE VALUES
* **/
// private static PRODUCT_PERMALINK: string = 'yyTnc';
GumroadLicenseValidator.PRODUCT_PERMALINK = 'AEFYq';
GumroadLicenseValidator.DOCUMENT_CONTAINER = SpreadsheetApp;
GumroadLicenseValidator.PROPERTY_STORE = PropertiesService.getScriptProperties();
GumroadLicenseValidator.INCREMENT_USES_COUNT = true;
GumroadLicenseValidator.MAX_NUMBER_OF_USES = 1;
GumroadLicenseValidator.IS_SUBSCRIPTION = false;
return GumroadLicenseValidator;
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment