import {z} from 'zod' export type Card = {payTypeCategories?: (Array<'CARD' | 'PBL'>) | undefined, externalId: string, notifyUrl: string, legalName: string, dbaName: string, address: {street: string, city: string, postalCode: string, countryCode: string}, phone: string, websiteUrl: string, mcc: number, riskClass: 'LOW' | 'NORMAL' | 'HIGH', representatives: Array<{name: string, phone: string}>, legalForm: 'FU' | 'FZ' | 'IB' | 'JST' | 'KOP' | 'PO' | 'PP' | 'PPZ' | 'PRPA' | 'SA' | 'SC' | 'SD' | 'SIK' | 'SJ' | 'SK' | 'SO' | 'SP' | 'SPOL' | 'ST', taxId: string, agreementDate: unknown}; export type CardPbl = {payTypeCategories?: (Array<'CARD' | 'PBL'>) | undefined, externalId: string, notifyUrl: string, legalName: string, dbaName: string, address: {street: string, city: string, postalCode: string, countryCode: string}, phone: string, websiteUrl: string, mcc: number, riskClass: 'LOW' | 'NORMAL' | 'HIGH', representatives: Array<{name: string, phone: string}>, legalForm: 'FU' | 'FZ' | 'IB' | 'JST' | 'KOP' | 'PO' | 'PP' | 'PPZ' | 'PRPA' | 'SA' | 'SC' | 'SD' | 'SIK' | 'SJ' | 'SK' | 'SO' | 'SP' | 'SPOL' | 'ST', taxId: string, agreementDate: unknown}; export type Pbl = {payTypeCategories?: (Array<'CARD' | 'PBL'>) | undefined, externalId: string, notifyUrl: string, legalName: string, dbaName: string, address: {street: string, city: string, postalCode: string, countryCode: string}, phone?: string | undefined, websiteUrl: string, mcc: number, riskClass?: ('LOW' | 'NORMAL' | 'HIGH') | undefined, representatives?: Array<{name: string, phone: string}> | undefined, legalForm?: ('FU' | 'FZ' | 'IB' | 'JST' | 'KOP' | 'PO' | 'PP' | 'PPZ' | 'PRPA' | 'SA' | 'SC' | 'SD' | 'SIK' | 'SJ' | 'SK' | 'SO' | 'SP' | 'SPOL' | 'ST') | undefined, taxId: string, agreementDate?: unknown | undefined}; export type CreateApiApiV21FirmsFirmPublicIdSubmerchantsArgs = {firmPublicId: string, 'Content-Type': string, Authorization: string, body: Card | CardPbl | Pbl}; export type CreateApiApiV21FirmsFirmPublicIdSubmerchantsOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiV21FirmsFirmPublicIdSubmerchantsResponse = z.object({id: z.string().optional(), externalId: z.string().optional(), status: z.enum(["NOT_ACTIVE", "ACTIVE", "BLOCKED"]).optional(), lastUpdated: z.string().optional(), notifyUrl: z.string().optional(), legalName: z.string().optional(), dbaName: z.string().optional(), address: z.object({street: z.string().optional(), city: z.string().optional(), postalCode: z.string().optional(), countryCode: z.string().optional()}).optional(), phone: z.string().optional(), websiteUrl: z.string().optional(), mcc: z.number().optional(), riskClass: z.enum(["LOW", "NORMAL", "HIGH"]).optional(), taxId: z.string().optional(), representatives: z.array(z.object({name: z.string().optional(), phone: z.string().optional()})).optional(), legalForm: z.string().optional(), agreementDate: z.string().optional(), payTypeCategories: z.array(z.string()).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 a Submerchant * * POST /api/v2_1/firms/{firmPublicId}/submerchants */ export const createApiApiV21FirmsFirmPublicIdSubmerchants = async (args: CreateApiApiV21FirmsFirmPublicIdSubmerchantsArgs, options?: CreateApiApiV21FirmsFirmPublicIdSubmerchantsOptions) => { const {baseUrl = "https://secure.payu.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['Content-Type'] != null) headers.set("Content-Type", String(args['Content-Type'])) if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2_1/firms/${encodeURIComponent(String(args.firmPublicId))}/submerchants${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(createApiApiV21FirmsFirmPublicIdSubmerchantsResponse, await res.json()) };