UNPKG

@qonversion/capacitor-plugin

Version:

Qonversion provides full in-app purchases infrastructure, so you do not need to build your own server for receipt validation. Implement in-app subscriptions, validate user receipts, check subscription status, and provide access to your app features and co

119 lines 5.51 kB
import Mapper from './Mapper'; import { NoCodesError } from '../dto/NoCodesError'; import { NoCodesErrorCode } from '../dto/enums'; import { NoCodesPlugin } from '../NoCodesNativePlugin'; import { sdkSource, sdkVersion } from './QonversionInternal'; const EVENT_SCREEN_SHOWN = 'nocodes_screen_shown'; const EVENT_FINISHED = 'nocodes_finished'; const EVENT_ACTION_STARTED = 'nocodes_action_started'; const EVENT_ACTION_FAILED = 'nocodes_action_failed'; const EVENT_ACTION_FINISHED = 'nocodes_action_finished'; const EVENT_SCREEN_FAILED_TO_LOAD = 'nocodes_screen_failed_to_load'; export class NoCodesInternal { constructor(config) { this.noCodesListener = null; this.purchaseDelegate = null; this.noCodeEventHandler = (event) => { var _a, _b, _c, _d, _e, _f, _g, _h; switch (event.name) { case EVENT_SCREEN_SHOWN: const screenId = (_b = (_a = event.payload) === null || _a === void 0 ? void 0 : _a.screenId) !== null && _b !== void 0 ? _b : ''; (_c = this.noCodesListener) === null || _c === void 0 ? void 0 : _c.onScreenShown(screenId); break; case EVENT_ACTION_STARTED: const actionStarted = Mapper.convertAction(event.payload); (_d = this.noCodesListener) === null || _d === void 0 ? void 0 : _d.onActionStartedExecuting(actionStarted); break; case EVENT_ACTION_FAILED: const actionFailed = Mapper.convertAction(event.payload); (_e = this.noCodesListener) === null || _e === void 0 ? void 0 : _e.onActionFailedToExecute(actionFailed); break; case EVENT_ACTION_FINISHED: const actionFinished = Mapper.convertAction(event.payload); (_f = this.noCodesListener) === null || _f === void 0 ? void 0 : _f.onActionFinishedExecuting(actionFinished); break; case EVENT_FINISHED: (_g = this.noCodesListener) === null || _g === void 0 ? void 0 : _g.onFinished(); break; case EVENT_SCREEN_FAILED_TO_LOAD: const error = Mapper.convertNoCodesError(event.payload); const defaultError = new NoCodesError(NoCodesErrorCode.UNKNOWN, 'Failed to load No-Code screen', 'Native error parsing failed.'); (_h = this.noCodesListener) === null || _h === void 0 ? void 0 : _h.onScreenFailedToLoad(error !== null && error !== void 0 ? error : defaultError); break; default: console.warn(`No-Codes SDK: Unknown event: ${event.name}`); break; } }; this.customPurchaseHandler = async (productData) => { if (!this.purchaseDelegate) { throw new Error('PurchaseDelegate is not set but custom purchase handling initiated'); } const product = Mapper.convertProduct(productData); this.purchaseDelegate .purchase(product) .then(() => { NoCodesPlugin.delegatedPurchaseCompleted(); }) .catch((error) => { NoCodesPlugin.delegatedPurchaseFailed({ errorMessage: error.message }); }); }; this.customRestoreHandler = async () => { if (!this.purchaseDelegate) { throw new Error('PurchaseDelegate is not set but custom restore handling initiated'); } this.purchaseDelegate .restore() .then(() => { NoCodesPlugin.delegatedRestoreCompleted(); }) .catch((error) => { NoCodesPlugin.delegatedRestoreFailed({ errorMessage: error.message }); }); }; NoCodesPlugin.initialize({ projectKey: config.projectKey, source: sdkSource, version: sdkVersion, proxyUrl: config.proxyUrl, locale: config.locale, theme: config.theme, }); if (config.noCodesListener) { this.setNoCodesListener(config.noCodesListener); } if (config.purchaseDelegate) { this.setPurchaseDelegate(config.purchaseDelegate); } } async setScreenPresentationConfig(config, contextKey) { const data = Mapper.convertScreenPresentationConfig(config); await NoCodesPlugin.setScreenPresentationConfig({ configData: data, contextKey }); } async showScreen(contextKey) { await NoCodesPlugin.showScreen({ contextKey }); } async close() { await NoCodesPlugin.close(); } setLocale(locale) { NoCodesPlugin.setLocale({ locale }); } setTheme(theme) { NoCodesPlugin.setTheme({ theme }); } setNoCodesListener(listener) { if (this.noCodesListener == null) { NoCodesPlugin.addListener('noCodesEvent', this.noCodeEventHandler); } this.noCodesListener = listener; } setPurchaseDelegate(delegate) { this.purchaseDelegate = delegate; NoCodesPlugin.addListener('noCodesPurchase', this.customPurchaseHandler); NoCodesPlugin.addListener('noCodesRestore', this.customRestoreHandler); NoCodesPlugin.setPurchaseDelegate(); } } //# sourceMappingURL=NoCodesInternal.js.map