Payment infrastructure might seem like a mundane technical detail, but for AI products, it's the difference between a hobby project and a sustainable business. Unlike traditional SaaS where users pay a flat monthly fee, AI products face unique challenges: variable compute costs, unpredictable usage patterns, and the need to meter API calls or token consumption in real-time.
123456789101112import Stripe from 'stripe'; const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!); // 1. Create a Product for your AI service const product = await stripe.products.create({ name: 'AI API Access', description: 'Pay-per-use access to our AI models', metadata: { tier: 'api', version: '2024-01' }
123456789101112import Stripe from 'stripe'; import { db } from './database'; const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!); async function reportUsageToStripe() { // Get unbilled usage records from our database const pendingUsage = await db.usageRecords.findMany({ where: { reportedToStripe: false }, include: { user: true } });
123456789101112import Stripe from 'stripe'; import { Redis } from 'ioredis'; const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!); const redis = new Redis(process.env.REDIS_URL!); interface UsageEvent { userId: string; subscriptionItemId: string; quantity: number; timestamp: number; eventType: 'tokens' | 'api_calls' | 'compute_minutes';
123456789101112import Stripe from 'stripe'; const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!); // Enable automatic tax calculation on checkout async function createTaxEnabledCheckout( customerId: string, priceId: string, customerLocation: { country: string; postalCode?: string } ): Promise<Stripe.Checkout.Session> { // First, update customer with tax location
123456789101112import Stripe from 'stripe'; import { db } from './database'; const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!); interface ProcessedEvent { eventId: string; processedAt: Date; } export async function handleWebhook(req: Request) { const sig = req.headers.get('stripe-signature')!;
123456789101112import Stripe from 'stripe'; const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!); interface DowngradeResult { success: boolean; effectiveDate: Date; refundAmount?: number; message: string; } export async function handleDowngrade(
123456789101112import Stripe from 'stripe'; const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!); export async function handleDisputeCreated( dispute: Stripe.Dispute ) { // Immediately alert the team await sendSlackAlert({ channel: '#billing-alerts', message: `🚨 New dispute: $${dispute.amount / 100} - ${dispute.reason}`, priority: 'high'