jellyfin-web/dashboard-ui/cordova/iap.js

295 lines
7.9 KiB
JavaScript
Raw Normal View History

2015-05-28 05:49:46 -07:00
(function () {
var updatedProducts = [];
2015-10-10 17:39:30 -07:00
var enteredEmail;
2015-05-28 05:49:46 -07:00
2015-10-01 23:14:04 -07:00
function getStoreFeatureId(feature) {
if (feature == 'embypremieremonthly') {
return 'emby.subscription.monthly';
}
return 'appunlock';
}
2015-06-03 21:50:10 -07:00
function updateProductInfo(product) {
2015-05-28 05:49:46 -07:00
2015-11-02 10:25:01 -07:00
//if (product.id == 'appunlock') {
// product.owned = false;
//}
2015-10-14 15:35:32 -07:00
2015-05-28 05:49:46 -07:00
updatedProducts = updatedProducts.filter(function (r) {
2015-06-03 21:50:10 -07:00
return r.id != product.id;
2015-05-28 05:49:46 -07:00
});
2015-06-03 21:50:10 -07:00
updatedProducts.push(product);
Events.trigger(IapManager, 'productupdated', [product]);
2015-05-28 05:49:46 -07:00
}
2015-10-01 23:14:04 -07:00
function getProduct(feature) {
2015-05-28 05:49:46 -07:00
2015-10-01 23:14:04 -07:00
var id = getStoreFeatureId(feature);
2015-05-28 05:49:46 -07:00
var products = updatedProducts.filter(function (r) {
return r.id == id;
});
return products.length ? products[0] : null;
}
2015-10-01 23:14:04 -07:00
function isPurchaseAvailable(feature) {
var product = getProduct(feature);
2015-05-28 05:49:46 -07:00
2015-06-03 21:50:10 -07:00
return product != null && product.valid /*&& product.canPurchase*/;
2015-05-28 05:49:46 -07:00
}
2015-10-01 23:14:04 -07:00
function beginPurchase(feature, email) {
2015-10-10 17:39:30 -07:00
if (email) {
enteredEmail = email;
}
2015-11-02 10:25:01 -07:00
validationCache = {};
2015-10-01 23:14:04 -07:00
var id = getStoreFeatureId(feature);
2015-06-03 21:50:10 -07:00
store.order(id);
2015-05-28 05:49:46 -07:00
}
2015-09-08 13:40:48 -07:00
function restorePurchase(id) {
2015-11-02 10:25:01 -07:00
validationCache = {};
2015-09-08 13:40:48 -07:00
store.refresh();
}
2015-11-02 10:25:01 -07:00
var validationCache = {};
2015-05-28 05:49:46 -07:00
function validateProduct(product, callback) {
2015-11-02 10:25:01 -07:00
var productId = product.id;
2015-11-15 19:33:32 -07: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 10:25:01 -07: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 05:49:46 -07:00
// product attributes:
// https://github.com/j3k0/cordova-plugin-purchase/blob/master/doc/api.md#validation-error-codes
2015-10-10 17:39:30 -07:00
var receipt = product.transaction.appStoreReceipt;
var price = product.price;
2015-10-24 08:33:22 -07:00
var postData = {
store: "Apple",
application: "com.emby.mobile",
product: productId,
type: "Subscription",
storeToken: receipt,
2015-10-27 10:26:04 -07:00
amt: price
2015-10-24 08:33:22 -07:00
};
2015-10-29 16:23:43 -07:00
var promise;
2015-10-24 08:33:22 -07:00
if (enteredEmail) {
postData.email = enteredEmail;
2015-10-27 10:26:04 -07:00
postData.storeId = enteredEmail;
2015-10-28 12:40:38 -07:00
postData.feature = "MBSClubMonthly";
2015-10-23 09:04:33 -07:00
2015-10-29 16:23:43 -07:00
promise = ApiClient.ajax({
type: "POST",
url: ApiClient.getUrl("Appstore/Register"),
data: {
Parameters: JSON.stringify(postData)
}
});
2015-10-22 08:03:08 -07:00
2015-10-29 16:23:43 -07:00
} else {
2015-11-28 01:07:44 -07:00
promise = fetch("http://mb3admin.com/admin/service/appstore/register", {
method: 'POST',
body: JSON.stringify(postData),
2015-10-30 09:58:36 -07:00
headers: {
2015-11-28 01:07:44 -07:00
"X-Emby-Token": "EMBY-APPLE-VALIDATE",
"Content-Type": "application/json"
2015-10-30 09:58:36 -07:00
}
2015-10-29 16:23:43 -07:00
});
}
2015-11-28 01:07:44 -07:00
promise.then(function () {
2015-05-28 05:49:46 -07:00
2015-11-02 10:25:01 -07:00
setCachedResult(cacheKey, true);
2015-10-10 17:39:30 -07:00
callback(true, product);
2015-05-28 05:49:46 -07:00
2015-11-28 01:07:44 -07:00
}, function (e) {
2015-05-28 05:49:46 -07:00
2015-10-28 12:40:38 -07:00
if (e.status == 402) {
2015-10-28 13:56:25 -07:00
2015-11-02 10:25:01 -07:00
setCachedResult(cacheKey, false, store.PURCHASE_EXPIRED, 'Subscription Expired');
2015-10-28 12:40:38 -07:00
callback(false, {
code: store.PURCHASE_EXPIRED,
error: {
message: "Subscription Expired"
}
});
2015-10-29 16:23:43 -07:00
2015-10-28 12:40:38 -07:00
} else {
2015-11-02 10:25:01 -07:00
//alert('validate fail - other ' + e.status);
validationCache = {};
2015-10-28 13:56:25 -07:00
2015-10-28 12:40:38 -07:00
callback(false, {
code: store.CONNECTION_FAILED,
error: {
message: "Connection Failure"
}
});
}
2015-10-10 17:39:30 -07:00
});
2015-05-28 05:49:46 -07:00
}
2015-11-02 10:25:01 -07:00
function setCachedResult(key, result, code, message) {
validationCache[key] = {
date: new Date().getTime(),
result: result,
errorCode: code,
errorMessage: message
};
}
2015-10-10 17:39:30 -07:00
function initProduct(id, requiresVerification, type) {
2015-05-28 05:49:46 -07:00
store.register({
2015-10-01 23:14:04 -07:00
id: id,
2015-10-10 17:39:30 -07:00
alias: id,
2015-10-01 23:14:04 -07:00
type: type
2015-05-28 05:49:46 -07: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 17:39:30 -07:00
//product.finish();
if (requiresVerification) {
product.verify();
} else {
product.finish();
}
2015-05-28 05:49:46 -07:00
});
2015-10-23 09:04:33 -07:00
if (requiresVerification) {
store.when(id).verified(function (p) {
2015-10-29 12:01:04 -07:00
//alert('verified');
2015-10-24 08:33:22 -07:00
updateProductInfo(p);
2015-10-23 09:04:33 -07:00
p.finish();
});
}
2015-06-03 21:50:10 -07:00
2015-05-28 05:49:46 -07:00
// The play button can only be accessed when the user
// owns the full version.
2015-10-01 23:14:04 -07:00
store.when(id).updated(function (product) {
2015-05-28 05:49:46 -07:00
2015-06-03 21:50:10 -07:00
if (product.loaded && product.valid && product.state == store.APPROVED) {
2015-06-26 20:27:38 -07:00
Logger.log('finishing previously created transaction');
2015-10-10 17:39:30 -07:00
if (requiresVerification) {
2015-10-28 12:40:38 -07:00
//product.verify();
2015-10-29 12:01:04 -07:00
if (product.owned) {
2015-10-29 16:23:43 -07:00
//alert('sub owned!');
2015-10-29 12:01:04 -07:00
}
2015-10-10 17:39:30 -07:00
} else {
product.finish();
}
2015-06-03 21:50:10 -07:00
}
2015-05-28 05:49:46 -07:00
updateProductInfo(product);
});
2015-10-01 23:14:04 -07: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 17:39:30 -07:00
initProduct(getStoreFeatureId(""), false, store.NON_CONSUMABLE);
initProduct(getStoreFeatureId("embypremieremonthly"), true, store.PAID_SUBSCRIPTION);
2015-05-28 05:49:46 -07: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 20:27:38 -07:00
Logger.log("Store ready");
2015-05-28 05:49:46 -07: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-01 23:14:04 -07: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 12:01:04 -07:00
o.id = getStoreFeatureId(o.feature);
2015-10-02 10:55:26 -07:00
o.buttonText = Globalize.translate(o.buttonText, getProduct(o.feature).price);
2015-11-02 10:25:01 -07:00
o.owned = getProduct(o.feature).owned;
2015-10-01 23:14:04 -07:00
return o;
});
deferred.resolveWith(null, [options]);
return deferred.promise();
}
2015-11-06 08:02:22 -07:00
function isUnlockedOverride(feature) {
2015-11-15 19:33:32 -07:00
2015-11-06 08:02:22 -07:00
var deferred = DeferredBuilder.Deferred();
deferred.resolveWith(null, [false]);
return deferred.promise();
}
2015-05-28 05:49:46 -07:00
window.IapManager = {
isPurchaseAvailable: isPurchaseAvailable,
2015-06-03 21:50:10 -07:00
getProductInfo: getProduct,
2015-09-08 13:40:48 -07:00
beginPurchase: beginPurchase,
2015-10-01 23:14:04 -07:00
restorePurchase: restorePurchase,
2015-11-06 08:02:22 -07:00
getSubscriptionOptions: getSubscriptionOptions,
isUnlockedOverride: isUnlockedOverride
2015-05-28 05:49:46 -07:00
};
initializeStore();
})();