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/iap.js

295 lines
7.9 KiB
JavaScript
Raw Normal View History

2015-05-28 08:49:46 -04:00
(function () {
var updatedProducts = [];
2015-10-10 20:39:30 -04:00
var enteredEmail;
2015-05-28 08:49:46 -04:00
2015-10-02 02:14:04 -04:00
function getStoreFeatureId(feature) {
if (feature == 'embypremieremonthly') {
return 'emby.subscription.monthly';
}
return 'appunlock';
}
2015-06-04 00:50:10 -04:00
function updateProductInfo(product) {
2015-05-28 08:49:46 -04:00
2015-11-02 12:25:01 -05:00
//if (product.id == 'appunlock') {
// product.owned = false;
//}
2015-10-14 18:35:32 -04:00
2015-05-28 08:49:46 -04:00
updatedProducts = updatedProducts.filter(function (r) {
2015-06-04 00:50:10 -04:00
return r.id != product.id;
2015-05-28 08:49:46 -04:00
});
2015-06-04 00:50:10 -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) {
2015-05-28 08:49:46 -04:00
2015-10-02 02:14:04 -04:00
var id = getStoreFeatureId(feature);
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-02 02:14:04 -04:00
function isPurchaseAvailable(feature) {
var product = getProduct(feature);
2015-05-28 08:49:46 -04:00
2015-06-04 00:50:10 -04:00
return product != null && product.valid /*&& product.canPurchase*/;
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 (email) {
enteredEmail = email;
}
2015-11-02 12:25:01 -05:00
validationCache = {};
2015-10-02 02:14:04 -04:00
var id = getStoreFeatureId(feature);
2015-06-04 00:50:10 -04:00
store.order(id);
2015-05-28 08:49:46 -04:00
}
2015-09-08 16:40:48 -04:00
function restorePurchase(id) {
2015-11-02 12:25:01 -05:00
validationCache = {};
2015-09-08 16:40:48 -04:00
store.refresh();
}
2015-11-02 12:25:01 -05:00
var validationCache = {};
2015-05-28 08:49:46 -04:00
function validateProduct(product, callback) {
2015-11-02 12:25:01 -05:00
var productId = product.id;
2015-11-15 21:33:32 -05:00
// We should never get in here with the unlock, but in case we do
if ((productId || '').toLowerCase().indexOf('appunlock') != -1) {
callback(true, product);
return;
}
2015-11-02 12:25:01 -05:00
var cacheKey = productId + (product.transaction.id || '');
var cachedResult = validationCache[cacheKey];
if (cachedResult && (new Date().getTime() - cachedResult.date) < 60000) {
if (cachedResult.result) {
callback(true, product);
} else {
callback(false, {
code: cachedResult.errorCode,
error: {
message: cachedResult.errorMessage
}
});
}
return;
}
2015-05-28 08:49:46 -04:00
// product attributes:
// https://github.com/j3k0/cordova-plugin-purchase/blob/master/doc/api.md#validation-error-codes
2015-10-10 20:39:30 -04:00
var receipt = product.transaction.appStoreReceipt;
var price = product.price;
2015-10-24 11:33:22 -04:00
var postData = {
store: "Apple",
application: "com.emby.mobile",
product: productId,
type: "Subscription",
storeToken: receipt,
2015-10-27 13:26:04 -04:00
amt: price
2015-10-24 11:33:22 -04:00
};
2015-10-29 19:23:43 -04:00
var promise;
2015-10-24 11:33:22 -04:00
if (enteredEmail) {
postData.email = enteredEmail;
2015-10-27 13:26:04 -04:00
postData.storeId = enteredEmail;
2015-10-28 15:40:38 -04:00
postData.feature = "MBSClubMonthly";
2015-10-23 12:04:33 -04:00
2015-10-29 19:23:43 -04:00
promise = ApiClient.ajax({
type: "POST",
url: ApiClient.getUrl("Appstore/Register"),
data: {
Parameters: JSON.stringify(postData)
}
});
2015-10-22 11:03:08 -04:00
2015-10-29 19:23:43 -04:00
} else {
2015-11-28 03:07:44 -05:00
promise = fetch("http://mb3admin.com/admin/service/appstore/register", {
method: 'POST',
body: JSON.stringify(postData),
2015-10-30 12:58:36 -04:00
headers: {
2015-11-28 03:07:44 -05:00
"X-Emby-Token": "EMBY-APPLE-VALIDATE",
"Content-Type": "application/json"
2015-10-30 12:58:36 -04:00
}
2015-10-29 19:23:43 -04:00
});
}
2015-11-28 03:07:44 -05:00
promise.then(function () {
2015-05-28 08:49:46 -04:00
2015-11-02 12:25:01 -05:00
setCachedResult(cacheKey, true);
2015-10-10 20:39:30 -04:00
callback(true, product);
2015-05-28 08:49:46 -04:00
2015-11-28 03:07:44 -05:00
}, function (e) {
2015-05-28 08:49:46 -04:00
2015-10-28 15:40:38 -04:00
if (e.status == 402) {
2015-10-28 16:56:25 -04:00
2015-11-02 12:25:01 -05:00
setCachedResult(cacheKey, false, store.PURCHASE_EXPIRED, 'Subscription Expired');
2015-10-28 15:40:38 -04:00
callback(false, {
code: store.PURCHASE_EXPIRED,
error: {
message: "Subscription Expired"
}
});
2015-10-29 19:23:43 -04:00
2015-10-28 15:40:38 -04:00
} else {
2015-11-02 12:25:01 -05:00
//alert('validate fail - other ' + e.status);
validationCache = {};
2015-10-28 16:56:25 -04:00
2015-10-28 15:40:38 -04:00
callback(false, {
code: store.CONNECTION_FAILED,
error: {
message: "Connection Failure"
}
});
}
2015-10-10 20:39:30 -04:00
});
2015-05-28 08:49:46 -04:00
}
2015-11-02 12:25:01 -05:00
function setCachedResult(key, result, code, message) {
validationCache[key] = {
date: new Date().getTime(),
result: result,
errorCode: code,
errorMessage: message
};
}
2015-10-10 20:39:30 -04:00
function initProduct(id, requiresVerification, type) {
2015-05-28 08:49:46 -04:00
store.register({
2015-10-02 02:14:04 -04:00
id: id,
2015-10-10 20:39:30 -04:00
alias: id,
2015-10-02 02:14:04 -04:00
type: type
2015-05-28 08:49:46 -04:00
});
// When purchase of the full version is approved,
// show some logs and finish the transaction.
store.when(id).approved(function (product) {
2015-10-10 20:39:30 -04:00
//product.finish();
if (requiresVerification) {
product.verify();
} else {
product.finish();
}
2015-05-28 08:49:46 -04:00
});
2015-10-23 12:04:33 -04:00
if (requiresVerification) {
store.when(id).verified(function (p) {
2015-10-29 15:01:04 -04:00
//alert('verified');
2015-10-24 11:33:22 -04:00
updateProductInfo(p);
2015-10-23 12:04:33 -04:00
p.finish();
});
}
2015-06-04 00:50:10 -04:00
2015-05-28 08:49:46 -04:00
// The play button can only be accessed when the user
// owns the full version.
2015-10-02 02:14:04 -04:00
store.when(id).updated(function (product) {
2015-05-28 08:49:46 -04:00
2015-06-04 00:50:10 -04:00
if (product.loaded && product.valid && product.state == store.APPROVED) {
2015-06-26 23:27:38 -04:00
Logger.log('finishing previously created transaction');
2015-10-10 20:39:30 -04:00
if (requiresVerification) {
2015-10-28 15:40:38 -04:00
//product.verify();
2015-10-29 15:01:04 -04:00
if (product.owned) {
2015-10-29 19:23:43 -04:00
//alert('sub owned!');
2015-10-29 15:01:04 -04:00
}
2015-10-10 20:39:30 -04:00
} else {
product.finish();
}
2015-06-04 00:50:10 -04:00
}
2015-05-28 08:49:46 -04:00
updateProductInfo(product);
});
2015-10-02 02:14:04 -04:00
}
function initializeStore() {
// Let's set a pretty high verbosity level, so that we see a lot of stuff
// in the console (reassuring us that something is happening).
store.verbosity = store.INFO;
store.validator = validateProduct;
2015-10-10 20:39:30 -04:00
initProduct(getStoreFeatureId(""), false, store.NON_CONSUMABLE);
initProduct(getStoreFeatureId("embypremieremonthly"), true, store.PAID_SUBSCRIPTION);
2015-05-28 08:49:46 -04:00
// When every goes as expected, it's time to celebrate!
// The "ready" event should be welcomed with music and fireworks,
// go ask your boss about it! (just in case)
store.ready(function () {
2015-06-26 23:27:38 -04:00
Logger.log("Store ready");
2015-05-28 08:49:46 -04:00
});
// After we've done our setup, we tell the store to do
// it's first refresh. Nothing will happen if we do not call store.refresh()
store.refresh();
}
2015-10-02 02:14:04 -04:00
function getSubscriptionOptions() {
var deferred = DeferredBuilder.Deferred();
var options = [];
options.push({
feature: 'embypremieremonthly',
buttonText: 'EmbyPremiereMonthlyWithPrice'
});
options = options.filter(function (o) {
return getProduct(o.feature) != null;
}).map(function (o) {
2015-10-29 15:01:04 -04:00
o.id = getStoreFeatureId(o.feature);
2015-10-02 13:55:26 -04:00
o.buttonText = Globalize.translate(o.buttonText, getProduct(o.feature).price);
2015-11-02 12:25:01 -05:00
o.owned = getProduct(o.feature).owned;
2015-10-02 02:14:04 -04:00
return o;
});
deferred.resolveWith(null, [options]);
return deferred.promise();
}
2015-11-06 10:02:22 -05:00
function isUnlockedOverride(feature) {
2015-11-15 21:33:32 -05:00
2015-11-06 10:02:22 -05:00
var deferred = DeferredBuilder.Deferred();
deferred.resolveWith(null, [false]);
return deferred.promise();
}
2015-05-28 08:49:46 -04:00
window.IapManager = {
isPurchaseAvailable: isPurchaseAvailable,
2015-06-04 00:50:10 -04:00
getProductInfo: getProduct,
2015-09-08 16:40:48 -04:00
beginPurchase: beginPurchase,
2015-10-02 02:14:04 -04:00
restorePurchase: restorePurchase,
2015-11-06 10:02:22 -05:00
getSubscriptionOptions: getSubscriptionOptions,
isUnlockedOverride: isUnlockedOverride
2015-05-28 08:49:46 -04:00
};
initializeStore();
})();