技能标识:stripemeter
Integrate Stripe usage-based billing with idempotent event ingestion, late-event handling, and pre-invoice reconciliation. Use when implementing usage metering, tracking API calls or seats, pushing usage to Stripe, handling billing drift, or building usage-based pricing.
StripeMeter 是一款原生集成 Stripe 的用量计量系统,可确保基于用量的计费获得准确的用量总计。它能对重试进行去重,通过水印处理延迟事件,维护持续运行的计数器,并仅将增量数据推送至 Stripe。
bash
git clone https://github.com/geminimir/stripemeter && cd stripemeter
cp .env.example .env && docker compose up -d && pnpm -r build
pnpm db:migrate && pnpm dev
bash
curl -X POST http://localhost:3000/v1/events/ingest \
-H Content-Type: application/json \
-d {
events: [{
tenantId: your-tenant-id,
metric: api_calls,
customerRef: cus_ABC123,
quantity: 100,
ts: 2025-01-16T14:30:00Z
}]
}
bash
curl -X POST http://localhost:3000/v1/usage/projection \
-H Content-Type: application/json \
-d {tenantId: your-tenant-id, customerRef: cus_ABC123}
typescript
import { createClient } from @stripemeter/sdk-node;
const client = createClient({
apiUrl: http://localhost:3000,
tenantId: your-tenant-id,
customerId: cus_ABC123
});
// 记录用量
await client.track({
metric: api_calls,
customerRef: cus_ABC123,
quantity: 100,
meta: { endpoint: /v1/search }
});
// 获取实时用量
const usage = await client.getUsage(cus_ABC123);
const projection = await client.getProjection(cus_ABC123);
python
from stripemeter import StripeMeterClient
client = StripeMeterClient(
api_url=http://localhost:3000,
tenant_id=your-tenant-id,
customerid=cusABC123
)
client.track(
metric=api_calls,
customerref=cusABC123,
quantity=100
)
如需直接集成 Stripe,请使用 stripe-driver 包:
typescript
import { StripeBillingDriverImpl } from @stripemeter/stripe-driver;
const driver = new StripeBillingDriverImpl({
liveKey: process.env.STRIPESECRETKEY,
testKey: process.env.STRIPETESTSECRET_KEY
});
// 向 Stripe 记录用量
await driver.recordUsage({
mode: live,
stripeAccount: default,
subscriptionItemId: si_xxx,
quantity: 100,
periodStart: 2025-01-01,
idempotencyKey: unique-key
});
// 获取用量摘要
const summary = await driver.getUsageSummary(
si_xxx,
2025-01-01,
default
);
在不影响实际发票的情况下测试 Stripe 用量提交:
typescript
import { InvoiceSimulator } from @stripemeter/pricing-lib;
const simulator = new InvoiceSimulator();
const result = simulator.simulate({
customerId: test,
periodStart: 2024-01-01,
periodEnd: 2024-02-01,
usageItems: [{
metric: api_calls,
quantity: 25000,
priceConfig: {
model: tiered,
currency: USD,
tiers: [
{ upTo: 10000, unitPrice: 0.01 },
{ upTo: 50000, unitPrice: 0.008 },
{ upTo: null, unitPrice: 0.005 }
]
}
}]
});
stripemeter/
├── packages/
│ ├── core/ # 共享类型、模式
│ ├── database/ # Drizzle ORM + Redis
│ ├── pricing-lib/ # 定价计算器
│ ├── stripe-driver/ # 直接 Stripe API 驱动
│ ├── sdk-node/ # Node.js SDK
│ └── sdk-python/ # Python SDK
├── apps/
│ ├── api/ # REST API (Fastify)
│ ├── workers/ # 后台工作进程 (BullMQ)
│ ├── admin-ui/ # 管理后台
│ └── customer-widget/# 可嵌入组件
bash
STRIPESECRETKEY=sklivexxx # 正式 Stripe 密钥
STRIPETESTSECRETKEY=sktest_xxx # 测试 Stripe 密钥(用于影子模式)
DATABASE_URL=postgres://... # PostgreSQL 连接
REDIS_URL=redis://... # Redis 连接
bash
检查本地总计与 Stripe 之间的偏差:
bash
curl -X POST http://localhost:3000/v1/replay \
-H Content-Type: application/json \
-d {tenantId: X, dryRun: true}
以下为平台配置的接入选项,并非逐项实测通过。能否安装取决于客户端支持、技能来源和运行环境:
帮我安装 SkillHub 和 stripemeter-1776295457 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 stripemeter-1776295457 技能
skillhub install stripemeter-1776295457
文件大小: 18.25 KB | 发布时间: 2026-4-16 15:40