(function () {
function isAndroid() {
var platform = (device.platform || '').toLowerCase();
return platform.indexOf('android') != -1;
}
function getPremiumUnlockFeatureId() {
if (isAndroid()) {
return "com.mb.android.unlock";
}
return 'premiumunlock';
}
function validatePlayback(deferred) {
// Don't require validation on android
if (isAndroid()) {
deferred.resolve();
return;
}
validateFeature({
id: getPremiumUnlockFeatureId()
}, deferred);
}
function validateLiveTV(deferred) {
validateFeature({
id: getPremiumUnlockFeatureId()
}, deferred);
}
function getRegistrationInfo(feature, enableSupporterUnlock) {
if (!enableSupporterUnlock) {
var deferred = $.Deferred();
deferred.resolveWith(null, [{}]);
return deferred.promise();
}
return ConnectionManager.getRegistrationInfo(feature, ApiClient);
}
function validateFeature(info, deferred) {
if (IapManager.hasPurchased(info.id)) {
deferred.resolve();
return;
}
var productInfo = {
enableSupporterUnlock: isAndroid(),
enableAppUnlock: IapManager.isPurchaseAvailable(info.id),
id: info.id
};
var prefix = isAndroid() ? 'android' : 'ios';
// Get supporter status
getRegistrationInfo(prefix + 'appunlock', productInfo.enableSupporterUnlock).done(function (registrationInfo) {
if (registrationInfo.IsRegistered) {
deferred.resolve();
return;
}
showInAppPurchaseInfo(productInfo, registrationInfo, deferred);
}).fail(function () {
deferred.reject();
});
}
function getInAppPurchaseElement(info) {
cancelInAppPurchase();
var html = '';
html += '
';
$(document.body).append(html);
return $('.inAppPurchaseOverlay');
}
function cancelInAppPurchase() {
$('.inAppPurchaseOverlay').remove();
}
var currentDisplayingProductInfo = null;
var currentDisplayingDeferred = null;
function clearCurrentDisplayingInfo() {
currentDisplayingProductInfo = null;
currentDisplayingDeferred = null;
}
function showInAppPurchaseInfo(info, serverRegistrationInfo, deferred) {
var elem = getInAppPurchaseElement(info);
currentDisplayingProductInfo = info;
currentDisplayingDeferred = deferred;
$('.inAppPurchaseForm', elem).on('submit', function () {
IapManager.beginPurchase(info.id);
return false;
});
$('.btnCancel', elem).on('click', function () {
clearCurrentDisplayingInfo();
cancelInAppPurchase();
// For testing purposes
if (!info.enableSupporterUnlock && !info.enableAppUnlock) {
deferred.resolve();
} else {
deferred.reject();
}
});
$('.btnSignInSupporter', elem).on('click', function () {
clearCurrentDisplayingInfo();
Dashboard.alert({
message: Globalize.translate('MessagePleaseSignInLocalNetwork'),
callback: function () {
cancelInAppPurchase();
Dashboard.logout();
}
});
});
}
function onProductUpdated(e, product) {
var currentInfo = currentDisplayingProductInfo;
var deferred = currentDisplayingDeferred;
if (currentInfo && deferred) {
if (product.owned && product.id == currentInfo.id) {
clearCurrentDisplayingInfo();
cancelInAppPurchase();
deferred.resolve();
}
}
}
window.RegistrationServices = {
renderPluginInfo: function (page, pkg, pluginSecurityInfo) {
},
addRecurringFields: function (page, period) {
},
initSupporterForm: function (page) {
$('.recurringSubscriptionCancellationHelp', page).html('');
},
validateFeature: function (name) {
var deferred = DeferredBuilder.Deferred();
if (name == 'playback') {
validatePlayback(deferred);
} else if (name == 'livetv') {
validateLiveTV(deferred);
} else {
deferred.resolve();
}
return deferred.promise();
}
};
function onIapManagerLoaded() {
Events.on(IapManager, 'productupdated', onProductUpdated);
}
if (isAndroid()) {
requirejs(['thirdparty/cordova/android/iap', onIapManagerLoaded]);
} else {
requirejs(['thirdparty/cordova/iap', onIapManagerLoaded]);
}
})();