@ordinatio/billing 1.3.0 → 1.4.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
@@ -225,7 +225,7 @@ type SignatureResult = {
225
225
  ok: true;
226
226
  } | {
227
227
  ok: false;
228
- reason: 'missing' | 'malformed' | 'stale' | 'mismatch';
228
+ reason: 'missing' | 'malformed' | 'stale' | 'mismatch' | 'config';
229
229
  };
230
230
  interface SignatureInput {
231
231
  header: string | null | undefined;
@@ -280,11 +280,11 @@ type CanStartCheckout = {
280
280
  ok: true;
281
281
  } | {
282
282
  ok: false;
283
- reason: 'no_plan' | 'already_paying' | 'still_covered';
283
+ reason: 'no_plan' | 'already_paying' | 'still_covered' | 'paused';
284
284
  };
285
285
  declare function canStartCheckout(record: CheckoutRecord, now: Date): CanStartCheckout;
286
- /** What a checkout was made for, as text: `month:25`. */
287
- declare const checkoutKey: (interval: Interval, seats: number) => string;
286
+ /** What a checkout was made for, as text: `month:25`, or `cranberry-crm:month:25` when the catalog key is given (a changed catalog then never reuses a checkout made from the old prices). */
287
+ declare const checkoutKey: (interval: Interval, seats: number, catalogKey?: string) => string;
288
288
  declare const PENDING_CHECKOUT_MS: number;
289
289
  /** The waiting transaction id to give the person again, or null when there is none, it is too old, or it was made for something else. */
290
290
  declare function reusableCheckout(pending: {
package/dist/index.js CHANGED
@@ -198,7 +198,8 @@ function sameText(a, b) {
198
198
  function verifyPaddleSignature(input) {
199
199
  const { header, rawBody, secret, nowSeconds, hmacHex } = input;
200
200
  if (typeof header !== "string" || header.trim() === "") return { ok: false, reason: "missing" };
201
- if (typeof rawBody !== "string" || typeof secret !== "string" || secret === "" || !Number.isFinite(nowSeconds)) return { ok: false, reason: "malformed" };
201
+ if (typeof rawBody !== "string") return { ok: false, reason: "malformed" };
202
+ if (typeof secret !== "string" || secret === "" || !Number.isFinite(nowSeconds)) return { ok: false, reason: "config" };
202
203
  let ts = null;
203
204
  const hashes = [];
204
205
  for (const part of header.split(";")) {
@@ -215,13 +216,13 @@ function verifyPaddleSignature(input) {
215
216
  }
216
217
  if (ts === null || !/^\d{1,12}$/.test(ts) || hashes.length === 0 || hashes.some((hash) => !/^[0-9a-f]{64}$/.test(hash))) return { ok: false, reason: "malformed" };
217
218
  const tolerance = input.toleranceSeconds ?? 300;
218
- if (!Number.isFinite(tolerance) || tolerance < 0) return { ok: false, reason: "malformed" };
219
+ if (!Number.isFinite(tolerance) || tolerance < 0) return { ok: false, reason: "config" };
219
220
  if (Math.abs(nowSeconds - Number(ts)) > tolerance) return { ok: false, reason: "stale" };
220
221
  let expected;
221
222
  try {
222
223
  expected = hmacHex(secret, `${ts}:${rawBody}`).toLowerCase();
223
224
  } catch {
224
- return { ok: false, reason: "mismatch" };
225
+ return { ok: false, reason: "config" };
225
226
  }
226
227
  let matched = false;
227
228
  for (const hash of hashes) if (sameText(hash, expected)) matched = true;
@@ -294,12 +295,13 @@ function readPaddleTransaction(data) {
294
295
  function canStartCheckout(record, now) {
295
296
  if (record === null) return { ok: false, reason: "no_plan" };
296
297
  if (record.status === "trialing") return { ok: true };
297
- if (record.status === "canceled" || record.status === "paused") {
298
+ if (record.status === "paused") return { ok: false, reason: "paused" };
299
+ if (record.status === "canceled") {
298
300
  return record.currentPeriodEnd !== null && record.currentPeriodEnd.getTime() > now.getTime() ? { ok: false, reason: "still_covered" } : { ok: true };
299
301
  }
300
302
  return { ok: false, reason: "already_paying" };
301
303
  }
302
- var checkoutKey = (interval, seats) => `${interval}:${seats}`;
304
+ var checkoutKey = (interval, seats, catalogKey) => catalogKey ? `${catalogKey}:${interval}:${seats}` : `${interval}:${seats}`;
303
305
  var PENDING_CHECKOUT_MS = 30 * 6e4;
304
306
  function reusableCheckout(pending, wantedKey, now, maxAgeMs = PENDING_CHECKOUT_MS) {
305
307
  if (pending.transactionId === null || pending.transactionId === "" || pending.at === null || pending.key !== wantedKey) return null;
@@ -309,6 +311,10 @@ function reusableCheckout(pending, wantedKey, now, maxAgeMs = PENDING_CHECKOUT_M
309
311
  function paddleTransactionBody(items, organizationId) {
310
312
  if (items.length === 0) throw new Error("A checkout needs at least one item.");
311
313
  if (typeof organizationId !== "string" || organizationId === "") throw new Error("A checkout needs the organization id.");
314
+ for (const item of items) {
315
+ if (typeof item.priceId !== "string" || item.priceId === "") throw new Error("Every checkout item needs a price id.");
316
+ if (!Number.isInteger(item.quantity) || item.quantity < 1) throw new Error("Every checkout item needs a whole quantity of at least 1.");
317
+ }
312
318
  return { items: items.map((item) => ({ price_id: item.priceId, quantity: item.quantity })), custom_data: { organization_id: organizationId }, collection_mode: "automatic" };
313
319
  }
314
320
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ordinatio/billing",
3
- "version": "1.3.0",
3
+ "version": "1.4.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",