@hjam91/billing 0.9.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -19,6 +19,22 @@ npm install @hjam91/billing
19
19
 
20
20
  `hono` and `react` are optional peer dependencies, needed only for `@hjam91/billing/hono` and `@hjam91/billing/react`.
21
21
 
22
+ ## 0.10.0: tiered plans (priced by a count your app reports)
23
+
24
+ ```ts
25
+ // billing.config.ts — the ladder is the source of truth; the service renders it as one tiered Stripe price
26
+ plans: { masjid: { price: { currency: 'usd', interval: 'month', tiers: [{ up_to: 10, amount: 2500 }, { up_to: 25, amount: 3900 }, { up_to: 500, amount: 19900 }] }, unit: 'students' } }
27
+
28
+ await billing.checkout({ accountId, plan: 'masjid', quantity: students, successUrl, cancelUrl }); // starts at the right tier
29
+ const r = await billing.setQuantity(accountId, students); // idempotent: unchanged | applied (now, prorated up a tier; no proration within) | scheduled (down a tier, period end) | requires_action
30
+ if (r.subscription?.over_max) contactUs(r.subscription.reported_quantity); // past the ladder: still active, held at the top tier
31
+ const { plans } = await billing.plans(); // plan.tiers [{from, up_to, amount}], max_quantity, unit
32
+ await billing.changePlan(accountId, 'masjid', { quantity: 6 }); // another ladder, with the count
33
+ ```
34
+
35
+ Above the ladder, Checkout and plan changes throw `custom_pricing_required`; the owner sets a custom plan or exempts the account.
36
+ `setQuantity` is `changeQuantity` under its new name. Needs a service on migration 0018.
37
+
22
38
  ## 0.8.0: discounts and offers
23
39
 
24
40
  Needs a billing service with migration 0016. Owner-made discount codes customers type, and automatic offers.
package/dist/config.d.ts CHANGED
@@ -20,11 +20,20 @@
20
20
  */
21
21
  export type FeatureType = 'boolean' | 'metered' | 'static' | 'credit_system';
22
22
  export interface PriceSpec {
23
- /** Minor units (1900 = $19.00). */
24
- amount: number;
23
+ /** Minor units (1900 = $19.00). Omitted when `tiers` prices the plan. */
24
+ amount?: number;
25
25
  currency: string;
26
26
  interval?: 'day' | 'week' | 'month' | 'year';
27
27
  interval_count?: number;
28
+ /**
29
+ * 0.10: a ladder — the plan is priced by its quantity (a count the app reports: students, seats) in flat tiers.
30
+ * Ascending `up_to` boundaries (inclusive), a flat `amount` for the whole tier; the last `up_to` is the most the
31
+ * plan sells (above it: custom pricing). The service renders it as one volume-tiered price.
32
+ */
33
+ tiers?: Array<{
34
+ up_to: number;
35
+ amount: number;
36
+ }>;
28
37
  }
29
38
  /** true / false for an on/off feature, or the allowance of a counted one. */
