@antelopejs/payment-subscriptions 0.0.0-stage → 0.0.2
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/LICENSE +190 -0
- package/README.md +102 -2
- package/dist/calendar.d.ts +2 -0
- package/dist/calendar.js +33 -0
- package/dist/calendar.js.map +1 -0
- package/dist/clock.d.ts +7 -0
- package/dist/clock.js +18 -0
- package/dist/clock.js.map +1 -0
- package/dist/config.d.ts +13 -0
- package/dist/config.js +28 -0
- package/dist/config.js.map +1 -0
- package/dist/engine.d.ts +38 -0
- package/dist/engine.js +250 -0
- package/dist/engine.js.map +1 -0
- package/dist/events.d.ts +6 -0
- package/dist/events.js +47 -0
- package/dist/events.js.map +1 -0
- package/dist/implementation.d.ts +19 -0
- package/dist/implementation.js +233 -0
- package/dist/implementation.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +75 -0
- package/dist/index.js.map +1 -0
- package/dist/initial.d.ts +11 -0
- package/dist/initial.js +86 -0
- package/dist/initial.js.map +1 -0
- package/dist/records.d.ts +60 -0
- package/dist/records.js +68 -0
- package/dist/records.js.map +1 -0
- package/dist/runtime.d.ts +10 -0
- package/dist/runtime.js +14 -0
- package/dist/runtime.js.map +1 -0
- package/dist/schedule.d.ts +15 -0
- package/dist/schedule.js +35 -0
- package/dist/schedule.js.map +1 -0
- package/dist/transitions.d.ts +25 -0
- package/dist/transitions.js +197 -0
- package/dist/transitions.js.map +1 -0
- package/dist/triggers.d.ts +10 -0
- package/dist/triggers.js +129 -0
- package/dist/triggers.js.map +1 -0
- package/dist/view.d.ts +3 -0
- package/dist/view.js +34 -0
- package/dist/view.js.map +1 -0
- package/docs/1.introduction.md +61 -0
- package/docs/2.renewal-clock.md +95 -0
- package/docs/3.failed-renewals.md +126 -0
- package/docs/4.provider-schedule.md +31 -0
- package/package.json +85 -4
- package/skills/payment-subscriptions/SKILL.md +73 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.construct = construct;
|
|
37
|
+
exports.start = start;
|
|
38
|
+
exports.stop = stop;
|
|
39
|
+
exports.destroy = destroy;
|
|
40
|
+
exports.ActiveClock = ActiveClock;
|
|
41
|
+
const logging_1 = require("@antelopejs/interface-core/logging");
|
|
42
|
+
const interface_core_1 = require("@antelopejs/interface-core");
|
|
43
|
+
const subscriptions = __importStar(require("@antelopejs/interface-subscriptions"));
|
|
44
|
+
const engine_1 = require("./engine");
|
|
45
|
+
const schedule_1 = require("./schedule");
|
|
46
|
+
const runtime_1 = require("./runtime");
|
|
47
|
+
const implementation_1 = require("./implementation");
|
|
48
|
+
const config_1 = require("./config");
|
|
49
|
+
const triggers_1 = require("./triggers");
|
|
50
|
+
let trigger;
|
|
51
|
+
function construct(config) {
|
|
52
|
+
const settings = (0, config_1.ResolveSettings)(config);
|
|
53
|
+
const schedule = (0, schedule_1.ResolveSchedule)(settings.providerSchedule);
|
|
54
|
+
(0, runtime_1.SetRuntime)({ engine: new engine_1.Engine(settings, schedule), settings, schedule });
|
|
55
|
+
(0, interface_core_1.ImplementInterface)(subscriptions, implementation_1.Implementation);
|
|
56
|
+
}
|
|
57
|
+
async function start() {
|
|
58
|
+
const { engine, settings } = (0, runtime_1.Runtime)();
|
|
59
|
+
trigger = await (0, triggers_1.ChooseTrigger)(engine, settings);
|
|
60
|
+
engine.onWrite((record) => trigger?.schedule(record));
|
|
61
|
+
trigger.start();
|
|
62
|
+
logging_1.Logging.Info(`payment-subscriptions: renewal clock is ${trigger.kind}`);
|
|
63
|
+
}
|
|
64
|
+
async function stop() {
|
|
65
|
+
await trigger?.stop();
|
|
66
|
+
trigger = undefined;
|
|
67
|
+
await (0, runtime_1.Runtime)().engine.drain();
|
|
68
|
+
}
|
|
69
|
+
function destroy() {
|
|
70
|
+
(0, runtime_1.SetRuntime)(undefined);
|
|
71
|
+
}
|
|
72
|
+
function ActiveClock() {
|
|
73
|
+
return trigger?.kind;
|
|
74
|
+
}
|
|
75
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],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CAAC;AAED,SAAgB,OAAO;IACrB,IAAA,oBAAU,EAAC,SAAS,CAAC,CAAC;AACxB,CAAC;AAGD,SAAgB,WAAW;IACzB,OAAO,OAAO,EAAE,IAAI,CAAC;AACvB,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { SubscriptionRequest } from "@antelopejs/interface-subscriptions";
|
|
2
|
+
import type { Settings } from "./config";
|
|
3
|
+
import type { SubscriptionRecord } from "./records";
|
|
4
|
+
export interface Creation {
|
|
5
|
+
id: string;
|
|
6
|
+
provider: string;
|
|
7
|
+
now: number;
|
|
8
|
+
instanceId: string;
|
|
9
|
+
settings: Settings;
|
|
10
|
+
}
|
|
11
|
+
export declare function InitialRecord(request: SubscriptionRequest, creation: Creation): SubscriptionRecord;
|
package/dist/initial.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.InitialRecord = InitialRecord;
|
|
4
|
+
const events_1 = require("./events");
|
|
5
|
+
const records_1 = require("./records");
|
|
6
|
+
const BEFORE_FIRST_PERIOD = -1;
|
|
7
|
+
const NO_LEASE = 0;
|
|
8
|
+
const ORDERS_PER_MILLISECOND = 1000;
|
|
9
|
+
let lastOrder = 0;
|
|
10
|
+
function creationOrder(now) {
|
|
11
|
+
lastOrder = Math.max(now * ORDERS_PER_MILLISECOND, lastOrder + 1);
|
|
12
|
+
return lastOrder;
|
|
13
|
+
}
|
|
14
|
+
function base(id, request, provider, now) {
|
|
15
|
+
return {
|
|
16
|
+
_id: id,
|
|
17
|
+
revision: (0, records_1.NewRevision)(),
|
|
18
|
+
phase: "live",
|
|
19
|
+
provider,
|
|
20
|
+
reference: request.reference,
|
|
21
|
+
customer: request.customer,
|
|
22
|
+
paymentMethod: request.paymentMethod,
|
|
23
|
+
amount: { value: request.amount.value, currency: request.amount.currency },
|
|
24
|
+
interval: request.interval,
|
|
25
|
+
intervalCount: request.intervalCount ?? 1,
|
|
26
|
+
description: request.description ?? null,
|
|
27
|
+
metadata: request.metadata ? { ...request.metadata } : null,
|
|
28
|
+
status: "active",
|
|
29
|
+
anchor: now,
|
|
30
|
+
periodIndex: BEFORE_FIRST_PERIOD,
|
|
31
|
+
currentPeriodStart: now,
|
|
32
|
+
currentPeriodEnd: now,
|
|
33
|
+
trialEnd: null,
|
|
34
|
+
cancelAtPeriodEnd: false,
|
|
35
|
+
endedAt: null,
|
|
36
|
+
endReason: null,
|
|
37
|
+
latestPayment: null,
|
|
38
|
+
attempt: 0,
|
|
39
|
+
pendingPayment: null,
|
|
40
|
+
refusedPayment: null,
|
|
41
|
+
refusal: null,
|
|
42
|
+
attemptMethod: null,
|
|
43
|
+
attemptStartedAt: null,
|
|
44
|
+
attemptReplayUntil: null,
|
|
45
|
+
reconciliationRequired: false,
|
|
46
|
+
providerReference: null,
|
|
47
|
+
nextActionAt: now,
|
|
48
|
+
dueAt: null,
|
|
49
|
+
leaseOwner: null,
|
|
50
|
+
leaseUntil: NO_LEASE,
|
|
51
|
+
outbox: [],
|
|
52
|
+
operationKeys: [],
|
|
53
|
+
createdAt: now,
|
|
54
|
+
createdOrder: creationOrder(now),
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
function deferred(record, startsAt, isTrial) {
|
|
58
|
+
const live = {
|
|
59
|
+
...record,
|
|
60
|
+
status: isTrial ? "trialing" : "active",
|
|
61
|
+
anchor: startsAt,
|
|
62
|
+
currentPeriodEnd: startsAt,
|
|
63
|
+
trialEnd: isTrial ? startsAt : null,
|
|
64
|
+
nextActionAt: startsAt,
|
|
65
|
+
dueAt: record.createdAt,
|
|
66
|
+
};
|
|
67
|
+
return {
|
|
68
|
+
...live,
|
|
69
|
+
outbox: [(0, events_1.RaiseEvent)(live, "subscription.created", record.createdAt)],
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
function InitialRecord(request, creation) {
|
|
73
|
+
const { id, provider, now, instanceId, settings } = creation;
|
|
74
|
+
const record = base(id, request, provider, now);
|
|
75
|
+
if (request.trialEnd !== undefined)
|
|
76
|
+
return deferred(record, request.trialEnd, true);
|
|
77
|
+
if (request.billingAnchor !== undefined)
|
|
78
|
+
return deferred(record, request.billingAnchor, false);
|
|
79
|
+
return {
|
|
80
|
+
...record,
|
|
81
|
+
phase: "creating",
|
|
82
|
+
leaseOwner: instanceId,
|
|
83
|
+
leaseUntil: now + settings.leaseMs,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
//# sourceMappingURL=initial.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"initial.js","sourceRoot":"","sources":["../src/initial.ts"],"names":[],"mappings":";;AA+FA,sCAgBC;AA7GD,qCAAsC;AACtC,uCAAwC;AAIxC,MAAM,mBAAmB,GAAG,CAAC,CAAC,CAAC;AAC/B,MAAM,QAAQ,GAAG,CAAC,CAAC;AACnB,MAAM,sBAAsB,GAAG,IAAI,CAAC;AAEpC,IAAI,SAAS,GAAG,CAAC,CAAC;AAElB,SAAS,aAAa,CAAC,GAAW;IAChC,SAAS,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,GAAG,sBAAsB,EAAE,SAAS,GAAG,CAAC,CAAC,CAAC;IAClE,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,SAAS,IAAI,CACX,EAAU,EACV,OAA4B,EAC5B,QAAgB,EAChB,GAAW;IAEX,OAAO;QACL,GAAG,EAAE,EAAE;QACP,QAAQ,EAAE,IAAA,qBAAW,GAAE;QACvB,KAAK,EAAE,MAAM;QACb,QAAQ;QACR,SAAS,EAAE,OAAO,CAAC,SAAS;QAC5B,QAAQ,EAAE,OAAO,CAAC,QAAQ;QAC1B,aAAa,EAAE,OAAO,CAAC,aAAa;QACpC,MAAM,EAAE,EAAE,KAAK,EAAE,OAAO,CAAC,MAAM,CAAC,KAAK,EAAE,QAAQ,EAAE,OAAO,CAAC,MAAM,CAAC,QAAQ,EAAE;QAC1E,QAAQ,EAAE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C,CAAC,IAAI;QACnC,YAAY,EAAE,QAAQ;QACtB,KAAK,EAAE,MAAM,CAAC,SAAS;KACxB,CAAC;IACF,OAAO;QACL,GAAG,IAAI;QACP,MAAM,EAAE,CAAC,IAAA,mBAAU,EAAC,IAAI,EAAE,sBAAsB,EAAE,MAAM,CAAC,SAAS,CAAC,CAAC;KACrE,CAAC;AACJ,CAAC;AAUD,SAAgB,aAAa,CAC3B,OAA4B,EAC5B,QAAkB;IAElB,MAAM,EAAE,EAAE,EAAE,QAAQ,EAAE,GAAG,EAAE,UAAU,EAAE,QAAQ,EAAE,GAAG,QAAQ,CAAC;IAC7D,MAAM,MAAM,GAAG,IAAI,CAAC,EAAE,EAAE,OAAO,EAAE,QAAQ,EAAE,GAAG,CAAC,CAAC;IAChD,IAAI,OAAO,CAAC,QAAQ,KAAK,SAAS;QAChC,OAAO,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;IAClD,IAAI,OAAO,CAAC,aAAa,KAAK,SAAS;QACrC,OAAO,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC,aAAa,EAAE,KAAK,CAAC,CAAC;IACxD,OAAO;QACL,GAAG,MAAM;QACT,KAAK,EAAE,UAAU;QACjB,UAAU,EAAE,UAAU;QACtB,UAAU,EAAE,GAAG,GAAG,QAAQ,CAAC,OAAO;KACnC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { Amount } from "@antelopejs/interface-payment";
|
|
2
|
+
import type { Subscription, SubscriptionEndReason, SubscriptionEventType, SubscriptionInterval, SubscriptionStatus } from "@antelopejs/interface-subscriptions";
|
|
3
|
+
type Phase = "creating" | "live" | "refused";
|
|
4
|
+
export type Snapshot = Omit<Subscription, "vendorData">;
|
|
5
|
+
export interface StoredEvent {
|
|
6
|
+
id: string;
|
|
7
|
+
type: SubscriptionEventType;
|
|
8
|
+
createdAt: number;
|
|
9
|
+
subscription: Snapshot;
|
|
10
|
+
payment: string | null;
|
|
11
|
+
}
|
|
12
|
+
export interface SubscriptionRecord {
|
|
13
|
+
_id: string;
|
|
14
|
+
revision: string;
|
|
15
|
+
phase: Phase;
|
|
16
|
+
provider: string;
|
|
17
|
+
reference: string;
|
|
18
|
+
customer: string;
|
|
19
|
+
paymentMethod: string;
|
|
20
|
+
amount: Amount;
|
|
21
|
+
interval: SubscriptionInterval;
|
|
22
|
+
intervalCount: number;
|
|
23
|
+
description: string | null;
|
|
24
|
+
metadata: Record<string, string> | null;
|
|
25
|
+
status: SubscriptionStatus;
|
|
26
|
+
anchor: number;
|
|
27
|
+
periodIndex: number;
|
|
28
|
+
currentPeriodStart: number;
|
|
29
|
+
currentPeriodEnd: number;
|
|
30
|
+
trialEnd: number | null;
|
|
31
|
+
cancelAtPeriodEnd: boolean;
|
|
32
|
+
endedAt: number | null;
|
|
33
|
+
endReason: SubscriptionEndReason | null;
|
|
34
|
+
latestPayment: string | null;
|
|
35
|
+
attempt: number;
|
|
36
|
+
pendingPayment: string | null;
|
|
37
|
+
refusedPayment: string | null;
|
|
38
|
+
refusal: string | null;
|
|
39
|
+
attemptMethod: string | null;
|
|
40
|
+
attemptStartedAt?: number | null;
|
|
41
|
+
attemptReplayUntil?: number | null;
|
|
42
|
+
reconciliationRequired?: boolean;
|
|
43
|
+
providerReference: string | null;
|
|
44
|
+
nextActionAt: number | null;
|
|
45
|
+
dueAt: number | null;
|
|
46
|
+
leaseOwner: string | null;
|
|
47
|
+
leaseUntil: number;
|
|
48
|
+
outbox: StoredEvent[];
|
|
49
|
+
operationKeys: string[];
|
|
50
|
+
createdAt: number;
|
|
51
|
+
createdOrder: number;
|
|
52
|
+
}
|
|
53
|
+
export type RecordPatch = Partial<Omit<SubscriptionRecord, "_id" | "revision">>;
|
|
54
|
+
export declare function NewRevision(): string;
|
|
55
|
+
export declare function LoadRecord(id: string): Promise<SubscriptionRecord | undefined>;
|
|
56
|
+
export declare function InsertRecord(record: SubscriptionRecord): Promise<boolean>;
|
|
57
|
+
export declare function MutateRecord(record: SubscriptionRecord, patch: RecordPatch): Promise<SubscriptionRecord | undefined>;
|
|
58
|
+
export declare function DueRecordIds(now: number, limit: number): Promise<string[]>;
|
|
59
|
+
export declare function CustomerRecords(customer: string, provider: string): Promise<SubscriptionRecord[]>;
|
|
60
|
+
export {};
|
package/dist/records.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NewRevision = NewRevision;
|
|
4
|
+
exports.LoadRecord = LoadRecord;
|
|
5
|
+
exports.InsertRecord = InsertRecord;
|
|
6
|
+
exports.MutateRecord = MutateRecord;
|
|
7
|
+
exports.DueRecordIds = DueRecordIds;
|
|
8
|
+
exports.CustomerRecords = CustomerRecords;
|
|
9
|
+
const node_crypto_1 = require("node:crypto");
|
|
10
|
+
const interface_database_1 = require("@antelopejs/interface-database");
|
|
11
|
+
const SCHEMA_ID = "payment-subscriptions";
|
|
12
|
+
const NOT_DUE = null;
|
|
13
|
+
const schema = new interface_database_1.Schema(SCHEMA_ID, {
|
|
14
|
+
subscriptions: {
|
|
15
|
+
fields: {},
|
|
16
|
+
indexes: { dueAt: {}, customer: {} },
|
|
17
|
+
},
|
|
18
|
+
});
|
|
19
|
+
function table() {
|
|
20
|
+
return schema.instance().table("subscriptions");
|
|
21
|
+
}
|
|
22
|
+
function NewRevision() {
|
|
23
|
+
return (0, node_crypto_1.randomUUID)();
|
|
24
|
+
}
|
|
25
|
+
async function LoadRecord(id) {
|
|
26
|
+
return (await table().get(id)) ?? undefined;
|
|
27
|
+
}
|
|
28
|
+
async function InsertRecord(record) {
|
|
29
|
+
try {
|
|
30
|
+
await table().insert(record);
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
if (await LoadRecord(record._id))
|
|
35
|
+
return false;
|
|
36
|
+
throw error;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
async function MutateRecord(record, patch) {
|
|
40
|
+
const nextRevision = NewRevision();
|
|
41
|
+
const outcome = await table().atomicMutation(record._id, {
|
|
42
|
+
type: "update",
|
|
43
|
+
revisionField: "revision",
|
|
44
|
+
expectedRevision: record.revision,
|
|
45
|
+
nextRevision,
|
|
46
|
+
patch,
|
|
47
|
+
});
|
|
48
|
+
if (outcome !== "applied")
|
|
49
|
+
return undefined;
|
|
50
|
+
return { ...record, ...patch, revision: nextRevision };
|
|
51
|
+
}
|
|
52
|
+
async function DueRecordIds(now, limit) {
|
|
53
|
+
const due = await table()
|
|
54
|
+
.filter((record) => record.key("dueAt").ne(NOT_DUE).and(record.key("dueAt").le(now)))
|
|
55
|
+
.orderBy("dueAt", "asc")
|
|
56
|
+
.slice(0, limit);
|
|
57
|
+
return due.map((record) => record._id);
|
|
58
|
+
}
|
|
59
|
+
async function CustomerRecords(customer, provider) {
|
|
60
|
+
return table()
|
|
61
|
+
.filter((record) => record
|
|
62
|
+
.key("customer")
|
|
63
|
+
.eq(customer)
|
|
64
|
+
.and(record.key("provider").eq(provider))
|
|
65
|
+
.and(record.key("phase").eq("live")))
|
|
66
|
+
.orderBy("createdOrder", "desc");
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=records.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"records.js","sourceRoot":"","sources":["../src/records.ts"],"names":[],"mappings":";;AAqFA,kCAEC;AAED,gCAIC;AAED,oCAUC;AAED,oCAcC;AAED,oCAWC;AAED,0CAaC;AArJD,6CAAyC;AACzC,uEAAwD;AAsExD,MAAM,SAAS,GAAG,uBAAuB,CAAC;AAC1C,MAAM,OAAO,GAAG,IAAI,CAAC;AAErB,MAAM,MAAM,GAAG,IAAI,2BAAM,CAAS,SAAS,EAAE;IAC3C,aAAa,EAAE;QACb,MAAM,EAAE,EAAE;QACV,OAAO,EAAE,EAAE,KAAK,EAAE,EAAE,EAAE,QAAQ,EAAE,EAA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EAAE,CACjB,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,EAAE,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CACjE;SACA,OAAO,CAAC,OAAO,EAAE,KAAK,CAAC;SACvB,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC;IACnB,OAAO,GAAG,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;AACzC,CAAC;AAEM,KAAK,UAAU,eAAe,CACnC,QAAgB,EAChB,QAAgB;IAEhB,OAAO,KAAK,EAAE;SACX,MAAM,CAAC,CAAC,MAAM,EAAE,EAAE,CACjB,MAAM;SACH,GAAG,CAAC,UAAU,CAAC;SACf,EAAE,CAAC,QAAQ,CAAC;SACZ,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,EAAE,CAAC,QAAQ,CAAC,CAAC;SACxC,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,EAAE,CAAC,MAAM,CAAC,CAAC,CACvC;SACA,OAAO,CAAC,cAAc,EAAE,MAAM,CAAC,CAAC;AACrC,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Engine } from "./engine";
|
|
2
|
+
import type { Settings } from "./config";
|
|
3
|
+
import type { ProviderSchedule } from "./schedule";
|
|
4
|
+
export interface RuntimeState {
|
|
5
|
+
engine: Engine;
|
|
6
|
+
settings: Settings;
|
|
7
|
+
schedule: ProviderSchedule;
|
|
8
|
+
}
|
|
9
|
+
export declare function SetRuntime(next: RuntimeState | undefined): void;
|
|
10
|
+
export declare function Runtime(): RuntimeState;
|
package/dist/runtime.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SetRuntime = SetRuntime;
|
|
4
|
+
exports.Runtime = Runtime;
|
|
5
|
+
let state;
|
|
6
|
+
function SetRuntime(next) {
|
|
7
|
+
state = next;
|
|
8
|
+
}
|
|
9
|
+
function Runtime() {
|
|
10
|
+
if (!state)
|
|
11
|
+
throw new Error("payment-subscriptions has not been constructed");
|
|
12
|
+
return state;
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=runtime.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runtime.js","sourceRoot":"","sources":["../src/runtime.ts"],"names":[],"mappings":";;AAYA,gCAEC;AAED,0BAGC;AATD,IAAI,KAA+B,CAAC;AAEpC,SAAgB,UAAU,CAAC,IAA8B;IACvD,KAAK,GAAG,IAAI,CAAC;AACf,CAAC;AAED,SAAgB,OAAO;IACrB,IAAI,CAAC,KAAK;QAAE,MAAM,IAAI,KAAK,CAAC,gDAAgD,CAAC,CAAC;IAC9E,OAAO,KAAK,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type Payment } from "@antelopejs/interface-payment";
|
|
2
|
+
import type { Snapshot } from "./records";
|
|
3
|
+
interface ScheduledCharge {
|
|
4
|
+
subscription: Snapshot;
|
|
5
|
+
provider: string | undefined;
|
|
6
|
+
providerReference: string | undefined;
|
|
7
|
+
idempotencyKey: string;
|
|
8
|
+
}
|
|
9
|
+
export interface ProviderSchedule {
|
|
10
|
+
register(subscription: Snapshot, provider: string | undefined): Promise<string | undefined>;
|
|
11
|
+
charge(charge: ScheduledCharge): Promise<Payment>;
|
|
12
|
+
release(subscription: Snapshot, providerReference: string | undefined, provider: string | undefined): Promise<void>;
|
|
13
|
+
}
|
|
14
|
+
export declare function ResolveSchedule(name: string): ProviderSchedule;
|
|
15
|
+
export {};
|
package/dist/schedule.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ResolveSchedule = ResolveSchedule;
|
|
4
|
+
const interface_payment_1 = require("@antelopejs/interface-payment");
|
|
5
|
+
const interface_subscriptions_1 = require("@antelopejs/interface-subscriptions");
|
|
6
|
+
function chargeOffSession(charge) {
|
|
7
|
+
const { subscription } = charge;
|
|
8
|
+
return (0, interface_payment_1.CreatePayment)({
|
|
9
|
+
amount: subscription.amount,
|
|
10
|
+
reference: subscription.reference,
|
|
11
|
+
customer: subscription.customer,
|
|
12
|
+
paymentMethod: subscription.paymentMethod,
|
|
13
|
+
offSession: true,
|
|
14
|
+
metadata: {
|
|
15
|
+
...subscription.metadata,
|
|
16
|
+
[interface_subscriptions_1.SUBSCRIPTION_METADATA_KEY]: subscription.id,
|
|
17
|
+
},
|
|
18
|
+
}, charge.idempotencyKey, charge.provider);
|
|
19
|
+
}
|
|
20
|
+
const PAYMENT_INTERFACE_SCHEDULE = Object.freeze({
|
|
21
|
+
register: () => Promise.resolve(undefined),
|
|
22
|
+
charge: chargeOffSession,
|
|
23
|
+
release: () => Promise.resolve(),
|
|
24
|
+
});
|
|
25
|
+
const SCHEDULES = {
|
|
26
|
+
"payment-interface": PAYMENT_INTERFACE_SCHEDULE,
|
|
27
|
+
};
|
|
28
|
+
function ResolveSchedule(name) {
|
|
29
|
+
const schedule = SCHEDULES[name];
|
|
30
|
+
if (!schedule) {
|
|
31
|
+
throw new Error(`Unknown providerSchedule "${name}"; known: ${Object.keys(SCHEDULES).join(", ")}`);
|
|
32
|
+
}
|
|
33
|
+
return schedule;
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=schedule.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schedule.js","sourceRoot":"","sources":["../src/schedule.ts"],"names":[],"mappings":";;AAsDA,0CAQC;AA9DD,qEAA4E;AAC5E,iFAAgF;AAwBhF,SAAS,gBAAgB,CAAC,MAAuB;IAC/C,MAAM,EAAE,YAAY,EAAE,GAAG,MAAM,CAAC;IAChC,OAAO,IAAA,iCAAa,EAClB;QACE,MAAM,EAAE,YAAY,CAAC,MAAM;QAC3B,SAAS,EAAE,YAAY,CAAC,SAAS;QACjC,QAAQ,EAAE,YAAY,CAAC,QAAQ;QAC/B,aAAa,EAAE,YAAY,CAAC,aAAa;QACzC,UAAU,EAAE,IAAI;QAChB,QAAQ,EAAE;YACR,GAAG,YAAY,CAAC,QAAQ;YACxB,CAAC,mDAAyB,CAAC,EAAE,YAAY,CAAC,EAAE;SAC7C;KACF,EACD,MAAM,CAAC,cAAc,EACrB,MAAM,CAAC,QAAQ,CAChB,CAAC;AACJ,CAAC;AAED,MAAM,0BAA0B,GAAqB,MAAM,CAAC,MAAM,CAAC;IACjE,QAAQ,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC;IAC1C,MAAM,EAAE,gBAAgB;IACxB,OAAO,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,OAAO,EAAE;CACjC,CAAC,CAAC;AAEH,MAAM,SAAS,GAAqC;IAClD,mBAAmB,EAAE,0BAA0B;CAChD,CAAC;AAEF,SAAgB,eAAe,CAAC,IAAY;IAC1C,MAAM,QAAQ,GAAG,SAAS,CAAC,IAAI,CAAC,CAAC;IACjC,IAAI,CAAC,QAAQ,EAAE,CAAC;QACd,MAAM,IAAI,KAAK,CACb,6BAA6B,IAAI,aAAa,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAClF,CAAC;IACJ,CAAC;IACD,OAAO,QAAQ,CAAC;AAClB,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { PaymentStatus } from "@antelopejs/interface-payment";
|
|
2
|
+
import type { Settings } from "./config";
|
|
3
|
+
import type { RecordPatch, StoredEvent, SubscriptionRecord } from "./records";
|
|
4
|
+
type DecisionKind = "charge" | "settle" | "end";
|
|
5
|
+
export interface Decision {
|
|
6
|
+
kind: DecisionKind;
|
|
7
|
+
periodIndex: number;
|
|
8
|
+
attempt: number;
|
|
9
|
+
fingerprint: string;
|
|
10
|
+
}
|
|
11
|
+
export type OutcomeKind = "succeeded" | "failed" | "pending" | "error" | "end" | "reconcile";
|
|
12
|
+
export interface Outcome {
|
|
13
|
+
kind: OutcomeKind;
|
|
14
|
+
payment: string | null;
|
|
15
|
+
reason?: string;
|
|
16
|
+
}
|
|
17
|
+
export interface Transition {
|
|
18
|
+
patch: RecordPatch;
|
|
19
|
+
events: StoredEvent[];
|
|
20
|
+
}
|
|
21
|
+
export declare const RESULT_OF_PAYMENT: Record<PaymentStatus, OutcomeKind>;
|
|
22
|
+
export declare function Decide(record: SubscriptionRecord, now: number): Decision;
|
|
23
|
+
export declare function ChargeKey(record: SubscriptionRecord, decided: Decision): string;
|
|
24
|
+
export declare function Apply(record: SubscriptionRecord, decided: Decision, outcome: Outcome, now: number, settings: Settings): Transition | undefined;
|
|
25
|
+
export {};
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RESULT_OF_PAYMENT = void 0;
|
|
4
|
+
exports.Decide = Decide;
|
|
5
|
+
exports.ChargeKey = ChargeKey;
|
|
6
|
+
exports.Apply = Apply;
|
|
7
|
+
const events_1 = require("./events");
|
|
8
|
+
const calendar_1 = require("./calendar");
|
|
9
|
+
exports.RESULT_OF_PAYMENT = {
|
|
10
|
+
succeeded: "succeeded",
|
|
11
|
+
failed: "failed",
|
|
12
|
+
canceled: "failed",
|
|
13
|
+
requires_action: "failed",
|
|
14
|
+
pending: "pending",
|
|
15
|
+
authorized: "pending",
|
|
16
|
+
};
|
|
17
|
+
function Fingerprint(record) {
|
|
18
|
+
return `${record.periodIndex}:${record.attempt}:${record.pendingPayment ?? ""}`;
|
|
19
|
+
}
|
|
20
|
+
function decision(record, kind, periodIndex, attempt) {
|
|
21
|
+
return { kind, periodIndex, attempt, fingerprint: Fingerprint(record) };
|
|
22
|
+
}
|
|
23
|
+
function Decide(record, now) {
|
|
24
|
+
if (record.status === "canceled" && record.pendingPayment === null) {
|
|
25
|
+
return decision(record, "end", record.periodIndex, record.attempt);
|
|
26
|
+
}
|
|
27
|
+
if (record.pendingPayment !== null) {
|
|
28
|
+
return decision(record, "settle", record.periodIndex, record.attempt);
|
|
29
|
+
}
|
|
30
|
+
if (record.cancelAtPeriodEnd && now >= record.currentPeriodEnd) {
|
|
31
|
+
return decision(record, "end", record.periodIndex, record.attempt);
|
|
32
|
+
}
|
|
33
|
+
if (record.status === "past_due") {
|
|
34
|
+
return decision(record, "charge", record.periodIndex, record.attempt);
|
|
35
|
+
}
|
|
36
|
+
return decision(record, "charge", record.periodIndex + 1, 0);
|
|
37
|
+
}
|
|
38
|
+
function ChargeKey(record, decided) {
|
|
39
|
+
return `payment-subscriptions:${record._id}:${decided.periodIndex}:${decided.attempt}`;
|
|
40
|
+
}
|
|
41
|
+
function period(record, index) {
|
|
42
|
+
const boundary = (at) => (0, calendar_1.PeriodBoundary)(record.anchor, record.interval, record.intervalCount, at);
|
|
43
|
+
return {
|
|
44
|
+
periodIndex: index,
|
|
45
|
+
currentPeriodStart: boundary(index),
|
|
46
|
+
currentPeriodEnd: boundary(index + 1),
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
function withEvents(record, patch, now, types, charge) {
|
|
50
|
+
const after = { ...record, ...patch };
|
|
51
|
+
const occurrence = charge.occurrence ?? charge.payment;
|
|
52
|
+
return {
|
|
53
|
+
patch,
|
|
54
|
+
events: types.map((type) => (0, events_1.RaiseEvent)(after, type, now, charge.payment, occurrence)),
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
function attemptOccurrence(outcome, decided) {
|
|
58
|
+
return outcome.payment ?? `refused-${decided.periodIndex}-${decided.attempt}`;
|
|
59
|
+
}
|
|
60
|
+
const succeeded = (record, decided, outcome, now) => {
|
|
61
|
+
const paid = period(record, decided.periodIndex);
|
|
62
|
+
const patch = {
|
|
63
|
+
...paid,
|
|
64
|
+
status: "active",
|
|
65
|
+
attempt: 0,
|
|
66
|
+
pendingPayment: null,
|
|
67
|
+
attemptMethod: null,
|
|
68
|
+
attemptStartedAt: null,
|
|
69
|
+
attemptReplayUntil: null,
|
|
70
|
+
reconciliationRequired: false,
|
|
71
|
+
latestPayment: outcome.payment,
|
|
72
|
+
nextActionAt: paid.currentPeriodEnd,
|
|
73
|
+
};
|
|
74
|
+
return withEvents(record, patch, now, ["subscription.payment_succeeded"], {
|
|
75
|
+
payment: outcome.payment,
|
|
76
|
+
});
|
|
77
|
+
};
|
|
78
|
+
const failed = (record, decided, outcome, now, settings) => {
|
|
79
|
+
const base = {
|
|
80
|
+
...period(record, decided.periodIndex),
|
|
81
|
+
pendingPayment: null,
|
|
82
|
+
attemptMethod: null,
|
|
83
|
+
attemptStartedAt: null,
|
|
84
|
+
attemptReplayUntil: null,
|
|
85
|
+
reconciliationRequired: false,
|
|
86
|
+
latestPayment: outcome.payment ?? record.latestPayment,
|
|
87
|
+
};
|
|
88
|
+
const occurrence = attemptOccurrence(outcome, decided);
|
|
89
|
+
const retryDelay = settings.retryDelaysMs[decided.attempt];
|
|
90
|
+
if (retryDelay === undefined) {
|
|
91
|
+
const ended = {
|
|
92
|
+
...base,
|
|
93
|
+
status: "canceled",
|
|
94
|
+
endedAt: now,
|
|
95
|
+
endReason: "payment_failed",
|
|
96
|
+
nextActionAt: null,
|
|
97
|
+
};
|
|
98
|
+
return withEvents(record, ended, now, ["subscription.payment_failed", "subscription.canceled"], { payment: outcome.payment, occurrence });
|
|
99
|
+
}
|
|
100
|
+
const retry = {
|
|
101
|
+
...base,
|
|
102
|
+
status: "past_due",
|
|
103
|
+
attempt: decided.attempt + 1,
|
|
104
|
+
nextActionAt: now + retryDelay,
|
|
105
|
+
};
|
|
106
|
+
return withEvents(record, retry, now, ["subscription.payment_failed"], {
|
|
107
|
+
payment: outcome.payment,
|
|
108
|
+
occurrence,
|
|
109
|
+
});
|
|
110
|
+
};
|
|
111
|
+
const pending = (record, decided, outcome, now, settings) => ({
|
|
112
|
+
patch: {
|
|
113
|
+
...period(record, decided.periodIndex),
|
|
114
|
+
status: record.status === "trialing" ? "active" : record.status,
|
|
115
|
+
attemptMethod: null,
|
|
116
|
+
latestPayment: outcome.payment,
|
|
117
|
+
pendingPayment: outcome.payment,
|
|
118
|
+
nextActionAt: now + settings.settlementPollMs,
|
|
119
|
+
},
|
|
120
|
+
events: [],
|
|
121
|
+
});
|
|
122
|
+
const unreachable = (_record, _decided, _outcome, now, settings) => ({
|
|
123
|
+
patch: { nextActionAt: now + settings.errorBackoffMs },
|
|
124
|
+
events: [],
|
|
125
|
+
});
|
|
126
|
+
const end = (record, _decided, _outcome, now) => {
|
|
127
|
+
const patch = {
|
|
128
|
+
reconciliationRequired: record.reconciliationRequired || record.attemptMethod !== null,
|
|
129
|
+
status: "canceled",
|
|
130
|
+
endedAt: record.currentPeriodEnd,
|
|
131
|
+
endReason: "requested",
|
|
132
|
+
nextActionAt: null,
|
|
133
|
+
};
|
|
134
|
+
return withEvents(record, patch, now, ["subscription.canceled"], {
|
|
135
|
+
payment: null,
|
|
136
|
+
});
|
|
137
|
+
};
|
|
138
|
+
const RULES = {
|
|
139
|
+
reconcile: () => ({
|
|
140
|
+
patch: { reconciliationRequired: true, nextActionAt: null },
|
|
141
|
+
events: [],
|
|
142
|
+
}),
|
|
143
|
+
succeeded,
|
|
144
|
+
failed,
|
|
145
|
+
pending,
|
|
146
|
+
error: unreachable,
|
|
147
|
+
end,
|
|
148
|
+
};
|
|
149
|
+
const SETTLED_ON_ENDED = {
|
|
150
|
+
pendingPayment: null,
|
|
151
|
+
attemptMethod: null,
|
|
152
|
+
attemptStartedAt: null,
|
|
153
|
+
attemptReplayUntil: null,
|
|
154
|
+
reconciliationRequired: false,
|
|
155
|
+
nextActionAt: null,
|
|
156
|
+
};
|
|
157
|
+
const ENDED_RULES = {
|
|
158
|
+
reconcile: () => ({
|
|
159
|
+
patch: { reconciliationRequired: true, nextActionAt: null },
|
|
160
|
+
events: [],
|
|
161
|
+
}),
|
|
162
|
+
succeeded: (record, outcome, now) => withEvents(record, { ...SETTLED_ON_ENDED, latestPayment: outcome.payment }, now, ["subscription.payment_succeeded"], { payment: outcome.payment }),
|
|
163
|
+
failed: (record, outcome) => ({
|
|
164
|
+
patch: {
|
|
165
|
+
...SETTLED_ON_ENDED,
|
|
166
|
+
latestPayment: outcome.payment ?? record.latestPayment,
|
|
167
|
+
},
|
|
168
|
+
events: [],
|
|
169
|
+
}),
|
|
170
|
+
pending: (_record, outcome, now, settings) => ({
|
|
171
|
+
patch: {
|
|
172
|
+
latestPayment: outcome.payment,
|
|
173
|
+
pendingPayment: outcome.payment,
|
|
174
|
+
nextActionAt: now + settings.settlementPollMs,
|
|
175
|
+
},
|
|
176
|
+
events: [],
|
|
177
|
+
}),
|
|
178
|
+
error: (record, _outcome, now, settings) => ({
|
|
179
|
+
patch: record.pendingPayment === null
|
|
180
|
+
? {
|
|
181
|
+
reconciliationRequired: record.reconciliationRequired || record.attemptMethod !== null,
|
|
182
|
+
nextActionAt: null,
|
|
183
|
+
}
|
|
184
|
+
: { nextActionAt: now + settings.errorBackoffMs },
|
|
185
|
+
events: [],
|
|
186
|
+
}),
|
|
187
|
+
end: () => ({ patch: { nextActionAt: null }, events: [] }),
|
|
188
|
+
};
|
|
189
|
+
function Apply(record, decided, outcome, now, settings) {
|
|
190
|
+
if (Fingerprint(record) !== decided.fingerprint)
|
|
191
|
+
return undefined;
|
|
192
|
+
if (record.status === "canceled") {
|
|
193
|
+
return ENDED_RULES[outcome.kind](record, outcome, now, settings);
|
|
194
|
+
}
|
|
195
|
+
return RULES[outcome.kind](record, decided, outcome, now, settings);
|
|
196
|
+
}
|
|
197
|
+
//# sourceMappingURL=transitions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transitions.js","sourceRoot":"","sources":["../src/transitions.ts"],"names":[],"mappings":";;;AAyDA,wBAcC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GAAG,EAAE,GAAG,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,EAAE,YAAY,EAAE,IAAI,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC;CAC3D,CAAC;AAEF,SAAgB,KAAK,CACnB,MAA0B,EAC1B,OAAiB,EACjB,OAAgB,EAChB,GAAW,EACX,QAAkB;IAElB,IAAI,WAAW,CAAC,MAAM,CAAC,KAAK,OAAO,CAAC,WAAW;QAAE,OAAO,SAAS,CAAC;IAClE,IAAI,MAAM,CAAC,MAAM,KAAK,UAAU,EAAE,CAAC;QACjC,OAAO,WAAW,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,OAAO,EAAE,GAAG,EAAE,QAAQ,CAAC,CAAC;IACnE,CAAC;IACD,OAAO,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,GAAG,EAAE,QAAQ,CAAC,CAAC;AACtE,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Engine } from "./engine";
|
|
2
|
+
import type { Settings } from "./config";
|
|
3
|
+
import type { SubscriptionRecord } from "./records";
|
|
4
|
+
export interface Trigger {
|
|
5
|
+
readonly kind: "polling" | "redis";
|
|
6
|
+
start(): void;
|
|
7
|
+
stop(): Promise<void>;
|
|
8
|
+
schedule(record: SubscriptionRecord): void;
|
|
9
|
+
}
|
|
10
|
+
export declare function ChooseTrigger(engine: Engine, settings: Settings): Promise<Trigger>;
|