@zonastery/bursar 0.0.1 → 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/README.md +1 -1
- package/dist/billing/billing-manager.d.ts +66 -0
- package/dist/billing/billing-manager.d.ts.map +1 -0
- package/dist/billing/billing-manager.js +560 -0
- package/dist/billing/billing-manager.js.map +1 -0
- package/dist/billing/billing-store.d.ts +27 -0
- package/dist/billing/billing-store.d.ts.map +1 -0
- package/dist/billing/billing-store.js +9 -0
- package/dist/billing/billing-store.js.map +1 -0
- package/dist/billing/billing-types.d.ts +203 -0
- package/dist/billing/billing-types.d.ts.map +1 -0
- package/dist/billing/billing-types.js +6 -0
- package/dist/billing/billing-types.js.map +1 -0
- package/dist/billing/index.d.ts +7 -0
- package/dist/billing/index.d.ts.map +1 -0
- package/dist/billing/index.js +6 -0
- package/dist/billing/index.js.map +1 -0
- package/dist/billing/memory-billing-store.d.ts +37 -0
- package/dist/billing/memory-billing-store.d.ts.map +1 -0
- package/dist/billing/memory-billing-store.js +197 -0
- package/dist/billing/memory-billing-store.js.map +1 -0
- package/dist/billing/postgres-billing-store.d.ts +35 -0
- package/dist/billing/postgres-billing-store.d.ts.map +1 -0
- package/dist/billing/postgres-billing-store.js +274 -0
- package/dist/billing/postgres-billing-store.js.map +1 -0
- package/dist/billing/supabase-billing-store.d.ts +29 -0
- package/dist/billing/supabase-billing-store.d.ts.map +1 -0
- package/dist/billing/supabase-billing-store.js +250 -0
- package/dist/billing/supabase-billing-store.js.map +1 -0
- package/dist/config.d.ts +3 -2
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +125 -7
- package/dist/config.js.map +1 -1
- package/dist/engine.d.ts +1 -1
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +2 -2
- package/dist/engine.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/load-pricing-file.d.ts.map +1 -1
- package/dist/load-pricing-file.js +61 -6
- package/dist/load-pricing-file.js.map +1 -1
- package/dist/manager.d.ts +26 -3
- package/dist/manager.d.ts.map +1 -1
- package/dist/manager.js +45 -4
- package/dist/manager.js.map +1 -1
- package/dist/providers/dodo/event-mapper.d.ts +4 -0
- package/dist/providers/dodo/event-mapper.d.ts.map +1 -0
- package/dist/providers/dodo/event-mapper.js +169 -0
- package/dist/providers/dodo/event-mapper.js.map +1 -0
- package/dist/providers/dodo/index.d.ts +3 -0
- package/dist/providers/dodo/index.d.ts.map +1 -0
- package/dist/providers/dodo/index.js +3 -0
- package/dist/providers/dodo/index.js.map +1 -0
- package/dist/providers/dodo/provider.d.ts +43 -0
- package/dist/providers/dodo/provider.d.ts.map +1 -0
- package/dist/providers/dodo/provider.js +135 -0
- package/dist/providers/dodo/provider.js.map +1 -0
- package/dist/providers/index.d.ts +7 -0
- package/dist/providers/index.d.ts.map +1 -0
- package/dist/providers/index.js +6 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/providers/mock/index.d.ts +2 -0
- package/dist/providers/mock/index.d.ts.map +1 -0
- package/dist/providers/mock/index.js +2 -0
- package/dist/providers/mock/index.js.map +1 -0
- package/dist/providers/mock/provider.d.ts +37 -0
- package/dist/providers/mock/provider.d.ts.map +1 -0
- package/dist/providers/mock/provider.js +56 -0
- package/dist/providers/mock/provider.js.map +1 -0
- package/dist/providers/stripe/event-mapper.d.ts +7 -0
- package/dist/providers/stripe/event-mapper.d.ts.map +1 -0
- package/dist/providers/stripe/event-mapper.js +233 -0
- package/dist/providers/stripe/event-mapper.js.map +1 -0
- package/dist/providers/stripe/index.d.ts +3 -0
- package/dist/providers/stripe/index.d.ts.map +1 -0
- package/dist/providers/stripe/index.js +3 -0
- package/dist/providers/stripe/index.js.map +1 -0
- package/dist/providers/stripe/provider.d.ts +39 -0
- package/dist/providers/stripe/provider.d.ts.map +1 -0
- package/dist/providers/stripe/provider.js +131 -0
- package/dist/providers/stripe/provider.js.map +1 -0
- package/dist/providers/types.d.ts +79 -0
- package/dist/providers/types.d.ts.map +1 -0
- package/dist/providers/types.js +2 -0
- package/dist/providers/types.js.map +1 -0
- package/dist/stores/credit-store.d.ts +29 -2
- package/dist/stores/credit-store.d.ts.map +1 -1
- package/dist/stores/credit-store.js +44 -0
- package/dist/stores/credit-store.js.map +1 -1
- package/dist/stores/memory-store.d.ts +11 -2
- package/dist/stores/memory-store.d.ts.map +1 -1
- package/dist/stores/memory-store.js +66 -9
- package/dist/stores/memory-store.js.map +1 -1
- package/dist/stores/postgres-store.d.ts +7 -2
- package/dist/stores/postgres-store.d.ts.map +1 -1
- package/dist/stores/postgres-store.js +23 -4
- package/dist/stores/postgres-store.js.map +1 -1
- package/dist/stores/supabase-store.d.ts +14 -3
- package/dist/stores/supabase-store.d.ts.map +1 -1
- package/dist/stores/supabase-store.js +88 -5
- package/dist/stores/supabase-store.js.map +1 -1
- package/dist/types.d.ts +3 -2
- package/dist/types.d.ts.map +1 -1
- package/package.json +49 -7
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://github.com/Zonastery/bursar/actions/workflows/ci.yml)
|
|
4
4
|
[](https://www.npmjs.com/package/@zonastery/bursar)
|
|
5
|
-
[](https://github.com/Zonastery/bursar/blob/main/LICENSE)
|
|
6
6
|
|
|
7
7
|
Add usage-based credits to your AI SaaS in minutes — not weeks.
|
|
8
8
|
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { CreditManager } from "../manager.js";
|
|
2
|
+
import type { BillingStore } from "./billing-store.js";
|
|
3
|
+
import type { BillingConfig, BillingEvent, BillingEventResult, BillingSubscriptionState } from "./billing-types.js";
|
|
4
|
+
type ResolveUserFn = (provider: string, providerCustomerId: string | null, email: string | null) => string | null;
|
|
5
|
+
export interface BillingManagerOptions {
|
|
6
|
+
creditManager?: CreditManager | null;
|
|
7
|
+
resolveUser?: ResolveUserFn | null;
|
|
8
|
+
onTrialWillEnd?: (event: BillingEvent) => void | Promise<void>;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Provider-agnostic billing lifecycle state machine.
|
|
12
|
+
* Mirrors Python bursar/billing/manager.py.
|
|
13
|
+
*/
|
|
14
|
+
export declare class BillingManager {
|
|
15
|
+
private store;
|
|
16
|
+
private cm;
|
|
17
|
+
private resolveUser;
|
|
18
|
+
private onTrialWillEnd;
|
|
19
|
+
private handlerMap;
|
|
20
|
+
private offerCache;
|
|
21
|
+
private readonly OFFER_CACHE_TTL_MS;
|
|
22
|
+
private readonly IGNORED_EVENT_TYPES;
|
|
23
|
+
constructor(store: BillingStore, options?: BillingManagerOptions);
|
|
24
|
+
getUserSubscription(userId: string): Promise<BillingSubscriptionState | null>;
|
|
25
|
+
/**
|
|
26
|
+
* Sync billing configuration (offers, topups, provider refs) from a config object.
|
|
27
|
+
* Delegates to the store's sync method. Idempotent — safe to call on every
|
|
28
|
+
* BillingManager initialization.
|
|
29
|
+
*/
|
|
30
|
+
syncBillingFromConfig(config: BillingConfig): Promise<void>;
|
|
31
|
+
handleEvent(event: BillingEvent): Promise<BillingEventResult>;
|
|
32
|
+
private routeEvent;
|
|
33
|
+
private resolveUserId;
|
|
34
|
+
private resolveBillingOfferCached;
|
|
35
|
+
private handleCustomerCreated;
|
|
36
|
+
private handleCustomerUpdated;
|
|
37
|
+
private handleCustomerDeleted;
|
|
38
|
+
private handleCheckoutCompleted;
|
|
39
|
+
private getExistingSubscription;
|
|
40
|
+
private buildSubscriptionState;
|
|
41
|
+
private resolveOfferAndKeys;
|
|
42
|
+
private handleSubscriptionCreated;
|
|
43
|
+
private handleSubscriptionUpdated;
|
|
44
|
+
private handleSubscriptionActivated;
|
|
45
|
+
private handleSubscriptionRenewed;
|
|
46
|
+
private handleSubscriptionPlanChanged;
|
|
47
|
+
private handleCancellationScheduled;
|
|
48
|
+
private handleCancellationUnscheduled;
|
|
49
|
+
private handleSubscriptionCanceled;
|
|
50
|
+
private handleSubscriptionExpired;
|
|
51
|
+
private handleSubscriptionPaused;
|
|
52
|
+
private handleSubscriptionResumed;
|
|
53
|
+
private handleTrialWillEnd;
|
|
54
|
+
private handleInvoicePaid;
|
|
55
|
+
private handlePaymentSucceeded;
|
|
56
|
+
private handlePaymentFailed;
|
|
57
|
+
private handleRefundCreated;
|
|
58
|
+
private handleDisputeCreated;
|
|
59
|
+
private handleDisputeClosed;
|
|
60
|
+
private provisionSubscription;
|
|
61
|
+
private revokeSubscription;
|
|
62
|
+
private reEvaluateAccess;
|
|
63
|
+
private resolveOffer;
|
|
64
|
+
}
|
|
65
|
+
export {};
|
|
66
|
+
//# sourceMappingURL=billing-manager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"billing-manager.d.ts","sourceRoot":"","sources":["../../src/billing/billing-manager.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AACnD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AACvD,OAAO,KAAK,EACV,aAAa,EACb,YAAY,EACZ,kBAAkB,EAClB,wBAAwB,EACzB,MAAM,oBAAoB,CAAC;AAE5B,KAAK,aAAa,GAAG,CACnB,QAAQ,EAAE,MAAM,EAChB,kBAAkB,EAAE,MAAM,GAAG,IAAI,EACjC,KAAK,EAAE,MAAM,GAAG,IAAI,KACjB,MAAM,GAAG,IAAI,CAAC;AAEnB,MAAM,WAAW,qBAAqB;IACpC,aAAa,CAAC,EAAE,aAAa,GAAG,IAAI,CAAC;IACrC,WAAW,CAAC,EAAE,aAAa,GAAG,IAAI,CAAC;IACnC,cAAc,CAAC,EAAE,CAAC,KAAK,EAAE,YAAY,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAChE;AAED;;;GAGG;AACH,qBAAa,cAAc;IACzB,OAAO,CAAC,KAAK,CAAe;IAC5B,OAAO,CAAC,EAAE,CAAuB;IACjC,OAAO,CAAC,WAAW,CAAuB;IAC1C,OAAO,CAAC,cAAc,CAAyD;IAC/E,OAAO,CAAC,UAAU,CAAuE;IACzF,OAAO,CAAC,UAAU,CAGd;IACJ,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAAU;IAC7C,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAAqD;gBAE7E,KAAK,EAAE,YAAY,EAAE,OAAO,CAAC,EAAE,qBAAqB;IA+B1D,mBAAmB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,wBAAwB,GAAG,IAAI,CAAC;IAInF;;;;OAIG;IACG,qBAAqB,CAAC,MAAM,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC;IAI3D,WAAW,CAAC,KAAK,EAAE,YAAY,GAAG,OAAO,CAAC,kBAAkB,CAAC;YA+BrD,UAAU;YAWV,aAAa;YA0Bb,yBAAyB;YAezB,qBAAqB;YAerB,qBAAqB;YAIrB,qBAAqB;YAIrB,uBAAuB;YAkBvB,uBAAuB;IAUrC,OAAO,CAAC,sBAAsB;YAqChB,mBAAmB;YA8BnB,yBAAyB;YA6BzB,yBAAyB;YAsBzB,2BAA2B;YAwB3B,yBAAyB;YA0BzB,6BAA6B;YAwB7B,2BAA2B;YAe3B,6BAA6B;YAe7B,0BAA0B;YAkB1B,yBAAyB;YAkBzB,wBAAwB;YAkBxB,yBAAyB;YAyBzB,kBAAkB;YAclB,iBAAiB;YAsBjB,sBAAsB;YAuDtB,mBAAmB;YAwBnB,mBAAmB;YAuCnB,oBAAoB;YAepB,mBAAmB;YAenB,qBAAqB;YA4CrB,kBAAkB;YAKlB,gBAAgB;YAwBhB,YAAY;CAS3B"}
|
|
@@ -0,0 +1,560 @@
|
|
|
1
|
+
import Decimal from "decimal.js";
|
|
2
|
+
/**
|
|
3
|
+
* Provider-agnostic billing lifecycle state machine.
|
|
4
|
+
* Mirrors Python bursar/billing/manager.py.
|
|
5
|
+
*/
|
|
6
|
+
export class BillingManager {
|
|
7
|
+
store;
|
|
8
|
+
cm;
|
|
9
|
+
resolveUser;
|
|
10
|
+
onTrialWillEnd;
|
|
11
|
+
handlerMap;
|
|
12
|
+
offerCache = new Map();
|
|
13
|
+
OFFER_CACHE_TTL_MS = 60_000;
|
|
14
|
+
IGNORED_EVENT_TYPES = new Set(["checkout.expired", "invoice.upcoming"]);
|
|
15
|
+
constructor(store, options) {
|
|
16
|
+
this.store = store;
|
|
17
|
+
this.cm = options?.creditManager ?? null;
|
|
18
|
+
this.resolveUser = options?.resolveUser ?? null;
|
|
19
|
+
this.onTrialWillEnd = options?.onTrialWillEnd ?? null;
|
|
20
|
+
this.handlerMap = {
|
|
21
|
+
"customer.created": this.handleCustomerCreated.bind(this),
|
|
22
|
+
"customer.updated": this.handleCustomerUpdated.bind(this),
|
|
23
|
+
"customer.deleted": this.handleCustomerDeleted.bind(this),
|
|
24
|
+
"checkout.completed": this.handleCheckoutCompleted.bind(this),
|
|
25
|
+
"subscription.created": this.handleSubscriptionCreated.bind(this),
|
|
26
|
+
"subscription.updated": this.handleSubscriptionUpdated.bind(this),
|
|
27
|
+
"subscription.activated": this.handleSubscriptionActivated.bind(this),
|
|
28
|
+
"subscription.renewed": this.handleSubscriptionRenewed.bind(this),
|
|
29
|
+
"subscription.plan_changed": this.handleSubscriptionPlanChanged.bind(this),
|
|
30
|
+
"subscription.cancellation_scheduled": this.handleCancellationScheduled.bind(this),
|
|
31
|
+
"subscription.cancellation_unscheduled": this.handleCancellationUnscheduled.bind(this),
|
|
32
|
+
"subscription.canceled": this.handleSubscriptionCanceled.bind(this),
|
|
33
|
+
"subscription.expired": this.handleSubscriptionExpired.bind(this),
|
|
34
|
+
"subscription.paused": this.handleSubscriptionPaused.bind(this),
|
|
35
|
+
"subscription.resumed": this.handleSubscriptionResumed.bind(this),
|
|
36
|
+
"subscription.trial_will_end": this.handleTrialWillEnd.bind(this),
|
|
37
|
+
"invoice.paid": this.handleInvoicePaid.bind(this),
|
|
38
|
+
"payment.succeeded": this.handlePaymentSucceeded.bind(this),
|
|
39
|
+
"payment.failed": this.handlePaymentFailed.bind(this),
|
|
40
|
+
"refund.created": this.handleRefundCreated.bind(this),
|
|
41
|
+
"dispute.created": this.handleDisputeCreated.bind(this),
|
|
42
|
+
"dispute.closed": this.handleDisputeClosed.bind(this),
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
async getUserSubscription(userId) {
|
|
46
|
+
return this.store.getUserSubscription(userId);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Sync billing configuration (offers, topups, provider refs) from a config object.
|
|
50
|
+
* Delegates to the store's sync method. Idempotent — safe to call on every
|
|
51
|
+
* BillingManager initialization.
|
|
52
|
+
*/
|
|
53
|
+
async syncBillingFromConfig(config) {
|
|
54
|
+
await this.store.syncBillingFromConfig(config);
|
|
55
|
+
}
|
|
56
|
+
async handleEvent(event) {
|
|
57
|
+
const claim = await this.store.claimBillingEvent(event.provider, event.eventId, event.eventType);
|
|
58
|
+
if (claim.status === "duplicate") {
|
|
59
|
+
return { handled: true, action: "duplicate" };
|
|
60
|
+
}
|
|
61
|
+
if (claim.status === "retry") {
|
|
62
|
+
return { handled: false, error: "claim_failed_retry" };
|
|
63
|
+
}
|
|
64
|
+
try {
|
|
65
|
+
const result = await this.routeEvent(event);
|
|
66
|
+
await this.store.completeBillingEvent(event.provider, event.eventId);
|
|
67
|
+
return result;
|
|
68
|
+
}
|
|
69
|
+
catch (err) {
|
|
70
|
+
console.error(`[BillingManager] failed to handle billing event ${event.provider}/${event.eventId}`, err);
|
|
71
|
+
await this.store.failBillingEvent(event.provider, event.eventId);
|
|
72
|
+
return {
|
|
73
|
+
handled: false,
|
|
74
|
+
error: err instanceof Error ? err.message : String(err),
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
async routeEvent(event) {
|
|
79
|
+
const handler = this.handlerMap[event.eventType];
|
|
80
|
+
if (!handler) {
|
|
81
|
+
if (this.IGNORED_EVENT_TYPES.has(event.eventType)) {
|
|
82
|
+
return { handled: true, action: "ignored" };
|
|
83
|
+
}
|
|
84
|
+
return { handled: false, error: "unhandled_event_type" };
|
|
85
|
+
}
|
|
86
|
+
return handler(event);
|
|
87
|
+
}
|
|
88
|
+
async resolveUserId(event) {
|
|
89
|
+
if (event.userId)
|
|
90
|
+
return event.userId;
|
|
91
|
+
if (event.customer?.providerCustomerId) {
|
|
92
|
+
const uid = await this.store.getBillingCustomer(event.provider, event.customer.providerCustomerId);
|
|
93
|
+
if (uid)
|
|
94
|
+
return uid;
|
|
95
|
+
}
|
|
96
|
+
if (this.resolveUser && event.customer?.providerCustomerId) {
|
|
97
|
+
return this.resolveUser(event.provider, event.customer.providerCustomerId, event.customer.email ?? null);
|
|
98
|
+
}
|
|
99
|
+
if (event.subscription?.providerSubscriptionId) {
|
|
100
|
+
const existing = await this.store.getBillingSubscription(event.provider, event.subscription.providerSubscriptionId);
|
|
101
|
+
if (existing?.userId)
|
|
102
|
+
return existing.userId;
|
|
103
|
+
}
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
async resolveBillingOfferCached(provider, productId, priceId) {
|
|
107
|
+
const cacheKey = `${provider}:${productId ?? ""}:${priceId ?? ""}`;
|
|
108
|
+
const cached = this.offerCache.get(cacheKey);
|
|
109
|
+
if (cached && cached.expires > Date.now()) {
|
|
110
|
+
return cached.offer;
|
|
111
|
+
}
|
|
112
|
+
const offer = await this.store.resolveBillingOffer(provider, productId, priceId);
|
|
113
|
+
this.offerCache.set(cacheKey, { offer, expires: Date.now() + this.OFFER_CACHE_TTL_MS });
|
|
114
|
+
return offer;
|
|
115
|
+
}
|
|
116
|
+
async handleCustomerCreated(event) {
|
|
117
|
+
if (event.customer?.providerCustomerId) {
|
|
118
|
+
const uid = await this.resolveUserId(event);
|
|
119
|
+
if (uid) {
|
|
120
|
+
await this.store.upsertBillingCustomer(event.provider, event.customer.providerCustomerId, uid, event.customer.email ?? null);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return { handled: true, action: "customer_created" };
|
|
124
|
+
}
|
|
125
|
+
async handleCustomerUpdated(_event) {
|
|
126
|
+
return { handled: true, action: "customer_updated" };
|
|
127
|
+
}
|
|
128
|
+
async handleCustomerDeleted(_event) {
|
|
129
|
+
return { handled: true, action: "customer_deleted" };
|
|
130
|
+
}
|
|
131
|
+
async handleCheckoutCompleted(event) {
|
|
132
|
+
if (event.customer?.providerCustomerId) {
|
|
133
|
+
const uid = await this.resolveUserId(event);
|
|
134
|
+
if (uid) {
|
|
135
|
+
await this.store.upsertBillingCustomer(event.provider, event.customer.providerCustomerId, uid, event.customer.email ?? null);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
if (event.subscription?.providerSubscriptionId) {
|
|
139
|
+
return this.handleSubscriptionCreated(event);
|
|
140
|
+
}
|
|
141
|
+
return { handled: true, action: "checkout_completed" };
|
|
142
|
+
}
|
|
143
|
+
async getExistingSubscription(event) {
|
|
144
|
+
if (!event.subscription?.providerSubscriptionId)
|
|
145
|
+
return null;
|
|
146
|
+
return this.store.getBillingSubscription(event.provider, event.subscription.providerSubscriptionId);
|
|
147
|
+
}
|
|
148
|
+
buildSubscriptionState(event, userId, existing, overrides) {
|
|
149
|
+
if (!event.subscription) {
|
|
150
|
+
throw new Error("no_subscription_data");
|
|
151
|
+
}
|
|
152
|
+
const sub = event.subscription;
|
|
153
|
+
return {
|
|
154
|
+
userId,
|
|
155
|
+
provider: event.provider,
|
|
156
|
+
providerSubscriptionId: sub.providerSubscriptionId,
|
|
157
|
+
providerCustomerId: event.customer?.providerCustomerId ?? existing?.providerCustomerId ?? null,
|
|
158
|
+
offerKey: overrides?.offerKey ?? existing?.offerKey ?? null,
|
|
159
|
+
planKey: overrides?.planKey ?? existing?.planKey ?? null,
|
|
160
|
+
status: overrides?.status ?? sub.status ?? existing?.status ?? "incomplete",
|
|
161
|
+
currentPeriodStart: sub.periodStart ?? existing?.currentPeriodStart ?? null,
|
|
162
|
+
currentPeriodEnd: sub.periodEnd ?? existing?.currentPeriodEnd ?? null,
|
|
163
|
+
cancelAtPeriodEnd: overrides?.cancelAtPeriodEnd ??
|
|
164
|
+
sub.cancelAtPeriodEnd ??
|
|
165
|
+
existing?.cancelAtPeriodEnd ??
|
|
166
|
+
false,
|
|
167
|
+
interval: sub.interval ?? existing?.interval ?? null,
|
|
168
|
+
intervalCount: sub.intervalCount ?? existing?.intervalCount ?? null,
|
|
169
|
+
metadata: event.metadata ?? existing?.metadata ?? null,
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
async resolveOfferAndKeys(event) {
|
|
173
|
+
const refs = event.subscription?.refs;
|
|
174
|
+
if (!refs)
|
|
175
|
+
return { offer: null, offerKey: null, planKey: null };
|
|
176
|
+
const offer = await this.resolveBillingOfferCached(event.provider, refs.productId ?? null, refs.priceId ?? null);
|
|
177
|
+
if (offer) {
|
|
178
|
+
return {
|
|
179
|
+
offer,
|
|
180
|
+
offerKey: offer?.offerKey ?? null,
|
|
181
|
+
planKey: offer?.planKey ?? null,
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
// Fallback to lookupKey when no provider refs match (mock/Dodo webhooks)
|
|
185
|
+
if (refs.lookupKey) {
|
|
186
|
+
return {
|
|
187
|
+
offer: { planKey: refs.lookupKey, entitlementMode: "allowance" },
|
|
188
|
+
offerKey: null,
|
|
189
|
+
planKey: refs.lookupKey,
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
return { offer: null, offerKey: null, planKey: null };
|
|
193
|
+
}
|
|
194
|
+
async handleSubscriptionCreated(event) {
|
|
195
|
+
const uid = await this.resolveUserId(event);
|
|
196
|
+
if (!uid)
|
|
197
|
+
return { handled: false, error: "user_not_found" };
|
|
198
|
+
if (!event.subscription?.providerSubscriptionId)
|
|
199
|
+
return { handled: false, error: "no_subscription_data" };
|
|
200
|
+
const existing = await this.getExistingSubscription(event);
|
|
201
|
+
const { offer, offerKey, planKey } = await this.resolveOfferAndKeys(event);
|
|
202
|
+
await this.store.upsertBillingSubscription(this.buildSubscriptionState(event, uid, existing, {
|
|
203
|
+
status: event.subscription.status ?? "incomplete",
|
|
204
|
+
cancelAtPeriodEnd: event.subscription.cancelAtPeriodEnd ?? false,
|
|
205
|
+
offerKey: offerKey ?? existing?.offerKey ?? null,
|
|
206
|
+
planKey: planKey ?? existing?.planKey ?? null,
|
|
207
|
+
}));
|
|
208
|
+
if (this.cm &&
|
|
209
|
+
event.subscription.status &&
|
|
210
|
+
["active", "trialing"].includes(event.subscription.status)) {
|
|
211
|
+
await this.provisionSubscription(uid, offer ?? (existing?.planKey ? { planKey: existing.planKey } : null), event);
|
|
212
|
+
}
|
|
213
|
+
return { handled: true, action: "subscription_created" };
|
|
214
|
+
}
|
|
215
|
+
async handleSubscriptionUpdated(event) {
|
|
216
|
+
const uid = await this.resolveUserId(event);
|
|
217
|
+
if (!uid)
|
|
218
|
+
return { handled: false, error: "user_not_found" };
|
|
219
|
+
if (!event.subscription?.providerSubscriptionId)
|
|
220
|
+
return { handled: false, error: "no_subscription_data" };
|
|
221
|
+
const existing = await this.getExistingSubscription(event);
|
|
222
|
+
const { offerKey, planKey } = await this.resolveOfferAndKeys(event);
|
|
223
|
+
await this.store.upsertBillingSubscription(this.buildSubscriptionState(event, uid, existing, {
|
|
224
|
+
status: event.subscription.status ?? existing?.status ?? "incomplete",
|
|
225
|
+
cancelAtPeriodEnd: event.subscription.cancelAtPeriodEnd ?? existing?.cancelAtPeriodEnd ?? false,
|
|
226
|
+
offerKey: offerKey ?? existing?.offerKey ?? null,
|
|
227
|
+
planKey: planKey ?? existing?.planKey ?? null,
|
|
228
|
+
}));
|
|
229
|
+
if (this.cm) {
|
|
230
|
+
await this.reEvaluateAccess(uid, event);
|
|
231
|
+
}
|
|
232
|
+
return { handled: true, action: "subscription_updated" };
|
|
233
|
+
}
|
|
234
|
+
async handleSubscriptionActivated(event) {
|
|
235
|
+
const uid = await this.resolveUserId(event);
|
|
236
|
+
if (!uid)
|
|
237
|
+
return { handled: false, error: "user_not_found" };
|
|
238
|
+
if (!event.subscription?.providerSubscriptionId)
|
|
239
|
+
return { handled: false, error: "no_subscription_data" };
|
|
240
|
+
const existing = await this.getExistingSubscription(event);
|
|
241
|
+
const { offer, offerKey, planKey } = await this.resolveOfferAndKeys(event);
|
|
242
|
+
await this.store.upsertBillingSubscription(this.buildSubscriptionState(event, uid, existing, {
|
|
243
|
+
status: "active",
|
|
244
|
+
offerKey: offerKey ?? existing?.offerKey ?? null,
|
|
245
|
+
planKey: planKey ?? existing?.planKey ?? null,
|
|
246
|
+
}));
|
|
247
|
+
if (this.cm) {
|
|
248
|
+
await this.provisionSubscription(uid, offer ?? (existing?.planKey ? { planKey: existing.planKey } : null), event);
|
|
249
|
+
}
|
|
250
|
+
return { handled: true, action: "subscription_activated" };
|
|
251
|
+
}
|
|
252
|
+
async handleSubscriptionRenewed(event) {
|
|
253
|
+
const uid = await this.resolveUserId(event);
|
|
254
|
+
if (!uid)
|
|
255
|
+
return { handled: false, error: "user_not_found" };
|
|
256
|
+
if (!event.subscription?.providerSubscriptionId)
|
|
257
|
+
return { handled: false, error: "no_subscription_data" };
|
|
258
|
+
const existing = await this.getExistingSubscription(event);
|
|
259
|
+
const { offerKey, planKey } = await this.resolveOfferAndKeys(event);
|
|
260
|
+
const resolvedPlanKey = planKey ?? existing?.planKey ?? null;
|
|
261
|
+
await this.store.upsertBillingSubscription(this.buildSubscriptionState(event, uid, existing, {
|
|
262
|
+
status: "active",
|
|
263
|
+
offerKey: offerKey ?? existing?.offerKey ?? null,
|
|
264
|
+
planKey: resolvedPlanKey,
|
|
265
|
+
}));
|
|
266
|
+
if (this.cm && resolvedPlanKey) {
|
|
267
|
+
const periodStart = event.subscription.periodStart;
|
|
268
|
+
await this.cm.setUserPlan(uid, resolvedPlanKey, periodStart ? new Date(periodStart) : undefined);
|
|
269
|
+
}
|
|
270
|
+
return { handled: true, action: "subscription_renewed" };
|
|
271
|
+
}
|
|
272
|
+
async handleSubscriptionPlanChanged(event) {
|
|
273
|
+
const uid = await this.resolveUserId(event);
|
|
274
|
+
if (!uid)
|
|
275
|
+
return { handled: false, error: "user_not_found" };
|
|
276
|
+
if (!event.subscription?.providerSubscriptionId)
|
|
277
|
+
return { handled: false, error: "no_subscription_data" };
|
|
278
|
+
const existing = await this.getExistingSubscription(event);
|
|
279
|
+
const { offer, offerKey, planKey } = await this.resolveOfferAndKeys(event);
|
|
280
|
+
await this.store.upsertBillingSubscription(this.buildSubscriptionState(event, uid, existing, {
|
|
281
|
+
status: event.subscription.status ?? "active",
|
|
282
|
+
offerKey: offerKey ?? existing?.offerKey ?? null,
|
|
283
|
+
planKey: planKey ?? existing?.planKey ?? null,
|
|
284
|
+
}));
|
|
285
|
+
if (this.cm && (planKey ?? existing?.planKey)) {
|
|
286
|
+
await this.provisionSubscription(uid, offer ?? (existing?.planKey ? { planKey: existing.planKey } : null), event);
|
|
287
|
+
}
|
|
288
|
+
return { handled: true, action: "subscription_plan_changed" };
|
|
289
|
+
}
|
|
290
|
+
async handleCancellationScheduled(event) {
|
|
291
|
+
const uid = await this.resolveUserId(event);
|
|
292
|
+
if (!uid)
|
|
293
|
+
return { handled: false, error: "user_not_found" };
|
|
294
|
+
if (!event.subscription?.providerSubscriptionId)
|
|
295
|
+
return { handled: false, error: "no_subscription_data" };
|
|
296
|
+
const existing = await this.getExistingSubscription(event);
|
|
297
|
+
await this.store.upsertBillingSubscription(this.buildSubscriptionState(event, uid, existing, {
|
|
298
|
+
status: existing?.status ?? event.subscription.status ?? "active",
|
|
299
|
+
cancelAtPeriodEnd: true,
|
|
300
|
+
}));
|
|
301
|
+
return { handled: true, action: "cancellation_scheduled" };
|
|
302
|
+
}
|
|
303
|
+
async handleCancellationUnscheduled(event) {
|
|
304
|
+
const uid = await this.resolveUserId(event);
|
|
305
|
+
if (!uid)
|
|
306
|
+
return { handled: false, error: "user_not_found" };
|
|
307
|
+
if (!event.subscription?.providerSubscriptionId)
|
|
308
|
+
return { handled: false, error: "no_subscription_data" };
|
|
309
|
+
const existing = await this.getExistingSubscription(event);
|
|
310
|
+
await this.store.upsertBillingSubscription(this.buildSubscriptionState(event, uid, existing, {
|
|
311
|
+
status: existing?.status ?? event.subscription.status ?? "active",
|
|
312
|
+
cancelAtPeriodEnd: false,
|
|
313
|
+
}));
|
|
314
|
+
return { handled: true, action: "cancellation_unscheduled" };
|
|
315
|
+
}
|
|
316
|
+
async handleSubscriptionCanceled(event) {
|
|
317
|
+
const uid = await this.resolveUserId(event);
|
|
318
|
+
if (!uid)
|
|
319
|
+
return { handled: false, error: "user_not_found" };
|
|
320
|
+
if (!event.subscription?.providerSubscriptionId)
|
|
321
|
+
return { handled: false, error: "no_subscription_data" };
|
|
322
|
+
const existing = await this.getExistingSubscription(event);
|
|
323
|
+
await this.store.upsertBillingSubscription(this.buildSubscriptionState(event, uid, existing, {
|
|
324
|
+
status: "canceled",
|
|
325
|
+
cancelAtPeriodEnd: event.subscription.cancelAtPeriodEnd ?? true,
|
|
326
|
+
}));
|
|
327
|
+
if (this.cm) {
|
|
328
|
+
await this.revokeSubscription(uid);
|
|
329
|
+
}
|
|
330
|
+
return { handled: true, action: "subscription_canceled" };
|
|
331
|
+
}
|
|
332
|
+
async handleSubscriptionExpired(event) {
|
|
333
|
+
const uid = await this.resolveUserId(event);
|
|
334
|
+
if (!uid)
|
|
335
|
+
return { handled: false, error: "user_not_found" };
|
|
336
|
+
if (!event.subscription?.providerSubscriptionId)
|
|
337
|
+
return { handled: false, error: "no_subscription_data" };
|
|
338
|
+
const existing = await this.getExistingSubscription(event);
|
|
339
|
+
await this.store.upsertBillingSubscription(this.buildSubscriptionState(event, uid, existing, {
|
|
340
|
+
status: "expired",
|
|
341
|
+
cancelAtPeriodEnd: event.subscription.cancelAtPeriodEnd ?? true,
|
|
342
|
+
}));
|
|
343
|
+
if (this.cm) {
|
|
344
|
+
await this.revokeSubscription(uid);
|
|
345
|
+
}
|
|
346
|
+
return { handled: true, action: "subscription_expired" };
|
|
347
|
+
}
|
|
348
|
+
async handleSubscriptionPaused(event) {
|
|
349
|
+
const uid = await this.resolveUserId(event);
|
|
350
|
+
if (!uid)
|
|
351
|
+
return { handled: false, error: "user_not_found" };
|
|
352
|
+
if (!event.subscription?.providerSubscriptionId)
|
|
353
|
+
return { handled: false, error: "no_subscription_data" };
|
|
354
|
+
const existing = await this.getExistingSubscription(event);
|
|
355
|
+
await this.store.upsertBillingSubscription(this.buildSubscriptionState(event, uid, existing, {
|
|
356
|
+
status: "paused",
|
|
357
|
+
cancelAtPeriodEnd: existing?.cancelAtPeriodEnd ?? false,
|
|
358
|
+
}));
|
|
359
|
+
if (this.cm) {
|
|
360
|
+
await this.revokeSubscription(uid);
|
|
361
|
+
}
|
|
362
|
+
return { handled: true, action: "subscription_paused" };
|
|
363
|
+
}
|
|
364
|
+
async handleSubscriptionResumed(event) {
|
|
365
|
+
const uid = await this.resolveUserId(event);
|
|
366
|
+
if (!uid)
|
|
367
|
+
return { handled: false, error: "user_not_found" };
|
|
368
|
+
if (!event.subscription?.providerSubscriptionId)
|
|
369
|
+
return { handled: false, error: "no_subscription_data" };
|
|
370
|
+
const existing = await this.getExistingSubscription(event);
|
|
371
|
+
const { offer, offerKey, planKey } = await this.resolveOfferAndKeys(event);
|
|
372
|
+
await this.store.upsertBillingSubscription(this.buildSubscriptionState(event, uid, existing, {
|
|
373
|
+
status: "active",
|
|
374
|
+
cancelAtPeriodEnd: false,
|
|
375
|
+
offerKey: offerKey ?? existing?.offerKey ?? null,
|
|
376
|
+
planKey: planKey ?? existing?.planKey ?? null,
|
|
377
|
+
}));
|
|
378
|
+
if (this.cm) {
|
|
379
|
+
await this.provisionSubscription(uid, offer ?? (existing?.planKey ? { planKey: existing.planKey } : null), event);
|
|
380
|
+
}
|
|
381
|
+
return { handled: true, action: "subscription_resumed" };
|
|
382
|
+
}
|
|
383
|
+
async handleTrialWillEnd(event) {
|
|
384
|
+
if (this.onTrialWillEnd) {
|
|
385
|
+
try {
|
|
386
|
+
await this.onTrialWillEnd(event);
|
|
387
|
+
}
|
|
388
|
+
catch (err) {
|
|
389
|
+
console.error(`[BillingManager] onTrialWillEnd callback failed for ${event.provider}/${event.eventId}`, err);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
return { handled: true, action: "trial_will_end_notified" };
|
|
393
|
+
}
|
|
394
|
+
async handleInvoicePaid(event) {
|
|
395
|
+
if (event.invoice) {
|
|
396
|
+
const uid = await this.resolveUserId(event);
|
|
397
|
+
if (uid) {
|
|
398
|
+
await this.store.upsertBillingInvoice(event.provider, event.invoice.providerInvoiceId, event.subscription?.providerSubscriptionId, uid, event.invoice.status, event.invoice.amountPaidMinor, event.invoice.amountDueMinor, event.invoice.currency, event.invoice.periodStart, event.invoice.periodEnd);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
if (event.subscription)
|
|
402
|
+
return this.handleSubscriptionRenewed(event);
|
|
403
|
+
return { handled: true, action: "invoice_paid" };
|
|
404
|
+
}
|
|
405
|
+
async handlePaymentSucceeded(event) {
|
|
406
|
+
if (!event.payment)
|
|
407
|
+
return { handled: true, action: "payment_succeeded" };
|
|
408
|
+
const uid = await this.resolveUserId(event);
|
|
409
|
+
const refs = event.payment.refs;
|
|
410
|
+
let topupConfig = null;
|
|
411
|
+
if (refs) {
|
|
412
|
+
topupConfig = await this.store.resolveCreditTopup(event.provider, refs.productId ?? null, refs.priceId ?? null);
|
|
413
|
+
}
|
|
414
|
+
if (uid) {
|
|
415
|
+
const paymentMetadata = topupConfig && event.payment.purpose === "credit_topup"
|
|
416
|
+
? { creditsPerMajorUnit: Number(topupConfig.creditsPerMajorUnit ?? 1000) }
|
|
417
|
+
: null;
|
|
418
|
+
await this.store.upsertBillingPayment(event.provider, event.payment.providerPaymentId, undefined, uid, event.payment.amountMinor, event.payment.taxMinor, event.payment.currency, event.payment.purpose, paymentMetadata);
|
|
419
|
+
}
|
|
420
|
+
if (topupConfig && this.cm && event.payment.purpose === "credit_topup" && uid) {
|
|
421
|
+
const currency = String(topupConfig.currency ?? "USD");
|
|
422
|
+
if (event.payment.currency.toUpperCase() === currency.toUpperCase()) {
|
|
423
|
+
const minAmount = Number(topupConfig.minAmountMinor ?? 0);
|
|
424
|
+
const maxAmount = Number(topupConfig.maxAmountMinor ?? Number.MAX_SAFE_INTEGER);
|
|
425
|
+
if (event.payment.amountMinor >= minAmount && event.payment.amountMinor <= maxAmount) {
|
|
426
|
+
const credits = await this.store.computeTopupCredits(event.payment.amountMinor, topupConfig);
|
|
427
|
+
if (credits > 0) {
|
|
428
|
+
await this.cm.addCredits(uid, new Decimal(credits), {
|
|
429
|
+
type: "purchase",
|
|
430
|
+
tier: topupConfig.tier ?? "purchased",
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
return { handled: true, action: "payment_succeeded" };
|
|
437
|
+
}
|
|
438
|
+
async handlePaymentFailed(event) {
|
|
439
|
+
const uid = await this.resolveUserId(event);
|
|
440
|
+
if (uid && event.payment) {
|
|
441
|
+
await this.store.upsertBillingPayment(event.provider, event.payment.providerPaymentId, undefined, uid, event.payment.amountMinor, undefined, event.payment.currency, event.payment.purpose);
|
|
442
|
+
}
|
|
443
|
+
if (uid && event.subscription && this.cm) {
|
|
444
|
+
const existing = await this.getExistingSubscription(event);
|
|
445
|
+
await this.store.upsertBillingSubscription(this.buildSubscriptionState(event, uid, existing, { status: "past_due" }));
|
|
446
|
+
await this.revokeSubscription(uid);
|
|
447
|
+
}
|
|
448
|
+
return { handled: true, action: "payment_failed_recorded" };
|
|
449
|
+
}
|
|
450
|
+
async handleRefundCreated(event) {
|
|
451
|
+
const uid = await this.resolveUserId(event);
|
|
452
|
+
if (uid && event.refund) {
|
|
453
|
+
await this.store.upsertBillingRefund(event.provider, event.refund.providerRefundId, event.refund.providerPaymentId, uid, event.refund.amountMinor, event.refund.currency, event.refund.reason);
|
|
454
|
+
if (event.refund.providerPaymentId && this.cm) {
|
|
455
|
+
const payment = await this.store.getBillingPayment(event.provider, event.refund.providerPaymentId);
|
|
456
|
+
if (payment?.purpose === "credit_topup") {
|
|
457
|
+
const payMeta = (payment.metadata ?? {});
|
|
458
|
+
const creditsPerMajor = Number(payMeta.creditsPerMajorUnit ?? 0);
|
|
459
|
+
if (!creditsPerMajor) {
|
|
460
|
+
console.warn(`[BillingManager] cannot claw back credits for refund ${event.refund.providerRefundId}: no creditsPerMajorUnit in payment metadata`);
|
|
461
|
+
return { handled: true, action: "refund_recorded_no_clawback" };
|
|
462
|
+
}
|
|
463
|
+
const credits = Math.trunc((event.refund.amountMinor * creditsPerMajor) / 100);
|
|
464
|
+
if (credits > 0) {
|
|
465
|
+
await this.cm.deductCredits(uid, credits, {
|
|
466
|
+
txType: "refund_clawback",
|
|
467
|
+
tier: "purchased",
|
|
468
|
+
});
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
return { handled: true, action: "refund_recorded" };
|
|
474
|
+
}
|
|
475
|
+
async handleDisputeCreated(event) {
|
|
476
|
+
const uid = await this.resolveUserId(event);
|
|
477
|
+
if (uid && event.dispute) {
|
|
478
|
+
await this.store.upsertBillingDispute(event.provider, event.dispute.providerDisputeId, event.dispute.providerPaymentId ?? undefined, uid, "needs_response", event.dispute.reason ?? undefined);
|
|
479
|
+
}
|
|
480
|
+
return { handled: true, action: "dispute_recorded" };
|
|
481
|
+
}
|
|
482
|
+
async handleDisputeClosed(event) {
|
|
483
|
+
const uid = await this.resolveUserId(event);
|
|
484
|
+
if (uid && event.dispute) {
|
|
485
|
+
await this.store.upsertBillingDispute(event.provider, event.dispute.providerDisputeId, event.dispute.providerPaymentId ?? undefined, uid, "closed", event.dispute.reason ?? undefined);
|
|
486
|
+
}
|
|
487
|
+
return { handled: true, action: "dispute_closed" };
|
|
488
|
+
}
|
|
489
|
+
async provisionSubscription(uid, offer, event) {
|
|
490
|
+
if (!offer) {
|
|
491
|
+
console.log(`[BillingManager] provisionSubscription: no offer for user ${uid}`);
|
|
492
|
+
return;
|
|
493
|
+
}
|
|
494
|
+
if (!this.cm) {
|
|
495
|
+
console.log(`[BillingManager] provisionSubscription: no creditManager for user ${uid}`);
|
|
496
|
+
return;
|
|
497
|
+
}
|
|
498
|
+
const planKey = offer.planKey;
|
|
499
|
+
if (!planKey) {
|
|
500
|
+
console.log(`[BillingManager] provisionSubscription: no planKey in offer for user ${uid}`);
|
|
501
|
+
return;
|
|
502
|
+
}
|
|
503
|
+
const periodStart = event.subscription?.periodStart;
|
|
504
|
+
const planAssignedAt = periodStart
|
|
505
|
+
? (() => {
|
|
506
|
+
const d = new Date(periodStart);
|
|
507
|
+
return isNaN(d.getTime()) ? undefined : d;
|
|
508
|
+
})()
|
|
509
|
+
: undefined;
|
|
510
|
+
await this.cm.setUserPlan(uid, planKey, planAssignedAt);
|
|
511
|
+
const entitlementMode = offer?.entitlementMode;
|
|
512
|
+
if (entitlementMode === "cycle_grant" && this.cm) {
|
|
513
|
+
const cycleCredits = offer?.cycleGrantCredits;
|
|
514
|
+
if (cycleCredits && cycleCredits > 0) {
|
|
515
|
+
const cycleTier = offer?.cycleGrantTier ?? "purchased";
|
|
516
|
+
const replacePrior = offer?.cycleGrantReplacePrior ?? true;
|
|
517
|
+
if (replacePrior) {
|
|
518
|
+
await this.cm.revokeCreditsByTxType(uid, "cycle_grant");
|
|
519
|
+
}
|
|
520
|
+
await this.cm.addCredits(uid, new Decimal(cycleCredits), {
|
|
521
|
+
type: "cycle_grant",
|
|
522
|
+
tier: cycleTier,
|
|
523
|
+
});
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
async revokeSubscription(uid) {
|
|
528
|
+
if (!this.cm)
|
|
529
|
+
return;
|
|
530
|
+
await this.cm.unsetUserPlan(uid);
|
|
531
|
+
}
|
|
532
|
+
async reEvaluateAccess(uid, event) {
|
|
533
|
+
if (!this.cm || !event.subscription)
|
|
534
|
+
return;
|
|
535
|
+
const status = event.subscription.status;
|
|
536
|
+
if (status && ["active", "trialing"].includes(status)) {
|
|
537
|
+
const offer = await this.resolveOffer(event);
|
|
538
|
+
if (offer) {
|
|
539
|
+
await this.provisionSubscription(uid, offer, event);
|
|
540
|
+
}
|
|
541
|
+
else {
|
|
542
|
+
const existing = await this.store.getBillingSubscription(event.provider, event.subscription.providerSubscriptionId);
|
|
543
|
+
if (existing?.planKey) {
|
|
544
|
+
await this.provisionSubscription(uid, { planKey: existing.planKey }, event);
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
else if (status &&
|
|
549
|
+
["canceled", "expired", "unpaid", "paused", "incomplete_expired"].includes(status)) {
|
|
550
|
+
await this.revokeSubscription(uid);
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
async resolveOffer(event) {
|
|
554
|
+
const refs = event.subscription?.refs;
|
|
555
|
+
if (!refs)
|
|
556
|
+
return null;
|
|
557
|
+
return this.resolveBillingOfferCached(event.provider, refs.productId ?? null, refs.priceId ?? null);
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
//# sourceMappingURL=billing-manager.js.map
|