import {z} from 'zod' export type GetApiV1PaymentsPaymentIdArgs = {paymentId: string}; export type GetApiV1PaymentsPaymentIdOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const postLink = z.object({href: z.string().optional(), method: z.string().optional(), params: z.record(z.string(), z.object({})).optional(), type: z.string().optional()}); export const link = z.object({href: z.string().optional(), method: z.string().optional()}); export const paymentLinks = z.object({auth_url_post: postLink.optional(), cancel: postLink.optional(), capture: postLink.optional(), events: link.optional(), next_url: link.optional(), next_url_post: postLink.optional(), refunds: link.optional(), self: link.optional()}); export const threeDSecure = z.object({required: z.boolean().optional()}); export const authorisationSummary = z.object({three_d_secure: threeDSecure.optional()}); export const address = z.object({city: z.string().max(255).optional(), country: z.string().optional(), line1: z.string().max(255).optional(), line2: z.string().max(255).optional(), postcode: z.string().max(25).optional()}); export const cardDetails = z.object({billing_address: address.optional(), card_brand: z.string().optional(), card_type: z.enum(["debit", "credit", "null"]).optional(), cardholder_name: z.string().optional(), expiry_date: z.string().optional(), first_digits_card_number: z.string().optional(), last_digits_card_number: z.string().optional(), wallet_type: z.enum(["Apple Pay", "Google Pay"]).optional()}); export const outcome = z.object({result: z.string().optional()}); export const exemption = z.object({outcome: outcome.optional(), requested: z.boolean().optional(), type: z.string().optional()}); export const externalMetadata = z.object({metadata: z.record(z.string(), z.object({})).optional()}); export const refundSummary = z.object({amount_available: z.number().int().optional(), amount_submitted: z.number().int().optional(), status: z.string().optional()}); export const paymentSettlementSummary = z.object({capture_submit_time: z.string().optional(), captured_date: z.string().optional(), settled_date: z.string().optional()}); export const paymentState = z.object({can_retry: z.boolean().nullable().optional(), code: z.string().optional(), finished: z.boolean().optional(), message: z.string().optional(), status: z.string().optional()}); export const paymentWithAllLinks = z.object({_links: paymentLinks.optional(), agreement_payment_type: z.enum(["instalment", "recurring", "unscheduled"]).optional(), amount: z.number().int().optional(), authorisation_mode: z.enum(["web", "moto_api", "external"]).optional(), authorisation_summary: authorisationSummary.optional(), card_brand: z.string().optional(), card_details: cardDetails.optional(), corporate_card_surcharge: z.number().int().optional(), created_date: z.string().optional(), delayed_capture: z.boolean().optional(), description: z.string().optional(), email: z.string().optional(), exemption: exemption.optional(), fee: z.number().int().optional(), language: z.enum(["en", "cy"]).optional(), metadata: externalMetadata.optional(), moto: z.boolean().optional(), net_amount: z.number().int().optional(), payment_id: z.string().optional(), payment_provider: z.string().optional(), provider_id: z.string().optional(), reference: z.string().optional(), refund_summary: refundSummary.optional(), return_url: z.string().optional(), settlement_summary: paymentSettlementSummary.optional(), state: paymentState.optional(), total_amount: z.number().int().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 information about a single payment * * GET /v1/payments/{paymentId} */ export const getApiV1PaymentsPaymentId = async (args: GetApiV1PaymentsPaymentIdArgs, options?: GetApiV1PaymentsPaymentIdOptions) => { const {baseUrl = "https://publicapi.payments.service.gov.uk", 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(/\/+$/, '')}/v1/payments/${encodeURIComponent(String(args.paymentId))}${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(paymentWithAllLinks, await res.json()) };