@kirz/react-native-toolkit
Version:
Toolkit to speed up React Native development
97 lines (96 loc) • 4.06 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.ApphudPlugin = void 0;
var _reactNativeApphud = _interopRequireDefault(require("@kirz/react-native-apphud"));
var _reactNative = require("react-native");
var _index = require("../../index");
var _Plugin = require("../Plugin");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
class ApphudPlugin extends _Plugin.Plugin {
constructor(options) {
super();
this.options = options;
_defineProperty(this, "name", 'ApphudPlugin');
_defineProperty(this, "features", ['IAPReceiptValidator']);
_defineProperty(this, "initializationTimeout", 15000);
}
async initialize() {
await _reactNativeApphud.default.start({
...this.options,
observerMode: true
});
}
async isTrialAvailable(subscriptionId) {
try {
return await _index.PromiseUtils.timeout(_reactNativeApphud.default.checkEligibilitiesForIntroductoryOffer(subscriptionId), 5000);
} catch {
return false;
}
}
async hasPremiumAccess() {
return await _reactNativeApphud.default.hasPremiumAccess();
}
async hasActiveSubscription() {
return await _reactNativeApphud.default.hasActiveSubscription();
}
async getPurchasedSubscriptions() {
const subscriptions = await _reactNativeApphud.default.subscriptions();
return subscriptions.map(x => {
if (_reactNative.Platform.OS === 'ios') {
const subscription = x;
return {
productId: subscription.productId,
expiresAt: new Date(subscription.expiresDate * 1000).toISOString(),
startedAt: new Date(subscription.startedAt * 1000).toISOString(),
cancelledAt: subscription.canceledAt ? new Date(subscription.canceledAt * 1000).toISOString() : undefined,
isAutoRenewEnabled: subscription.isAutorenewEnabled,
isActive: subscription.isActive,
isSandbox: subscription.isSandbox
};
}
return {
productId: x.productId,
expiresAt: new Date(parseInt(x.expiresAt, 10)).toISOString(),
// @ts-ignore
startedAt: new Date(parseInt(x.statedAt, 10)).toISOString(),
cancelledAt: x.cancelledAt,
isAutoRenewEnabled: x.isInRetryBilling,
isActive: x.isActive,
isSandbox: false
};
});
}
async getActiveSubscription() {
const subscriptions = await this.getPurchasedSubscriptions();
return subscriptions.find(x => x.isActive) ?? null;
}
async getPurchasedProducts() {
const purchases = await _reactNativeApphud.default.nonRenewingPurchases();
return purchases.map(x => ({
productId: x.productId,
purchasedAt: x.purchasedAt,
canceledAt: x.canceledAt
}));
}
isProductPurchased(productId) {
return _reactNativeApphud.default.isNonRenewingPurchaseActive(productId);
}
async restorePurchases() {
if (_reactNative.Platform.OS === 'android') {
await _reactNativeApphud.default.syncPurchases();
}
await _reactNativeApphud.default.restorePurchases();
}
async handlePurchase() {
if (_reactNative.Platform.OS === 'android') {
await _reactNativeApphud.default.syncPurchases();
}
}
}
exports.ApphudPlugin = ApphudPlugin;
//# sourceMappingURL=ApphudPlugin.js.map