@ordinatio/billing 1.5.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
@@ -318,7 +318,27 @@ interface ManageActions {
318
318
  endsAt: Date | null;
319
319
  updatePayment: boolean;
320
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;
321
323
  }
322
324
  declare function manageActionsFor(record: ManageRecord, now: Date): ManageActions;
323
325
 
324
- export { type Access, type Band, type BillingRecord, type CanStartCheckout, type CheckoutRecord, DAY_MS, type Interval, InvalidPricePlanError, 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, manageActionsFor, mustCheckOnRenew, paddleCatalog, paddleItems, paddleTransactionBody, priceFor, readPaddleSubscription, readPaddleTransaction, reusableCheckout, seatLimitFor, seatsUsed, syncPaddleCatalog, trialEndFrom, verifyPaddleSignature };
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
@@ -319,21 +319,42 @@ function paddleTransactionBody(items, organizationId) {
319
319
  }
320
320
 
321
321
  // src/manage.ts
322
- var NOTHING = { state: "none", endsAt: null, updatePayment: false, cancel: false };
322
+ var NOTHING = { state: "none", endsAt: null, updatePayment: false, cancel: false, keep: false };
323
323
  function manageActionsFor(record, now) {
324
324
  if (record === null) return NOTHING;
325
- if (record.status === "active") return { state: "active", endsAt: null, updatePayment: true, cancel: true };
326
- if (record.status === "past_due") return { state: "past_due", endsAt: null, updatePayment: true, cancel: true };
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
327
  if (record.status === "canceled" || record.status === "paused") {
328
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 };
330
- return { state: "ended", endsAt: null, updatePayment: false, cancel: false };
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
331
  }
332
332
  return NOTHING;
333
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
+ }
334
354
  export {
335
355
  DAY_MS,
336
356
  InvalidPricePlanError,
357
+ KEEP_LOCK_MS,
337
358
  PENDING_CHECKOUT_MS,
338
359
  accessFor,
339
360
  averagePerSeatCents,
@@ -344,6 +365,7 @@ export {
344
365
  definePricePlan,
345
366
  formatCents,
346
367
  isLocked,
368
+ keepPlanCheck,
347
369
  manageActionsFor,
348
370
  mustCheckOnRenew,
349
371
  paddleCatalog,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ordinatio/billing",
3
- "version": "1.5.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",