Convert Stripe Charge JSON to TypeScript Interfaces
Use json to typescript to model a Stripe Charge response. Covers nullable refunds, money amounts, metadata maps, and PaymentMethod nesting.
Detailed Explanation
A Real Stripe Charge in TypeScript
Stripe responses include enums for status, integer money amounts, freeform metadata maps, and deep nesting under payment_method_details. A naive converter loses this structure.
Example JSON (trimmed)
{
"id": "ch_3PqW2L2eZvKYlo2C0abc1234",
"object": "charge",
"amount": 2500,
"currency": "usd",
"status": "succeeded",
"captured": true,
"paid": true,
"refunded": false,
"customer": "cus_QabcDEF",
"metadata": { "order_id": "ord_991", "campaign": "summer-sale" },
"payment_method_details": {
"type": "card",
"card": { "brand": "visa", "last4": "4242", "exp_month": 12, "exp_year": 2027 }
}
}
Generated TypeScript
interface CardDetails {
brand: "visa" | "mastercard" | "amex" | "discover" | "jcb" | "unionpay";
last4: string;
exp_month: number;
exp_year: number;
}
interface PaymentMethodDetails {
type: "card";
card: CardDetails;
}
interface Charge {
id: string;
object: "charge";
amount: number; // smallest currency unit (cents)
currency: string; // ISO 4217, lowercase
status: "succeeded" | "pending" | "failed";
captured: boolean;
paid: boolean;
refunded: boolean;
customer: string | null;
metadata: Record<string, string>;
payment_method_details: PaymentMethodDetails;
}
Stripe-Specific Hints for the Converter
amountis always an integer in the smallest unit. Type it asnumber, neverstring— even though display code may format it.metadatais alwaysRecord<string, string>; Stripe coerces all values to strings on ingest.objectis a literal discriminator across Stripe resources ("charge","customer","refund"). Keep it as a literal so a union over all resources can narrow.- Optional Stripe fields are usually
null, not absent. Prefercustomer: string | nullovercustomer?: string.
This shape composes well with the Stripe SDK's own types if you later install stripe, but generating it from raw JSON keeps frontend bundles slim.
Use Case
Building an internal admin dashboard that consumes Stripe webhooks via a Next.js API route and needs typed access to charge details without bundling the Stripe SDK.
Try It — JSON to TypeScript
Related Topics
Type a Generic Webhook Payload in TypeScript
Real-World API Schemas
JSON to TypeScript Discriminated Union — Result/Error Pattern
Real-World API Schemas
Convert GitHub Repository API JSON to TypeScript
Real-World API Schemas
Convert OpenAI Chat Completion JSON to TypeScript
Real-World API Schemas
Type ISO Date Strings vs Plain Strings in JSON-to-TypeScript
TypeScript Patterns