import {z} from 'zod' export type Link = {href?: string | undefined, rel?: string | undefined} | null; export type LinkWS = {href?: string | undefined, id?: string | undefined} | null; export type Currency = 'USD' | 'CAD' | 'EUR' | 'JPY' | 'GBP' | 'KRW' | 'CHF' | 'SEK' | 'SGD' | 'MYR' | 'AUD' | 'MXN' | 'INR' | 'BRL' | 'DKK' | 'NZD' | 'NOK' | 'ZAR' | 'PHP' | 'CNY' | 'SAR' | 'GTQ' | 'IDR' | 'ARS' | 'COP' | 'PEN'; export type GatewayResponseWS = {avs?: boolean | undefined, code?: (string | null) | undefined, cvv?: boolean | undefined, message?: (string | null) | undefined}; export type PaymentMethod = 'CREDIT_CARD' | 'PAYPAL' | 'MANUAL' | 'EXTERNAL_INVOICE' | 'MARKETPLACE_CREDITS'; export type PaymentResult = 'SUCCESSFUL' | 'FAILED' | 'GATEWAY_NOT_AVAILABLE' | 'MANUAL'; export type PaymentWS = {links?: Array | undefined, amount?: (number | null) | undefined, company?: LinkWS | undefined, currency?: Currency | undefined, date?: (number | null) | undefined, gatewayResponse?: GatewayResponseWS | undefined, id?: (number | null) | undefined, method?: PaymentMethod | undefined, result?: PaymentResult | undefined, transactionId?: (string | null) | undefined, user?: LinkWS | undefined} | null; export type CreateApiBillingV1InvoicesInvoiceNumberPaymentsArgs = {invoiceNumber: number, body: PaymentWS}; export type CreateApiBillingV1InvoicesInvoiceNumberPaymentsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const link = z.object({href: z.string().optional(), rel: z.string().optional()}).nullable(); export const linkWS = z.object({href: z.string().optional(), id: z.string().optional()}).nullable(); export const currency = z.enum(["USD", "CAD", "EUR", "JPY", "GBP", "KRW", "CHF", "SEK", "SGD", "MYR", "AUD", "MXN", "INR", "BRL", "DKK", "NZD", "NOK", "ZAR", "PHP", "CNY", "SAR", "GTQ", "IDR", "ARS", "COP", "PEN"]); export const gatewayResponseWS = z.object({avs: z.boolean().optional(), code: z.string().nullable().optional(), cvv: z.boolean().optional(), message: z.string().nullable().optional()}); export const paymentMethod = z.enum(["CREDIT_CARD", "PAYPAL", "MANUAL", "EXTERNAL_INVOICE", "MARKETPLACE_CREDITS"]); export const paymentResult = z.enum(["SUCCESSFUL", "FAILED", "GATEWAY_NOT_AVAILABLE", "MANUAL"]); export const paymentWS = z.object({links: z.array(link).optional(), amount: z.number().nullable().optional(), company: linkWS.optional(), currency: currency.optional(), date: z.number().nullable().optional(), gatewayResponse: gatewayResponseWS.optional(), id: z.number().nullable().optional(), method: paymentMethod.optional(), result: paymentResult.optional(), transactionId: z.string().nullable().optional(), user: linkWS.optional()}).nullable(); 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 }; /** * Pay an Invoice * * POST /billing/v1/invoices/{invoiceNumber}/payments */ export const createApiBillingV1InvoicesInvoiceNumberPayments = async (args: CreateApiBillingV1InvoicesInvoiceNumberPaymentsArgs, options?: CreateApiBillingV1InvoicesInvoiceNumberPaymentsOptions) => { const {baseUrl = "https://marketplace.appdirect.com/api", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/billing/v1/invoices/${encodeURIComponent(String(args.invoiceNumber))}/payments${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(paymentWS, await res.json()) };