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

203 lines
5.4 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-10-14 15:35:32 -07:00
if (product.id == 'appunlock') {
product.owned = false;
}
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-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) {
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
2015-10-10 17:39:30 -07:00
var productId = product.id;
var transactionId = product.transaction.id;
var receipt = product.transaction.appStoreReceipt;
var price = product.price;
2015-10-23 09:04:33 -07:00
var url = ApiClient.getUrl("Appstore/Register");
2015-10-22 08:03:08 -07:00
ApiClient.ajax({
2015-10-10 17:39:30 -07:00
type: "POST",
2015-10-22 08:03:08 -07:00
url: ApiClient.getUrl("Appstore/Register"),
2015-10-10 17:39:30 -07:00
data: {
2015-10-22 08:03:08 -07:00
Parameters: JSON.stringify({
store: "Apple",
application: "com.emby.mobile",
product: productId,
type: "Subscription",
feature: "MBSClubMonthly",
email: enteredEmail,
storeToken: receipt,
amt: price,
storeId: transactionId
})
2015-10-10 17:39:30 -07:00
}
}).done(function () {
2015-05-28 05:49:46 -07:00
2015-10-23 09:04:33 -07:00
alert('validate ok');
2015-10-10 17:39:30 -07:00
callback(true, product);
2015-05-28 05:49:46 -07:00
2015-10-23 09:04:33 -07:00
}).fail(function (e) {
2015-05-28 05:49:46 -07:00
2015-10-23 09:04:33 -07:00
alert('validate fail: ' + e.status + ' ' + url);
alert(JSON.stringify(e));
2015-10-10 17:39:30 -07:00
callback(false, product);
});
2015-05-28 05:49:46 -07:00
}
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) {
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-23 09:04:33 -07:00
//product.verify();
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-02 10:55:26 -07:00
o.buttonText = Globalize.translate(o.buttonText, getProduct(o.feature).price);
2015-10-01 23:14:04 -07:00
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,
getSubscriptionOptions: getSubscriptionOptions
2015-05-28 05:49:46 -07:00
};
initializeStore();
})();