@ordinatio/billing 1.4.0 → 1.6.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/dist/index.d.ts CHANGED
@@ -308,4 +308,37 @@ declare function paddleTransactionBody(items: ReadonlyArray<{
308
308
  quantity: number;
309
309
  }>, organizationId: string): TransactionBody;
310
310
 
311
- export { type Access, type Band, type BillingRecord, type CanStartCheckout, type CheckoutRecord, DAY_MS, type Interval, InvalidPricePlanError, type LockCandidate, type LockDecision, PENDING_CHECKOUT_MS, type PaddleCatalog, type PaddleCatalogOptions, type PaddleCatalogPort, type PaddleExistingPrice, type PaddleItems, type PaddlePriceSpec, type PaddleSyncResult, type PlanStatus, type PlanUpdate, type Price, type PriceLine, type PricePlan, type PricePlanInput, type SeatCheck, type SeatSubscription, type SignatureInput, type SignatureResult, type SubscriptionReading, type TransactionBody, type TransactionCompleted, accessFor, averagePerSeatCents, canAddSeat, canStartCheckout, checkoutKey, decideLock, definePricePlan, formatCents, isLocked, mustCheckOnRenew, paddleCatalog, paddleItems, paddleTransactionBody, priceFor, readPaddleSubscription, readPaddleTransaction, reusableCheckout, seatLimitFor, seatsUsed, syncPaddleCatalog, trialEndFrom, verifyPaddleSignature };
311
+ type ManageRecord = {
312
+ status: string;
313
+ currentPeriodEnd: Date | null;
314
+ } | null;
315
+ interface ManageActions {
316
+ state: 'active' | 'past_due' | 'ends_on' | 'pauses_on' | 'ended' | 'none';
317
+ /** The date a scheduled end or pause takes effect; null in every other state. */
318
+ endsAt: Date | null;
319
+ updatePayment: boolean;
320
+ cancel: boolean;
321
+ /** A scheduled cancel or pause may be undone (`ends_on` and `pauses_on` only; `keepPlanCheck` then checks the live subscription). */
322
+ keep: boolean;
323
+ }
324
+ declare function manageActionsFor(record: ManageRecord, now: Date): ManageActions;
325
+
326
+ /** Paddle refuses to change a subscription this close to a renewal or to the end of its period. */
327
+ declare const KEEP_LOCK_MS: number;
328
+ interface KeepExpectation {
329
+ subscriptionId: string;
330
+ customerId: string;
331
+ /** The state the page showed: a scheduled cancel (`ends_on`) or a scheduled pause (`pauses_on`). */
332
+ stored: 'ends_on' | 'pauses_on';
333
+ now: Date;
334
+ }
335
+ type KeepPlanCheck = {
336
+ ok: true;
337
+ remove: 'cancel' | 'pause';
338
+ } | {
339
+ ok: false;
340
+ reason: 'unusable' | 'mismatch' | 'ended' | 'not_available' | 'nothing_scheduled' | 'stale' | 'too_late';
341
+ };
342
+ declare function keepPlanCheck(live: unknown, expect: KeepExpectation): KeepPlanCheck;
343
+
344
+ export { type Access, type Band, type BillingRecord, type CanStartCheckout, type CheckoutRecord, DAY_MS, type Interval, InvalidPricePlanError, KEEP_LOCK_MS, type KeepExpectation, type KeepPlanCheck, type LockCandidate, type LockDecision, type ManageActions, type ManageRecord, PENDING_CHECKOUT_MS, type PaddleCatalog, type PaddleCatalogOptions, type PaddleCatalogPort, type PaddleExistingPrice, type PaddleItems, type PaddlePriceSpec, type PaddleSyncResult, type PlanStatus, type PlanUpdate, type Price, type PriceLine, type PricePlan, type PricePlanInput, type SeatCheck, type SeatSubscription, type SignatureInput, type SignatureResult, type SubscriptionReading, type TransactionBody, type TransactionCompleted, accessFor, averagePerSeatCents, canAddSeat, canStartCheckout, checkoutKey, decideLock, definePricePlan, formatCents, isLocked, keepPlanCheck, manageActionsFor, mustCheckOnRenew, paddleCatalog, paddleItems, paddleTransactionBody, priceFor, readPaddleSubscription, readPaddleTransaction, reusableCheckout, seatLimitFor, seatsUsed, syncPaddleCatalog, trialEndFrom, verifyPaddleSignature };
package/dist/index.js CHANGED
@@ -317,9 +317,44 @@ function paddleTransactionBody(items, organizationId) {
317
317
  }