30
39
  export type ItemSpec = boolean | {
@@ -58,6 +67,8 @@ export interface PlanSpec {
58
67
  hidden?: true;
59
68
  trial_days?: number;
60
69
  items?: Record<string, ItemSpec>;
70
+ /** 0.10: what the quantity counts ("students"), shown on the pricing page. */
71
+ unit?: string;
61
72
  }
62
73
  export interface BillingConfig {
63
74
  automatic_tax?: boolean;
package/dist/index.d.ts CHANGED
@@ -333,6 +333,15 @@ export interface Billing {
333
333
  changeQuantity(accountId: string, quantity: number, opts?: PlanChangeOptions): Promise<AccountStatus & {
334
334
  change: PlanChange;
335
335
  }>;
336
+ /**
337
+ * 0.10: report the subscription's quantity (on a tiered plan, the count it is priced by: students, seats). Idempotent and
338
+ * cheap: call it on every change and in a daily reconcile. Up a tier: now, prorated; down a tier: at the period end;
339
+ * within a tier: quantity only, no proration; above the ladder: held at the top tier with `change.over_max`.
340
+ * The same as changeQuantity (its name since 0.9).
341
+ */
342
+ setQuantity(accountId: string, quantity: number, opts?: PlanChangeOptions): Promise<AccountStatus & {
343
+ change: PlanChange;
344
+ }>;
336
345
  previewQuantity(accountId: string, quantity: number, opts?: PlanChangeOptions): Promise<ChangePreview>;
337
346
  cancelScheduledChange(accountId: string, select?: Omit<SubscriptionSelector, 'all'>): Promise<AccountStatus>;
338
347
  /** Create a subscription billed by invoice, when enabled in the app's billing policy. */
@@ -388,5 +397,7 @@ export interface PlanChangeOptions {
388
397
  prorationDate?: number;
389
398
  subscriptionId?: string;
390
399
  coversAccountId?: string;
400
+ /** 0.10: set the quantity with the plan change (the count on a tiered plan), in one prorated step. Above the ladder: custom_pricing_required. */
401
+ quantity?: number;
391
402
  }
392
403
  export declare function createBilling(opts: BillingOptions): Billing;
package/dist/index.js CHANGED
@@ -60,6 +60,7 @@ export const BATCH_LIMIT = 200;
60
60
  const changeBody = (accountId, plan, o = {}) => ({
61
61
  account_id: accountId,
62
62
  plan,
63
+ ...(o.quantity !== undefined ? { quantity: o.quantity } : {}),
63
64
  ...(o.when ? { when: o.when } : {}),
64
65
  ...(o.proration ? { proration: o.proration } : {}),
65
66
  ...(o.prorationDate !== undefined ? { proration_date: o.prorationDate } : {}),
@@ -500,6 +501,9 @@ export function createBilling(opts) {
500
501
  return r;
501
502
  },
502
503
  previewChange: (accountId, plan, o) => call('POST', '/v1/subscriptions/change/preview', changeBody(accountId, plan, o)),
504
+ async setQuantity(accountId, quantity, o = {}) {
505
+ return self.changeQuantity(accountId, quantity, o);
506
+ },
503
507
  async changeQuantity(accountId, quantity, o = {}) {
504
508
  const { plan: _plan, ...body } = changeBody(accountId, '', o);
505
509
  const result = await call('POST', '/v1/subscriptions/quantity', { ...body, quantity });
package/dist/types.d.ts CHANGED
@@ -208,10 +208,16 @@ export type BillingEvent = EntitlementChangedEvent | UsageEvent;
208
208
  export interface PlanChange {
209
209
  quantity?: number;
210
210
  from_quantity?: number;
211
- status: 'applied' | 'scheduled' | 'requires_action';
211
+ /** 'unchanged' (0.10): the reported quantity was already in place; nothing was sent to the provider. */
212
+ status: 'applied' | 'scheduled' | 'requires_action' | 'unchanged';
212
213
  plan: string;
213
214
  from_plan: string | null;
214
- direction: 'upgrade' | 'downgrade' | 'interval';
215
+ direction: 'upgrade' | 'downgrade' | 'interval' | 'none';
216
+ /** 0.10, tiered plans: the count was above the ladder; the subscription is held at the top tier (see SubscriptionView.over_max). */
217
+ over_max?: boolean;
218
+ reported_quantity?: number | null;
219
+ /** 0.10: an unchanged report canceled a pending downgrade. */
220
+ schedule_canceled?: boolean;
215
221
  effective_at: string | null;
216
222
  /** requires_action: the invoice page where the customer completes 3DS / pays. */
217
223
  action_url: string | null;
@@ -255,6 +261,18 @@ export interface PublicPlan {
255
261
  amount: number;
256
262
  expires_in_days?: number;
257
263
  }>;
264
+ /** 0.10: a tiered plan's ladder, from its config: the flat amount (minor units) for each range of the quantity. */
265
+ tiers?: PlanTier[];
266
+ /** 0.10: the most a tiered plan sells; above it the service refuses Checkout and flags a subscription `over_max`. */
267
+ max_quantity?: number;
268
+ /** 0.10: what the quantity counts ("students"). */
269
+ unit?: string;
270
+ }
271
+ /** One tier of a tiered plan (since 0.10.0). `from`..`up_to` inclusive, `amount` is the flat price of the tier. */
272
+ export interface PlanTier {
273
+ from: number;
274
+ up_to: number;
275
+ amount: number;
258
276
  }
259
277
  /** A discount as an app shows it (since 0.8.0). Amounts in minor units. */
260
278
  export interface DiscountView {
@@ -328,6 +346,17 @@ export interface SubscriptionView {
328
346
  quantity: number;
329
347
  effective_at: string;
330
348
  };
349
+ /** 0.10, tiered plans: the tier the current quantity falls in. */
350
+ tier?: PlanTier & {
351
+ currency: string;
352
+ interval: string | null;
353
+ };
354
+ /** 0.10, tiered plans: the ladder's top boundary. */
355
+ max_quantity?: number;
356
+ /** 0.10: the app reported a count above the ladder; the subscription stays at the top tier and access continues ("contact us"). */
357
+ over_max?: boolean;
358
+ /** 0.10: that reported count (null within the ladder). */
359
+ reported_quantity?: number | null;
331
360
  invoice?: {
332
361
  id: string;
333
362
  due_at: string | null;
@@ -453,7 +482,7 @@ export interface EntitlementChangedEvent {
453
482
  /** Error codes the API returns (stable; branch on these, not on messages). */
454
483
  export type BillingErrorCode = 'missing_credentials' | 'invalid_credentials' | 'app_disabled'
455
484
  /** 0.7 security: the service is locked down (only the owner key works). */
456
- | 'service_locked_down' | 'ip_not_allowed' | 'rate_limited' | 'too_many_auth_failures' | 'invalid_json' | 'invalid_response' | 'invalid_account_id' | 'invalid_field' | 'invalid_email' | 'invalid_url' | 'origin_not_allowed' | 'unknown_plan' | 'price_not_found' | 'exempt' | 'covered_by_account' | 'already_subscribed' | 'no_billing_customer' | 'no_subscription' | 'coverage_self' | 'coverage_chain' | 'trial_used' | 'trial_unavailable' | 'cancel_failed' | 'account_not_found' | 'portal_not_configured' | 'covers_self' | 'too_many_covers' | 'covered_elsewhere' | 'accounts_not_eligible' | 'not_launched' | 'promotion_code_not_supported' | 'parent_chain' | 'parent_self' | 'too_many_ids' | 'key_scope_denied' | 'invoice_not_found' | 'invoice_not_paid' | 'nothing_refundable' | 'over_refundable' | 'bad_amount' | 'no_charge' | 'feature_not_found' | 'feature_not_metered' | 'invalid_feature_id' | 'too_many_events' | 'plan_not_purchasable' | 'lock_not_found' | 'lock_not_held' | 'already_lifetime' | 'covers_not_supported' | 'resume_failed' | 'same_plan' | 'same_quantity' | 'operation_in_progress' | 'invoice_billing_disabled' | 'invoice_required' | 'purchase_not_found' | 'purchase_not_refundable' | 'purchase_credits_reserved' | 'refund_conflict' | 'add_on_requires_base' | 'not_an_add_on' | 'add_on_not_found' | 'already_entitled' | 'currency_mismatch' | 'pack_too_small'
485
+ | 'service_locked_down' | 'ip_not_allowed' | 'rate_limited' | 'too_many_auth_failures' | 'invalid_json' | 'invalid_response' | 'invalid_account_id' | 'invalid_field' | 'invalid_email' | 'invalid_url' | 'origin_not_allowed' | 'unknown_plan' | 'price_not_found' | 'exempt' | 'covered_by_account' | 'already_subscribed' | 'no_billing_customer' | 'no_subscription' | 'coverage_self' | 'coverage_chain' | 'trial_used' | 'trial_unavailable' | 'cancel_failed' | 'account_not_found' | 'portal_not_configured' | 'covers_self' | 'too_many_covers' | 'covered_elsewhere' | 'accounts_not_eligible' | 'not_launched' | 'promotion_code_not_supported' | 'parent_chain' | 'parent_self' | 'too_many_ids' | 'key_scope_denied' | 'invoice_not_found' | 'invoice_not_paid' | 'nothing_refundable' | 'over_refundable' | 'bad_amount' | 'no_charge' | 'feature_not_found' | 'feature_not_metered' | 'invalid_feature_id' | 'too_many_events' | 'plan_not_purchasable' | 'lock_not_found' | 'lock_not_held' | 'already_lifetime' | 'covers_not_supported' | 'resume_failed' | 'same_plan' | 'same_quantity' | 'custom_pricing_required' | 'operation_in_progress' | 'invoice_billing_disabled' | 'invoice_required' | 'purchase_not_found' | 'purchase_not_refundable' | 'purchase_credits_reserved' | 'refund_conflict' | 'add_on_requires_base' | 'not_an_add_on' | 'add_on_not_found' | 'already_entitled' | 'currency_mismatch' | 'pack_too_small'
457
486
  /** 0.7 review / prerelease: codes the service answers that earlier unions missed. */
458
487
  | 'price_not_owned' | 'reason_required' | 'prune_refused' | 'storage_unavailable' | 'invalid_admin_key' | 'app_not_found' | 'not_found' | 'forbidden' | 'misconfigured' | 'stripe_not_configured' | 'stripe_permission' | 'stripe_key_unreadable' | 'no_stripe_key' | 'invalid_config' | 'invalid_items' | 'invalid_plans' | 'invalid_origin' | 'invalid_event' | 'invalid_signature' | 'invalid_app_id' | 'confirm_required' | 'feature_exists' | 'feature_in_use' | 'too_many_features' | 'plan_exists' | 'plan_not_found' | 'version_not_found' | 'key_not_found' | 'too_many_keys' | 'has_live_subscriptions' | 'mark_key_in_use' | 'no_accounts' | 'no_customer' | 'no_payment_method' | 'no_pending_setup' | 'no_subscription_item' | 'price_unreadable' | 'customer_owned_by_another_account' | 'different_customer_already_linked' | 'subscription_customer_mismatch' | 'subscription_missing_in_stripe' | 'subscription_owned_by_another_account' | 'webhook_not_found' | 'no_webhook_url' | 'no_webhook_secret' | 'invalid_webhook_secret' | 'invalid_stripe_key' | 'stripe_mode_mismatch' | 'endpoint_exists' | 'unsupported_media_type'
459
488
  /** 0.8 discounts and offers. `discount_invalid` carries `details.reason` (a DiscountReason). */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hjam91/billing",
3
- "version": "0.9.0",
3
+ "version": "0.10.0",
4
4
  "description": "Zero-dependency client for a billing-kit service: cached, fail-open entitlement and feature checks, usage tracking, Stripe Checkout / portal, signed webhook verification, a Hono gate and an optional React billing card.",
5
5
  "keywords": [
6
6
  "billing",