kiwi's notebooks

  • Billing Solutions Architect: User fee - /kiwi/stripe-billing-user-fees
    Last edited 6 years ago
    const stripe = require("stripe")(process.env.STRIPE_TEST_KEY); async function main() { // (Step 1) Create a product const product = await stripe.products.create({ type: "service", name: "User Fees" }); // (Step 2) Create a plan with a flat amount for the first 5 items const plan = await stripe.plans.create({ product: product.id, billing_scheme: "tiered", tiers_mode: "graduated", usage_type: "metered", aggregate_usage: "last_ever", interval: "month", currency: "usd", tiers: [ { up_to: 5, flat_amount: 7500 }, { up_to: "inf", unit_amount: 1500 } ] }); // (Step 3) Create a customer with a valid card const r = Math.floor(Math.random() * 1000); const customer = await stripe.customers.create({ source: "tok_visa", description: `Customer ${r}`, email: `ark+${r}@stripe.com` }); // (Step 4) Subscribe a customer const sub = await stripe.subscriptions.create({ customer: customer.id, items: [{ plan: plan.id }] }); const si_id = sub.items.data[0].id; // (Step 5) Set usage (effectively, number of seats) const urA = await stripe.usageRecords.create(si_id, { quantity: 1, timestamp: new Date().getTime() }); const urB = await stripe.usageRecords.create(si_id, { quantity: 6, timestamp: new Date().getTime() }); console.log(urA, urB); } main();