318
318
  return { items: items.map((item) => ({ price_id: item.priceId, quantity: item.quantity })), custom_data: { organization_id: organizationId }, collection_mode: "automatic" };
319
319
  }
320
+
321
+ // src/manage.ts
322
+ var NOTHING = { state: "none", endsAt: null, updatePayment: false, cancel: false, keep: false };
323
+ function manageActionsFor(record, now) {
324
+ if (record === null) return NOTHING;
325
+ if (record.status === "active") return { state: "active", endsAt: null, updatePayment: true, cancel: true, keep: false };
326
+ if (record.status === "past_due") return { state: "past_due", endsAt: null, updatePayment: true, cancel: true, keep: false };
327
+ if (record.status === "canceled" || record.status === "paused") {
328
+ const end = record.currentPeriodEnd;
329
+ if (end !== null && now.getTime() < end.getTime()) return { state: record.status === "canceled" ? "ends_on" : "pauses_on", endsAt: end, updatePayment: false, cancel: false, keep: true };
330
+ return { state: "ended", endsAt: null, updatePayment: false, cancel: false, keep: false };
331
+ }
332
+ return NOTHING;
333
+ }
334
+
335
+ // src/keep.ts
336
+ var KEEP_LOCK_MS = 30 * 6e4;
337
+ var isObject2 = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
338
+ var no = (reason) => ({ ok: false, reason });
339
+ function keepPlanCheck(live, expect) {
340
+ if (!isObject2(live) || typeof live.status !== "string") return no("unusable");
341
+ if (live.id !== expect.subscriptionId || live.customer_id !== expect.customerId) return no("mismatch");
342
+ if (live.status === "canceled") return no("ended");
343
+ if (live.status !== "active" && live.status !== "past_due") return no("not_available");
344
+ const change = live.scheduled_change;
345
+ if (change === null || change === void 0) return no("nothing_scheduled");
346
+ if (!isObject2(change) || typeof change.action !== "string") return no("unusable");
347
+ if (change.action !== "cancel" && change.action !== "pause") return no("not_available");
348
+ if (change.action !== (expect.stored === "ends_on" ? "cancel" : "pause")) return no("stale");
349
+ const effective = typeof change.effective_at === "string" ? Date.parse(change.effective_at) : Number.NaN;
350
+ if (Number.isNaN(effective)) return no("unusable");
351
+ if (!(effective - expect.now.getTime() > KEEP_LOCK_MS)) return no("too_late");
352
+ return { ok: true, remove: change.action };
353
+ }
320
354
  export {
321
355
  DAY_MS,
322
356
  InvalidPricePlanError,
357
+ KEEP_LOCK_MS,
323
358
  PENDING_CHECKOUT_MS,
324
359
  accessFor,
325
360
  averagePerSeatCents,
@@ -330,6 +365,8 @@ export {
330
365
  definePricePlan,
331
366
  formatCents,
332
367
  isLocked,
368
+ keepPlanCheck,
369
+ manageActionsFor,
333
370
  mustCheckOnRenew,
334
371
  paddleCatalog,
335
372
  paddleItems,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ordinatio/billing",
3
- "version": "1.4.0",
3
+ "version": "1.6.0",
4
4
  "private": false,
5
5
  "license": "MIT",
6
6
  "description": "Billing rules for any subscription product, pure and with no database: graduated seat pricing from a plan you pass in (validated once, in integer cents), the trial / paying / locked state machine, which team to use when one is locked, and seat counting with the can-add-a-seat rule",