SaaS биллинг на основе использования (Usage-Based Billing)
Посентажный или метрический биллинг: клиент платит за фактическое потребление — API-вызовы, GB трафика, активных пользователей, сгенерированные изображения. Stripe Meters — современный способ реализации.
Stripe Meters (новый API)
// Создание Meter
const meter = await stripe.billing.meters.create({
display_name: 'API Calls',
event_name: 'api_call',
default_aggregation: {
formula: 'sum',
},
customer_mapping: {
event_payload_key: 'stripe_customer_id',
type: 'by_id',
},
value_settings: {
event_payload_key: 'value', // количество в каждом событии
},
});
// Создание Price привязанной к Meter
const price = await stripe.prices.create({
currency: 'usd',
unit_amount: 100, // $0.01 за единицу
recurring: {
interval: 'month',
usage_type: 'metered',
aggregate_usage: 'sum',
},
billing_scheme: 'per_unit',
product: productId,
});
Отправка событий использования
// Отправка события при каждом API-вызове
export async function trackApiUsage(
customerId: string,
quantity: number = 1,
metadata?: Record<string, string>
) {
await stripe.billing.meterEvents.create({
event_name: 'api_call',
payload: {
stripe_customer_id: customerId,
value: quantity.toString(),
...metadata,
},
timestamp: Math.floor(Date.now() / 1000),
});
}
// Middleware для автоматического трекинга
export function trackUsageMiddleware(req: Request, res: Response, next: NextFunction) {
const originalEnd = res.end;
res.end = function(...args) {
// Трекаем только успешные API-вызовы
if (res.statusCode < 400 && req.user?.stripeCustomerId) {
trackApiUsage(req.user.stripeCustomerId, 1, {
endpoint: req.path,
method: req.method,
}).catch(console.error);
}
return originalEnd.apply(this, args);
};
next();
}
Тарифные ступени (Tiered Pricing)
// Tiered pricing: чем больше используешь, тем дешевле единица
const tieredPrice = await stripe.prices.create({
currency: 'usd',
billing_scheme: 'tiered',
tiers_mode: 'graduated', // или 'volume'
tiers: [
{
up_to: 1000,
unit_amount: 100, // $0.01 за каждый из первых 1000
},
{
up_to: 10000,
unit_amount: 50, // $0.005 за следующие 9000
},
{
up_to: 'inf',
unit_amount: 10, // $0.001 за всё сверх 10000
},
],
recurring: {
interval: 'month',
usage_type: 'metered',
aggregate_usage: 'sum',
},
product: productId,
});
Локальный трекинг использования
Stripe Meters имеют задержку. Для real-time лимитов — локальный счётчик:
// Redis: real-time счётчики использования
import { Redis } from '@upstash/redis';
const redis = new Redis({
url: process.env.UPSTASH_REDIS_URL!,
token: process.env.UPSTASH_REDIS_TOKEN!,
});
export async function checkAndIncrementUsage(
tenantId: string,
resource: string,
limit: number
): Promise<{ allowed: boolean; current: number; limit: number }> {
const key = `usage:${tenantId}:${resource}:${getCurrentMonthKey()}`;
// Атомарная операция: проверка + инкремент
const pipeline = redis.pipeline();
pipeline.incr(key);
pipeline.expire(key, 60 * 60 * 24 * 35); // 35 дней
const [current] = await pipeline.exec() as [number, number];
if (current > limit) {
// Откатываем инкремент
await redis.decr(key);
return { allowed: false, current: current - 1, limit };
}
// Отправляем в Stripe асинхронно
syncUsageToStripe(tenantId, resource, 1).catch(console.error);
return { allowed: true, current, limit };
}
function getCurrentMonthKey(): string {
const now = new Date();
return `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}`;
}
UI: виджет использования
// components/UsageWidget.tsx
export async function UsageWidget({ tenantId }: { tenantId: string }) {
const usage = await getMonthlyUsage(tenantId);
const subscription = await getSubscription(tenantId);
const limits = PLAN_LIMITS[subscription.plan];
return (
<div className="space-y-4">
{Object.entries(usage).map(([resource, current]) => {
const limit = limits[resource as keyof typeof limits];
const percentage = limit === Infinity
? 0
: Math.min((current / limit) * 100, 100);
return (
<div key={resource}>
<div className="flex justify-between text-sm mb-1">
<span className="capitalize">{resource.replace(/_/g, ' ')}</span>
<span>
{current.toLocaleString()}
{limit !== Infinity && ` / ${limit.toLocaleString()}`}
</span>
</div>
{limit !== Infinity && (
<div className="h-2 bg-gray-200 rounded">
<div
className={`h-2 rounded transition-all ${
percentage > 90 ? 'bg-red-500' :
percentage > 70 ? 'bg-yellow-500' : 'bg-blue-500'
}`}
style={{ width: `${percentage}%` }}
/>
</div>
)}
</div>
);
})}
<div className="text-xs text-gray-500">
Сбрасывается {getNextBillingDate(subscription.currentPeriodEnd).toLocaleDateString('ru-RU')}
</div>
</div>
);
}
Настройка usage-based биллинга через Stripe Meters с Redis счётчиками и UI виджетом — 3–5 рабочих дней.







