1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00
jellyfin-web/dashboard-ui/cordova/android/iap.js

193 lines
4.7 KiB
JavaScript
Raw Normal View History

2015-05-28 08:49:46 -04:00
(function () {
var updatedProducts = [];
2015-06-04 00:50:10 -04:00
function updateProductInfo(id, owned, price) {
2015-05-28 08:49:46 -04:00
updatedProducts = updatedProducts.filter(function (r) {
return r.id != id;
});
2015-06-01 10:49:23 -04:00
var product = {
2015-05-28 08:49:46 -04:00
id: id,
2015-06-04 00:50:10 -04:00
owned: owned,
price: price
2015-06-01 10:49:23 -04:00
};
updatedProducts.push(product);
Events.trigger(IapManager, 'productupdated', [product]);
2015-05-28 08:49:46 -04:00
}
2015-10-02 02:14:04 -04:00
function getProduct(feature) {
var id;
if (feature == 'embypremieremonthly') {
2015-11-10 13:58:05 -05:00
id = NativeIapManager.getPremiereMonthlySku();
} else {
2015-11-10 13:58:05 -05:00
id = NativeIapManager.getUnlockProductSku();
}
2015-10-02 02:14:04 -04:00
2015-05-28 08:49:46 -04:00
var products = updatedProducts.filter(function (r) {
return r.id == id;
});
return products.length ? products[0] : null;
}
2015-10-10 20:39:30 -04:00
var storeReady = false;
function onStoreReady() {
storeReady = true;
refreshPurchases();
}
function isPurchaseAvailable() {
2015-05-28 08:49:46 -04:00
2015-10-10 20:39:30 -04:00
return storeReady;
2015-05-28 08:49:46 -04:00
}
2015-10-02 02:14:04 -04:00
function beginPurchase(feature, email) {
2015-10-10 20:39:30 -04:00
if (feature == 'embypremieremonthly') {
return MainActivity.purchasePremiereMonthly(email);
}
2015-11-13 23:19:05 -05:00
return MainActivity.purchaseUnlock();
2015-05-28 08:49:46 -04:00
}
2015-05-31 15:12:58 -04:00
function onPurchaseComplete(result) {
2015-06-01 10:49:23 -04:00
2015-11-22 00:15:00 -05:00
if (result === true) {
2015-06-01 10:49:23 -04:00
refreshPurchases();
}
2015-11-22 00:15:00 -05:00
else if (result) {
ApiClient.ajax({
type: "POST",
url: ApiClient.getUrl("Appstore/Register"),
data: {
Parameters: JSON.stringify(result)
}
2015-11-28 03:07:44 -05:00
}).then(function () {
2015-11-22 00:15:00 -05:00
refreshPurchases();
2015-11-28 03:07:44 -05:00
}, function (e) {
2015-11-22 00:15:00 -05:00
refreshPurchases();
});
}
2015-06-01 10:49:23 -04:00
}
function refreshPurchases() {
NativeIapManager.getPurchaseInfos("window.IapManager.updateProduct");
2015-10-02 02:14:04 -04:00
}
function getSubscriptionOptions() {
var deferred = DeferredBuilder.Deferred();
var options = [];
options.push({
feature: 'embypremieremonthly',
2015-10-08 12:22:14 -04:00
buttonText: 'EmbyPremiereMonthly'
2015-10-02 02:14:04 -04:00
});
options = options.filter(function (o) {
return getProduct(o.feature) != null;
}).map(function (o) {
2015-11-10 13:58:05 -05:00
var prod = getProduct(o.feature);
o.buttonText = Globalize.translate(o.buttonText, prod.price);
o.owned = prod.owned;
2015-10-02 02:14:04 -04:00
return o;
});
deferred.resolveWith(null, [options]);
return deferred.promise();
2015-05-31 15:12:58 -04:00
}
2015-11-06 10:02:22 -05:00
function isUnlockedOverride(feature) {
var deferred = DeferredBuilder.Deferred();
2015-11-13 23:19:05 -05:00
if (feature == 'playback' || feature == 'livetv') {
2015-11-23 11:12:06 -05:00
isPlaybackUnlockedViaOldApp(deferred);
2015-11-06 10:02:22 -05:00
} else {
deferred.resolveWith(null, [false]);
}
return deferred.promise();
}
function isPlaybackUnlockedViaOldApp(deferred) {
2015-11-28 03:07:44 -05:00
testDeviceId(ConnectionManager.deviceId()).then(function (isUnlocked) {
2015-11-06 10:02:22 -05:00
if (isUnlocked) {
deferred.resolveWith(null, [true]);
return;
}
2015-11-28 03:07:44 -05:00
testDeviceId(device.uuid).then(function (isUnlocked) {
2015-11-06 10:02:22 -05:00
if (isUnlocked) {
deferred.resolveWith(null, [true]);
return;
}
deferred.resolveWith(null, [false]);
});
});
}
function testDeviceId(deviceId) {
var cacheKey = 'oldapp-' + deviceId;
var cacheValue = appStorage.getItem(cacheKey);
if (cacheValue) {
2015-12-01 13:55:35 -05:00
return new Promise(function (resolve, reject) {
resolve(cacheValue == 'true');
});
2015-11-06 10:02:22 -05:00
} else {
2015-12-01 13:55:35 -05:00
return fetch('https://mb3admin.com/admin/service/statistics/appAccess?application=AndroidV1&deviceId=' + deviceId, {
method: 'GET'
2015-11-06 10:02:22 -05:00
2015-12-01 13:55:35 -05:00
}).then(function (response) {
2015-11-06 10:02:22 -05:00
2015-12-01 13:55:35 -05:00
if (response.status == 404) {
appStorage.setItem(cacheKey, 'false');
} else if (response.status < 400) {
appStorage.setItem(cacheKey, 'true');
return true;
}
return false;
2015-11-06 10:02:22 -05:00
2015-11-28 03:07:44 -05:00
}, function (e) {
2015-11-06 10:02:22 -05:00
2015-11-28 10:40:03 -05:00
return false;
2015-11-06 10:02:22 -05:00
});
}
}
2015-05-28 08:49:46 -04:00
window.IapManager = {
isPurchaseAvailable: isPurchaseAvailable,
2015-06-04 00:50:10 -04:00
getProductInfo: getProduct,
2015-05-28 08:49:46 -04:00
updateProduct: updateProductInfo,
2015-05-31 15:12:58 -04:00
beginPurchase: beginPurchase,
2015-10-02 02:14:04 -04:00
onPurchaseComplete: onPurchaseComplete,
2015-10-10 20:39:30 -04:00
getSubscriptionOptions: getSubscriptionOptions,
2015-11-06 10:02:22 -05:00
onStoreReady: onStoreReady,
isUnlockedOverride: isUnlockedOverride
2015-05-28 08:49:46 -04:00
};
2015-10-10 20:39:30 -04:00
NativeIapManager.initStore();
2015-05-28 08:49:46 -04:00
})();