@ordinatio/billing 1.3.0 → 1.5.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 +18 -5
- package/dist/index.js +26 -5
- package/package.json +1 -1
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: {
|
|
@@ -308,4 +308,17 @@ declare function paddleTransactionBody(items: ReadonlyArray<{
|
|
|
308
308
|
quantity: number;
|
|
309
309
|
}>, organizationId: string): TransactionBody;
|
|
310
310
|
|
|
311
|
-
|
|
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
|
+
}
|
|
322
|
+
declare function manageActionsFor(record: ManageRecord, now: Date): ManageActions;
|
|
323
|
+
|
|
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 };
|
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"
|
|
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: "
|
|
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: "
|
|
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 === "
|
|
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,8 +311,26 @@ 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
|
}
|
|
320
|
+
|
|
321
|
+
// src/manage.ts
|
|
322
|
+
var NOTHING = { state: "none", endsAt: null, updatePayment: false, cancel: 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 };
|
|
326
|
+
if (record.status === "past_due") return { state: "past_due", endsAt: null, updatePayment: true, cancel: true };
|
|
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 };
|
|
330
|
+
return { state: "ended", endsAt: null, updatePayment: false, cancel: false };
|
|
331
|
+
}
|
|
332
|
+
return NOTHING;
|
|
333
|
+
}
|
|
314
334
|
export {
|
|
315
335
|
DAY_MS,
|
|
316
336
|
InvalidPricePlanError,
|
|
@@ -324,6 +344,7 @@ export {
|
|
|
324
344
|
definePricePlan,
|
|
325
345
|
formatCents,
|
|
326
346
|
isLocked,
|
|
347
|
+
manageActionsFor,
|
|
327
348
|
mustCheckOnRenew,
|
|
328
349
|
paddleCatalog,
|
|
329
350
|
paddleItems,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ordinatio/billing",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.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",
|