import {z} from 'zod' export type CustomerAddress = {zip_code: string, line_1: string, line_2: string | null, state: string, city: string, metadata?: (Record | null) | undefined}; export type ZpyEnterpriseResourcesDomainPaymentOrderModelValueObjectsOrderValueObjectsCustomer = {name: string, email?: (string | null) | undefined, phone?: (string | null) | undefined, document?: (string | null) | undefined, birthdate?: (string | null) | undefined, address?: (CustomerAddress | null) | undefined, external_id?: (string | null) | undefined, metadata?: (Record | null) | undefined}; export type CardBillingAddress = {zip_code: string, line_1?: (string | null) | undefined, line_2?: (string | null) | undefined, state: string, city: string}; export type ZpyEnterpriseResourcesDomainPaymentTransactionModelValueObjectsCardValueObjectsCard = {number?: (string | null) | undefined, holder_name?: (string | null) | undefined, holder_document?: (string | null) | undefined, exp_month?: (number | null) | undefined, exp_year?: (number | null) | undefined, cvv?: (string | null) | undefined, brand?: (string | null) | undefined, billing_address?: (CardBillingAddress | null) | undefined, card_hash?: (string | null) | undefined}; export type SubscriptionItemDTO = {product_id: string, amount: number | string, description?: string | undefined, type?: string | undefined, status?: string | undefined, metadata?: Record | undefined}; export type FirstPaymentDTOInput = {amount?: (number | string | null) | undefined, due_date?: (string | null) | undefined}; export type PaymentMethodParamsDTO = {acceptance_deadline?: (string | null) | undefined, type?: (string | null) | undefined}; export type SubscriptionCreationDTO = {plan_id?: (string | null) | undefined, customer?: (ZpyEnterpriseResourcesDomainPaymentOrderModelValueObjectsOrderValueObjectsCustomer | null) | undefined, customer_id?: (string | null) | undefined, installments?: number | undefined, card?: (ZpyEnterpriseResourcesDomainPaymentTransactionModelValueObjectsCardValueObjectsCard | null) | undefined, card_token?: (string | null) | undefined, payment_method: string, external_id?: (string | null) | undefined, name?: (string | null) | undefined, interval?: (string | null) | undefined, interval_count?: (number | null) | undefined, interval_free?: (number | null) | undefined, billing_type?: (string | null) | undefined, items?: (Array | null) | undefined, metadata?: (Record | null) | undefined, first_payment?: (FirstPaymentDTOInput | null) | undefined, payment_method_params?: (PaymentMethodParamsDTO | null) | undefined}; export type CreateApiRecurrenceSubscriptionsArgs = {body: SubscriptionCreationDTO}; export type CreateApiRecurrenceSubscriptionsOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const subscriptionCurrentCycleDTOOutput = z.object({start_at: z.string(), end_at: z.string().nullable().optional()}); export const customerAddress = z.object({zip_code: z.string(), line_1: z.string(), line_2: z.string().nullable(), state: z.string(), city: z.string(), metadata: z.object({}).nullable().optional()}); export const zpyEnterpriseResourcesDomainPaymentOrderModelValueObjectsOrderValueObjectsCustomer = z.object({name: z.string(), email: z.string().nullable().optional(), phone: z.string().nullable().optional(), document: z.string().nullable().optional(), birthdate: z.string().nullable().optional(), address: customerAddress.nullable().optional(), external_id: z.string().nullable().optional(), metadata: z.object({}).nullable().optional()}); export const zpyEnterpriseResourcesDomainPaymentOrderDtosOrderResponseDtosAddressResponseDTO = z.object({zip_code: z.string(), state: z.string(), city: z.string(), line_1: z.string().nullable().optional(), line_2: z.string().nullable().optional(), metadata: z.object({}).optional()}); export const zpyEnterpriseResourcesDomainPaymentOrderDtosOrderResponseDtosCardResponseDTO = z.object({last_four_digits: z.union([z.string(), z.number().int()]).nullable(), brand: z.string().nullable(), holder_name: z.string().nullable(), exp_month: z.number().int().nullable(), exp_year: z.number().int().nullable(), status: z.string().nullable().optional(), billing_address: zpyEnterpriseResourcesDomainPaymentOrderDtosOrderResponseDtosAddressResponseDTO, type: z.string().nullable().optional(), created_at: z.string().optional(), updated_at: z.string().optional()}); export const planItemCreatedDTO = z.object({product_id: z.string(), amount: z.string(), description: z.string().optional(), type: z.string().optional(), status: z.string().optional(), metadata: z.object({}).optional()}); export const firstPaymentDTOOutput = z.object({amount: z.string().nullable().optional(), due_date: z.string().nullable().optional()}); export const paymentMethodParamsDTO = z.object({acceptance_deadline: z.string().nullable().optional(), type: z.string().nullable().optional()}); export const paymentMethodDataDTO = z.object({qr_code_url: z.string().nullable().optional(), qr_code_data: z.string().nullable().optional(), expiration_date: z.string().nullable().optional()}); export const subscriptionCreatedDTO = z.object({subscription_id: z.string(), payment_method: z.string(), interval_count: z.number().int(), plan_id: z.string().nullable().optional(), current_cycle: subscriptionCurrentCycleDTOOutput, billing_type: z.string(), next_billing_at: z.string().optional(), installments: z.number().int().nullable().optional(), customer: zpyEnterpriseResourcesDomainPaymentOrderModelValueObjectsOrderValueObjectsCustomer.nullable().optional(), card: zpyEnterpriseResourcesDomainPaymentOrderDtosOrderResponseDtosCardResponseDTO.nullable().optional(), external_id: z.string().nullable().optional(), items: z.array(planItemCreatedDTO), status: z.string().optional(), created_at: z.string(), updated_at: z.string(), metadata: z.object({}).nullable().optional(), first_payment: firstPaymentDTOOutput.nullable().optional(), payment_method_params: paymentMethodParamsDTO.nullable().optional(), payment_method_data: paymentMethodDataDTO.nullable().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 }; /** * Criar assinatura a partir de um Plano * * POST /recurrence/subscriptions/ */ export const createApiRecurrenceSubscriptions = async (args: CreateApiRecurrenceSubscriptionsArgs, options?: CreateApiRecurrenceSubscriptionsOptions) => { const {baseUrl = "https://api.b2b.usezapay.com.br/v2", 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(/\/+$/, '')}/recurrence/subscriptions/${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(subscriptionCreatedDTO, await res.json()) };