import {z} from 'zod' export type StandardPayout = {shopId: string, payout?: ({description?: string | undefined, extPayoutId: string, amount: number, additionalVariables?: unknown | undefined}) | undefined}; export type Payout = {description?: string | undefined, extPayoutId: string, amount: unknown, additionalVariables?: unknown | undefined, foreign?: string | undefined}; export type Account = {accountNumber: string, bankName?: string | undefined, swiftCode?: string | undefined}; export type CustomerAddress = {name: string, countryCode?: string | undefined, city?: string | undefined, postalCode?: string | undefined, street?: string | undefined}; export type BankAddress = {street?: string | undefined, postalCode?: string | undefined, city?: string | undefined, countryCode?: string | undefined}; export type BankAccountPayout = {shopId: string, payout: Payout, account: Account, customerAddress: CustomerAddress, bankAddress?: BankAddress | undefined}; export type Payee = {extCustomerId: string, accountCreationDate: unknown, phoneNumber?: string | undefined, email: string}; export type CardForPayout = {number: string, expirationMonth: string, expirationYear: string}; export type SubmerchantForCardPayout = {id: string}; export type CardPayout = {shopId: string, payout: Payout, payee: Payee, customerAddress: CustomerAddress, cardToken?: string | undefined, card?: CardForPayout | undefined, submerchant?: SubmerchantForCardPayout | undefined}; export type MarketplacePayout = {shopId: string, account: {extCustomerId: string}, payout: {description?: string | undefined, extPayoutId: string, amount: unknown, currencyCode: unknown}}; export type FxData = {partnerId: string, currencyCode: 'PLN' | 'EUR' | 'CZK', amount: unknown, rate: unknown, tableId: string}; export type FxPayout = {shopId: string, account: {extCustomerId: string}, payout: {description?: string | undefined, extPayoutId: string, amount: unknown, currencyCode: unknown}, fxData: FxData}; export type CreateApiApiV21PayoutsArgs = {'Content-Type': string, Authorization: string, body: StandardPayout | BankAccountPayout | CardPayout | MarketplacePayout | FxPayout}; export type CreateApiApiV21PayoutsOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiV21PayoutsResponse = z.object({payout: z.object({payoutId: z.string().optional(), extPayoutId: z.string().optional(), extCustomerId: z.string().optional(), amount: z.unknown(), currencyCode: z.string().optional(), status: z.enum(["PENDING", "WAITING", "CANCELLED", "REALIZED"]).optional(), fxData: z.object({currencyCode: z.string().optional(), amount: z.unknown(), rate: z.unknown(), partnerId: z.string().optional(), tableId: z.string().optional()}).optional()}).optional(), status: z.object({statusCode: z.string().optional()}).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 Payout * * POST /api/v2_1/payouts */ export const createApiApiV21Payouts = async (args: CreateApiApiV21PayoutsArgs, options?: CreateApiApiV21PayoutsOptions) => { 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/payouts${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(createApiApiV21PayoutsResponse, await res.json()) };