react-native-purchases
Version:
React Native in-app purchases and subscriptions made easy. Supports iOS and Android.
892 lines • 76.4 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LOG_LEVEL = exports.REFUND_REQUEST_STATUS = exports.BILLING_FEATURE = exports.PURCHASE_TYPE = void 0;
var react_native_1 = require("react-native");
var purchases_typescript_internal_1 = require("@revenuecat/purchases-typescript-internal");
// This export is kept to keep backwards compatibility to any possible users using this file directly
var purchases_typescript_internal_2 = require("@revenuecat/purchases-typescript-internal");
Object.defineProperty(exports, "PURCHASE_TYPE", { enumerable: true, get: function () { return purchases_typescript_internal_2.PURCHASE_TYPE; } });
Object.defineProperty(exports, "BILLING_FEATURE", { enumerable: true, get: function () { return purchases_typescript_internal_2.BILLING_FEATURE; } });
Object.defineProperty(exports, "REFUND_REQUEST_STATUS", { enumerable: true, get: function () { return purchases_typescript_internal_2.REFUND_REQUEST_STATUS; } });
Object.defineProperty(exports, "LOG_LEVEL", { enumerable: true, get: function () { return purchases_typescript_internal_2.LOG_LEVEL; } });
var react_native_2 = require("react-native");
var RNPurchases = react_native_1.NativeModules.RNPurchases;
var eventEmitter = new react_native_1.NativeEventEmitter(RNPurchases);
var customerInfoUpdateListeners = [];
var shouldPurchasePromoProductListeners = [];
var customLogHandler;
eventEmitter.addListener("Purchases-CustomerInfoUpdated", function (customerInfo) {
customerInfoUpdateListeners.forEach(function (listener) { return listener(customerInfo); });
});
eventEmitter.addListener("Purchases-ShouldPurchasePromoProduct", function (_a) {
var callbackID = _a.callbackID;
shouldPurchasePromoProductListeners.forEach(function (listener) {
return listener(function () { return RNPurchases.makeDeferredPurchase(callbackID); });
});
});
eventEmitter.addListener("Purchases-LogHandlerEvent", function (_a) {
var logLevel = _a.logLevel, message = _a.message;
var logLevelEnum = purchases_typescript_internal_1.LOG_LEVEL[logLevel];
customLogHandler(logLevelEnum, message);
});
var Purchases = /** @class */ (function () {
function Purchases() {
}
/**
* Sets up Purchases with your API key and an app user id.
* @param {String} apiKey RevenueCat API Key. Needs to be a String
* @param {String?} appUserID An optional unique id for identifying the user. Needs to be a string.
* @param {boolean} [observerMode=false] An optional boolean. Set this to TRUE if you have your own IAP implementation and want to use only RevenueCat's backend. Default is FALSE.
* @param {boolean} [usesStoreKit2IfAvailable=false] DEPRECATED. An optional boolean. iOS-only. Defaults to FALSE. Setting this to TRUE will enable StoreKit2 on compatible devices.
* We recommend not using this parameter, letting RevenueCat decide for you which StoreKit implementation to use.
* @param {ENTITLEMENT_VERIFICATION_MODE} [entitlementVerificationMode=ENTITLEMENT_VERIFICATION_MODE.DISABLED] Sets the entitlement verifciation mode to use. For more details, check https://rev.cat/trusted-entitlements
* @param {boolean} [useAmazon=false] An optional boolean. Android-only. Set this to TRUE to enable Amazon on compatible devices.
* @param {String?} userDefaultsSuiteName An optional string. iOS-only, will be ignored for Android.
* Set this if you would like the RevenueCat SDK to store its preferences in a different NSUserDefaults suite, otherwise it will use standardUserDefaults.
* Default is null, which will make the SDK use standardUserDefaults.
*/
Purchases.configure = function (_a) {
var apiKey = _a.apiKey, _b = _a.appUserID, appUserID = _b === void 0 ? null : _b, _c = _a.observerMode, observerMode = _c === void 0 ? false : _c, userDefaultsSuiteName = _a.userDefaultsSuiteName, _d = _a.usesStoreKit2IfAvailable, usesStoreKit2IfAvailable = _d === void 0 ? false : _d, _e = _a.useAmazon, useAmazon = _e === void 0 ? false : _e, _f = _a.shouldShowInAppMessagesAutomatically, shouldShowInAppMessagesAutomatically = _f === void 0 ? true : _f, _g = _a.entitlementVerificationMode, entitlementVerificationMode = _g === void 0 ? purchases_typescript_internal_1.ENTITLEMENT_VERIFICATION_MODE.DISABLED : _g;
if (apiKey === undefined || typeof apiKey !== "string") {
throw new Error("Invalid API key. It must be called with an Object: configure({apiKey: \"key\"})");
}
if (appUserID !== null && typeof appUserID !== "undefined" && typeof appUserID !== "string") {
throw new Error("appUserID needs to be a string");
}
RNPurchases.setupPurchases(apiKey, appUserID, observerMode, userDefaultsSuiteName, usesStoreKit2IfAvailable, useAmazon, shouldShowInAppMessagesAutomatically, entitlementVerificationMode);
};
/**
* @deprecated, configure behavior through the RevenueCat dashboard instead.
* If an user tries to purchase a product that is active on the current app store account,
* we will treat it as a restore and alias the new ID with the previous id.
* @param {boolean} allowSharing Set this to true if you are passing in an appUserID but it is anonymous,
* this is true by default if you didn't pass an appUserID
* @returns {Promise<void>} The promise will be rejected if configure has not been called yet.
*/
Purchases.setAllowSharingStoreAccount = function (allowSharing) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, Purchases.throwIfNotConfigured()];
case 1:
_a.sent();
RNPurchases.setAllowSharingStoreAccount(allowSharing);
return [2 /*return*/];
}
});
});
};
/**
* @param {boolean} finishTransactions Set finishTransactions to false if you aren't using Purchases SDK to
* make the purchase
* @returns {Promise<void>} The promise will be rejected if configure has not been called yet.
*/
Purchases.setFinishTransactions = function (finishTransactions) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, Purchases.throwIfNotConfigured()];
case 1:
_a.sent();
RNPurchases.setFinishTransactions(finishTransactions);
return [2 /*return*/];
}
});
});
};
/**
* iOS only.
* @param {boolean} simulatesAskToBuyInSandbox Set this property to true *only* when testing the ask-to-buy / SCA
* purchases flow. More information: http://errors.rev.cat/ask-to-buy
* @returns {Promise<void>} The promise will be rejected if configure has not been called yet.
*/
Purchases.setSimulatesAskToBuyInSandbox = function (simulatesAskToBuyInSandbox) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
if (react_native_2.Platform.OS === "ios") {
RNPurchases.setSimulatesAskToBuyInSandbox(simulatesAskToBuyInSandbox);
}
return [2 /*return*/];
});
});
};
/**
* Sets a function to be called on updated customer info
* @param {CustomerInfoUpdateListener} customerInfoUpdateListener CustomerInfo update listener
*/
Purchases.addCustomerInfoUpdateListener = function (customerInfoUpdateListener) {
if (typeof customerInfoUpdateListener !== "function") {
throw new Error("addCustomerInfoUpdateListener needs a function");
}
customerInfoUpdateListeners.push(customerInfoUpdateListener);
};
/**
* Removes a given CustomerInfoUpdateListener
* @param {CustomerInfoUpdateListener} listenerToRemove CustomerInfoUpdateListener reference of the listener to remove
* @returns {boolean} True if listener was removed, false otherwise
*/
Purchases.removeCustomerInfoUpdateListener = function (listenerToRemove) {
if (customerInfoUpdateListeners.includes(listenerToRemove)) {
customerInfoUpdateListeners = customerInfoUpdateListeners.filter(function (listener) { return listenerToRemove !== listener; });
return true;
}
return false;
};
/**
* Sets a function to be called on purchases initiated on the Apple App Store. This is only used in iOS.
* @param {ShouldPurchasePromoProductListener} shouldPurchasePromoProductListener Called when a user initiates a
* promotional in-app purchase from the App Store. If your app is able to handle a purchase at the current time, run
* the deferredPurchase function. If the app is not in a state to make a purchase: cache the deferredPurchase, then
* call the deferredPurchase when the app is ready to make the promotional purchase.
* If the purchase should never be made, you don't need to ever call the deferredPurchase and the app will not
* proceed with promotional purchases.
*/
Purchases.addShouldPurchasePromoProductListener = function (shouldPurchasePromoProductListener) {
if (typeof shouldPurchasePromoProductListener !== "function") {
throw new Error("addShouldPurchasePromoProductListener needs a function");
}
shouldPurchasePromoProductListeners.push(shouldPurchasePromoProductListener);
};
/**
* Removes a given ShouldPurchasePromoProductListener
* @param {ShouldPurchasePromoProductListener} listenerToRemove ShouldPurchasePromoProductListener reference of
* the listener to remove
* @returns {boolean} True if listener was removed, false otherwise
*/
Purchases.removeShouldPurchasePromoProductListener = function (listenerToRemove) {
if (shouldPurchasePromoProductListeners.includes(listenerToRemove)) {
shouldPurchasePromoProductListeners = shouldPurchasePromoProductListeners.filter(function (listener) { return listenerToRemove !== listener; });
return true;
}
return false;
};
/**
* Gets the map of entitlements -> offerings -> products
* @returns {Promise<PurchasesOfferings>} Promise of entitlements structure. The promise will be rejected if configure
* has not been called yet.
*/
Purchases.getOfferings = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, Purchases.throwIfNotConfigured()];
case 1:
_a.sent();
return [2 /*return*/, RNPurchases.getOfferings()];
}
});
});
};
/**
* Fetch the product info
* @param {String[]} productIdentifiers Array of product identifiers
* @param {String} type Optional type of products to fetch, can be SUBSCRIPTION or NON_SUBSCRIPTION. SUBSCRIPTION by default
* @returns {Promise<PurchasesStoreProduct[]>} A promise containing an array of products. The promise will be rejected
* if the products are not properly configured in RevenueCat or if there is another error retrieving them.
* Rejections return an error code, and a userInfo object with more information. The promise will also be rejected
* if configure has not been called yet.
*/
Purchases.getProducts = function (productIdentifiers, type) {
if (type === void 0) { type = purchases_typescript_internal_1.PRODUCT_CATEGORY.SUBSCRIPTION; }
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, Purchases.throwIfNotConfigured()];
case 1:
_a.sent();
return [2 /*return*/, RNPurchases.getProductInfo(productIdentifiers, type)];
}
});
});
};
/**
* Make a purchase
*
* @param {String} productIdentifier The product identifier of the product you want to purchase
* @param {UpgradeInfo} upgradeInfo Android only. Optional UpgradeInfo you wish to upgrade from containing the oldSKU
* and the optional prorationMode.
* @param {String} type Optional type of product, can be inapp or subs. Subs by default
* @deprecated, use purchaseStoreProduct instead
*/
Purchases.purchaseProduct = function (productIdentifier, upgradeInfo, type) {
if (type === void 0) { type = purchases_typescript_internal_1.PURCHASE_TYPE.SUBS; }
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, Purchases.throwIfNotConfigured()];
case 1:
_a.sent();
return [2 /*return*/, RNPurchases.purchaseProduct(productIdentifier, upgradeInfo, type, null, null, null).catch(function (error) {
error.userCancelled = error.code === purchases_typescript_internal_1.PURCHASES_ERROR_CODE.PURCHASE_CANCELLED_ERROR;
throw error;
})];
}
});
});
};
/**
* Make a purchase
*
* @param {PurchasesStoreProduct} product The product you want to purchase
* @param {GoogleProductChangeInfo} googleProductChangeInfo Android only. Optional GoogleProductChangeInfo you
* wish to upgrade from containing the oldProductIdentifier and the optional prorationMode.
* @param {boolean} googleIsPersonalizedPrice Android and Google only. Optional boolean indicates personalized pricing on products available for purchase in the EU.
* For compliance with EU regulations. User will see "This price has been customize for you" in the purchase dialog when true.
* See https://developer.android.com/google/play/billing/integrate#personalized-price for more info.
* @returns {Promise<{ productIdentifier: string, customerInfo:CustomerInfo }>} A promise of an object containing
* a customer info object and a product identifier. Rejections return an error code,
* a boolean indicating if the user cancelled the purchase, and an object with more information. The promise will
* also be rejected if configure has not been called yet.
*/
Purchases.purchaseStoreProduct = function (product, googleProductChangeInfo, googleIsPersonalizedPrice) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, Purchases.throwIfNotConfigured()];
case 1:
_a.sent();
return [2 /*return*/, RNPurchases.purchaseProduct(product.identifier, googleProductChangeInfo, product.productCategory, null, googleIsPersonalizedPrice == null ? null : { isPersonalizedPrice: googleIsPersonalizedPrice }, product.presentedOfferingIdentifier).catch(function (error) {
error.userCancelled = error.code === purchases_typescript_internal_1.PURCHASES_ERROR_CODE.PURCHASE_CANCELLED_ERROR;
throw error;
})];
}
});
});
};
/**
* iOS only. Purchase a product applying a given discount.
*
* @param {PurchasesStoreProduct} product The product you want to purchase
* @param {PurchasesPromotionalOffer} discount Discount to apply to this package. Retrieve this discount using getPromotionalOffer.
* @param {boolean} googleIsPersonalizedPrice Android and Google only. Optional boolean indicates personalized pricing on products available for purchase in the EU.
* For compliance with EU regulations. User will see "This price has been customize for you" in the purchase dialog when true.
* See https://developer.android.com/google/play/billing/integrate#personalized-price for more info.
* @returns {Promise<{ productIdentifier: string, customerInfo:CustomerInfo }>} A promise of an object containing
* a customer info object and a product identifier. Rejections return an error code,
* a boolean indicating if the user cancelled the purchase, and an object with more information. The promise will be
* rejected if configure has not been called yet.
*/
Purchases.purchaseDiscountedProduct = function (product, discount) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, Purchases.throwIfNotConfigured()];
case 1:
_a.sent();
if (typeof discount === "undefined" || discount == null) {
throw new Error("A discount is required");
}
return [2 /*return*/, RNPurchases.purchaseProduct(product.identifier, null, null, discount.timestamp.toString(), null, product.presentedOfferingIdentifier).catch(function (error) {
error.userCancelled = error.code === purchases_typescript_internal_1.PURCHASES_ERROR_CODE.PURCHASE_CANCELLED_ERROR;
throw error;
})];
}
});
});
};
/**
* Make a purchase
*
* @param {PurchasesPackage} aPackage The Package you wish to purchase. You can get the Packages by calling getOfferings
* @param {UpgradeInfo} upgradeInfo DEPRECATED. Use googleProductChangeInfo.
* @param {GoogleProductChangeInfo} googleProductChangeInfo Android only. Optional GoogleProductChangeInfo you
* wish to upgrade from containing the oldProductIdentifier and the optional prorationMode.
* @param {boolean} googleIsPersonalizedPrice Android and Google only. Optional boolean indicates personalized pricing on products available for purchase in the EU.
* For compliance with EU regulations. User will see "This price has been customize for you" in the purchase dialog when true.
* See https://developer.android.com/google/play/billing/integrate#personalized-price for more info.
* @returns {Promise<{ productIdentifier: string, customerInfo: CustomerInfo }>} A promise of an object containing
* a customer info object and a product identifier. Rejections return an error code, a boolean indicating if the
* user cancelled the purchase, and an object with more information. The promise will be also be rejected if configure
* has not been called yet.
*/
Purchases.purchasePackage = function (aPackage, upgradeInfo, googleProductChangeInfo, googleIsPersonalizedPrice) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, Purchases.throwIfNotConfigured()];
case 1:
_a.sent();
return [2 /*return*/, RNPurchases.purchasePackage(aPackage.identifier, aPackage.offeringIdentifier, googleProductChangeInfo || upgradeInfo, null, googleIsPersonalizedPrice == null ? null : { isPersonalizedPrice: googleIsPersonalizedPrice }).catch(function (error) {
error.userCancelled = error.code === purchases_typescript_internal_1.PURCHASES_ERROR_CODE.PURCHASE_CANCELLED_ERROR;
throw error;
})];
}
});
});
};
/**
* Google only. Make a purchase of a subscriptionOption
*
* @param {SubscriptionOption} subscriptionOption The SubscriptionOption you wish to purchase. You can get the SubscriptionOption from StoreProducts by calling getOfferings
* @param {GoogleProductChangeInfo} googleProductChangeInfo Android only. Optional GoogleProductChangeInfo you
* wish to upgrade from containing the oldProductIdentifier and the optional prorationMode.
* @param {boolean} googleIsPersonalizedPrice Android and Google only. Optional boolean indicates personalized pricing on products available for purchase in the EU.
* For compliance with EU regulations. User will see "This price has been customize for you" in the purchase dialog when true.
* See https://developer.android.com/google/play/billing/integrate#personalized-price for more info.
* @returns {Promise<{ productIdentifier: string, customerInfo: CustomerInfo }>} A promise of an object containing
* a customer info object and a product identifier. Rejections return an error code, a boolean indicating if the
* user cancelled the purchase, and an object with more information. The promise will be also be rejected if configure
* has not been called yet.
*/
Purchases.purchaseSubscriptionOption = function (subscriptionOption, googleProductChangeInfo, googleIsPersonalizedPrice) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, Purchases.throwIfNotConfigured()];
case 1:
_a.sent();
return [4 /*yield*/, Purchases.throwIfIOSPlatform()];
case 2:
_a.sent();
return [2 /*return*/, RNPurchases.purchaseSubscriptionOption(subscriptionOption.productId, subscriptionOption.id, googleProductChangeInfo, null, googleIsPersonalizedPrice == null ? null : { isPersonalizedPrice: googleIsPersonalizedPrice }, subscriptionOption.presentedOfferingIdentifier).catch(function (error) {
error.userCancelled = error.code === purchases_typescript_internal_1.PURCHASES_ERROR_CODE.PURCHASE_CANCELLED_ERROR;
throw error;
})];
}
});
});
};
/**
* iOS only. Purchase a package applying a given discount.
*
* @param {PurchasesPackage} aPackage The Package you wish to purchase. You can get the Packages by calling getOfferings
* @param {PurchasesPromotionalOffer} discount Discount to apply to this package. Retrieve this discount using getPromotionalOffer.
* @returns {Promise<{ productIdentifier: string, customerInfo: CustomerInfo }>} A promise of an object containing
* a customer info object and a product identifier. Rejections return an error code, a boolean indicating if the
* user cancelled the purchase, and an object with more information. The promise will be also be rejected if configure
* has not been called yet.
*/
Purchases.purchaseDiscountedPackage = function (aPackage, discount) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, Purchases.throwIfNotConfigured()];
case 1:
_a.sent();
if (typeof discount === "undefined" || discount == null) {
throw new Error("A discount is required");
}
return [2 /*return*/, RNPurchases.purchasePackage(aPackage.identifier, aPackage.offeringIdentifier, null, discount.timestamp.toString(), null).catch(function (error) {
error.userCancelled = error.code === purchases_typescript_internal_1.PURCHASES_ERROR_CODE.PURCHASE_CANCELLED_ERROR;
throw error;
})];
}
});
});
};
/**
* Restores a user's previous purchases and links their appUserIDs to any user's also using those purchases.
* @returns {Promise<CustomerInfo>} A promise of a customer info object. Rejections return an error code, and an
* userInfo object with more information. The promise will be also be rejected if configure has not been called yet.
*/
Purchases.restorePurchases = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, Purchases.throwIfNotConfigured()];
case 1:
_a.sent();
return [2 /*return*/, RNPurchases.restorePurchases()];
}
});
});
};
/**
* Get the appUserID
* @returns {Promise<string>} The app user id in a promise
*/
Purchases.getAppUserID = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, Purchases.throwIfNotConfigured()];
case 1:
_a.sent();
return [2 /*return*/, RNPurchases.getAppUserID()];
}
});
});
};
/**
* This function will logIn the current user with an appUserID. Typically this would be used after a log in
* to identify a user without calling configure.
* @param {String} appUserID The appUserID that should be linked to the currently user
* @returns {Promise<LogInResult>} A promise of an object that contains the customerInfo after logging in, as well
* as a boolean indicating whether the user has just been created for the first time in the RevenueCat backend. The
* promise will be rejected if configure has not been called yet or if there's an issue logging in.
*/
Purchases.logIn = function (appUserID) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, Purchases.throwIfNotConfigured()];
case 1:
_a.sent();
// noinspection SuspiciousTypeOfGuard
if (typeof appUserID !== "string") {
throw new Error("appUserID needs to be a string");
}
return [2 /*return*/, RNPurchases.logIn(appUserID)];
}
});
});
};
/**
* Logs out the Purchases client clearing the saved appUserID. This will generate a random user id and save it in the cache.
* @returns {Promise<CustomerInfo>} A promise of a customer info object. Rejections return an error code,
* and a userInfo object with more information. The promise will be rejected if configure has not been called yet or if
* there's an issue logging out.
*/
Purchases.logOut = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, Purchases.throwIfNotConfigured()];
case 1:
_a.sent();
return [2 /*return*/, RNPurchases.logOut()];
}
});
});
};
/**
* Enables/Disables debugs logs
* @param {boolean} enabled Enable or not debug logs
* @deprecated, use setLogLevel instead
*/
Purchases.setDebugLogsEnabled = function (enabled) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
RNPurchases.setDebugLogsEnabled(enabled);
return [2 /*return*/];
});
});
};
/**
* Used to set the log level. Useful for debugging issues with the lovely team @RevenueCat.
* The default is {LOG_LEVEL.INFO} in release builds and {LOG_LEVEL.DEBUG} in debug builds.
* @param {LOG_LEVEL} level
*/
Purchases.setLogLevel = function (level) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
RNPurchases.setLogLevel(level);
return [2 /*return*/];
});
});
};
/**
* Set a custom log handler for redirecting logs to your own logging system.
* By default, this sends info, warning, and error messages.
* If you wish to receive Debug level messages, see [setLogLevel].
* @param {LogHandler} logHandler It will get called for each log event.
* Use this function to redirect the log to your own logging system
*/
Purchases.setLogHandler = function (logHandler) {
customLogHandler = logHandler;
RNPurchases.setLogHandler();
};
/**
* Gets current customer info
* @returns {Promise<CustomerInfo>} A promise of a customer info object. Rejections return an error code, and an
* userInfo object with more information. The promise will be rejected if configure has not been called yet or if
* there's an issue getting the customer information.
*/
Purchases.getCustomerInfo = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, Purchases.throwIfNotConfigured()];
case 1:
_a.sent();
return [2 /*return*/, RNPurchases.getCustomerInfo()];
}
});
});
};
/**
* This method will send all the purchases to the RevenueCat backend. Call this when using your own implementation
* for subscriptions anytime a sync is needed, like after a successful purchase.
*
* @warning This function should only be called if you're not calling purchaseProduct/purchaseStoreProduct/purchasePackage/purchaseSubscriptionOption.
* @returns {Promise<void>} The promise will be rejected if configure has not been called yet or if there's an error
* syncing purchases.
*/
Purchases.syncPurchases = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, Purchases.throwIfNotConfigured()];
case 1:
_a.sent();
RNPurchases.syncPurchases();
return [2 /*return*/];
}
});
});
};
/**
* This method will send a purchase to the RevenueCat backend. This function should only be called if you are
* in Amazon observer mode or performing a client side migration of your current users to RevenueCat.
*
* The receipt IDs are cached if successfully posted so they are not posted more than once.
*
* @param {string} productID Product ID associated to the purchase.
* @param {string} receiptID ReceiptId that represents the Amazon purchase.
* @param {string} amazonUserID Amazon's userID. This parameter will be ignored when syncing a Google purchase.
* @param {(string|null|undefined)} isoCurrencyCode Product's currency code in ISO 4217 format.
* @param {(number|null|undefined)} price Product's price.
* @returns {Promise<void>} The promise will be rejected if configure has not been called yet or if there's an error
* syncing purchases.
*/
Purchases.syncObserverModeAmazonPurchase = function (productID, receiptID, amazonUserID, isoCurrencyCode, price) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, Purchases.throwIfIOSPlatform()];
case 1:
_a.sent();
return [4 /*yield*/, Purchases.throwIfNotConfigured()];
case 2:
_a.sent();
RNPurchases.syncObserverModeAmazonPurchase(productID, receiptID, amazonUserID, isoCurrencyCode, price);
return [2 /*return*/];
}
});
});
};
/**
* @deprecated, use enableAdServicesAttributionTokenCollection instead.
* Enable automatic collection of Apple Search Ad attribution. Disabled by default
* @param {boolean} enabled Enable or not automatic apple search ads attribution collection
* @returns {Promise<void>} The promise will be rejected if configure has not been called yet.
*/
Purchases.setAutomaticAppleSearchAdsAttributionCollection = function (enabled) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
if (react_native_2.Platform.OS === "ios") {
RNPurchases.setAutomaticAppleSearchAdsAttributionCollection(enabled);
}
return [2 /*return*/];
});
});
};
/**
* Enable automatic collection of Apple Search Ad attribution on iOS. Disabled by default
* @returns {Promise<void>} The promise will be rejected if configure has not been called yet.
*/
Purchases.enableAdServicesAttributionTokenCollection = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!(react_native_2.Platform.OS === "ios")) return [3 /*break*/, 2];
return [4 /*yield*/, Purchases.throwIfNotConfigured()];
case 1:
_a.sent();
RNPurchases.enableAdServicesAttributionTokenCollection();
_a.label = 2;
case 2: return [2 /*return*/];
}
});
});
};
/**
* @returns { Promise<boolean> } If the `appUserID` has been generated by RevenueCat or not.
* @returns {Promise<void>} The promise will be rejected if configure has not been called yet.
*/
Purchases.isAnonymous = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, Purchases.throwIfNotConfigured()];
case 1:
_a.sent();
return [2 /*return*/, RNPurchases.isAnonymous()];
}
});
});
};
/**
* iOS only. Computes whether or not a user is eligible for the introductory pricing period of a given product.
* You should use this method to determine whether or not you show the user the normal product price or the
* introductory price. This also applies to trials (trials are considered a type of introductory pricing).
*
* @note Subscription groups are automatically collected for determining eligibility. If RevenueCat can't
* definitively compute the eligibility, most likely because of missing group information, it will return
* `INTRO_ELIGIBILITY_STATUS_UNKNOWN`. The best course of action on unknown status is to display the non-intro
* pricing, to not create a misleading situation. To avoid this, make sure you are testing with the latest version of
* iOS so that the subscription group can be collected by the SDK. Android always returns INTRO_ELIGIBILITY_STATUS_UNKNOWN.
*
* @param productIdentifiers Array of product identifiers for which you want to compute eligibility
* @returns { Promise<[productId: string]: IntroEligibility> } A map of IntroEligility per productId. The promise
* will be rejected if configure has not been called yet or if there's in an error checking eligibility.
*/
Purchases.checkTrialOrIntroductoryPriceEligibility = function (productIdentifiers) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, Purchases.throwIfNotConfigured()];
case 1:
_a.sent();
return [2 /*return*/, RNPurchases.checkTrialOrIntroductoryPriceEligibility(productIdentifiers)];
}
});
});
};
/**
* iOS only. Use this function to retrieve the `PurchasesPromotionalOffer` for a given `PurchasesPackage`.
*
* @param product The `PurchasesStoreProduct` the user intends to purchase.
* @param discount The `PurchasesStoreProductDiscount` to apply to the product.
* @returns { Promise<PurchasesPromotionalOffer> } Returns when the `PurchasesPaymentDiscount` is returned.
* Null is returned for Android and incompatible iOS versions. The promise will be rejected if configure has not been
* called yet or if there's an error getting the payment discount.
*/
Purchases.getPromotionalOffer = function (product, discount) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, Purchases.throwIfNotConfigured()];
case 1:
_a.sent();
if (react_native_2.Platform.OS === "android") {
return [2 /*return*/, Promise.resolve(undefined)];
}
if (typeof discount === "undefined" || discount == null) {
throw new Error("A discount is required");
}
return [2 /*return*/, RNPurchases.getPromotionalOffer(product.identifier, discount.identifier)];
}
});
});
};
/**
* Invalidates the cache for customer information.
*
* Most apps will not need to use this method; invalidating the cache can leave your app in an invalid state.
* Refer to https://docs.revenuecat.com/docs/customer-info#section-get-user-information for more information on
* using the cache properly.
*
* This is useful for cases where customer information might have been updated outside of the app, like if a
* promotional subscription is granted through the RevenueCat dashboard.
* @returns {Promise<void>} The promise will be rejected if configure has not been called yet or there's an error
* invalidating the customer info cache.
*/
Purchases.invalidateCustomerInfoCache = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, Purchases.throwIfNotConfigured()];
case 1:
_a.sent();
RNPurchases.invalidateCustomerInfoCache();
return [2 /*return*/];
}
});
});
};
/** iOS only. Presents a code redemption sheet, useful for redeeming offer codes
* Refer to https://docs.revenuecat.com/docs/ios-subscription-offers#offer-codes for more information on how
* to configure and use offer codes
* @returns {Promise<void>} The promise will be rejected if configure has not been called yet or there's an error
* presenting the code redemption sheet.
*/
Purchases.presentCodeRedemptionSheet = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!(react_native_2.Platform.OS === "ios")) return [3 /*break*/, 2];
return [4 /*yield*/, Purchases.throwIfNotConfigured()];
case 1:
_a.sent();
RNPurchases.presentCodeRedemptionSheet();
_a.label = 2;
case 2: return [2 /*return*/];
}
});
});
};
/**
* Subscriber attributes are useful for storing additional, structured information on a user.
* Since attributes are writable using a public key they should not be used for
* managing secure or sensitive information such as subscription status, coins, etc.
*
* Key names starting with "$" are reserved names used by RevenueCat. For a full list of key
* restrictions refer to our guide: https://docs.revenuecat.com/docs/subscriber-attributes
*
* @param attributes Map of attributes by key. Set the value as an empty string to delete an attribute.
* @returns {Promise<void>} The promise will be rejected if configure has not been called yet or there's an error
* setting the subscriber attributes.
*/
Purchases.setAttributes = function (attributes) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, Purchases.throwIfNotConfigured()];
case 1:
_a.sent();
RNPurchases.setAttributes(attributes);
return [2 /*return*/];
}
});
});
};
/**
* Subscriber attribute associated with the email address for the user
*
* @param email Empty String or null will delete the subscriber attribute.
* @returns {Promise<void>} The promise will be rejected if configure has not been called yet or if there's an error
* setting the email.
*/
Purchases.setEmail = function (email) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, Purchases.throwIfNotConfigured()];
case 1:
_a.sent();
RNPurchases.setEmail(email);
return [2 /*return*/];
}
});
});
};
/**
* Subscriber attribute associated with the phone number for the user
*
* @param phoneNumber Empty String or null will delete the subscriber attribute.
* @returns {Promise<void>} The promise will be rejected if configure has not been called yet or if there's an error
* setting the phone number.
*/
Purchases.setPhoneNumber = function (phoneNumber) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, Purchases.throwIfNotConfigured()];
case 1:
_a.sent();
RNPurchases.setPhoneNumber(phoneNumber);
return [2 /*return*/];
}
});
});
};
/**
* Subscriber attribute associated with the display name for the user
*
* @param displayName Empty String or null will delete the subscriber attribute.
* @returns {Promise<void>} The promise will be rejected if configure has not been called yet or if there's an error
* setting the display name.
*/
Purchases.setDisplayName = function (displayName) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, Purchases.throwIfNotConfigured()];
case 1:
_a.sent();
RNPurchases.setDisplayName(displayName);
return [2 /*return*/];
}
});
});
};
/**
* Subscriber attribute associated with the push token for the user
*
* @param pushToken null will delete the subscriber attribute.
* @returns {Promise<void>} The promise will be rejected if configure has not been called yet or if there's an error
* setting the push token.
*/
Purchases.setPushToken = function (pushToken) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, Purchases.throwIfNotConfigured()];
case 1:
_a.sent();
RNPurchases.setPushToken(pushToken);
return [2 /*return*/];
}
});
});
};
/**
* Set this property to your proxy URL before configuring Purchases *only* if you've received a proxy key value
* from your RevenueCat contact.
* @returns {Promise<void>} The promise will be rejected if configure has not been called yet or if there's an error
* setting the proxy url.
*/
Purchases.setProxyURL = function (url) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, Purchases.throwIfNotConfigured()];
case 1:
_a.sent();
RNPurchases.setProxyURLString(url);
return [2 /*return*/];
}
});
});
};
/**
* Automatically collect subscriber attributes associated with the device identifiers.
* $idfa, $idfv, $ip on iOS
* $gpsAdId, $androidId, $ip on Android
* @returns {Promise<void>} The promise will be rejected if configure has not been called yet or if there's an error
* setting collecting the device identifiers.
*/
Purchases.collectDeviceIdentifiers = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, Purchases.throwIfNotConfigured()];
case 1:
_a.sent();
RNPurchases.collectDeviceIdentifiers();
return [2 /*return*/];
}
});
});
};
/**
* Subscriber attri