(function () { function getRegistrationInfo(feature) { return ConnectionManager.getRegistrationInfo(feature, ApiClient); } function validateFeature(feature, deferred) { var unlockableProduct = IapManager.getProductInfo(feature) || {}; if (unlockableProduct.owned) { deferred.resolve(); return; } var unlockableProductInfo = IapManager.isPurchaseAvailable(feature) ? { enableAppUnlock: true, id: unlockableProduct.id, price: unlockableProduct.price, feature: feature } : null; var prefix = $.browser.android ? 'android' : 'ios'; IapManager.isUnlockedOverride(feature).done(function (isUnlocked) { if (isUnlocked) { deferred.resolve(); return; } // Get supporter status getRegistrationInfo(prefix + 'appunlock').done(function (registrationInfo) { if (registrationInfo.IsRegistered) { deferred.resolve(); return; } IapManager.getSubscriptionOptions().done(function (subscriptionOptions) { if (subscriptionOptions.filter(function (p) { return p.owned; }).length > 0) { deferred.resolve(); return; } var dialogOptions = { title: Globalize.translate('HeaderUnlockApp') }; showInAppPurchaseInfo(subscriptionOptions, unlockableProductInfo, registrationInfo, dialogOptions, deferred); }); }).fail(function () { deferred.reject(); }); }); } function cancelInAppPurchase() { var elem = document.querySelector('.inAppPurchaseOverlay'); if (elem) { PaperDialogHelper.close(elem); } } var isCancelled = true; var currentDisplayingProductInfos = []; var currentDisplayingDeferred = null; function clearCurrentDisplayingInfo() { currentDisplayingProductInfos = []; currentDisplayingDeferred = null; } function showInAppPurchaseElement(subscriptionOptions, unlockableProductInfo, dialogOptions, deferred) { cancelInAppPurchase(); // clone currentDisplayingProductInfos = subscriptionOptions.slice(0); if (unlockableProductInfo) { currentDisplayingProductInfos.push(unlockableProductInfo); } var dlg = PaperDialogHelper.createDialog(); var html = ''; html += '

'; html += ''; html += '
' + dialogOptions.title + '
'; html += '

'; html += '
'; html += '
'; html += '

'; if (unlockableProductInfo) { html += Globalize.translate('MessageUnlockAppWithPurchaseOrSupporter'); } else { html += Globalize.translate('MessageUnlockAppWithSupporter'); } html += '

'; html += '

'; html += Globalize.translate('MessageToValidateSupporter'); html += '

'; if (unlockableProductInfo) { var unlockText = Globalize.translate('ButtonUnlockWithPurchase'); if (unlockableProductInfo.price) { unlockText = Globalize.translate('ButtonUnlockPrice', unlockableProductInfo.price); } html += '

'; html += '' + unlockText + ''; html += '

'; } for (var i = 0, length = subscriptionOptions.length; i < length; i++) { html += '

'; html += ''; html += subscriptionOptions[i].buttonText; html += ''; html += '

'; } if (IapManager.restorePurchase) { html += '

'; html += '' + Globalize.translate('ButtonRestorePreviousPurchase') + ''; html += '

'; } html += '
'; html += '
'; dlg.innerHTML = html; document.body.appendChild(dlg); initInAppPurchaseElementEvents(dlg, deferred); PaperDialogHelper.openWithHash(dlg, 'iap'); $('.btnCloseDialog', dlg).on('click', function () { PaperDialogHelper.close(dlg); }); $(dlg).on('iron-overlay-closed', function () { if (window.TabBar) { TabBar.show(); } }); dlg.classList.add('inAppPurchaseOverlay'); } function initInAppPurchaseElementEvents(elem, deferred) { isCancelled = true; $('.btnPurchase', elem).on('click', function () { isCancelled = false; if (this.getAttribute('data-email') == 'true') { promptForEmail(this.getAttribute('data-feature')); } else { IapManager.beginPurchase(this.getAttribute('data-feature')); } }); $('.btnRestorePurchase', elem).on('click', function () { isCancelled = false; IapManager.restorePurchase(); }); $(elem).on('iron-overlay-closed', function () { clearCurrentDisplayingInfo(); if (isCancelled) { deferred.reject(); } $(this).remove(); }); } function showInAppPurchaseInfo(subscriptionOptions, unlockableProductInfo, serverRegistrationInfo, dialogOptions, deferred) { require(['components/paperdialoghelper'], function () { if (window.TabBar) { TabBar.hide(); } showInAppPurchaseElement(subscriptionOptions, unlockableProductInfo, dialogOptions, deferred); currentDisplayingDeferred = deferred; }); } function promptForEmail(feature) { require(['prompt'], function (prompt) { prompt({ text: Globalize.translate('TextPleaseEnterYourEmailAddressForSubscription'), title: Globalize.translate('HeaderEmailAddress'), callback: function (email) { if (email) { IapManager.beginPurchase(feature, email); } } }); }); } function onProductUpdated(e, product) { if (product.owned) { var deferred = currentDisplayingDeferred; if (deferred && currentDisplayingProductInfos.filter(function (p) { return product.id == p.id; }).length) { isCancelled = false; cancelInAppPurchase(); deferred.resolve(); } } } function validateSync(deferred) { Dashboard.getPluginSecurityInfo().done(function (pluginSecurityInfo) { if (pluginSecurityInfo.IsMBSupporter) { deferred.resolve(); return; } // Get supporter status getRegistrationInfo('Sync').done(function (registrationInfo) { if (registrationInfo.IsRegistered) { deferred.resolve(); return; } IapManager.getSubscriptionOptions().done(function (subscriptionOptions) { var dialogOptions = { title: Globalize.translate('HeaderUnlockSync') }; showInAppPurchaseInfo(subscriptionOptions, null, registrationInfo, dialogOptions, deferred); }); }).fail(function () { deferred.reject(); }); }); } window.RegistrationServices = { renderPluginInfo: function (page, pkg, pluginSecurityInfo) { }, validateFeature: function (name) { var deferred = DeferredBuilder.Deferred(); if (name == 'playback') { validateFeature(name, deferred); } else if (name == 'livetv') { validateFeature(name, deferred); } else if (name == 'sync') { validateSync(deferred); } else { deferred.resolve(); } return deferred.promise(); } }; function onIapManagerLoaded() { Events.on(IapManager, 'productupdated', onProductUpdated); } if ($.browser.android) { requirejs(['cordova/android/iap'], onIapManagerLoaded); } else { requirejs(['cordova/iap'], onIapManagerLoaded); } })();