import { Organization } from 'fhir/r4'; // Invoicing Phase 3 (direct patient payments, BYO processor), Slice A / A3 reader. // // Mirrors the server's utils/payment-gateway.ts - the server WRITES this exact URL, the plugin // READS it back - so the literal MUST match byte for byte (A2 A3). export const ORGANIZATION_PAYMENT_GATEWAY_EXTENSION_URL = 'https://akashichealthrecords.com/fhir/StructureDefinition/organization-payment-gateway'; // Pure reader - the PUBLIC half of an org's payment-gateway config. The SECRET half (secretKey + // webhookSecret) lives ONLY in AWS Secrets Manager and is NEVER placed on the Organization, so it // can never be read here. The server stores the public half as three nested sub-extensions under // the single payment-gateway extension: // [{url:'gatewayId',valueString}, {url:'publishableKey',valueString}, {url:'secretConfigured',valueBoolean}] // `configured` is true only when the server has a stored secret (secretConfigured) AND a publishable // key is present - a gateway is only usable when both halves exist. Everything defaults to ''/false // when the extension (or a sub-extension) is absent. export const getPaymentGatewayConfig = ( org?: Organization, ): { gatewayId: string; publishableKey: string; configured: boolean } => { const ext = org?.extension?.find((e) => e.url === ORGANIZATION_PAYMENT_GATEWAY_EXTENSION_URL); const gatewayId = ext?.extension?.find((s) => s.url === 'gatewayId')?.valueString ?? ''; const publishableKey = ext?.extension?.find((s) => s.url === 'publishableKey')?.valueString ?? ''; const secretConfigured = ext?.extension?.find((s) => s.url === 'secretConfigured')?.valueBoolean ?? false; return { gatewayId, publishableKey, configured: secretConfigured && publishableKey !== '' }; };