import {z} from 'zod' export type BuyerType = 'CUSTOMER' | 'PLATFORM_OWNER' | 'PARTNER'; export type Currency = 'USD' | 'CAD' | 'EUR' | 'JPY' | 'GBP' | 'KRW' | 'CHF' | 'SEK' | 'SGD' | 'MYR' | 'AUD' | 'MXN' | 'INR' | 'BRL' | 'DKK' | 'NZD' | 'NOK' | 'ZAR' | 'PHP' | 'CNY' | 'SAR' | 'GTQ' | 'IDR' | 'ARS' | 'COP' | 'PEN'; export type ServiceStrategy = 'CUSTOM' | 'IMMEDIATELY'; export type ServiceConfiguration = {date?: string | undefined, strategy?: ServiceStrategy | undefined}; export type BillingStrategy = 'CUSTOM' | 'NEXT_BILLING_CYCLE' | 'UPON_SERVICE_ACTIVATION'; export type BillingConfiguration = {date?: number | undefined, strategy?: BillingStrategy | undefined}; export type CustomPriceType = 'RESELLER_PURCHASE_PRICE' | 'MARKETPLACE_RESELLER_PURCHASE_PRICE'; export type PricingStrategy = 'FREE' | 'FLAT' | 'UNIT' | 'TIERED' | 'VOLUME' | 'VARIABLE_PRICE_STRATEGIES' | 'UNLIMITED' | 'CUSTOM'; export type PriceRange = {min: number, max?: number | undefined, price: number, salePrice?: unknown | undefined, wholesalePrice?: unknown | undefined}; export type Definition = {price?: number | undefined, priceRanges?: Array | undefined}; export type CustomPrice = {costId: string, priceType?: CustomPriceType | undefined, pricingStrategy?: PricingStrategy | undefined, definition?: Definition | undefined}; export type Unit = {quantity?: number | undefined, unit?: string | undefined}; export type CustomAttributes = {[key: string]: unknown}; export type ItemCustomAttributes = {vendorRequiredFields?: CustomAttributes | undefined, subscription?: CustomAttributes | undefined}; export type MinimumServiceLengthUnit = 'DAILY' | 'MONTHLY' | 'QUARTERLY' | 'SIX_MONTHS' | 'YEARLY' | 'TWO_YEARS' | 'THREE_YEARS'; export type GracePeriodUnit = 'NONE' | 'INFINITY' | 'DAY' | 'MONTH'; export type CancellationPeriodUnit = 'NONE' | 'DAY'; export type UnitContractTerms = {blockContractIncrease?: boolean | undefined, blockContractDecrease?: boolean | undefined, blockOriginalContractDecrease?: boolean | undefined}; export type TerminationFeeType = 'FIXED' | 'PERCENTAGE'; export type TerminationFee = {feeType?: TerminationFeeType | undefined, amount?: number | undefined, description?: string | undefined}; export type CustomContractConfiguration = {minimumServiceLength: number, minimumServiceLengthUnit: MinimumServiceLengthUnit, endOfContractGracePeriod?: number | undefined, endOfContractGracePeriodUnit?: GracePeriodUnit | undefined, contractCancellationPeriodLimit?: number | undefined, contractCancellationPeriodUnit?: CancellationPeriodUnit | undefined, continueWithoutContract?: boolean | undefined, autoExtensionPricingId?: string | undefined, terminationFeeGracePeriod?: number | undefined, terminationFeeGracePeriodUnit?: GracePeriodUnit | undefined, blockContractUpgrades?: boolean | undefined, blockContractDowngrades?: boolean | undefined, blockSwitchToShorterContract?: boolean | undefined, keepContractDateOnPlanChange?: boolean | undefined, alignWithParentCycleStartDate?: boolean | undefined, unitTerms?: {[key: string]: UnitContractTerms} | undefined, sourceId?: string | undefined, terminationFee?: TerminationFee | undefined, contractFee?: number | undefined}; export type ShoppingCartItem = {id?: string | undefined, referenceId?: string | undefined, subscriptionId?: string | undefined, pricingPlanId?: string | undefined, serviceConfiguration?: ServiceConfiguration | undefined, billingConfiguration?: BillingConfiguration | undefined, discountCode?: string | undefined, discountId?: string | undefined, customPrices?: Array | undefined, units?: Array | undefined, customAttributes?: ItemCustomAttributes | undefined, customContractConfiguration?: CustomContractConfiguration | undefined}; export type AssociationType = 'ADDON'; export type Association = {parentItemId?: string | undefined, childItemIds?: Array | undefined, type?: AssociationType | undefined}; export type Description = {defaultDescription?: string | undefined, localizedDescriptions?: {[key: string]: string} | undefined, params?: {[key: string]: string} | undefined}; export type RevenueShare = {appdirect?: number | undefined, channel?: number | undefined, vendor?: number | undefined}; export type Fee = {id?: string | undefined, externalId?: string | undefined, createdOn?: string | undefined, key?: string | undefined, currency?: string | undefined, amount?: number | undefined, description?: Description | undefined, revenueShare?: RevenueShare | undefined, itemIds?: Array | undefined, metadata?: {[key: string]: string} | undefined}; export type AccreditedAgent = {userId?: string | undefined, companyId?: string | undefined}; export type ShoppingCart = {id?: string | undefined, createdOn?: string | undefined, ownerUserId?: string | undefined, ownerCompanyId?: string | undefined, buyerType?: BuyerType | undefined, currency?: Currency | undefined, status?: string | undefined, isBlocked?: boolean | undefined, paymentMethodId?: string | undefined, preAuthId?: string | undefined, locale?: string | undefined, buyerUserId?: string | undefined, buyerCompanyId?: string | undefined, items?: Array | undefined, customAttributes?: ({purchase?: CustomAttributes | undefined, buyerUser?: CustomAttributes | undefined, buyerCompany?: CustomAttributes | undefined, ownerUser?: CustomAttributes | undefined, ownerCompany?: CustomAttributes | undefined}) | undefined, associations?: Array | undefined, fees?: Array | undefined, accreditedAgents?: Array | undefined}; export type CreateApiV3CheckoutShoppingCartsValidationArgs = {'AD-ShoppingCart-Partner': string, level: 'PRE_FINALIZE' | 'PREVENT_FINALIZE' | 'PREVENT_PROVISIONING', body: ShoppingCart}; export type CreateApiV3CheckoutShoppingCartsValidationOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV3CheckoutShoppingCartsValidationResponse = z.object({}); 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 }; /** * Validate non-persisted shopping carts * * POST /v3/checkout/shoppingCarts/validation */ export const createApiV3CheckoutShoppingCartsValidation = async (args: CreateApiV3CheckoutShoppingCartsValidationArgs, options?: CreateApiV3CheckoutShoppingCartsValidationOptions) => { const {baseUrl = "https://marketplace.appdirect.com/api", 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['AD-ShoppingCart-Partner'] != null) headers.set("AD-ShoppingCart-Partner", String(args['AD-ShoppingCart-Partner'])) if (args.level != null) query.append("level", String(args.level)) const url = `${baseUrl.replace(/\/+$/, '')}/v3/checkout/shoppingCarts/validation${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(createApiV3CheckoutShoppingCartsValidationResponse, await res.json()) };