import {z} from 'zod' export type CreateApiPublicCreateDigitalProductPricingPlanArgs = {body: {data: Array<{data: {fermionDigitalProductSlug: string, newPlanData: {fermionDigitalProductPriceId: string, fermionDigitalProductId: string, createdAt: string, updatedAt: string, planStatus: 'Published' | 'Unlisted' | 'Draft' | 'Deleted', planType: 'OneTimePaymentPlan', config: {friendlyPlanName: string, prices: Array<{currencyType: 'Aud' | 'Aed' | 'Bdt' | 'Brl' | 'Cad' | 'Cop' | 'Dop' | 'Etb' | 'Eur' | 'Gbp' | 'Idr' | 'Inr' | 'Kes' | 'Lkr' | 'Mad' | 'Myr' | 'Mxn' | 'Ngn' | 'Npr' | 'Nzd' | 'Pkr' | 'Pln' | 'Rub' | 'Thb' | 'Try' | 'Usd' | 'Sgd' | 'Cny' | 'Sar' | 'Twd' | 'Jpy' | 'Krw' | 'Vnd', currencySymbol: string, priceInLowestCurrencyUnit: number}>, inrCurrencySpecificData: {isAmountExclusiveOfGst: boolean} | unknown | unknown, defaultFallbackCurrency: 'Aud' | 'Aed' | 'Bdt' | 'Brl' | 'Cad' | 'Cop' | 'Dop' | 'Etb' | 'Eur' | 'Gbp' | 'Idr' | 'Inr' | 'Kes' | 'Lkr' | 'Mad' | 'Myr' | 'Mxn' | 'Ngn' | 'Npr' | 'Nzd' | 'Pkr' | 'Pln' | 'Rub' | 'Thb' | 'Try' | 'Usd' | 'Sgd' | 'Cny' | 'Sar' | 'Twd' | 'Jpy' | 'Krw' | 'Vnd', additionalDataToCollectOnCheckout?: ({customFields: Array<{fieldType: 'string', label: string, sublabel: string, isRequired?: boolean | undefined} | {fieldType: 'select', label: string, sublabel: string, isRequired: boolean, options: Array}>}) | undefined}} | {fermionDigitalProductPriceId: string, fermionDigitalProductId: string, createdAt: string, updatedAt: string, planStatus: 'Published' | 'Unlisted' | 'Draft' | 'Deleted', planType: 'PartialPaymentPlan', config: {friendlyPlanName: string, currencySpecificPlanInformation: Array<{currencySymbol: string, currencyType: 'Aud' | 'Aed' | 'Bdt' | 'Brl' | 'Cad' | 'Cop' | 'Dop' | 'Etb' | 'Eur' | 'Gbp' | 'Idr' | 'Inr' | 'Kes' | 'Lkr' | 'Mad' | 'Myr' | 'Mxn' | 'Ngn' | 'Npr' | 'Nzd' | 'Pkr' | 'Pln' | 'Rub' | 'Thb' | 'Try' | 'Usd' | 'Sgd' | 'Cny' | 'Sar' | 'Twd' | 'Jpy' | 'Krw' | 'Vnd', realFullPriceOfProductInLowestCurrencyUnitIrrespectiveOfTaxAndExclusiveOfDiscount: number, minimumMandatoryPaymentAmountInLowestCurrencyUnitIrrespectiveOfDiscountAndTax: number, emiIntervalInDays: number, totalNumberOfInstallmentOptions: Array}>, defaultFallbackCurrency: 'Aud' | 'Aed' | 'Bdt' | 'Brl' | 'Cad' | 'Cop' | 'Dop' | 'Etb' | 'Eur' | 'Gbp' | 'Idr' | 'Inr' | 'Kes' | 'Lkr' | 'Mad' | 'Myr' | 'Mxn' | 'Ngn' | 'Npr' | 'Nzd' | 'Pkr' | 'Pln' | 'Rub' | 'Thb' | 'Try' | 'Usd' | 'Sgd' | 'Cny' | 'Sar' | 'Twd' | 'Jpy' | 'Krw' | 'Vnd', inrCurrencySpecificData: {isAmountExclusiveOfGst: boolean} | unknown | unknown, additionalDataToCollectOnCheckout?: ({customFields: Array<{fieldType: 'string', label: string, sublabel: string, isRequired?: boolean | undefined} | {fieldType: 'select', label: string, sublabel: string, isRequired: boolean, options: Array}>}) | undefined}}}}>}}; export type CreateApiPublicCreateDigitalProductPricingPlanOptions = {baseUrl?: string | undefined, FERMIONAPIKEY?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiPublicCreateDigitalProductPricingPlanResponse = z.array(z.object({output: z.union([z.object({status: z.literal("ok"), data: z.object({newPricingPlan: z.union([z.object({fermionDigitalProductPriceId: z.string(), fermionDigitalProductId: z.string(), createdAt: z.string(), updatedAt: z.string(), planStatus: z.enum(["Published", "Unlisted", "Draft", "Deleted"]), planType: z.literal("OneTimePaymentPlan"), config: z.object({friendlyPlanName: z.string(), prices: z.array(z.object({currencyType: z.enum(["Aud", "Aed", "Bdt", "Brl", "Cad", "Cop", "Dop", "Etb", "Eur", "Gbp", "Idr", "Inr", "Kes", "Lkr", "Mad", "Myr", "Mxn", "Ngn", "Npr", "Nzd", "Pkr", "Pln", "Rub", "Thb", "Try", "Usd", "Sgd", "Cny", "Sar", "Twd", "Jpy", "Krw", "Vnd"]), currencySymbol: z.string(), priceInLowestCurrencyUnit: z.number().gte(0)})), inrCurrencySpecificData: z.union([z.object({isAmountExclusiveOfGst: z.boolean()}), z.unknown(), z.unknown()]), defaultFallbackCurrency: z.enum(["Aud", "Aed", "Bdt", "Brl", "Cad", "Cop", "Dop", "Etb", "Eur", "Gbp", "Idr", "Inr", "Kes", "Lkr", "Mad", "Myr", "Mxn", "Ngn", "Npr", "Nzd", "Pkr", "Pln", "Rub", "Thb", "Try", "Usd", "Sgd", "Cny", "Sar", "Twd", "Jpy", "Krw", "Vnd"]), additionalDataToCollectOnCheckout: z.object({customFields: z.array(z.union([z.object({fieldType: z.literal("string"), label: z.string(), sublabel: z.string(), isRequired: z.boolean().optional()}), z.object({fieldType: z.literal("select"), label: z.string(), sublabel: z.string(), isRequired: z.boolean(), options: z.array(z.string())})]))}).optional()})}), z.object({fermionDigitalProductPriceId: z.string(), fermionDigitalProductId: z.string(), createdAt: z.string(), updatedAt: z.string(), planStatus: z.enum(["Published", "Unlisted", "Draft", "Deleted"]), planType: z.literal("PartialPaymentPlan"), config: z.object({friendlyPlanName: z.string(), currencySpecificPlanInformation: z.array(z.object({currencySymbol: z.string(), currencyType: z.enum(["Aud", "Aed", "Bdt", "Brl", "Cad", "Cop", "Dop", "Etb", "Eur", "Gbp", "Idr", "Inr", "Kes", "Lkr", "Mad", "Myr", "Mxn", "Ngn", "Npr", "Nzd", "Pkr", "Pln", "Rub", "Thb", "Try", "Usd", "Sgd", "Cny", "Sar", "Twd", "Jpy", "Krw", "Vnd"]), realFullPriceOfProductInLowestCurrencyUnitIrrespectiveOfTaxAndExclusiveOfDiscount: z.number(), minimumMandatoryPaymentAmountInLowestCurrencyUnitIrrespectiveOfDiscountAndTax: z.number().gte(0), emiIntervalInDays: z.number().gte(0).lte(180), totalNumberOfInstallmentOptions: z.array(z.number().gte(1).lte(50))})), defaultFallbackCurrency: z.enum(["Aud", "Aed", "Bdt", "Brl", "Cad", "Cop", "Dop", "Etb", "Eur", "Gbp", "Idr", "Inr", "Kes", "Lkr", "Mad", "Myr", "Mxn", "Ngn", "Npr", "Nzd", "Pkr", "Pln", "Rub", "Thb", "Try", "Usd", "Sgd", "Cny", "Sar", "Twd", "Jpy", "Krw", "Vnd"]), inrCurrencySpecificData: z.union([z.object({isAmountExclusiveOfGst: z.boolean()}), z.unknown(), z.unknown()]), additionalDataToCollectOnCheckout: z.object({customFields: z.array(z.union([z.object({fieldType: z.literal("string"), label: z.string(), sublabel: z.string(), isRequired: z.boolean().optional()}), z.object({fieldType: z.literal("select"), label: z.string(), sublabel: z.string(), isRequired: z.boolean(), options: z.array(z.string())})]))}).optional()})})])})}), z.object({status: z.literal("error"), errorMessage: 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 }; /** * Create custom pricing plan for a product * * POST /public/create-digital-product-pricing-plan */ export const createApiPublicCreateDigitalProductPricingPlan = async (args: CreateApiPublicCreateDigitalProductPricingPlanArgs, options?: CreateApiPublicCreateDigitalProductPricingPlanOptions) => { const {baseUrl = "https://backend.codedamn.com/api", fetch: fetchFn = fetch, FERMIONAPIKEY} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (FERMIONAPIKEY !== undefined) headers.set("FERMION-API-KEY", FERMIONAPIKEY) const url = `${baseUrl.replace(/\/+$/, '')}/public/create-digital-product-pricing-plan${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(createApiPublicCreateDigitalProductPricingPlanResponse, await res.json()) };