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

163 lines
4.3 KiB
JavaScript
Raw Normal View History

2015-05-28 05:49:46 -07:00
(function () {
var updatedProducts = [];
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
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) {
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) {
store.refresh();
}
2015-05-28 05:49:46 -07:00
function validateProduct(product, callback) {
// product attributes:
// https://github.com/j3k0/cordova-plugin-purchase/blob/master/doc/api.md#validation-error-codes
callback(true, {
});
//callback(true, { ... transaction details ... }); // success!
//// OR
//callback(false, {
// error: {
// code: store.PURCHASE_EXPIRED,
// message: "XYZ"
// }
//});
//// OR
//callback(false, "Impossible to proceed with validation");
}
2015-10-01 23:14:04 -07:00
function initProduct(id, alias, type) {
2015-05-28 05:49:46 -07:00
store.register({
2015-10-01 23:14:04 -07:00
id: id,
alias: alias,
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.
2015-10-01 23:14:04 -07:00
store.when(id).approved(function (order) {
2015-05-28 05:49:46 -07:00
order.finish();
});
2015-10-01 23:14:04 -07:00
store.when(id).verified(function (p) {
2015-06-03 21:50:10 -07:00
p.finish();
});
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-06-03 21:50:10 -07:00
product.finish();
}
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;
initProduct(getStoreFeatureId(""), "premium features", store.NON_CONSUMABLE);
initProduct(getStoreFeatureId("embypremieremonthly"), "emby premiere monthly", 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) {
o.buttonText = Globalize.translate(o.buttonText, o.price);
return o;
});
deferred.resolveWith(null, [options]);
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,
getStoreFeatureId: getStoreFeatureId,
getSubscriptionOptions: getSubscriptionOptions
2015-05-28 05:49:46 -07:00
};
initializeStore();
})();