import {z} from 'zod' export type GetApiV2PaymentsPaymentUuidArgs = {paymentUuid: string}; export type GetApiV2PaymentsPaymentUuidOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const currency = z.object({code: z.string().optional(), description: z.string().optional()}); export const orderDetails = z.object({merchantNumber: z.string().optional(), orderId: z.string().optional(), productId: z.string().optional(), customerNumber: z.string().optional(), variableReference: z.string().optional(), comment1: z.string().optional(), comment2: z.string().optional(), merchantCustomerIdentification: z.string().optional(), deliveryAddressLine1: z.string().optional(), deliveryAddressLine2: z.string().optional(), deliveryCity: z.string().optional(), deliveryPostCode: z.string().optional(), deliveryCountry: z.string().optional()}); export const relatedPartyFireAccount = z.object({type: z.literal("FIRE_ACCOUNT").optional(), account: z.object({id: z.number().int().optional(), alias: z.string().optional(), bic: z.string().optional(), iban: z.string().optional(), nsc: z.string().optional(), accountNumber: z.string().optional()}).optional()}); export const to = z.object({type: z.enum(["FIRE_ACCOUNT", "WITHDRAWAL_ACCOUNT"]).optional(), account: relatedPartyFireAccount.optional()}); export const aspsp = z.object({aspspUuid: z.string().optional(), alias: z.string().optional(), logoUrl: z.string().optional(), country: z.object({code: z.string().optional(), description: z.string().optional()}).optional(), currency: z.object({code: z.string().optional(), description: z.string().optional()}).optional(), dateCreated: z.string().optional(), lastUpdated: z.string().optional()}); export const paymentRequestPayment = z.object({type: z.literal("OTHER").optional(), paymentRequestCode: z.string().min(8).max(8).optional(), transactionType: z.enum(["REFUND_REQUEST", "PAYMENT"]).optional(), endToEndId: z.string().optional(), paymentUuid: z.string().optional(), relatedPaymentUuid: z.string().optional(), status: z.enum(["AWAITING_AUTHORISATION", "AUTHORISED", "AWAITING_MULTI_AUTHORISATION", "NOT_AUTHORISED", "SETTLED", "REJECTED", "ACCEPTED", "RECEIVED", "FUNDS_CONFIRMED"]).optional(), reason: z.string().optional(), amountBeforeCharges: z.number().int().optional(), feeAmount: z.number().int().optional(), amountAfterCharges: z.number().int().optional(), currency: currency.optional(), myRef: z.string().optional(), description: z.string().optional(), returnUrl: z.string().optional(), webhookUrl: z.string().optional(), relatedAccountTxnId: z.number().int().optional(), relatedPayeeId: z.number().int().optional(), dateFundsReceived: z.string().optional(), dateCreated: z.string().optional(), lastUpdated: z.string().optional(), orderDetails: orderDetails.optional(), to: to.optional(), bank: aspsp.optional(), domesticPaymentId: z.string().optional(), allowFopReceipt: z.boolean().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 }; /** * Get payment details * * GET /v2/payments/{paymentUuid} */ export const getApiV2PaymentsPaymentUuid = async (args: GetApiV2PaymentsPaymentUuidArgs, options?: GetApiV2PaymentsPaymentUuidOptions) => { const {baseUrl = "https://api.fire.com/business", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/payments/${encodeURIComponent(String(args.paymentUuid))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(paymentRequestPayment, await res.json()) };