UNPKG

nativescript-purchase

Version:
184 lines (180 loc) 7.91 kB
/*! ***************************************************************************** Copyright (c) 2019 Tangra Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ***************************************************************************** */ "use strict"; function __export(m) { for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; } Object.defineProperty(exports, "__esModule", { value: true }); var product_1 = require("nativescript-purchase/product"); var transaction_1 = require("nativescript-purchase/transaction"); var common = require("./purchase-common"); __export(require("./purchase-common")); var productRequest; var productIds; var productRequestDelegate; var paymentTransactionObserver; var storedDeveloperPayload; var refreshReceiptRequest; var refreshReceiptRequestDelegate; function init(productIdentifiers) { return new Promise(function (resolve, reject) { productIds = NSMutableSet.alloc().init(); paymentTransactionObserver = new SKPaymentTransactionObserverImpl(); productIdentifiers.forEach(function (value) { return productIds.addObject(value); }); SKPaymentQueue.defaultQueue().addTransactionObserver(paymentTransactionObserver); resolve(); }); } exports.init = init; function getProducts() { return new Promise(function (resolve, reject) { productRequest = SKProductsRequest.alloc().initWithProductIdentifiers(productIds); productRequestDelegate = SKProductRequestDelegateImpl.initWithResolveReject(resolve, reject); productRequest.delegate = productRequestDelegate; productRequest.start(); }); } exports.getProducts = getProducts; function buyProduct(product, developerPayload) { if (!product.nativeValue) { throw "Invalid Product! (missing native value)"; } storedDeveloperPayload = developerPayload; var payment = SKPayment.paymentWithProduct(product.nativeValue); SKPaymentQueue.defaultQueue().addPayment(payment); } exports.buyProduct = buyProduct; function consumePurchase(token) { return new Promise(function (resolve, reject) { resolve(0); }); } exports.consumePurchase = consumePurchase; function restorePurchases() { SKPaymentQueue.defaultQueue().restoreCompletedTransactions(); return Promise.resolve(); } exports.restorePurchases = restorePurchases; function canMakePayments() { return SKPaymentQueue.canMakePayments(); } exports.canMakePayments = canMakePayments; function getStoreReceipt() { var receiptData = NSData.dataWithContentsOfURL(NSBundle.mainBundle.appStoreReceiptURL); if (receiptData) { return receiptData.base64EncodedStringWithOptions(0); } else { return null; } } exports.getStoreReceipt = getStoreReceipt; function refreshStoreReceipt() { return new Promise(function (resolve, reject) { refreshReceiptRequest = SKReceiptRefreshRequest.alloc().init(); refreshReceiptRequestDelegate = SKReceiptRefreshRequestDelegateImpl.initWithResolveReject(resolve, reject); refreshReceiptRequest.delegate = refreshReceiptRequestDelegate; refreshReceiptRequest.start(); }); } exports.refreshStoreReceipt = refreshStoreReceipt; var SKReceiptRefreshRequestDelegateImpl = SKReceiptRefreshRequestDelegateImpl_1 = (function (_super) { __extends(SKReceiptRefreshRequestDelegateImpl, _super); function SKReceiptRefreshRequestDelegateImpl() { return _super !== null && _super.apply(this, arguments) || this; } SKReceiptRefreshRequestDelegateImpl.initWithResolveReject = function (resolve, reject) { var delegate = SKReceiptRefreshRequestDelegateImpl_1.new(); delegate._resolve = resolve; delegate._reject = reject; return delegate; }; SKReceiptRefreshRequestDelegateImpl.prototype.requestDidFinish = function (request) { this._resolve(); this._cleanup(); }; SKReceiptRefreshRequestDelegateImpl.prototype.requestDidFailWithError = function (request, error) { this._reject(new Error(error.localizedDescription)); this._cleanup(); }; SKReceiptRefreshRequestDelegateImpl.prototype._cleanup = function () { refreshReceiptRequestDelegate = null; refreshReceiptRequest = null; }; return SKReceiptRefreshRequestDelegateImpl; }(NSObject)); SKReceiptRefreshRequestDelegateImpl = SKReceiptRefreshRequestDelegateImpl_1 = __decorate([ ObjCClass(SKRequestDelegate) ], SKReceiptRefreshRequestDelegateImpl); var SKProductRequestDelegateImpl = SKProductRequestDelegateImpl_1 = (function (_super) { __extends(SKProductRequestDelegateImpl, _super); function SKProductRequestDelegateImpl() { return _super !== null && _super.apply(this, arguments) || this; } SKProductRequestDelegateImpl.initWithResolveReject = function (resolve, reject) { var delegate = SKProductRequestDelegateImpl_1.new(); delegate._resolve = resolve; delegate._reject = reject; return delegate; }; SKProductRequestDelegateImpl.prototype.productsRequestDidReceiveResponse = function (request, response) { var products = response.products; var result = []; for (var loop = 0; loop < products.count; loop++) { result.push(new product_1.Product(products.objectAtIndex(loop))); } this._resolve(result); this._cleanup(); }; SKProductRequestDelegateImpl.prototype.requestDidFailWithError = function (request, error) { this._reject(new Error(error.localizedDescription)); this._cleanup(); }; SKProductRequestDelegateImpl.prototype._cleanup = function () { productRequestDelegate = null; productRequest = null; }; return SKProductRequestDelegateImpl; }(NSObject)); SKProductRequestDelegateImpl = SKProductRequestDelegateImpl_1 = __decorate([ ObjCClass(SKProductsRequestDelegate) ], SKProductRequestDelegateImpl); var SKPaymentTransactionObserverImpl = (function (_super) { __extends(SKPaymentTransactionObserverImpl, _super); function SKPaymentTransactionObserverImpl() { return _super !== null && _super.apply(this, arguments) || this; } SKPaymentTransactionObserverImpl.prototype.paymentQueueUpdatedTransactions = function (queue, transactions) { for (var loop = 0; loop < transactions.count; loop++) { var transaction = transactions.objectAtIndex(loop); var resultTransaction = null; if (transaction) { resultTransaction = new transaction_1.Transaction(transaction); resultTransaction.developerPayload = storedDeveloperPayload; } common._notify(common.transactionUpdatedEvent, resultTransaction); if (transaction && (transaction.transactionState === 2 /* Failed */ || transaction.transactionState === 1 /* Purchased */ || transaction.transactionState === 3 /* Restored */)) { SKPaymentQueue.defaultQueue().finishTransaction(transaction); storedDeveloperPayload = undefined; } } }; return SKPaymentTransactionObserverImpl; }(NSObject)); SKPaymentTransactionObserverImpl = __decorate([ ObjCClass(SKPaymentTransactionObserver) ], SKPaymentTransactionObserverImpl); var SKReceiptRefreshRequestDelegateImpl_1, SKProductRequestDelegateImpl_1;