import {z} from 'zod' export type CreditNoteFilterStatus = 'DRAFT' | 'FINALIZED' | 'VOIDED'; export type CreditNoteStatus = 'AVAILABLE' | 'CONSUMED' | 'VOIDED'; export type RefundStatus = 'PENDING' | 'SUCCEEDED' | 'FAILED'; export type CreditNoteReason = 'DUPLICATED_CHARGE' | 'PRODUCT_UNSATISFACTORY' | 'ORDER_CHANGE' | 'ORDER_CANCELLATION' | 'FRAUDULENT_CHARGE' | 'OTHER'; export type DateTimeWithZone = string; export type GetApiCreditNotesArgs = {per_page?: number | undefined, page?: number | undefined, external_customer_id?: string | undefined, invoice_id?: string | undefined, status?: CreditNoteFilterStatus | undefined, credit_status?: CreditNoteStatus | undefined, refund_status?: RefundStatus | undefined, reason?: CreditNoteReason | undefined, id?: string | undefined, number?: string | undefined, issuing_date_from?: DateTimeWithZone | undefined, issuing_date_to?: DateTimeWithZone | undefined, created_at_from?: DateTimeWithZone | undefined, created_at_to?: DateTimeWithZone | undefined, updated_at_from?: DateTimeWithZone | undefined, updated_at_to?: DateTimeWithZone | undefined}; export type GetApiCreditNotesOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const billingEntityCode = z.string(); export const date = z.string(); export const creditNoteStatus = z.enum(["AVAILABLE", "CONSUMED", "VOIDED"]); export const refundStatus = z.enum(["PENDING", "SUCCEEDED", "FAILED"]); export const creditNoteReason = z.enum(["DUPLICATED_CHARGE", "PRODUCT_UNSATISFACTORY", "ORDER_CHANGE", "ORDER_CANCELLATION", "FRAUDULENT_CHARGE", "OTHER"]); export const currency = z.string().min(3).max(3); export const amount = z.object({value: z.number().gte(0), currency_code: currency}); export const dateTimeWithZone = z.string(); export const feeObject = z.object({id: z.string().nullable().optional(), total_amount: amount.optional(), name: z.string().nullable().optional()}); export const creditNoteItemObject = z.object({id: z.string(), amount: amount, fee: feeObject}); export const creditNoteAppliedTaxObject = z.object({id: z.string().optional(), tax_id: z.string().optional(), tax_name: z.string().optional(), tax_code: z.string().optional(), tax_rate: z.number().optional(), tax_description: z.string().optional(), amount: amount.optional(), created_at: dateTimeWithZone.optional()}); export const creditNoteWithId = z.object({billing_entity_code: billingEntityCode, sequential_id: z.number().int(), number: z.string(), invoice_id: z.string(), invoice_number: z.string(), issuing_date: date, credit_status: creditNoteStatus.optional(), refund_status: refundStatus.optional(), reason: creditNoteReason, description: z.string().nullable().optional(), total_amount: amount, taxes_amount: amount, taxes_rate: z.number(), sub_total_excluding_taxes_amount: amount, balance_amount: amount, credit_amount: amount, refund_amount: amount, created_at: dateTimeWithZone, updated_at: dateTimeWithZone, items: z.array(creditNoteItemObject).optional(), applied_taxes: z.array(creditNoteAppliedTaxObject).optional(), id: z.string().optional()}); export const paginationMetadata = z.object({total_count: z.number().int().optional(), total_pages: z.number().int().optional(), current_page: z.number().int().optional()}); export const getApiCreditNotesResponse = z.object({credit_notes: z.array(creditNoteWithId).optional(), metadata: paginationMetadata.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 }; /** * Get all credit notes * * GET /credit-notes */ export const getApiCreditNotes = async (args: GetApiCreditNotesArgs, options?: GetApiCreditNotesOptions) => { const {baseUrl = "https://api-m.sandbox.paypal.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.page != null) query.append("page", String(args.page)) if (args.external_customer_id != null) query.append("external_customer_id", String(args.external_customer_id)) if (args.invoice_id != null) query.append("invoice_id", String(args.invoice_id)) if (args.status != null) query.append("status", String(args.status)) if (args.credit_status != null) query.append("credit_status", String(args.credit_status)) if (args.refund_status != null) query.append("refund_status", String(args.refund_status)) if (args.reason != null) query.append("reason", String(args.reason)) if (args.id != null) query.append("id", String(args.id)) if (args.number != null) query.append("number", String(args.number)) if (args.issuing_date_from != null) query.append("issuing_date_from", String(args.issuing_date_from)) if (args.issuing_date_to != null) query.append("issuing_date_to", String(args.issuing_date_to)) if (args.created_at_from != null) query.append("created_at_from", String(args.created_at_from)) if (args.created_at_to != null) query.append("created_at_to", String(args.created_at_to)) if (args.updated_at_from != null) query.append("updated_at_from", String(args.updated_at_from)) if (args.updated_at_to != null) query.append("updated_at_to", String(args.updated_at_to)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/credit-notes${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiCreditNotesResponse, await res.json()) };