/* * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT. */ import * as z from "zod/v3"; import { safeParse } from "../../lib/schemas.js"; import { ClosedEnum } from "../../types/enums.js"; import { Result as SafeParseResult } from "../../types/fp.js"; import { SDKValidationError } from "../errors/sdkvalidationerror.js"; /** * The payment processor via which the sale was made. */ export const PaymentProcessor = { Stripe: "stripe", Shopify: "shopify", Polar: "polar", Paddle: "paddle", Revenuecat: "revenuecat", Custom: "custom", } as const; /** * The payment processor via which the sale was made. */ export type PaymentProcessor = ClosedEnum; export type TrackSaleRequestBody = { /** * The unique ID of the customer in your system. Will be used to identify and attribute all future events to this customer. */ customerExternalId: string; /** * The amount of the sale in cents (for all two-decimal currencies). If the sale is in a zero-decimal currency, pass the full integer value (e.g. `1580` JPY). Learn more: https://d.to/currency */ amount: number; /** * The currency of the sale. Accepts ISO 4217 currency codes. Sales will be automatically converted and stored as USD at the latest exchange rates. Learn more: https://d.to/currency */ currency?: string | undefined; /** * The name of the sale event. Recommended format: `Invoice paid` or `Subscription created`. */ eventName?: string | undefined; /** * The payment processor via which the sale was made. */ paymentProcessor?: PaymentProcessor | undefined; /** * The invoice ID of the sale. Can be used as a idempotency key – only one sale event can be recorded for a given invoice ID. */ invoiceId?: string | null | undefined; /** * Additional metadata to be stored with the sale event. Max 10,000 characters when stringified. */ metadata?: { [k: string]: any } | null | undefined; /** * The name of the lead event that occurred before the sale (case-sensitive). This is used to associate the sale event with a particular lead event (instead of the latest lead event for a link-customer combination, which is the default behavior). For direct sale tracking, this field can also be used to specify the lead event name. */ leadEventName?: string | null | undefined; /** * [For direct sale tracking]: The unique ID of the click that the sale conversion event is attributed to. You can read this value from `dub_id` cookie. */ clickId?: string | null | undefined; /** * [For direct sale tracking]: The name of the customer. If not passed, a random name will be generated (e.g. “Big Red Caribou”). */ customerName?: string | null | undefined; /** * [For direct sale tracking]: The email address of the customer. */ customerEmail?: string | null | undefined; /** * [For direct sale tracking]: The avatar URL of the customer. */ customerAvatar?: string | null | undefined; }; export type TrackSaleCustomer = { id: string; name: string | null; email: string | null; avatar: string | null; externalId: string | null; }; export type Sale = { amount: number; currency: string; paymentProcessor: string; invoiceId: string | null; metadata: { [k: string]: any } | null; }; /** * A sale was tracked. */ export type TrackSaleResponseBody = { eventName: string; customer: TrackSaleCustomer | null; sale: Sale | null; }; /** @internal */ export const PaymentProcessor$outboundSchema: z.ZodNativeEnum< typeof PaymentProcessor > = z.nativeEnum(PaymentProcessor); /** @internal */ export type TrackSaleRequestBody$Outbound = { customerExternalId: string; amount: number; currency: string; eventName: string; paymentProcessor: string; invoiceId: string | null; metadata?: { [k: string]: any } | null | undefined; leadEventName: string | null; clickId?: string | null | undefined; customerName: string | null; customerEmail: string | null; customerAvatar: string | null; }; /** @internal */ export const TrackSaleRequestBody$outboundSchema: z.ZodType< TrackSaleRequestBody$Outbound, z.ZodTypeDef, TrackSaleRequestBody > = z.object({ customerExternalId: z.string(), amount: z.number().int(), currency: z.string().default("usd"), eventName: z.string().default("Purchase"), paymentProcessor: PaymentProcessor$outboundSchema.default("custom"), invoiceId: z.nullable(z.string()).default(null), metadata: z.nullable(z.record(z.any())).optional(), leadEventName: z.nullable(z.string()).default(null), clickId: z.nullable(z.string()).optional(), customerName: z.nullable(z.string()).default(null), customerEmail: z.nullable(z.string()).default(null), customerAvatar: z.nullable(z.string()).default(null), }); export function trackSaleRequestBodyToJSON( trackSaleRequestBody: TrackSaleRequestBody, ): string { return JSON.stringify( TrackSaleRequestBody$outboundSchema.parse(trackSaleRequestBody), ); } /** @internal */ export const TrackSaleCustomer$inboundSchema: z.ZodType< TrackSaleCustomer, z.ZodTypeDef, unknown > = z.object({ id: z.string(), name: z.nullable(z.string()), email: z.nullable(z.string()), avatar: z.nullable(z.string()), externalId: z.nullable(z.string()), }); export function trackSaleCustomerFromJSON( jsonString: string, ): SafeParseResult { return safeParse( jsonString, (x) => TrackSaleCustomer$inboundSchema.parse(JSON.parse(x)), `Failed to parse 'TrackSaleCustomer' from JSON`, ); } /** @internal */ export const Sale$inboundSchema: z.ZodType = z .object({ amount: z.number(), currency: z.string(), paymentProcessor: z.string(), invoiceId: z.nullable(z.string()), metadata: z.nullable(z.record(z.any())), }); export function saleFromJSON( jsonString: string, ): SafeParseResult { return safeParse( jsonString, (x) => Sale$inboundSchema.parse(JSON.parse(x)), `Failed to parse 'Sale' from JSON`, ); } /** @internal */ export const TrackSaleResponseBody$inboundSchema: z.ZodType< TrackSaleResponseBody, z.ZodTypeDef, unknown > = z.object({ eventName: z.string(), customer: z.nullable(z.lazy(() => TrackSaleCustomer$inboundSchema)), sale: z.nullable(z.lazy(() => Sale$inboundSchema)), }); export function trackSaleResponseBodyFromJSON( jsonString: string, ): SafeParseResult { return safeParse( jsonString, (x) => TrackSaleResponseBody$inboundSchema.parse(JSON.parse(x)), `Failed to parse 'TrackSaleResponseBody' from JSON`, ); }