import {z} from 'zod' export type ThreeDSecure = {eci_flag?: ('00' | '01' | '02' | '03' | '04' | '05' | '06' | '07') | undefined, enrolled?: string | undefined, cavv?: string | undefined, cavv_algorithm?: string | undefined, pares_status?: string | undefined, xid?: string | undefined, ds_transaction_id?: string | undefined, threeds_version?: ('1.0.2' | '2.1.0' | '2.2.0') | undefined}; export type ShippingInformationDto = {address: string, city: string, country: unknown, postal_code: string, email?: string | undefined, first_name?: string | undefined, last_name?: string | undefined, phone?: number | undefined, state?: string | undefined}; export type VippsPaymentInitiationRequest = {payment_provider_contract: unknown, threed_secure?: ThreeDSecure | undefined, app_phone_number?: string | undefined, redirect_url: string, is_app?: boolean | undefined, amount: number, auth_type?: ('PRE_AUTH' | 'FINAL_AUTH') | undefined, capture_now?: boolean | undefined, customer: unknown, customer_ip?: (string | string) | undefined, dynamic_descriptor?: string | undefined, merchant_reference: string, shipping_information?: ShippingInformationDto | undefined, user_agent?: string | undefined, sales_description?: string | undefined, currency_code: 'NOK', sca_compliance_level?: ('NONE' | 'WALLET' | 'FORCE_3DS') | undefined, sca_exemption?: ('1' | '2' | '3' | '4' | '5' | '6') | undefined}; export type CreateApiApiV2TransactionsVippsArgs = {'x-vfi-api-idempotencykey'?: string | undefined, body: VippsPaymentInitiationRequest}; export type CreateApiApiV2TransactionsVippsOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const ecomPaymentResponseDetails = z.object({auto_capture: z.boolean().optional(), mid: z.string().optional()}); export const paymentProductType = z.enum(["Affirm", "Amex", "Crypto", "Diners Club International", "Discover", "JCB", "Klarna", "Maestro", "Mastercard", "OP Online Payment", "Swish", "Unknown", "Visa"]); export const shippingInformationDto = z.object({address: z.string().max(300), city: z.string().max(28), country: z.string().min(2).max(2), postal_code: z.string().min(1).max(16), email: z.string().min(3).max(254).optional(), first_name: z.string().max(100).optional(), last_name: z.string().max(100).optional(), phone: z.number().optional(), state: z.string().max(35).optional()}); export const vippsPaymentInitiationResponse = z.object({id: z.string().optional(), redirect_url: z.string().optional(), amount: z.number().int().optional(), blocked: z.boolean().optional(), created_at: z.string().optional(), customer: z.string().optional(), details: ecomPaymentResponseDetails.optional(), merchant_reference: z.string().max(50).optional(), processor: z.string().optional(), payment_product: z.string().optional(), payment_product_type: paymentProductType.optional(), shipping_information: shippingInformationDto.optional(), status: z.enum(["INITIATED", "AUTHORIZED", "AUTHORIZATION_VOIDED", "CANCELLED", "CUSTOMER_ACCEPTED", "REFUNDED", "FAILED", "PENDING", "DECLINED", "SETTLEMENT_CANCELLED", "SETTLEMENT_REQUESTED", "SETTLEMENT_SUBMITTED", "SETTLEMENT_COMPLETED", "SETTLEMENT_PARTIAL", "SETTLEMENT_DECLINED", "VOIDED", "UNKNOWN"]).optional(), status_reason: z.string().optional(), created_by: z.string().optional(), shopper_interaction: z.enum(["ECOMMERCE", "MAIL", "TELEPHONE"]).optional(), geo_location: z.array(z.number()).optional(), city: z.string().optional(), country_code: z.string().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 }; /** * Initiate a Vipps payment * * POST /api/v2/transactions/vipps * * Also accepts auth: BearerAuth */ export const createApiApiV2TransactionsVipps = async (args: CreateApiApiV2TransactionsVippsArgs, options?: CreateApiApiV2TransactionsVippsOptions) => { const {baseUrl = "https://emea.gsc.verifone.cloud/oidc/checkout-service", fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-vfi-api-idempotencykey'] != null) headers.set("x-vfi-api-idempotencykey", String(args['x-vfi-api-idempotencykey'])) if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/transactions/vipps${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(vippsPaymentInitiationResponse, await res.json()) };