import {z} from 'zod' export type BillingCycle = 'WEEKLY' | 'MONTHLY' | 'QUARTERLY' | 'YEARLY'; export type Currency = string; export type Amount = {value: number, currency_code: Currency}; export type TaxCodes = Array; export type AmountNoCurrency = {value: number}; export type MinimumCommitmentRequest = {amount: AmountNoCurrency, invoice_display_name?: string | undefined, tax_codes?: TaxCodes | undefined}; export type ChargeModel = 'STANDARD' | 'GRADUATED' | 'PACKAGE' | 'PERCENTAGE' | 'VOLUME'; export type UsageBasedChargeUpdateRequest = {id: string, metric_id: string, charge_model?: ChargeModel | undefined, tax_codes?: TaxCodes | undefined, properties?: Record | undefined, min_amount?: AmountNoCurrency | undefined}; export type PlanUpdateRequest = {name?: string | undefined, code?: string | undefined, billing_cycle?: BillingCycle | undefined, description?: string | undefined, amount?: Amount | undefined, trial_period?: number | undefined, pay_in_advance?: boolean | undefined, tax_codes?: TaxCodes | undefined, minimum_commitment?: MinimumCommitmentRequest | undefined, cascading_updates?: boolean | undefined, usage_based_charges?: Array | undefined}; export type UpdateApiPlansPlanCodeArgs = {plan_code: string, body: PlanUpdateRequest}; export type UpdateApiPlansPlanCodeOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const billingCycle = z.enum(["WEEKLY", "MONTHLY", "QUARTERLY", "YEARLY"]); export const currency = z.string().min(3).max(3); export const amount = z.object({value: z.number().gte(0), currency_code: currency}); export const taxCode = z.string(); export const taxResponse = z.object({name: z.string(), rate: z.string().optional(), description: z.string().optional(), code: taxCode.optional(), id: z.string().optional(), created_at: z.string().optional()}); export const chargeModel = z.enum(["STANDARD", "GRADUATED", "PACKAGE", "PERCENTAGE", "VOLUME"]); export const usageBasedCharge = z.object({id: z.string(), metric_id: z.string(), charge_model: chargeModel.optional(), properties: z.object({}).optional(), taxes: z.array(taxResponse).optional(), min_amount: amount.optional()}); export const minimumCommitment = z.object({amount: amount, invoice_display_name: z.string().optional(), taxes: z.array(taxResponse).optional(), id: z.string().optional(), plan_code: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional()}); export const dateTimeWithZone = z.string(); export const planUpdateResponse = z.object({id: z.string().optional(), name: z.string().optional(), code: z.string().optional(), billing_cycle: billingCycle.optional(), description: z.string().optional(), amount: amount.optional(), trial_period: z.number().int().optional(), pay_in_advance: z.boolean().optional(), taxes: z.array(taxResponse).optional(), usage_based_charges: z.array(usageBasedCharge).optional(), minimum_commitment: minimumCommitment.optional(), created_at: dateTimeWithZone.optional(), cascading_updates: z.boolean().optional(), updated_at: dateTimeWithZone.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 }; /** * Update a plan by Code * * PUT /plans/{plan_code} */ export const updateApiPlansPlanCode = async (args: UpdateApiPlansPlanCodeArgs, options?: UpdateApiPlansPlanCodeOptions) => { const {baseUrl = "https://api-m.sandbox.paypal.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/plans/${encodeURIComponent(String(args.plan_code))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(planUpdateResponse, await res.json()) };