import {z} from 'zod' export type PblUpdatePaymentLinkRequest = {expires_at?: string | undefined}; export type PatchApiV2PaymentLinksPaymentLinkIdArgs = {payment_link_id: string, 'User-Agent': string, body: PblUpdatePaymentLinkRequest}; export type PatchApiV2PaymentLinksPaymentLinkIdOptions = {baseUrl: string, pblOauthDev?: string | undefined, fetch?: typeof fetch | undefined}; export const pblAmount = z.object({currency: z.enum(["AED", "AFN", "ALL", "AMD", "ANG", "AOA", "ARS", "AUD", "AWG", "AZN", "BAM", "BBD", "BDT", "BGN", "BHD", "BIF", "BMD", "BND", "BOB", "BOV", "BRL", "BSD", "BTN", "BWP", "BYN", "BYR", "BZD", "CAD", "CDF", "CHE", "CHF", "CHW", "CLF", "CLP", "CNY", "COP", "COU", "CRC", "CUC", "CUP", "CVE", "CZK", "DJF", "DKK", "DOP", "DZD", "EGP", "ERN", "ETB", "EUR", "FJD", "FKP", "GBP", "GEL", "GHS", "GIP", "GMD", "GNF", "GTQ", "GYD", "HKD", "HNL", "HRK", "HTG", "HUF", "IDR", "ILS", "INR", "IQD", "IRR", "ISK", "JMD", "JOD", "JPY", "KES", "KGS", "KHR", "KMF", "KPW", "KRW", "KWD", "KYD", "KZT", "LAK", "LBP", "LKR", "LRD", "LSL", "LYD", "MAD", "MDL", "MGA", "MKD", "MMK", "MNT", "MOP", "MRO", "MRU", "MUR", "MVR", "MWK", "MXN", "MXV", "MYR", "MZN", "NAD", "NGN", "NIO", "NOK", "NPR", "NZD", "OMR", "PAB", "PEN", "PGK", "PHP", "PKR", "PLN", "PYG", "QAR", "RON", "RSD", "RUB", "RWF", "SAR", "SBD", "SCR", "SDG", "SEK", "SGD", "SHP", "SLE", "SLL", "SOS", "SRD", "SSP", "STD", "STN", "SVC", "SYP", "SZL", "THB", "TJS", "TMT", "TND", "TOP", "TRY", "TTD", "TWD", "TZS", "UAH", "UGX", "USD", "USN", "UYI", "UYU", "UZS", "VED", "VEF", "VES", "VND", "VUV", "WST", "XAF", "XCD", "XCG", "XOF", "XPF", "YER", "ZAR", "ZMW", "ZWL"]), value: z.number().int().lte(9223372036854)}); export const pblAddressDto = z.object({city: z.string().max(100).optional(), country: z.string(), state: z.string().max(100).optional(), street_address_line1: z.string().max(100).optional(), street_address_line2: z.string().max(100).optional(), street_address_line3: z.string().max(100).optional(), zipcode: z.string().max(16).optional()}); export const pblCustomer = z.object({email: z.string().optional(), name: z.string().optional(), phone_number: z.string().optional()}); export const pblPaymentLinkLineItem = z.object({description: z.string(), quantity: z.number().int(), unit_price: z.number().int()}); export const pblGetPaymentLinkResponse = z.object({amount: pblAmount, billing_address: pblAddressDto.optional(), cancel_url: z.string().optional(), customer: pblCustomer.optional(), customer_success_email: z.string().optional(), expires_at: z.string().optional(), line_items: z.array(pblPaymentLinkLineItem).optional(), merchant_reference: z.string().max(60).optional(), merchant_success_email: z.string().optional(), metadata: z.record(z.string(), z.string()).optional(), payment_link_id: z.string(), post_success_payment: z.enum(["SHOW_SUCCESS_PAGE", "REDIRECT"]).optional(), required_customer_fields: z.array(z.enum(["SHIPPING_ADDRESS", "PHONE", "EMAIL"])).optional(), status: z.enum(["VALID", "COMPLETED", "MANUALLY_EXPIRED", "DISABLED", "EXPIRED"]), success_url: z.string().optional(), supported_card_brands: z.array(z.enum(["AMEX", "MASTERCARD", "VISA"])).optional(), supported_payment_methods: z.array(z.enum(["CARD", "TOKEN", "APPLE_PAY"])).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 }; /** * Updates an existing payment link * * PATCH /v2/payment-links/{payment_link_id} * * Also accepts auth: pbl_oauth_prd */ export const patchApiV2PaymentLinksPaymentLinkId = async (args: PatchApiV2PaymentLinksPaymentLinkIdArgs, options: PatchApiV2PaymentLinksPaymentLinkIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, pblOauthDev} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['User-Agent'] != null) headers.set("User-Agent", String(args['User-Agent'])) if (pblOauthDev !== undefined) headers.set('authorization', `Bearer ${pblOauthDev}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/payment-links/${encodeURIComponent(String(args.payment_link_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(pblGetPaymentLinkResponse, await res.json()) };