import {z} from 'zod' export type GetApiV3VaultSetupTokensIdArgs = {id: string}; export type GetApiV3VaultSetupTokensIdOptions = {baseUrl?: string | undefined, Oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const vaultId = z.string().min(1).max(36); export const merchantPartnerCustomerId = z.string().min(1).max(22); export const customer = z.object({id: merchantPartnerCustomerId.optional(), merchant_customer_id: z.string().min(1).max(64).optional()}); export const paymentTokenStatus = z.enum(["CREATED", "PAYER_ACTION_REQUIRED", "APPROVED", "VAULTED", "TOKENIZED"]).min(1).max(255); export const cardBrand = z.enum(["VISA", "MASTERCARD", "DISCOVER", "AMEX", "SOLO", "JCB", "STAR", "DELTA", "SWITCH", "MAESTRO", "CB_NATIONALE", "CONFIGOGA", "CONFIDIS", "ELECTRON", "CETELEM", "CHINA_UNION_PAY", "DINERS", "ELO", "HIPER", "HIPERCARD", "RUPAY", "GE", "SYNCHRONY", "EFTPOS", "UNKNOWN"]).min(1).max(255); export const dateYearMonth = z.string().min(7).max(7); export const countryCode = z.string().min(2).max(2); export const billingAddress = z.object({address_line_1: z.string().max(300).optional(), address_line_2: z.string().max(300).optional(), admin_area_2: z.string().max(120).optional(), admin_area_1: z.string().max(300).optional(), postal_code: z.string().max(60).optional(), country_code: countryCode, id: z.string().min(1).max(36).optional()}); export const cardVerificationStatus = z.enum(["VERIFIED", "FAILED"]).min(1).max(255); export const dateTime = z.string().min(20).max(64); export const currencyCode = z.string().min(3).max(3); export const money = z.object({currency_code: currencyCode, value: z.string().max(32)}); export const processorResponse = z.object({avs_code: z.enum(["A", "B", "C", "D", "E", "F", "G", "I", "M", "N", "P", "R", "S", "U", "W", "X", "Y", "Z", "Null", "0", "1", "2", "3", "4"]).optional(), cvv_code: z.enum(["E", "I", "M", "N", "P", "S", "U", "X", "All others", "0", "1", "2", "3", "4"]).optional()}); export const cardVerificationDetails = z.object({network_transaction_id: z.string().min(1).max(1024).optional(), date: z.string().min(4).max(4).optional(), network: cardBrand.optional(), time: dateTime.optional(), amount: money.optional(), processor_response: processorResponse.optional(), three_d_secure: z.unknown()}); export const networkTransactionReferenceResponse = z.object({id: z.string().min(9).max(36), date: z.string().min(4).max(4).optional(), network: cardBrand.optional(), time: dateTime.optional()}); export const paresStatus = z.enum(["Y", "N", "U", "A", "C", "R", "D", "I"]).min(1).max(255); export const enrolled = z.enum(["Y", "N", "U", "B"]).min(1).max(255); export const threeDSecureAuthenticationResponse2 = z.object({authentication_status: paresStatus.optional(), enrollment_status: enrolled.optional(), authentication_id: z.string().min(1).max(255).optional()}); export const authenticationResult = z.object({three_d_secure: threeDSecureAuthenticationResponse2.optional()}); export const countryCode2 = z.string().min(2).max(2); export const products = z.string().min(1).max(255); export const productsList = z.array(products); export const binDetails = z.object({bin: z.string().min(1).max(25).optional(), issuing_bank: z.string().min(1).max(64).optional(), bin_country_code: countryCode2.optional(), products: productsList.optional()}); export const cardType = z.enum(["CREDIT", "DEBIT", "PREPAID", "STORE", "UNKNOWN"]).min(1).max(255); export const shippingName = z.object({full_name: z.string().max(300).optional()}); export const shippingPhoneNumber = z.object({country_code: z.string().min(1).max(3), national_number: z.string().min(1).max(14)}); export const shippingAddress = z.object({address_line_1: z.string().max(300).optional(), address_line_2: z.string().max(300).optional(), admin_area_2: z.string().max(120).optional(), admin_area_1: z.string().max(300).optional(), postal_code: z.string().max(60).optional(), country_code: countryCode}); export const shipping = z.object({name: shippingName.optional(), phone_number: shippingPhoneNumber.optional(), type: z.enum(["SHIPPING", "PICKUP_IN_PERSON", "PICKUP_IN_STORE", "PICKUP_FROM_PERSON"]).min(1).max(255).optional(), address: shippingAddress.optional()}); export const email = z.string().min(3).max(254); export const accountId = z.string().min(13).max(13); export const name = z.object({prefix: z.string().max(140).optional(), given_name: z.string().max(140).optional(), surname: z.string().max(140).optional(), middle_name: z.string().max(140).optional(), suffix: z.string().max(140).optional(), alternate_full_name: z.string().max(300).optional(), full_name: z.string().max(300).optional()}); export const phoneType = z.enum(["FAX", "HOME", "MOBILE", "OTHER", "PAGER"]); export const phoneNumber = z.object({national_number: z.string().min(1).max(14)}); export const phoneWithType = z.object({phone_type: phoneType.optional(), phone_number: phoneNumber}); export const address = z.object({line1: z.string(), line2: z.string().optional(), city: z.string(), state: z.string().optional(), country_code: countryCode, postal_code: z.string().optional()}); export const phone = z.object({country_code: z.string().min(1).max(3), national_number: z.string().min(1).max(14), extension_number: z.string().min(1).max(15).optional()}); export const paypalWalletResponse = z.object({description: z.string().min(1).max(128).optional(), usage_pattern: z.enum(["IMMEDIATE", "DEFERRED", "RECURRING_PREPAID", "RECURRING_POSTPAID", "THRESHOLD_PREPAID", "THRESHOLD_POSTPAID", "UNSCHEDULED_POSTPAID", "UNSCHEDULED_PREPAID", "SUBSCRIPTION_PREPAID", "SUBSCRIPTION_POSTPAID", "INSTALLMENT_POSTPAID", "INSTALLMENT_PREPAID"]).min(1).max(30).optional(), shipping: shipping.optional(), permit_multiple_payment_tokens: z.boolean().optional(), usage_type: z.enum(["MERCHANT", "PLATFORM"]).min(1).max(255).optional(), customer_type: z.enum(["CONSUMER", "BUSINESS"]).min(1).max(255).optional(), email_address: email.optional(), payer_id: accountId.optional(), name: name.optional(), phone: phoneWithType.optional(), address: address.optional(), account_id: accountId.optional(), phone_number: phone.optional()}); export const venmoResponse = z.object({description: z.string().min(1).max(128).optional(), usage_pattern: z.enum(["IMMEDIATE", "DEFERRED", "RECURRING_PREPAID", "RECURRING_POSTPAID", "THRESHOLD_PREPAID", "THRESHOLD_POSTPAID", "UNSCHEDULED_POSTPAID", "UNSCHEDULED_PREPAID", "SUBSCRIPTION_PREPAID", "SUBSCRIPTION_POSTPAID", "INSTALLMENT_POSTPAID", "INSTALLMENT_PREPAID"]).min(1).max(30).optional(), shipping: shipping.optional(), permit_multiple_payment_tokens: z.boolean().optional(), usage_type: z.enum(["MERCHANT", "PLATFORM"]).min(1).max(255).optional(), customer_type: z.enum(["CONSUMER", "BUSINESS"]).min(1).max(255).optional(), email_address: email.optional(), payer_id: accountId.optional(), name: name.optional(), phone: phoneWithType.optional(), address: address.optional(), user_name: z.string().min(1).max(50).optional()}); export const linkDescription = z.object({href: z.string(), rel: z.string(), method: z.enum(["GET", "POST", "PUT", "DELETE", "HEAD", "CONNECT", "OPTIONS", "PATCH"]).optional()}); export const linkDescriptionList = z.array(linkDescription); export const setupTokenResponse = z.object({id: vaultId.optional(), customer: customer.optional(), status: paymentTokenStatus.optional(), payment_source: z.object({card: z.object({name: z.string().min(2).max(300).optional(), last_digits: z.string().min(2).max(4).optional(), brand: cardBrand.optional(), expiry: dateYearMonth.optional(), billing_address: billingAddress.optional(), verification_status: cardVerificationStatus.optional(), verification: cardVerificationDetails.optional(), network_transaction_reference: networkTransactionReferenceResponse.optional(), authentication_result: authenticationResult.optional(), bin_details: binDetails.optional(), type: cardType.optional()}).optional(), paypal: paypalWalletResponse.optional(), venmo: venmoResponse.optional()}).optional(), links: linkDescriptionList.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 }; /** * Retrieve a setup token * * GET /v3/vault/setup-tokens/{id} */ export const getApiV3VaultSetupTokensId = async (args: GetApiV3VaultSetupTokensIdArgs, options?: GetApiV3VaultSetupTokensIdOptions) => { const {baseUrl = "https://api-m.paypal.com", fetch: fetchFn = fetch, Oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (Oauth2 !== undefined) headers.set('authorization', `Bearer ${Oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/vault/setup-tokens/${encodeURIComponent(String(args.id))}${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(setupTokenResponse, await res.json()) };