import {z} from 'zod' export type CreateApiWebhookEventsBillingInstalmentCreatedOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const typesWebhookEventName = z.enum(["subscription.created", "subscription.draft.created", "subscription.activated", "subscription.updated", "subscription.paused", "subscription.cancelled", "subscription.resumed", "subscription.plan_changed", "subscription.phase.created", "subscription.phase.updated", "subscription.phase.deleted", "feature.created", "feature.updated", "feature.deleted", "feature.wallet_balance.alert", "entitlement.created", "entitlement.updated", "entitlement.deleted", "commitment.created", "commitment.updated", "commitment.terminated", "billing_terms.created", "billing_terms.updated", "billing_instalment.created", "billing_instalment.updated", "billing_instalment.cancelled", "billing_instalment.invoiced", "wallet.created", "wallet.updated", "wallet.terminated", "wallet.transaction.created", "wallet.transaction.updated", "payment.created", "payment.updated", "payment.failed", "payment.attempt.failed", "payment.success", "payment.pending", "refund.created", "refund.succeeded", "refund.failed", "customer.created", "customer.updated", "customer.deleted", "invoice.update.finalized", "invoice.update.payment", "invoice.update.voided", "invoice.update", "invoice.payment.overdue", "invoice.sync.success", "invoice.sync.failed", "wallet.credit_balance.dropped", "wallet.credit_balance.recovered", "wallet.ongoing_balance.dropped", "wallet.ongoing_balance.recovered", "wallet.ongoing_balance.updated", "subscription.spend.threshold_reached", "subscription.spend.threshold_recovered", "subscription.line_item_spend.threshold_reached", "subscription.line_item_spend.threshold_recovered", "subscription.group_spend.threshold_reached", "subscription.group_spend.threshold_recovered", "entitlement.grant.exhausted", "subscription.renewal.due", "invoice.communication.triggered", "credit_note.created", "credit_note.updated", "checkout.session.initiated", "checkout.session.completed", "checkout.session.failed", "checkout.session.expired", "event.rejected"]); export const typesInstalmentBillTrigger = z.enum(["date", "manual"]); export const typesInstalmentDelivery = z.enum(["subscription_invoice", "separate_invoice"]); export const typesInstalmentStatus = z.enum(["scheduled", "invoiced", "cancelled"]); export const typesInstalmentKind = z.enum(["instalment", "settlement"]); export const typesMetadata = z.record(z.string(), z.string()); export const typesBillingTermOwnerType = z.literal("commitment"); export const typesStatus = z.enum(["published", "deleted", "archived"]); export const billingtermInstalment = z.object({amount: z.string().optional(), bill_on: z.string().optional(), bill_trigger: typesInstalmentBillTrigger.optional(), billing_term_id: z.string().optional(), created_at: z.string().optional(), created_by: z.string().optional(), currency: z.string().optional(), customer_id: z.string().optional(), delivery: typesInstalmentDelivery.optional(), environment_id: z.string().optional(), id: z.string().optional(), instalment_status: typesInstalmentStatus.optional(), invoice_id: z.string().optional(), invoice_label: z.string().optional(), kind: typesInstalmentKind.optional(), metadata: typesMetadata.optional(), owner_id: z.string().optional(), owner_type: typesBillingTermOwnerType.optional(), service_period_end: z.string().optional(), service_period_start: z.string().optional(), status: typesStatus.optional(), subscription_id: z.string().optional(), tenant_id: z.string().optional(), triggered_at: z.string().optional(), updated_at: z.string().optional(), updated_by: z.string().optional()}); export const webhookDtoBillingInstalmentWebhookPayload = z.object({event_type: typesWebhookEventName.optional(), instalment: billingtermInstalment.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 }; /** * billing_instalment.created * * POST /webhook-events/billing_instalment.created */ export const createApiWebhookEventsBillingInstalmentCreated = async (options?: CreateApiWebhookEventsBillingInstalmentCreatedOptions) => { const {baseUrl = "https://us.api.flexprice.io/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/webhook-events/billing_instalment.created${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(webhookDtoBillingInstalmentWebhookPayload, await res.json()) };