import {z} from 'zod' export type SwishPayment = {payment_product_type: 'swish.swish', phone_number?: string | undefined, remember_me?: boolean | undefined}; export type VippsPayment = {payment_product_type: 'vipps', phone_number?: string | undefined, remember_me?: boolean | undefined}; export type BamboraMobilePayPayment = {payment_product_type: 'bambora.mobilepay', phone_number: string, remember_me?: boolean | undefined}; export type OrderAddress = {first_name?: string | undefined, last_name?: string | undefined, address_line?: string | undefined, address_line_2?: string | undefined, co_address?: string | undefined, business_name?: string | undefined, postal_code?: string | undefined, postal_place?: string | undefined, country?: unknown | undefined, phone_number?: string | undefined, email?: string | undefined, latitude?: number | undefined, longitude?: number | undefined, comment?: string | undefined, organization_number?: string | undefined, organization_type?: string | undefined, customer_reference?: string | undefined, cost_center?: string | undefined}; export type DinteroPspApplePayPayment = {payment_product_type: 'dintero_psp.applepay', psp_transaction_id: string, billing_address?: OrderAddress | undefined, shipping_address?: OrderAddress | undefined}; export type CollectorB2BAddress = {business_name: string, organization_number: string, address_line: string, address_line_2?: string | undefined, co_address?: string | undefined, postal_code: string, postal_place: string, country: unknown, phone_number: string, email: string, customer_reference?: string | undefined, cost_center?: string | undefined, first_name?: string | undefined, last_name?: string | undefined, address_id?: string | undefined}; export type CollectorInvoiceB2BPayment = {payment_product_type: 'collector.invoice_b2b', shipping_address: CollectorB2BAddress, billing_address: CollectorB2BAddress, remember_me?: boolean | undefined, authorization_code?: string | undefined}; export type CollectorAddress = {first_name: string, last_name: string, address_line: string, address_line_2?: string | undefined, business_name?: string | undefined, co_address?: string | undefined, postal_code: string, postal_place: string, country: unknown, phone_number: string, email: string}; export type CollectorFinancePayment = {payment_product_type: 'collector.finance', social_security_number: string, shipping_address: CollectorAddress, billing_address: CollectorAddress, remember_me?: boolean | undefined, authorization_code?: string | undefined}; export type PayExVippsPayment = {payment_product_type: 'payex.vipps', phone_number?: string | undefined, remember_me?: boolean | undefined}; export type CollectorInvoiceB2BPreapprovedPayment = {payment_product_type: 'collector.invoice_b2b_preapproved', shipping_address: CollectorB2BAddress, scc?: string | undefined, company_id: string, remember_me?: boolean | undefined, authorization_code?: string | undefined}; export type BamboraVippsPayment = {payment_product_type: 'bambora.vipps', phone_number?: string | undefined, remember_me?: boolean | undefined}; export type PayExSwishPayment = {payment_product_type: 'payex.swish', phone_number?: string | undefined, remember_me?: boolean | undefined}; export type CollectorInstallmentPayment = {payment_product_type: 'collector.installment', product: {id: string}, shipping_address: CollectorAddress, billing_address: CollectorAddress, remember_me?: boolean | undefined, authorization_code?: string | undefined, social_security_number?: string | undefined}; export type CollectorInvoicePayment = {payment_product_type: 'collector.invoice', shipping_address: CollectorAddress, billing_address: CollectorAddress, remember_me?: boolean | undefined, authorization_code?: string | undefined, social_security_number?: string | undefined}; export type KlarnaAddress = {first_name: string, last_name: string, address_line: string, address_line_2?: string | undefined, business_name?: string | undefined, postal_code: string, postal_place: string, country: unknown, phone_number: string, email: string}; export type KlarnaPayment = {payment_product_type: 'klarna.klarna', authorization_token: string, billing_address: KlarnaAddress, shipping_address: KlarnaAddress, locale: string}; export type PayExMobilePayPayment = {payment_product_type: 'payex.mobilepay', phone_number: string, remember_me?: boolean | undefined}; export type BamboraCreditcardCitPayment = {payment_product_type: 'bambora.creditcard', remember_me?: boolean | undefined, browser_information: {java_enabled: boolean, javascript_enabled: boolean, color_depth: number, screen_height: number, screen_width: number, timezone_offset: number}}; export type SeitatechInPersonPayment = {payment_product_type: 'seitatech.in_person'}; export type Payment = SwishPayment | VippsPayment | BamboraMobilePayPayment | DinteroPspApplePayPayment | CollectorInvoiceB2BPayment | CollectorFinancePayment | PayExVippsPayment | CollectorInvoiceB2BPreapprovedPayment | BamboraVippsPayment | PayExSwishPayment | CollectorInstallmentPayment | CollectorInvoicePayment | KlarnaPayment | PayExMobilePayPayment | BamboraCreditcardCitPayment | SeitatechInPersonPayment; export type CreateApiAccountsAidPaymentsSessionsSessionIdPayArgs = {'If-Unmodified-Since'?: string | undefined, body: Payment}; export type CreateApiAccountsAidPaymentsSessionsSessionIdPayOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const paymentResponse = z.object({success: z.boolean(), actions: z.array(z.object({source: z.string().optional(), code: z.string().optional(), type: z.enum(["success", "error"]).optional()})), redirect_url: z.string().optional(), poll_url: z.string().optional(), approval_url: z.string().optional(), qr_code: z.string().optional(), error: z.string().optional(), error_details: z.array(z.object({code: z.string().optional(), category: z.enum(["permanent", "temporary", "unknown"]).optional(), field: z.string().optional(), authentication_operations: z.array(z.object({href: z.string(), method: z.literal("POST"), content_type: z.literal("application/json"), rel: z.enum(["3d-secure-init-hosted", "3d-secure-init", "3d-secure-method-complete", "3d-secure-challenge-complete", "3d-secure-notification", "mastercard-taf-init-params", "mastercard-taf-challenge-complete"])})).optional(), error_codes: z.array(z.enum(["ERROR", "DECLINED", "ISSUER_ERROR", "PICK_UP_CARD", "INVALID_MERCHANT", "INVALID_TRANSACTION", "INVALID_AMOUNT", "INSUFFICIENT_FUNDS", "CLOSED_ACCOUNT", "EXPIRED_CARD", "EXCEEDS_APPROVAL_LIMIT", "EXCEEDS_WITHDRAWAL_FREQUENCY_LIMIT", "RESTRICTED_CARD", "PIN_REQUIRED", "FAILED_VERIFICATION", "ADDITIONAL_VERIFICATION_REQUIRED", "AUTHORIZATION_EXPIRED", "PENDING_ISSUER_RESPONSE", "UNKNOWN"])).optional()})).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 }; /** * checkout_sid_pay_post * * POST /accounts/{aid}/payments/sessions/{session_id}/pay */ export const createApiAccountsAidPaymentsSessionsSessionIdPay = async (args: CreateApiAccountsAidPaymentsSessionsSessionIdPayArgs, options?: CreateApiAccountsAidPaymentsSessionsSessionIdPayOptions) => { const {baseUrl = "https://api.dintero.com/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['If-Unmodified-Since'] != null) headers.set("If-Unmodified-Since", String(args['If-Unmodified-Since'])) const url = `${baseUrl.replace(/\/+$/, '')}/accounts/${encodeURIComponent(String(args.aid))}/payments/sessions/${encodeURIComponent(String(args.session_id))}/pay${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(paymentResponse, await res.json()) };