import {z} from 'zod' export type GetApiCreditorAgentsIcaCreditorsCreditorIdArgs = {ica: string, creditor_id: string}; export type GetApiCreditorAgentsIcaCreditorsCreditorIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const creditorLegalAddress = z.object({addressLine: z.string().max(70), country: z.string().min(2).max(2), countrySubDivision: z.string().max(35).optional(), postCode: z.string().optional(), townName: z.string().max(50).optional()}); export const organisationIdentification = z.object({identification: z.string().min(1).max(15), type: z.string()}); export const gatewayIdentification = z.object({merchantId: z.string().min(1).max(15).nullable().optional(), terminalIds: z.array(z.string().min(3).max(8)).optional(), gatewayName: z.string().min(1).max(64).optional(), bankMerchantId: z.string().min(1).max(16).optional(), acquirerBank: z.string().min(1).max(100).optional(), acquirerId: z.string().min(1).max(50).optional(), cardBrands: z.array(z.enum(["MASTERCARD", "VISA", "AMEX", "DINERS_CLUB", "JCB", "DISCOVER", "UNION_PAY", "MAESTRO"])).optional(), currencies: z.array(z.string().min(3).max(3)).optional(), pennyTxnTokenRefId: z.string().min(36).max(36).optional(), attributes: z.array(z.string().max(2048)).optional()}); export const payToAddress = z.object({addressLine: z.string().max(70), country: z.string().min(2).max(2), countrySubDivision: z.string().max(35), postCode: z.string(), townName: z.string().max(50)}); export const creditorProfile = z.object({isActive: z.boolean().optional(), email: z.string().min(6).max(255).optional(), gatewayIdentification: gatewayIdentification.optional(), payToAddress: payToAddress.optional(), paymentMethods: z.array(z.string()), creditorProfileId: z.string(), paymentProcessor: z.string().max(20).optional()}); export const creditorRegistration = z.object({dba: z.string().optional(), id: z.string().min(36).max(36).optional(), inactiveReason: z.string().optional(), isActive: z.boolean().optional(), legalBusinessAddress: creditorLegalAddress.optional(), legalBusinessName: z.string().optional(), organisationIdentifications: z.array(organisationIdentification).optional(), paymentProfiles: z.array(creditorProfile).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 }; /** * Retrieve a Creditor Registration * * GET /creditor-agents/{ica}/creditors/{creditor_id} */ export const getApiCreditorAgentsIcaCreditorsCreditorId = async (args: GetApiCreditorAgentsIcaCreditorsCreditorIdArgs, options?: GetApiCreditorAgentsIcaCreditorsCreditorIdOptions) => { 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))}/creditors/${encodeURIComponent(String(args.creditor_id))}${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(creditorRegistration, await res.json()) };