import {z} from 'zod' export type PblAmount = {currency: '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: number}; export type PblAddressDto = {city?: string | undefined, country: string, state?: string | undefined, street_address_line1?: string | undefined, street_address_line2?: string | undefined, street_address_line3?: string | undefined, zipcode?: string | undefined}; export type PblCustomer = {email?: string | undefined, name?: string | undefined, phone_number?: string | undefined}; export type PblDeliveryInfo = {email?: string | undefined, phone_number?: string | undefined}; export type PblPaymentLinkLineItem = {description: string, quantity: number, unit_price: number}; export type PblCreatePaymentLinkRequestV2 = {amount: PblAmount, billing_address?: PblAddressDto | undefined, cancel_url?: string | undefined, customer?: PblCustomer | undefined, customer_success_email?: string | undefined, delivery_info?: PblDeliveryInfo | undefined, expires_at?: string | undefined, language?: string | undefined, line_items?: Array | undefined, merchant_reference?: string | undefined, merchant_success_email?: string | undefined, metadata?: {[key: string]: string} | undefined, post_success_payment?: ('SHOW_SUCCESS_PAGE' | 'REDIRECT') | undefined, required_customer_fields?: (Array<'SHIPPING_ADDRESS' | 'PHONE' | 'EMAIL'>) | undefined, store_id?: string | undefined, success_url?: string | undefined, supported_card_brands?: (Array<'AMEX' | 'MASTERCARD' | 'VISA'>) | undefined, supported_payment_methods?: (Array<'CARD' | 'TOKEN' | 'APPLE_PAY'>) | undefined, transaction_type?: ('SALE' | 'ACCOUNT_VERIFICATION') | undefined, type?: 'SINGLE_USE' | undefined}; export type CreateApiV2PaymentLinksArgs = {'Idempotency-Key': string, 'User-Agent': string, body: PblCreatePaymentLinkRequestV2}; export type CreateApiV2PaymentLinksOptions = {baseUrl: string, pblOauthDev?: string | undefined, fetch?: typeof fetch | undefined}; export const pblCreatePaymentLinkResponse = z.object({payment_link: z.string().optional(), payment_link_id: z.string()}); 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 }; /** * Creates a new payment link * * POST /v2/payment-links * * Also accepts auth: pbl_oauth_prd */ export const createApiV2PaymentLinks = async (args: CreateApiV2PaymentLinksArgs, options: CreateApiV2PaymentLinksOptions) => { 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['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) 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${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(pblCreatePaymentLinkResponse, await res.json()) };