import {z} from 'zod' export type PaymentSourceTypeVersion4 = 'BankAccount' | 'Card'; export type PayableFundingAmountResourceRepresentationVersion4 = {currencyCode: string, amountInMinorUnits: number, isEstimated: boolean}; export type FailureInsightsResolutionResourceRepresentationVersion4 = {$type: string}; export type FailureInsightsSuggestionResourceRepresentationVersion4 = {$type: string}; export type PayableReversedAmountResourceRepresentationVersion4 = {currencyCode?: string | undefined, amountInMinorUnits: number}; export type ExternalPayableStatusVersion4 = 'Unknown' | 'Open' | 'UserInitiationInProgress' | 'AwaitingMultiAuth' | 'BankInitiating' | 'WaitingForCustomer' | 'TransfersInProgress' | 'Processing' | 'Cancelling' | 'Reversing' | 'Processed' | 'Cancelled' | 'Reversed' | 'Draft'; export type PayableResourceRepresentationVersion4 = {$type: 'Payable' | 'FeePayable', payableId?: string | undefined, fundingAmount?: PayableFundingAmountResourceRepresentationVersion4 | undefined, reference?: string | undefined, isLowPriority?: boolean | undefined, failureInsights?: (FailureInsightsResolutionResourceRepresentationVersion4 | FailureInsightsSuggestionResourceRepresentationVersion4 | null) | undefined, reversedAmount?: PayableReversedAmountResourceRepresentationVersion4 | undefined, status?: ExternalPayableStatusVersion4 | undefined, paymentSourceType?: PaymentSourceTypeVersion4 | undefined, paymentSourceId?: (string | null) | undefined}; export type FeePayableResourceRepresentationVersion4 = {$type: 'Payable' | 'FeePayable', payableId?: string | undefined, fundingAmount?: PayableFundingAmountResourceRepresentationVersion4 | undefined, reference?: string | undefined, isLowPriority?: boolean | undefined, failureInsights?: (FailureInsightsResolutionResourceRepresentationVersion4 | FailureInsightsSuggestionResourceRepresentationVersion4 | null) | undefined, reversedAmount?: PayableReversedAmountResourceRepresentationVersion4 | undefined, status?: ExternalPayableStatusVersion4 | undefined, paymentSourceType?: PaymentSourceTypeVersion4 | undefined, paymentSourceId?: (string | null) | undefined}; export type ExternalPayableGroupStatusVersion4 = 'Unknown' | 'Open' | 'Processed' | 'UserInitiationInProgress' | 'Processing' | 'Cancelling' | 'Draft'; export type PayableIdentifiersResourceRepresentationVersion4 = {payableId?: string | undefined, partnerEntityId: string}; export type PayableGroupResourceRepresentationVersion4 = {groupId?: string | undefined, status?: ExternalPayableGroupStatusVersion4 | undefined, partnerEntityId: string, partnerPayableIds: Array, reference: string}; export type ExternalPayableBatchStatusVersion4 = 'Unknown' | 'Open' | 'Processed' | 'UserInitiationInProgress' | 'Processing' | 'Cancelling' | 'Draft'; export type GroupIdentifiersResourceRepresentationVersion4 = {groupId: string, partnerEntityId: string | null}; export type BatchResourceRepresentationVersion4 = {batchId: string, paymentOrder: number, status: ExternalPayableBatchStatusVersion4, groups: Array, payables: Array}; export type ExternalPayRunStatusVersion4 = 'Unknown' | 'Open' | 'UserInitiationInProgress' | 'Processing' | 'Cancelling' | 'Processed' | 'Concluded' | 'Cancelled' | 'Draft'; export type PayRunMetadataResourceRepresentationVersion4 = {key: string, value: string}; export type PayRunResourceRepresentationVersion4 = {payRunId?: string | undefined, organisationId?: string | undefined, partnerEntityId: string, scheduledExecutionDate?: (string | null) | undefined, payerBankAccountId?: (string | null) | undefined, paymentSourceType?: PaymentSourceTypeVersion4 | undefined, paymentSourceId?: (string | null) | undefined, payables: Array, groups?: (Array | null) | undefined, batches?: Array | undefined, status?: ExternalPayRunStatusVersion4 | undefined, checkoutUri?: string | undefined, metadata?: Array | undefined}; export type CreateApiOrganisationsOrganisationIdPayRunsArgs = {organisationId: string, 'Crezco-Version'?: string | undefined, body: PayRunResourceRepresentationVersion4}; export type CreateApiOrganisationsOrganisationIdPayRunsOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const paymentSourceTypeVersion4 = z.enum(["BankAccount", "Card"]); export const payableFundingAmountResourceRepresentationVersion4 = z.object({currencyCode: z.string(), amountInMinorUnits: z.number().int(), isEstimated: z.boolean()}); export const failureInsightsResolutionResourceRepresentationVersion4 = z.object({$type: z.string()}); export const failureInsightsSuggestionResourceRepresentationVersion4 = z.object({$type: z.string()}); export const payableReversedAmountResourceRepresentationVersion4 = z.object({currencyCode: z.string().optional(), amountInMinorUnits: z.number().int()}); export const externalPayableStatusVersion4 = z.enum(["Unknown", "Open", "UserInitiationInProgress", "AwaitingMultiAuth", "BankInitiating", "WaitingForCustomer", "TransfersInProgress", "Processing", "Cancelling", "Reversing", "Processed", "Cancelled", "Reversed", "Draft"]); export const payableResourceRepresentationVersion4 = z.object({$type: z.enum(["Payable", "FeePayable"]), payableId: z.string().optional(), fundingAmount: payableFundingAmountResourceRepresentationVersion4.optional(), reference: z.string().optional(), isLowPriority: z.boolean().optional(), failureInsights: z.union([failureInsightsResolutionResourceRepresentationVersion4, failureInsightsSuggestionResourceRepresentationVersion4]).nullable().optional(), reversedAmount: payableReversedAmountResourceRepresentationVersion4.optional(), status: externalPayableStatusVersion4.optional(), paymentSourceType: paymentSourceTypeVersion4.optional(), paymentSourceId: z.string().nullable().optional()}); export const feePayableResourceRepresentationVersion4 = z.object({$type: z.enum(["Payable", "FeePayable"]), payableId: z.string().optional(), fundingAmount: payableFundingAmountResourceRepresentationVersion4.optional(), reference: z.string().optional(), isLowPriority: z.boolean().optional(), failureInsights: z.union([failureInsightsResolutionResourceRepresentationVersion4, failureInsightsSuggestionResourceRepresentationVersion4]).nullable().optional(), reversedAmount: payableReversedAmountResourceRepresentationVersion4.optional(), status: externalPayableStatusVersion4.optional(), paymentSourceType: paymentSourceTypeVersion4.optional(), paymentSourceId: z.string().nullable().optional()}); export const externalPayableGroupStatusVersion4 = z.enum(["Unknown", "Open", "Processed", "UserInitiationInProgress", "Processing", "Cancelling", "Draft"]); export const payableIdentifiersResourceRepresentationVersion4 = z.object({payableId: z.string().optional(), partnerEntityId: z.string().min(1).max(50)}); export const payableGroupResourceRepresentationVersion4 = z.object({groupId: z.string().optional(), status: externalPayableGroupStatusVersion4.optional(), partnerEntityId: z.string().min(1).max(50), partnerPayableIds: z.array(payableIdentifiersResourceRepresentationVersion4), reference: z.string().min(1).max(18)}); export const externalPayableBatchStatusVersion4 = z.enum(["Unknown", "Open", "Processed", "UserInitiationInProgress", "Processing", "Cancelling", "Draft"]); export const groupIdentifiersResourceRepresentationVersion4 = z.object({groupId: z.string(), partnerEntityId: z.string().nullable()}); export const batchResourceRepresentationVersion4 = z.object({batchId: z.string(), paymentOrder: z.number().int(), status: externalPayableBatchStatusVersion4, groups: z.array(groupIdentifiersResourceRepresentationVersion4), payables: z.array(payableIdentifiersResourceRepresentationVersion4)}); export const externalPayRunStatusVersion4 = z.enum(["Unknown", "Open", "UserInitiationInProgress", "Processing", "Cancelling", "Processed", "Concluded", "Cancelled", "Draft"]); export const payRunMetadataResourceRepresentationVersion4 = z.object({key: z.string().min(1).max(100), value: z.string().min(1).max(100)}); export const payRunResourceRepresentationVersion4 = z.object({payRunId: z.string().optional(), organisationId: z.string().optional(), partnerEntityId: z.string().min(1).max(50), scheduledExecutionDate: z.string().nullable().optional(), payerBankAccountId: z.string().nullable().optional(), paymentSourceType: paymentSourceTypeVersion4.optional(), paymentSourceId: z.string().nullable().optional(), payables: z.array(z.union([payableResourceRepresentationVersion4, feePayableResourceRepresentationVersion4])), groups: z.array(payableGroupResourceRepresentationVersion4).nullable().optional(), batches: z.array(batchResourceRepresentationVersion4).optional(), status: externalPayRunStatusVersion4.optional(), checkoutUri: z.string().optional(), metadata: z.array(payRunMetadataResourceRepresentationVersion4).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 }; /** * Create pay run * * POST /organisations/{organisationId}/pay-runs */ export const createApiOrganisationsOrganisationIdPayRuns = async (args: CreateApiOrganisationsOrganisationIdPayRunsArgs, options?: CreateApiOrganisationsOrganisationIdPayRunsOptions) => { const {baseUrl = "https://api.partners.uat.crezco.com", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Crezco-Version'] != null) headers.set("Crezco-Version", String(args['Crezco-Version'])) if (Bearer !== undefined) headers.set("Authorization", Bearer) const url = `${baseUrl.replace(/\/+$/, '')}/organisations/${encodeURIComponent(String(args.organisationId))}/pay-runs${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(payRunResourceRepresentationVersion4, await res.json()) };