import {z} from 'zod' export type GetApiCreditorAgentsIcaPaymentProfilesCreditorProfileIdPaymentControlsArgs = {ica: string, creditor_profile_id: string}; export type GetApiCreditorAgentsIcaPaymentProfilesCreditorProfileIdPaymentControlsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const paymentControlChargesInformation = z.object({flatAmount: z.string().nullable().optional(), percentageAmount: z.string().optional()}); export const paymentControlInstructedAmount = z.object({max: z.string().optional(), min: z.string().optional()}); export const time = z.object({maxDaysAfterRelatedDate: z.string().optional()}); export const paymentControlReferredDocumentInformation = z.object({time: time, type: z.string().max(4)}); export const excludeOrganisationIdentification = z.object({identification: z.string().optional(), type: z.string().optional()}); export const excludeDebtors = z.object({organisationIdentification: excludeOrganisationIdentification.optional()}); export const includeOrganisationIdentification = z.object({identification: z.string().optional(), type: z.string().optional()}); export const includeDebtors = z.object({organisationIdentification: includeOrganisationIdentification.optional()}); export const relationship = z.object({excludeDebtors: z.array(excludeDebtors).optional(), includeDebtors: z.array(includeDebtors).optional()}); export const conditions = z.object({chargesInformation: paymentControlChargesInformation.optional(), instructedAmount: paymentControlInstructedAmount.optional(), referredDocumentInformation: z.array(paymentControlReferredDocumentInformation).optional(), relationship: relationship.optional()}); export const creditorPaymentControl = z.object({conditions: conditions.optional(), decision: z.string(), paymentControlId: z.string().min(36).max(36).optional(), paymentMethod: z.string(), creditorId: z.string().optional()}); export const getAllCreditorPaymentControls = z.object({paymentControls: z.array(creditorPaymentControl).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 All Creditor Payment Controls * * GET /creditor-agents/{ica}/payment-profiles/{creditor_profile_id}/payment-controls */ export const getApiCreditorAgentsIcaPaymentProfilesCreditorProfileIdPaymentControls = async (args: GetApiCreditorAgentsIcaPaymentProfilesCreditorProfileIdPaymentControlsArgs, options?: GetApiCreditorAgentsIcaPaymentProfilesCreditorProfileIdPaymentControlsOptions) => { const {baseUrl = "https://api.mastercard.com/commercial/payments", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/creditor-agents/${encodeURIComponent(String(args.ica))}/payment-profiles/${encodeURIComponent(String(args.creditor_profile_id))}/payment-controls${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(getAllCreditorPaymentControls, await res.json()) };