import {z} from 'zod' export type PaymentScheme = 'VISA' | 'MC'; export type FromCurrencyFxRateV2 = string; export type ToCurrencyFxRateV2 = string; export type GetFxRatesV2Request = {fromAmount: number, fromCurrency: FromCurrencyFxRateV2, toCurrency: ToCurrencyFxRateV2, issuerMarkupPercentage: number}; export type CreateApiV2PaymentSchemeFxArgs = {paymentScheme: PaymentScheme, auditUser: string, body: GetFxRatesV2Request}; export type CreateApiV2PaymentSchemeFxOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const fromCurrencyFxRateV2 = z.string(); export const toCurrencyFxRateV2 = z.string(); export const getFxRatesV2Response = z.object({fromAmount: z.number().optional(), fromCurrency: fromCurrencyFxRateV2.optional(), toCurrency: toCurrencyFxRateV2.optional(), toAmountWithSchemeRate: z.number().optional(), toAmountWithMarkup: z.number().optional(), toAmountWithEcbRate: z.number().optional(), fxRatePaymentScheme: z.number().optional(), fxRatePaymentSchemeRaw: z.number().optional(), fxRateWithMarkup: z.number().optional(), fxRateWithMarkupRaw: z.number().optional(), fxRateEcb: z.number().optional(), fxRateEcbRaw: z.number().optional(), markupPercentageOverEcb: z.number().gte(0).lte(100).optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * FX Exchange Rates for supported payment schemas (Visa, Mastercard) * * POST /v2/{paymentScheme}/fx */ export const createApiV2PaymentSchemeFx = async (args: CreateApiV2PaymentSchemeFxArgs, options: CreateApiV2PaymentSchemeFxOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.auditUser != null) query.append("auditUser", String(args.auditUser)) const url = `${baseUrl.replace(/\/+$/, '')}/v2/${encodeURIComponent(String(args.paymentScheme))}/fx${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getFxRatesV2Response, await res.json()) };