import {z} from 'zod' export type AuthorizationAmount = {gross: number, net: number, tax: number}; export type TinyText = string; export type OrderDuration = number; export type CountryCode = string; export type CreateOrderAddressRequest = {addition?: TinyText | undefined, house_number?: TinyText | undefined, street: TinyText, city: TinyText, postal_code: TinyText, country: CountryCode, organization_name?: TinyText | undefined}; export type LegalForm = string; export type PostalCode = string; export type LegacyCreateOrderDebtorCompanyRequest = {merchant_customer_id: TinyText, name: TinyText, tax_id?: TinyText | undefined, tax_number?: TinyText | undefined, registration_court?: TinyText | undefined, registration_number?: TinyText | undefined, industry_sector?: TinyText | undefined, subindustry_sector?: TinyText | undefined, employees_number?: TinyText | undefined, legal_form: LegalForm, established_customer?: boolean | undefined, address_addition?: TinyText | undefined, address_house_number?: TinyText | undefined, address_street: TinyText, address_city: TinyText, address_postal_code: PostalCode, address_country: CountryCode, organisation_type?: string | undefined}; export type AuthorizationStrictEmail = string; export type AuthorizationPhoneNumber = string; export type AmountDTO = {gross: number, net: number, tax: number}; export type CreateOrderLineItemRequest = {external_id: TinyText, title: TinyText, description?: string | undefined, quantity: number, category?: TinyText | undefined, brand?: TinyText | undefined, gtin?: TinyText | undefined, mpn?: TinyText | undefined, product_url?: TinyText | undefined, image_url?: TinyText | undefined, type?: TinyText | undefined, quantity_unit?: TinyText | undefined, tax_rate?: number | undefined, total_discount_amount?: number | undefined, unit_price?: number | undefined, amount: AmountDTO}; export type HppCheckoutSessionECommerce = {channel: 'ecommerce', amount: AuthorizationAmount, comment?: TinyText | undefined, duration: OrderDuration, order_id?: TinyText | undefined, delivery_address?: CreateOrderAddressRequest | undefined, billing_address?: CreateOrderAddressRequest | undefined, debtor_company: LegacyCreateOrderDebtorCompanyRequest, debtor_person: {email: AuthorizationStrictEmail, salutation?: ('m' | 'f') | undefined, first_name?: string | undefined, last_name?: string | undefined, phone_number?: AuthorizationPhoneNumber | undefined}, line_items: Array, merchant_urls: {accept_url: string, decline_url: string}}; export type HppCheckoutSessionTelesales = {channel: 'telesales', amount: AuthorizationAmount, comment?: TinyText | undefined, duration: OrderDuration, order_id?: TinyText | undefined, delivery_address?: CreateOrderAddressRequest | undefined, billing_address?: CreateOrderAddressRequest | undefined, debtor_company: LegacyCreateOrderDebtorCompanyRequest, debtor_person: {email: AuthorizationStrictEmail, salutation?: ('m' | 'f') | undefined, first_name?: string | undefined, last_name?: string | undefined, phone_number?: AuthorizationPhoneNumber | undefined}, line_items: Array, merchant_urls?: ({accept_url?: string | undefined, decline_url?: string | undefined}) | undefined}; export type HppCheckoutSessionInstore = {channel: 'instore', amount: AuthorizationAmount, comment?: TinyText | undefined, duration: OrderDuration, order_id?: TinyText | undefined, delivery_address?: CreateOrderAddressRequest | undefined, billing_address?: CreateOrderAddressRequest | undefined, debtor_company: LegacyCreateOrderDebtorCompanyRequest, debtor_person: {email: AuthorizationStrictEmail, salutation?: ('m' | 'f') | undefined, first_name?: string | undefined, last_name?: string | undefined, phone_number?: AuthorizationPhoneNumber | undefined}, line_items: Array, merchant_urls?: ({accept_url?: string | undefined, decline_url?: string | undefined}) | undefined}; export type CreateApiHppCheckoutSessionsArgs = {body: HppCheckoutSessionECommerce | HppCheckoutSessionTelesales | HppCheckoutSessionInstore}; export type CreateApiHppCheckoutSessionsOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const uUID = z.string().min(36).max(36); export const createApiHppCheckoutSessionsResponse = z.object({session_id: uUID, expires_at: z.string(), hpp_url: 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 }; /** * Hpp Checkout Session Create * * POST /hpp-checkout-sessions */ export const createApiHppCheckoutSessions = async (args: CreateApiHppCheckoutSessionsArgs, options?: CreateApiHppCheckoutSessionsOptions) => { const {baseUrl = "https://paella-sandbox.billie.io/api/v2", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/hpp-checkout-sessions${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(createApiHppCheckoutSessionsResponse, await res.json()) };