UNPKG

expo-mercadopago-integration

Version:

Expo library for integrating Mercado Pago native SDK without ejecting

82 lines (81 loc) 2.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createMercadoPagoSDK = createMercadoPagoSDK; const expo_modules_core_1 = require("expo-modules-core"); const react_native_1 = require("react-native"); // Obtener el módulo nativo real const NativeMercadoPagoModule = react_native_1.NativeModules.ExpoMercadoPagoModule; class ExpoMercadoPagoModule { constructor(config) { this.eventEmitter = new expo_modules_core_1.EventEmitter(NativeMercadoPagoModule); this.isInitialized = false; this.config = config; } async initialize() { if (this.isInitialized) { return; } try { await ExpoMercadoPagoModule.initializeAsync(this.config); this.isInitialized = true; } catch (error) { throw new Error(`Failed to initialize MercadoPago SDK: ${error}`); } } async startPayment(preferenceId) { if (!this.isInitialized) { throw new Error("MercadoPago SDK must be initialized before starting payment"); } const finalPreferenceId = preferenceId || this.config.preferenceId; if (!finalPreferenceId) { throw new Error("Preference ID is required. Provide it in config or as parameter"); } try { return await ExpoMercadoPagoModule.startPaymentAsync(finalPreferenceId); } catch (error) { throw new Error(`Payment failed: ${error}`); } } addPaymentResultListener(callback) { return this.eventEmitter.addListener("onPaymentResult", callback); } removePaymentResultListener(subscription) { this.eventEmitter.removeSubscription(subscription); } async isMercadoPagoInstalled() { try { return await ExpoMercadoPagoModule.isMercadoPagoInstalledAsync(); } catch (error) { console.warn("Error checking if MercadoPago is installed:", error); return false; } } async getSDKVersion() { try { return await ExpoMercadoPagoModule.getSDKVersionAsync(); } catch (error) { throw new Error(`Failed to get SDK version: ${error}`); } } // Métodos estáticos que llaman al módulo nativo real static async initializeAsync(config) { return await NativeMercadoPagoModule.initializeAsync(config); } static async startPaymentAsync(preferenceId) { return await NativeMercadoPagoModule.startPaymentAsync(preferenceId); } static async isMercadoPagoInstalledAsync() { return await NativeMercadoPagoModule.isMercadoPagoInstalledAsync(); } static async getSDKVersionAsync() { return await NativeMercadoPagoModule.getSDKVersionAsync(); } } function createMercadoPagoSDK(config) { return new ExpoMercadoPagoModule(config); } exports.default = ExpoMercadoPagoModule;