@hjam91/billing 0.12.0 → 0.13.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
@@ -77,6 +77,11 @@ for (const o of await billing.offers(orgId)) {
77
77
  discount on the account's own live discounted subscriptions so the next invoice is full price, and answers the
78
78
  account status (`support` scope, like `cancel`; 404 `discount_not_found` when there is none, 409
79
79
  `operation_in_progress` under a pending plan change). The code or offer itself is untouched.
80
+ - Admin, since 0.13.0: `grantFreeMonths(accountId, {idempotencyKey, months?, reason?})` gives the account's own paying
81
+ monthly subscription free renewals (100% off from now; it stays active; rewards add up; a retried key answers the
82
+ first grant) and answers a `FreeMonthsGrant` (`support` scope; 409 `no_subscription` → give a trial instead,
83
+ `subscription_ending`, `interval_not_supported`, `idempotency_key_reused`). `freeMonths(accountId)` lists the grants
84
+ (`read`). `AccountStatus.first_paid_at` says when the account first paid real money (absent = never).
80
85
  - `billingRoutes` (Hono) adds `GET /offers`, `POST /discount {code, plan?}`, `POST /accept-offer {offer_id}`, and
81
86
  `discount_code` / `offer_id` on `POST /checkout`; a refused code answers its `reason`.
82
87
 
package/dist/admin.d.ts CHANGED
@@ -13,7 +13,7 @@
13
13
  * Every method throws BillingError on failure (nothing here fails open: these
14
14
  * are actions and owner reads, not entitlement checks).
15
15
  */
16
- import type { AccountStatus, DiscountView, OfferAudience } from './types.js';
16
+ import type { AccountStatus, DiscountView, FreeMonthsGrant, OfferAudience } from './types.js';
17
17
  export interface BillingAdminOptions {
18
18
  baseUrl: string;
19
19
  appId: string;
@@ -330,6 +330,21 @@ export interface BillingAdmin {
330
330
  removeDiscount(accountId: string, params?: {
331
331
  subscriptionId?: string;
332
332
  }): Promise<AccountStatus>;
333
+ /**
334
+ * Free months on the account's OWN paying monthly subscription (a referral reward): the next `months` (default 1,
335
+ * at most 12) renewals are invoiced at 0 and it stays active; rewards add up. Idempotent per `idempotencyKey` (a
336
+ * retry answers the first grant, `replayed: true`). 409 no_subscription (give a trial instead: setTrial),
337
+ * subscription_ending, interval_not_supported, operation_in_progress, idempotency_key_reused. Support scope; since 0.13.0.
338
+ */
339
+ grantFreeMonths(accountId: string, params: {
340
+ idempotencyKey: string;
341
+ months?: number;
342
+ reason?: string;
343
+ }): Promise<FreeMonthsGrant>;
344
+ /** The account's free-month grants, newest first (read; since 0.13.0). */
345
+ freeMonths(accountId: string): Promise<{
346
+ grants: FreeMonthsGrant[];
347
+ }>;
333
348
  }
334
349
  export declare function createBillingAdmin(opts: BillingAdminOptions): BillingAdmin;
335
350
  export {};
package/dist/admin.js CHANGED
@@ -112,6 +112,12 @@ export function createBillingAdmin(opts) {
112
112
  })).offer,
113
113
  redemptions: (o = {}) => call('GET', `/redemptions${o.accountId ? `?account=${enc(o.accountId)}` : ''}`),
114
114
  removeDiscount: (accountId, p = {}) => call('POST', `/accounts/${enc(accountId)}/discount/remove`, p.subscriptionId ? { subscription_id: p.subscriptionId } : {}),
115
+ grantFreeMonths: (accountId, p) => call('POST', `/accounts/${enc(accountId)}/free-months`, {
116
+ idempotency_key: p.idempotencyKey,
117
+ ...(p.months !== undefined ? { months: p.months } : {}),
118
+ ...(p.reason !== undefined ? { reason: p.reason } : {}),
119
+ }),
120
+ freeMonths: (accountId) => call('GET', `/accounts/${enc(accountId)}/free-months`),
115
121
  purchases: (accountId) => call('GET', `/accounts/${enc(accountId)}/purchases`),
116
122
  refundPurchase: (accountId, purchaseId, o) => call('POST', `/accounts/${enc(accountId)}/purchases/${enc(purchaseId)}/refund`, o),
117
123
  invoices: (accountId, o = {}) => call('GET', `/accounts/${enc(accountId)}/invoices${o.limit ? `?limit=${o.limit}` : ''}`),
package/dist/types.d.ts CHANGED
@@ -446,6 +446,29 @@ export interface AccountStatus {
446
446
  created_at?: string | null;
447
447
  /** Only on auto-renew / cancel answers. Since 0.4.0. */
448
448
  results?: SubscriptionResult[];
449
+ /** When this account first paid an invoice with money in it (ISO); absent = never. Since 0.13.0. */
450
+ first_paid_at?: string | null;
451
+ }
452
+ /** POST /admin/apps/:app/accounts/:id/free-months (admin.grantFreeMonths). Since 0.13.0. */
453
+ export interface FreeMonthsGrant {
454
+ /** The next N monthly renewals are invoiced at 0 (no charge, no tax); the subscription stays active. */
455
+ kind: 'free_invoices';
456
+ account_id: string;
457
+ subscription_id: string;
458
+ /** Months this grant added. */
459
+ months: number;
460
+ /** Free renewals now pending on the subscription (this grant plus earlier unused ones). */
461
+ free_months_total: number;
462
+ /** What one month is worth before tax and other discounts, minor units (an estimate). */
463
+ value_per_month: number | null;
464
+ currency: string | null;
465
+ /** End of the last free period (an estimate). */
466
+ free_until: string | null;
467
+ reason: string | null;
468
+ idempotency_key: string;
469
+ created_at: string;
470
+ /** True when a retried idempotency key answered the stored first grant. */
471
+ replayed: boolean;
449
472
  }
450
473
  /** PUT /v1/accounts/:id and POST /v1/accounts/upsert. A field left out is left alone; null clears it. Since 0.4.0. */
451
474
  export interface AccountInput {
@@ -482,7 +505,9 @@ export interface EntitlementChangedEvent {
482
505
  /** Error codes the API returns (stable; branch on these, not on messages). */
483
506
  export type BillingErrorCode = 'missing_credentials' | 'invalid_credentials' | 'app_disabled'
484
507
  /** 0.7 security: the service is locked down (only the owner key works). */
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' | 'tax_behavior_locked' | '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'
508
+ | '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' | 'tax_behavior_locked'
509
+ /** 0.13 free months: refused when the subscription is set to end or not billed monthly, or a key is reused for another account. */
510
+ | 'subscription_ending' | 'interval_not_supported' | 'idempotency_key_reused' | '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'
486
511
  /** 0.7 review / prerelease: codes the service answers that earlier unions missed. */
487
512
  | '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'
488
513
  /** 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.12.0",
3
+ "version": "0.13.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",