@openlup/core 0.0.0 → 0.7.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/CHANGELOG.md +225 -0
- package/CODE_OF_CONDUCT.md +39 -0
- package/CONTRIBUTING.md +34 -0
- package/LICENSE +204 -0
- package/MAINTAINERS.md +33 -0
- package/README.md +85 -3
- package/SECURITY.md +29 -0
- package/dist/bundle/contracts.d.ts +9 -0
- package/dist/bundle/contracts.js +1 -0
- package/dist/bundle/ports.d.ts +41 -0
- package/dist/bundle/ports.js +1 -0
- package/dist/catalog/identifiers.d.ts +31 -0
- package/dist/catalog/identifiers.js +46 -0
- package/dist/catalog/index.d.ts +2 -0
- package/dist/catalog/index.js +2 -0
- package/dist/checkout/idempotency.d.ts +29 -0
- package/dist/checkout/idempotency.js +37 -0
- package/dist/checkout/index.d.ts +2 -0
- package/dist/checkout/index.js +2 -0
- package/dist/company-identity/contracts.d.ts +220 -0
- package/dist/company-identity/contracts.js +167 -0
- package/dist/company-identity/index.d.ts +6 -0
- package/dist/company-identity/index.js +2 -0
- package/dist/company-identity/ports.d.ts +13 -0
- package/dist/company-identity/ports.js +1 -0
- package/dist/fulfillment/facts.d.ts +103 -0
- package/dist/fulfillment/facts.js +195 -0
- package/dist/fulfillment/index.d.ts +4 -0
- package/dist/fulfillment/index.js +2 -0
- package/dist/inventory/atp.d.ts +172 -0
- package/dist/inventory/atp.js +211 -0
- package/dist/inventory/index.d.ts +2 -0
- package/dist/inventory/index.js +2 -0
- package/dist/marketing/research/contracts.d.ts +75 -0
- package/dist/marketing/research/contracts.js +60 -0
- package/dist/marketing/research/index.d.ts +4 -0
- package/dist/marketing/research/index.js +4 -0
- package/dist/marketing/research/ports.d.ts +15 -0
- package/dist/marketing/research/ports.js +1 -0
- package/dist/partners/index.d.ts +4 -0
- package/dist/partners/index.js +4 -0
- package/dist/partners/ports.d.ts +12 -0
- package/dist/partners/ports.js +1 -0
- package/dist/partners/status.d.ts +48 -0
- package/dist/partners/status.js +35 -0
- package/dist/payment/executionBaseContracts.d.ts +81 -0
- package/dist/payment/executionBaseContracts.js +8 -0
- package/dist/payment/index.d.ts +36 -0
- package/dist/payment/index.js +18 -0
- package/dist/payment/methodLifecycle.d.ts +91 -0
- package/dist/payment/methodLifecycle.js +76 -0
- package/dist/payment/paymentControlTypes.d.ts +135 -0
- package/dist/payment/paymentControlTypes.js +110 -0
- package/dist/payment/paymentFailureTaxonomy.d.ts +80 -0
- package/dist/payment/paymentFailureTaxonomy.js +155 -0
- package/dist/payment/paymentFailureTaxonomyContracts.d.ts +106 -0
- package/dist/payment/paymentFailureTaxonomyContracts.js +177 -0
- package/dist/payment/paymentRecovery.d.ts +26 -0
- package/dist/payment/paymentRecovery.js +73 -0
- package/dist/payment/paymentRecoveryContracts.d.ts +53 -0
- package/dist/payment/paymentRecoveryContracts.js +10 -0
- package/dist/payment/paymentStateMachine.d.ts +28 -0
- package/dist/payment/paymentStateMachine.js +184 -0
- package/dist/payment/providerAttemptIdempotency.d.ts +23 -0
- package/dist/payment/providerAttemptIdempotency.js +31 -0
- package/dist/payment/providerCapability.d.ts +148 -0
- package/dist/payment/providerCapability.js +13 -0
- package/dist/platform-runtime/contracts.d.ts +9 -0
- package/dist/platform-runtime/contracts.js +13 -0
- package/dist/platform-runtime/index.d.ts +10 -0
- package/dist/platform-runtime/index.js +4 -0
- package/dist/platform-runtime/platformKernel.d.ts +44 -0
- package/dist/platform-runtime/platformKernel.js +39 -0
- package/dist/platform-runtime/ports.d.ts +100 -0
- package/dist/platform-runtime/ports.js +1 -0
- package/dist/pricing/index.d.ts +8 -0
- package/dist/pricing/index.js +8 -0
- package/dist/pricing/ports.d.ts +22 -0
- package/dist/pricing/ports.js +7 -0
- package/dist/pricing/pricingBreakdown.d.ts +86 -0
- package/dist/pricing/pricingBreakdown.js +155 -0
- package/dist/pricing/targetPriceAllocation.d.ts +97 -0
- package/dist/pricing/targetPriceAllocation.js +202 -0
- package/dist/pricing/types.d.ts +32 -0
- package/dist/pricing/types.js +4 -0
- package/dist/promo/adjustmentEngine.d.ts +4 -0
- package/dist/promo/adjustmentEngine.js +194 -0
- package/dist/promo/adjustmentTypes.d.ts +103 -0
- package/dist/promo/adjustmentTypes.js +13 -0
- package/dist/promo/index.d.ts +8 -0
- package/dist/promo/index.js +8 -0
- package/dist/promo/promoEvaluator.d.ts +25 -0
- package/dist/promo/promoEvaluator.js +135 -0
- package/dist/promo/types.d.ts +86 -0
- package/dist/promo/types.js +12 -0
- package/dist/risk/core/evaluator.d.ts +3 -0
- package/dist/risk/core/evaluator.js +32 -0
- package/dist/risk/core/rules.d.ts +3 -0
- package/dist/risk/core/rules.js +118 -0
- package/dist/risk/core/types.d.ts +65 -0
- package/dist/risk/core/types.js +1 -0
- package/dist/risk/index.d.ts +8 -0
- package/dist/risk/index.js +8 -0
- package/dist/risk/types.d.ts +32 -0
- package/dist/risk/types.js +43 -0
- package/dist/shipping/index.d.ts +4 -0
- package/dist/shipping/index.js +4 -0
- package/dist/shipping/shippingResolver.d.ts +26 -0
- package/dist/shipping/shippingResolver.js +88 -0
- package/dist/shipping/types.d.ts +45 -0
- package/dist/shipping/types.js +4 -0
- package/dist/subscription/cycleHardening.d.ts +95 -0
- package/dist/subscription/cycleHardening.js +118 -0
- package/dist/subscription/index.d.ts +14 -0
- package/dist/subscription/index.js +14 -0
- package/dist/subscription/subscriptionEngineCore.d.ts +13 -0
- package/dist/subscription/subscriptionEngineCore.js +145 -0
- package/dist/subscription/subscriptionEngineLifecycle.d.ts +150 -0
- package/dist/subscription/subscriptionEngineLifecycle.js +433 -0
- package/dist/subscription/subscriptionEnginePayment.d.ts +25 -0
- package/dist/subscription/subscriptionEnginePayment.js +127 -0
- package/dist/subscription/subscriptionEngineTypes.d.ts +94 -0
- package/dist/subscription/subscriptionEngineTypes.js +4 -0
- package/dist/subscription/subscriptionEngineUtils.d.ts +32 -0
- package/dist/subscription/subscriptionEngineUtils.js +74 -0
- package/dist/subscription/types.d.ts +58 -0
- package/dist/subscription/types.js +47 -0
- package/dist/testing.d.ts +48 -0
- package/dist/testing.js +105 -0
- package/package.json +215 -4
- package/release-gates.json +232 -0
- package/src/bundle/contracts.ts +15 -0
- package/src/bundle/ports.ts +32 -0
- package/src/catalog/identifiers.ts +50 -0
- package/src/catalog/index.ts +2 -0
- package/src/checkout/idempotency.ts +49 -0
- package/src/checkout/index.ts +2 -0
- package/src/company-identity/contracts.ts +208 -0
- package/src/company-identity/index.ts +28 -0
- package/src/company-identity/ports.ts +19 -0
- package/src/fulfillment/facts.ts +300 -0
- package/src/fulfillment/index.ts +34 -0
- package/src/inventory/atp.ts +249 -0
- package/src/inventory/index.ts +2 -0
- package/src/marketing/research/contracts.ts +91 -0
- package/src/marketing/research/index.ts +4 -0
- package/src/marketing/research/ports.ts +26 -0
- package/src/partners/index.ts +4 -0
- package/src/partners/ports.ts +18 -0
- package/src/partners/status.ts +49 -0
- package/src/payment/executionBaseContracts.ts +93 -0
- package/src/payment/index.ts +128 -0
- package/src/payment/methodLifecycle.ts +130 -0
- package/src/payment/paymentControlTypes.ts +197 -0
- package/src/payment/paymentFailureTaxonomy.ts +201 -0
- package/src/payment/paymentFailureTaxonomyContracts.ts +228 -0
- package/src/payment/paymentRecovery.ts +95 -0
- package/src/payment/paymentRecoveryContracts.ts +61 -0
- package/src/payment/paymentStateMachine.ts +263 -0
- package/src/payment/providerAttemptIdempotency.ts +57 -0
- package/src/payment/providerCapability.ts +172 -0
- package/src/platform-runtime/contracts.ts +33 -0
- package/src/platform-runtime/index.ts +36 -0
- package/src/platform-runtime/platformKernel.ts +86 -0
- package/src/platform-runtime/ports.ts +88 -0
- package/src/pricing/index.ts +8 -0
- package/src/pricing/ports.ts +27 -0
- package/src/pricing/pricingBreakdown.ts +243 -0
- package/src/pricing/targetPriceAllocation.ts +293 -0
- package/src/pricing/types.ts +36 -0
- package/src/promo/adjustmentEngine.ts +253 -0
- package/src/promo/adjustmentTypes.ts +94 -0
- package/src/promo/index.ts +8 -0
- package/src/promo/promoEvaluator.ts +137 -0
- package/src/promo/types.ts +97 -0
- package/src/risk/core/evaluator.ts +41 -0
- package/src/risk/core/rules.ts +128 -0
- package/src/risk/core/types.ts +73 -0
- package/src/risk/index.ts +8 -0
- package/src/risk/types.ts +60 -0
- package/src/shipping/index.ts +4 -0
- package/src/shipping/shippingResolver.ts +101 -0
- package/src/shipping/types.ts +49 -0
- package/src/subscription/cycleHardening.ts +174 -0
- package/src/subscription/index.ts +14 -0
- package/src/subscription/subscriptionEngineCore.ts +181 -0
- package/src/subscription/subscriptionEngineLifecycle.ts +574 -0
- package/src/subscription/subscriptionEnginePayment.ts +179 -0
- package/src/subscription/subscriptionEngineTypes.ts +114 -0
- package/src/subscription/subscriptionEngineUtils.ts +110 -0
- package/src/subscription/types.ts +104 -0
- package/src/testing.ts +169 -0
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import type { SubscriptionCycleStatus } from "./types.js";
|
|
2
|
+
import type {
|
|
3
|
+
CreateInitialSubscriptionCheckoutInput,
|
|
4
|
+
CycleMutationResult,
|
|
5
|
+
EngineSubscription,
|
|
6
|
+
InitialSubscriptionCheckoutModel,
|
|
7
|
+
PlanCycleInput,
|
|
8
|
+
SubscriptionEngineResult,
|
|
9
|
+
} from "./subscriptionEngineTypes.js";
|
|
10
|
+
import { DEFAULT_EDIT_WINDOW_HOURS, DEFAULT_TIMEZONE } from "./subscriptionEngineTypes.js";
|
|
11
|
+
import {
|
|
12
|
+
cloneRecord,
|
|
13
|
+
cloneTemplate,
|
|
14
|
+
failure,
|
|
15
|
+
makeEvent,
|
|
16
|
+
parseIso,
|
|
17
|
+
success,
|
|
18
|
+
validateTemplate,
|
|
19
|
+
} from "./subscriptionEngineUtils.js";
|
|
20
|
+
|
|
21
|
+
/** @beta */
|
|
22
|
+
export function createInitialSubscriptionCheckoutModel(
|
|
23
|
+
input: CreateInitialSubscriptionCheckoutInput,
|
|
24
|
+
): SubscriptionEngineResult<InitialSubscriptionCheckoutModel> {
|
|
25
|
+
const now = parseIso(input.now);
|
|
26
|
+
const firstCycleAt = parseIso(input.firstCycleAt);
|
|
27
|
+
if (!now || !firstCycleAt) {
|
|
28
|
+
return failure("invalid_timestamp", "now and firstCycleAt must be valid ISO timestamps");
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const template = cloneTemplate(input.template);
|
|
32
|
+
const templateError = validateTemplate(template);
|
|
33
|
+
if (templateError) return templateError;
|
|
34
|
+
|
|
35
|
+
const subscription: EngineSubscription = {
|
|
36
|
+
id: input.subscriptionId,
|
|
37
|
+
clientRef: input.clientRef,
|
|
38
|
+
status: "active",
|
|
39
|
+
template,
|
|
40
|
+
templateVersion: 1,
|
|
41
|
+
nextCycleAt: firstCycleAt.toISOString(),
|
|
42
|
+
paymentMethodRef: input.paymentMethodRef ?? null,
|
|
43
|
+
paymentMethodKind: input.paymentMethodKind ?? null,
|
|
44
|
+
timezone: input.timezone ?? DEFAULT_TIMEZONE,
|
|
45
|
+
createdAt: now.toISOString(),
|
|
46
|
+
updatedAt: now.toISOString(),
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const planned = planSubscriptionCycle({
|
|
50
|
+
subscription,
|
|
51
|
+
cycleNumber: 1,
|
|
52
|
+
now: now.toISOString(),
|
|
53
|
+
pricingSnapshot: input.pricingSnapshot,
|
|
54
|
+
idempotencyKey: input.idempotencyKey,
|
|
55
|
+
});
|
|
56
|
+
if (planned.ok === false) {
|
|
57
|
+
return failure(planned.error.code, planned.error.message);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return success({
|
|
61
|
+
subscription,
|
|
62
|
+
cycle: planned.value.cycle,
|
|
63
|
+
events: [
|
|
64
|
+
makeEvent({
|
|
65
|
+
eventType: "subscription.created",
|
|
66
|
+
subscription,
|
|
67
|
+
idempotencyKey: `${input.idempotencyKey}:subscription.created`,
|
|
68
|
+
occurredAt: now.toISOString(),
|
|
69
|
+
payload: { clientRef: input.clientRef, templateVersion: 1 },
|
|
70
|
+
}),
|
|
71
|
+
...planned.value.events,
|
|
72
|
+
],
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** @beta */
|
|
77
|
+
export function planSubscriptionCycle(
|
|
78
|
+
input: PlanCycleInput,
|
|
79
|
+
): SubscriptionEngineResult<CycleMutationResult> {
|
|
80
|
+
const now = parseIso(input.now);
|
|
81
|
+
const scheduledAt = parseIso(input.subscription.nextCycleAt);
|
|
82
|
+
if (!now || !scheduledAt) {
|
|
83
|
+
return failure("invalid_timestamp", "now and subscription.nextCycleAt must be valid ISO timestamps");
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (input.subscription.status !== "active") {
|
|
87
|
+
return failure("inactive_subscription", "only active subscriptions can generate cycles");
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const templateSnapshot = cloneTemplate(input.subscription.template);
|
|
91
|
+
const hasPaymentMethod = Boolean(input.subscription.paymentMethodRef);
|
|
92
|
+
const status: SubscriptionCycleStatus = hasPaymentMethod ? "payment_pending" : "payment_failed";
|
|
93
|
+
const failureReason = hasPaymentMethod ? null : "missing_payment_method";
|
|
94
|
+
|
|
95
|
+
const cycle = {
|
|
96
|
+
subscriptionId: input.subscription.id,
|
|
97
|
+
cycleNumber: input.cycleNumber,
|
|
98
|
+
status,
|
|
99
|
+
scheduledAt: scheduledAt.toISOString(),
|
|
100
|
+
paidAt: null,
|
|
101
|
+
templateVersion: input.subscription.templateVersion,
|
|
102
|
+
templateSnapshot,
|
|
103
|
+
pricingSnapshot: cloneRecord(input.pricingSnapshot),
|
|
104
|
+
paymentMethodRef: input.subscription.paymentMethodRef,
|
|
105
|
+
retryAttempt: 0,
|
|
106
|
+
nextRetryAt: null,
|
|
107
|
+
failureReason,
|
|
108
|
+
engineIdempotencyKey: input.idempotencyKey,
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
const events = [
|
|
112
|
+
makeEvent({
|
|
113
|
+
eventType: "subscription.cycle_planned",
|
|
114
|
+
subscription: input.subscription,
|
|
115
|
+
cycleNumber: input.cycleNumber,
|
|
116
|
+
idempotencyKey: `${input.idempotencyKey}:cycle_planned`,
|
|
117
|
+
occurredAt: now.toISOString(),
|
|
118
|
+
payload: {
|
|
119
|
+
scheduledAt: cycle.scheduledAt,
|
|
120
|
+
templateVersion: cycle.templateVersion,
|
|
121
|
+
},
|
|
122
|
+
}),
|
|
123
|
+
];
|
|
124
|
+
|
|
125
|
+
events.push(
|
|
126
|
+
hasPaymentMethod
|
|
127
|
+
? makeEvent({
|
|
128
|
+
eventType: "subscription.payment_requested",
|
|
129
|
+
subscription: input.subscription,
|
|
130
|
+
cycleNumber: input.cycleNumber,
|
|
131
|
+
idempotencyKey: `${input.idempotencyKey}:payment_requested`,
|
|
132
|
+
occurredAt: now.toISOString(),
|
|
133
|
+
payload: { paymentMethodRef: input.subscription.paymentMethodRef },
|
|
134
|
+
})
|
|
135
|
+
: makeEvent({
|
|
136
|
+
eventType: "subscription.payment_failed",
|
|
137
|
+
subscription: input.subscription,
|
|
138
|
+
cycleNumber: input.cycleNumber,
|
|
139
|
+
idempotencyKey: `${input.idempotencyKey}:missing_payment_method`,
|
|
140
|
+
occurredAt: now.toISOString(),
|
|
141
|
+
payload: { reason: failureReason },
|
|
142
|
+
}),
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
return success({ cycle, events });
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** @beta */
|
|
149
|
+
export function findDueSubscriptions(
|
|
150
|
+
subscriptions: EngineSubscription[],
|
|
151
|
+
nowIso: string,
|
|
152
|
+
): EngineSubscription[] {
|
|
153
|
+
return subscriptions.filter((subscription) => isRenewalDue(subscription, nowIso));
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** @beta */
|
|
157
|
+
export function isRenewalDue(subscription: EngineSubscription, nowIso: string): boolean {
|
|
158
|
+
if (subscription.status !== "active") return false;
|
|
159
|
+
const now = parseIso(nowIso);
|
|
160
|
+
const nextCycleAt = parseIso(subscription.nextCycleAt);
|
|
161
|
+
if (!now || !nextCycleAt) return false;
|
|
162
|
+
return nextCycleAt.getTime() <= now.getTime();
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** @beta */
|
|
166
|
+
export function editCutoffAt(subscription: EngineSubscription): string | null {
|
|
167
|
+
const nextCycleAt = parseIso(subscription.nextCycleAt);
|
|
168
|
+
if (!nextCycleAt) return null;
|
|
169
|
+
const editWindowHours = subscription.template.edit_window_hours ?? DEFAULT_EDIT_WINDOW_HOURS;
|
|
170
|
+
return new Date(nextCycleAt.getTime() - editWindowHours * 60 * 60 * 1000).toISOString();
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** @beta */
|
|
174
|
+
export function isEditWindowOpen(subscription: EngineSubscription, nowIso: string): boolean {
|
|
175
|
+
if (subscription.status !== "active") return false;
|
|
176
|
+
const now = parseIso(nowIso);
|
|
177
|
+
const cutoff = editCutoffAt(subscription);
|
|
178
|
+
const cutoffAt = cutoff ? parseIso(cutoff) : null;
|
|
179
|
+
if (!now || !cutoffAt) return false;
|
|
180
|
+
return now.getTime() < cutoffAt.getTime();
|
|
181
|
+
}
|