import {z} from 'zod' export type CreateApiWebhookEventsWalletCreditBalanceRecoveredOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const typesAlertThresholdType = z.enum(["absolute", "percentage"]); export const typesAlertCondition = z.enum(["above", "below"]); export const typesAlertThreshold = z.object({condition: typesAlertCondition.optional(), threshold: z.string().optional()}); export const typesAlertSettings = z.object({alert_enabled: z.boolean().optional(), alert_threshold_type: typesAlertThresholdType.optional(), critical: typesAlertThreshold.optional(), info: typesAlertThreshold.optional(), warning: typesAlertThreshold.optional()}); export const webhookDtoWalletAlertInfo = z.object({alert_settings: typesAlertSettings.optional(), alert_type: z.string().optional(), credit_balance: z.number().optional(), current_balance: z.number().optional(), state: z.string().optional()}); export const webhookDtoCustomer = z.object({address_city: z.string().optional(), address_country: z.string().optional(), address_line1: z.string().optional(), address_line2: z.string().optional(), address_postal_code: z.string().optional(), address_state: z.string().optional(), billing_entity_id: z.string().optional(), email: z.string().optional(), environment_id: z.string().optional(), external_id: z.string().optional(), id: z.string().optional(), metadata: z.record(z.string(), z.string()).optional(), name: z.string().optional(), timezone: z.string().optional()}); 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 typesWalletStatus = z.enum(["active", "frozen", "closed"]); export const typesWalletType = z.enum(["PRE_PAID", "POST_PAID"]); export const webhookDtoWallet = z.object({balance: z.string().optional(), conversion_rate: z.string().optional(), credit_balance: z.string().optional(), currency: z.string().optional(), customer_id: z.string().optional(), id: z.string().optional(), name: z.string().optional(), wallet_status: typesWalletStatus.optional(), wallet_type: typesWalletType.optional()}); export const webhookDtoWalletWebhookPayload = z.object({alert: webhookDtoWalletAlertInfo.optional(), customer: webhookDtoCustomer.optional(), event_type: typesWebhookEventName.optional(), wallet: webhookDtoWallet.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 }; /** * wallet.credit_balance.recovered * * POST /webhook-events/wallet.credit_balance.recovered */ export const createApiWebhookEventsWalletCreditBalanceRecovered = async (options?: CreateApiWebhookEventsWalletCreditBalanceRecoveredOptions) => { 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/wallet.credit_balance.recovered${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(webhookDtoWalletWebhookPayload, await res.json()) };