import {TabbyPurchaseType} from '../constants/payment'; import { CheckoutSession, Currency, TabbyCheckoutPayload, } from '../constants/payment'; import {SdkConfig, parseSdkConfig} from '../constants/sdkConfig'; import {getVersionHeader} from './utils/headers'; import {joinPath} from './utils/url'; export type TabbySession = { sessionId: string; paymentId: string; availableProducts: TabbyProduct[]; rejectionReason?: string; // Optional, if the session is not available }; export enum TabbyEnv { STAGING = 'dev', PRODUCTION = 'ai', } export type TabbyProduct = { type: TabbyPurchaseType; webUrl: string; }; class TabbyRN { private env: TabbyEnv = TabbyEnv.PRODUCTION; private apiKey: string | null = null; private config: SdkConfig | null = null; public async setup( apiKey: string, env: TabbyEnv = TabbyEnv.PRODUCTION, ): Promise { if (!apiKey || apiKey.length === 0) { throw new Error('TabbySDK setup failed: apiKey is required'); } const bootstrapUrl = `https://api.tabby.${env}/api/v1/sdk/config`; const response = await fetch(bootstrapUrl, { method: 'POST', headers: { Accept: 'application/json', 'Content-Type': 'application/json', 'X-SDK-Version': getVersionHeader(), }, body: '{}', }); if (!response.ok) { const body = await response.text().catch(() => ''); console.error(`TabbySDK setup failed: HTTP ${response.status}`, body); throw new Error(`TabbySDK setup failed: HTTP ${response.status}`); } let json: unknown; try { json = await response.json(); } catch (e) { throw new Error('TabbySDK setup failed: malformed config (invalid JSON)'); } const parsed = parseSdkConfig(json); this.apiKey = apiKey; this.env = env; this.config = parsed; } private checkSetup(): void { if (!this.apiKey || !this.config) { throw new Error( 'TabbySDK was not setup. Call await Tabby.setup(...) before any other calls.', ); } } public widgetsBaseUrlFor(currency: Currency): string { this.checkSetup(); return this.config!.endpointsFor(currency).widgetsBaseUrl; } public async createSession( payload: TabbyCheckoutPayload, ): Promise { this.checkSetup(); const endpoints = this.config!.endpointsFor(payload.payment.currency); const url = joinPath(endpoints.checkoutApiBaseUrl, 'api/v2/checkout'); const response = await fetch(url, { method: 'POST', headers: { Accept: 'application/json', 'Content-Type': 'application/json', 'X-SDK-Version': getVersionHeader(), Authorization: `Bearer ${this.apiKey}`, }, body: JSON.stringify(payload), }); if (!response.ok) { const body = await response.text().catch(() => ''); console.error( `TabbySDK createSession failed: HTTP ${response.status}`, body, ); throw new Error(`TabbySDK createSession failed: HTTP ${response.status}`); } const checkoutSession: CheckoutSession = await response.json(); const { id, configuration: {available_products, products}, payment, } = checkoutSession; const availableProducts: TabbyProduct[] = []; if (available_products.installments) { const [installmentsPlan] = available_products.installments; if (installmentsPlan) { availableProducts.push({ type: 'installments', webUrl: installmentsPlan.web_url, }); } } const result: TabbySession = { sessionId: id, paymentId: payment.id, availableProducts, rejectionReason: products.installments?.rejection_reason, }; return result; } } const Tabby = new TabbyRN(); export {Tabby};