import {z} from 'zod' export type CreateApiWebhookEventsPaymentAttemptFailedOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const typesMetadata = z.record(z.string(), z.string()); export const webhookDtoPaymentAttempt = z.object({attempt_number: z.number().int().optional(), created_at: z.string().optional(), error_message: z.string().optional(), gateway_attempt_id: z.string().optional(), id: z.string().optional(), metadata: typesMetadata.optional()}); export const typesCheckoutAction = z.enum(["create_subscription", "modify_subscription", "wallet_topup", "add_addon", "pay_invoice"]); export const typesCheckoutStatus = z.enum(["initiated", "pending", "completed", "failed", "expired"]); export const typesPaymentActionType = z.enum(["checkout_url", "payment_link"]); export const typesPaymentAction = z.object({type: typesPaymentActionType.optional(), url: z.string().optional()}); export const typesCheckoutPaymentProvider = z.enum(["razorpay", "chargebee", "stripe"]); export const webhookDtoCheckoutSession = z.object({action: typesCheckoutAction.optional(), cancelled_at: z.string().optional(), checkout_invoice_id: z.string().optional(), checkout_payment_id: z.string().optional(), checkout_status: typesCheckoutStatus.optional(), completed_at: z.string().optional(), customer_id: z.string().optional(), expires_at: z.string().optional(), failure_reason: z.string().optional(), id: z.string().optional(), payment_action: typesPaymentAction.optional(), payment_provider: typesCheckoutPaymentProvider.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 typesPaymentDestinationType = z.enum(["INVOICE", "CUSTOMER"]); export const typesPaymentMethodType = z.enum(["CARD", "ACH", "OFFLINE", "CREDITS", "PAYMENT_LINK", "UPI"]); export const typesPaymentStatus = z.enum(["INITIATED", "PENDING", "PROCESSING", "SUCCEEDED", "OVERPAID", "FAILED", "REFUNDED", "PARTIALLY_REFUNDED", "VOIDED"]); export const webhookDtoPayment = z.object({amount: z.string().optional(), currency: z.string().optional(), destination_id: z.string().optional(), destination_type: typesPaymentDestinationType.optional(), error_message: z.string().optional(), failed_at: z.string().optional(), id: z.string().optional(), invoice_number: z.string().optional(), payment_gateway: z.string().optional(), payment_method_type: typesPaymentMethodType.optional(), payment_status: typesPaymentStatus.optional(), refunded_at: z.string().optional(), succeeded_at: z.string().optional(), voided_at: z.string().optional()}); export const webhookDtoPaymentAttemptWebhookPayload = z.object({attempt: webhookDtoPaymentAttempt.optional(), checkout_session: webhookDtoCheckoutSession.optional(), event_type: typesWebhookEventName.optional(), payment: webhookDtoPayment.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 }; /** * payment.attempt.failed * * POST /webhook-events/payment.attempt.failed */ export const createApiWebhookEventsPaymentAttemptFailed = async (options?: CreateApiWebhookEventsPaymentAttemptFailedOptions) => { 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/payment.attempt.failed${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(webhookDtoPaymentAttemptWebhookPayload, await res.json()) };