UNPKG

nimbbl-mobile-react-native-sdk

Version:

Nimbbl React Native SDK for payment integration

257 lines 9.25 kB
"use strict"; /** * @fileoverview Main Nimbbl SDK for React Native * @version 1.0.0 * @author Nimbbl Tech * * This is a wrapper around the native Android and iOS SDKs */ Object.defineProperty(exports, "__esModule", { value: true }); exports.nimbblSDK = void 0; const react_native_1 = require("react-native"); const constants_1 = require("./constants"); const { NimbblReactNativeSDK } = react_native_1.NativeModules; // Create event emitter for native events with null check const eventEmitter = NimbblReactNativeSDK ? new react_native_1.NativeEventEmitter(NimbblReactNativeSDK) : null; /** * Main Nimbbl SDK Class * Provides the primary interface for integrating Nimbbl payment functionality * This wrapper matches the native iOS/Android SDK patterns */ class NimbblSDK { constructor() { this.eventListeners = new Map(); this.eventSubscriptions = new Map(); if (!NimbblReactNativeSDK) { throw new Error('NimbblReactNativeSDK native module is not available. Make sure you have properly linked the library.'); } this.config = null; this.isInitialized = false; this.eventEmitter = eventEmitter; } /** * Get shared instance (matching iOS pattern) */ static getSharedInstance() { if (!NimbblSDK.shared) { NimbblSDK.shared = new NimbblSDK(); } return NimbblSDK.shared; } /** * Test native module availability */ async testNativeModule() { if (!NimbblReactNativeSDK) { throw new Error('NimbblReactNativeSDK native module is not available'); } const result = await NimbblReactNativeSDK.testModule(); return result; } /** * Initialize the SDK with configuration * @param config - SDK configuration object * @returns Promise resolving to initialization result */ async initialize(config) { try { // Validate configuration this.validateConfig(config); // Merge with default configuration this.config = { ...constants_1.DEFAULT_CONFIG, ...config, options: { timeout: constants_1.DEFAULT_CONFIG.timeout, enable_logging: constants_1.DEFAULT_CONFIG.enable_logging, enable_analytics: constants_1.DEFAULT_CONFIG.enable_analytics, ...config.options, }, }; // Initialize native module const result = await NimbblReactNativeSDK.initialize(this.config); if (result.success) { this.isInitialized = true; } return result; } catch (error) { console.error('SDK initialization failed:', error); throw error; } } /** * Set environment URL for the SDK * @param url - Environment URL to set */ setEnvironmentUrl(url) { if (!this.isInitialized) { throw new Error(constants_1.ERROR_MESSAGES[constants_1.ERROR_CODES.SDK_NOT_INITIALIZED]); } if (this.config) { this.config.options = { ...this.config.options, api_base_url: url, }; } } /** * Create shop order (matching iOS SDK pattern) * @param currency - Currency code * @param amount - Order amount * @param productId - Product ID for header customization * @param orderLineItems - Whether to enable order line items * @param checkoutExperience - Checkout experience type * @param paymentMode - Payment mode code * @param subPaymentMode - Sub payment mode code * @param user - User details object * @returns Promise resolving to order response */ async createShopOrder(currency, amount, productId, orderLineItems, checkoutExperience, paymentMode, subPaymentMode, user) { if (!this.isInitialized) { throw new Error(constants_1.ERROR_MESSAGES[constants_1.ERROR_CODES.SDK_NOT_INITIALIZED]); } try { // Build order data matching iOS sample app exactly const orderData = { currency, amount, // Keep as string to match iOS product_id: productId, orderLineItems, checkout_experience: checkoutExperience, payment_mode: paymentMode, subPaymentMode, user: user || {}, // Always include user, empty object if not provided }; const response = await NimbblReactNativeSDK.createShopOrder(orderData); return { success: true, data: response }; } catch (error) { console.error('Error creating shop order:', error); return { success: false, error }; } } /** * Checkout with options (matching iOS SDK pattern) * @param options - Checkout options * @returns Promise resolving to checkout result */ async checkout(options) { if (!this.isInitialized) { throw new Error(constants_1.ERROR_MESSAGES[constants_1.ERROR_CODES.SDK_NOT_INITIALIZED]); } try { const checkoutPayload = { order_token: options.orderToken, payment_mode_code: options.paymentModeCode || '', bank_code: options.bankCode || '', wallet_code: options.walletCode || '', payment_flow: options.paymentFlow || '', }; await NimbblReactNativeSDK.checkout(checkoutPayload); return { success: true, message: 'Checkout initiated successfully' }; } catch (error) { console.error('Error during checkout:', error); return { success: false, message: error instanceof Error ? error.message : 'Checkout failed', }; } } /** * Add event listener for native events * @param eventName - Event name to listen for * @param listener - Event listener function */ addEventListener(eventName, listener) { if (!this.eventEmitter) { console.warn(`Event emitter is not initialized. Cannot add listener for event: ${eventName}`); return; } if (!this.eventListeners.has(eventName)) { this.eventListeners.set(eventName, []); this.eventSubscriptions.set(eventName, []); } this.eventListeners.get(eventName).push(listener); // Add native event listener and track subscription const subscription = this.eventEmitter.addListener(eventName, listener); this.eventSubscriptions.get(eventName).push(subscription); } /** * Remove event listener * @param eventName - Event name * @param listener - Event listener function to remove */ removeEventListener(eventName, listener) { if (!this.eventEmitter) { console.warn(`Event emitter is not initialized. Cannot remove listener for event: ${eventName}`); return; } const listeners = this.eventListeners.get(eventName); const subscriptions = this.eventSubscriptions.get(eventName); if (listeners && subscriptions) { const index = listeners.indexOf(listener); if (index > -1) { listeners.splice(index, 1); // Remove the corresponding subscription const subscription = subscriptions[index]; if (subscription) { subscription.remove(); subscriptions.splice(index, 1); } } } } /** * Remove all event listeners */ removeAllEventListeners() { if (!this.eventEmitter) { console.warn('Event emitter is not initialized. Cannot remove all listeners.'); return; } // Remove all subscriptions this.eventSubscriptions.forEach((subscriptions) => { subscriptions.forEach((subscription) => { subscription.remove(); }); }); this.eventListeners.clear(); this.eventSubscriptions.clear(); } /** * Get SDK configuration * @returns Current SDK configuration */ getConfig() { return this.config; } /** * Get initialization status * @returns Whether the SDK is initialized */ isSDKInitialized() { return this.isInitialized; } /** * Validate SDK configuration * @param config - Configuration to validate */ validateConfig(config) { if (!config) { throw new Error(constants_1.ERROR_MESSAGES[constants_1.ERROR_CODES.INVALID_CONFIG]); } if (!config.environment || !Object.values(constants_1.ENVIRONMENTS).includes(config.environment)) { throw new Error('Invalid environment specified'); } } } NimbblSDK.shared = null; exports.default = NimbblSDK; // Export shared instance exports.nimbblSDK = NimbblSDK.getSharedInstance(); //# sourceMappingURL=NimbblSDK.js.map