import {z} from 'zod' export type CartParameters = {sellerId: string, sellerCategory: string, name?: string | undefined, description?: string | undefined, quantity?: number | undefined, price?: number | undefined, number?: string | undefined}; export type TransactionSplitPayment = {merchantId: number, posId: number, sessionId: string, amount: number, currency: string, description: string, email: string, client?: string | undefined, address?: string | undefined, zip?: string | undefined, city?: string | undefined, country: string, phone?: string | undefined, language: string, method?: number | undefined, urlReturn: string, urlStatus?: string | undefined, timeLimit?: number | undefined, channel?: number | undefined, shipping?: number | undefined, transferLabel?: string | undefined, sdkVersion?: string | undefined, sign: string, encoding?: string | undefined, cart?: Array | undefined, methodRefId?: string | undefined, splitPaymentDetails: {vatAmount: number, invoiceNumber: string, nip: string, iban?: string | undefined}}; export type CreateApiApiV1TransactionRegisterSplitpaymentArgs = {body: TransactionSplitPayment}; export type CreateApiApiV1TransactionRegisterSplitpaymentOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const transactionRegistrationResponse = z.object({data: z.object({token: z.string().optional()}).optional(), responseCode: z.number().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 }; /** * Split Payment * * POST /api/v1/transaction/register/splitpayment */ export const createApiApiV1TransactionRegisterSplitpayment = async (args: CreateApiApiV1TransactionRegisterSplitpaymentArgs, options?: CreateApiApiV1TransactionRegisterSplitpaymentOptions) => { const {baseUrl = "https://sandbox.przelewy24.pl", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/transaction/register/splitpayment${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(transactionRegistrationResponse, await res.json()) };