@zonastery/bursar 2.1.1 → 2.2.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/README.md +20 -3
- package/dist/billing/auto-recharge-service.d.ts.map +1 -1
- package/dist/billing/auto-recharge-service.js +6 -4
- package/dist/billing/auto-recharge-service.js.map +1 -1
- package/dist/billing/billing-service.d.ts +19 -3
- package/dist/billing/billing-service.d.ts.map +1 -1
- package/dist/billing/billing-service.js +34 -5
- package/dist/billing/billing-service.js.map +1 -1
- package/dist/billing/billing-store.d.ts +12 -6
- package/dist/billing/billing-store.d.ts.map +1 -1
- package/dist/billing/billing-store.js +2 -0
- package/dist/billing/billing-store.js.map +1 -1
- package/dist/billing/contracts.d.ts +20 -3
- package/dist/billing/contracts.d.ts.map +1 -1
- package/dist/billing/contracts.js.map +1 -1
- package/dist/billing/event-handlers.d.ts +1 -0
- package/dist/billing/event-handlers.d.ts.map +1 -1
- package/dist/billing/event-handlers.js +40 -13
- package/dist/billing/event-handlers.js.map +1 -1
- package/dist/billing/event-processor.d.ts +2 -1
- package/dist/billing/event-processor.d.ts.map +1 -1
- package/dist/billing/event-processor.js +82 -16
- package/dist/billing/event-processor.js.map +1 -1
- package/dist/billing/financial-event-handlers.d.ts +2 -0
- package/dist/billing/financial-event-handlers.d.ts.map +1 -1
- package/dist/billing/financial-event-handlers.js +160 -48
- package/dist/billing/financial-event-handlers.js.map +1 -1
- package/dist/billing/index.d.ts +1 -1
- package/dist/billing/index.d.ts.map +1 -1
- package/dist/billing/index.js.map +1 -1
- package/dist/billing/management.d.ts +6 -3
- package/dist/billing/management.d.ts.map +1 -1
- package/dist/billing/management.js +5 -2
- package/dist/billing/management.js.map +1 -1
- package/dist/billing/postgres/repositories/auto-recharge.d.ts +3 -1
- package/dist/billing/postgres/repositories/auto-recharge.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/auto-recharge.js +23 -7
- package/dist/billing/postgres/repositories/auto-recharge.js.map +1 -1
- package/dist/billing/postgres/repositories/invoice.d.ts +3 -2
- package/dist/billing/postgres/repositories/invoice.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/invoice.js +33 -3
- package/dist/billing/postgres/repositories/invoice.js.map +1 -1
- package/dist/billing/postgres/repositories/offer.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/offer.js +2 -1
- package/dist/billing/postgres/repositories/offer.js.map +1 -1
- package/dist/billing/postgres/repositories/payment.d.ts +6 -1
- package/dist/billing/postgres/repositories/payment.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/payment.js +25 -17
- package/dist/billing/postgres/repositories/payment.js.map +1 -1
- package/dist/billing/postgres/repositories/subscription-change.d.ts +1 -0
- package/dist/billing/postgres/repositories/subscription-change.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/subscription-change.js +24 -4
- package/dist/billing/postgres/repositories/subscription-change.js.map +1 -1
- package/dist/billing/postgres/repositories/subscription.d.ts +1 -0
- package/dist/billing/postgres/repositories/subscription.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/subscription.js +40 -43
- package/dist/billing/postgres/repositories/subscription.js.map +1 -1
- package/dist/billing/postgres/repositories/topup.d.ts +6 -1
- package/dist/billing/postgres/repositories/topup.d.ts.map +1 -1
- package/dist/billing/postgres/repositories/topup.js +9 -5
- package/dist/billing/postgres/repositories/topup.js.map +1 -1
- package/dist/billing/postgres/store.d.ts +24 -31
- package/dist/billing/postgres/store.d.ts.map +1 -1
- package/dist/billing/postgres/store.js +110 -113
- package/dist/billing/postgres/store.js.map +1 -1
- package/dist/billing/types/catalog.d.ts +1 -0
- package/dist/billing/types/catalog.d.ts.map +1 -1
- package/dist/billing/types/catalog.js.map +1 -1
- package/dist/billing/types/documents.d.ts +15 -1
- package/dist/billing/types/documents.d.ts.map +1 -1
- package/dist/billing/types/documents.js.map +1 -1
- package/dist/billing/types/events.d.ts +2 -0
- package/dist/billing/types/events.d.ts.map +1 -1
- package/dist/billing/types/events.js +15 -0
- package/dist/billing/types/events.js.map +1 -1
- package/dist/billing/types/subscriptions.d.ts +10 -0
- package/dist/billing/types/subscriptions.d.ts.map +1 -1
- package/dist/billing/types/subscriptions.js.map +1 -1
- package/dist/breakdown.d.ts +2 -24
- package/dist/breakdown.d.ts.map +1 -1
- package/dist/breakdown.js +1 -29
- package/dist/breakdown.js.map +1 -1
- package/dist/bursar.d.ts +5 -17
- package/dist/bursar.d.ts.map +1 -1
- package/dist/bursar.js +26 -44
- package/dist/bursar.js.map +1 -1
- package/dist/cli.js +0 -6
- package/dist/cli.js.map +1 -1
- package/dist/commerce/service.d.ts +1 -0
- package/dist/commerce/service.d.ts.map +1 -1
- package/dist/commerce/service.js +162 -38
- package/dist/commerce/service.js.map +1 -1
- package/dist/commerce/types.d.ts +2 -0
- package/dist/commerce/types.d.ts.map +1 -1
- package/dist/commerce/types.js.map +1 -1
- package/dist/config/normalize.d.ts +3 -1
- package/dist/config/normalize.d.ts.map +1 -1
- package/dist/config/normalize.js +12 -5
- package/dist/config/normalize.js.map +1 -1
- package/dist/config/parse-access.d.ts.map +1 -1
- package/dist/config/parse-access.js +4 -1
- package/dist/config/parse-access.js.map +1 -1
- package/dist/config/parse-commerce.d.ts.map +1 -1
- package/dist/config/parse-commerce.js +32 -17
- package/dist/config/parse-commerce.js.map +1 -1
- package/dist/config/parse-credits.d.ts.map +1 -1
- package/dist/config/parse-credits.js +5 -5
- package/dist/config/parse-credits.js.map +1 -1
- package/dist/config/parse-plans.d.ts.map +1 -1
- package/dist/config/parse-plans.js +13 -9
- package/dist/config/parse-plans.js.map +1 -1
- package/dist/config/parse-pricing.d.ts.map +1 -1
- package/dist/config/parse-pricing.js +7 -5
- package/dist/config/parse-pricing.js.map +1 -1
- package/dist/config/parse-utils.d.ts +2 -0
- package/dist/config/parse-utils.d.ts.map +1 -1
- package/dist/config/parse-utils.js +11 -1
- package/dist/config/parse-utils.js.map +1 -1
- package/dist/config.d.ts +2 -2
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +94 -86
- package/dist/config.js.map +1 -1
- package/dist/credits/amount.d.ts.map +1 -1
- package/dist/credits/amount.js +5 -4
- package/dist/credits/amount.js.map +1 -1
- package/dist/credits/catalog-runtime.d.ts +15 -2
- package/dist/credits/catalog-runtime.d.ts.map +1 -1
- package/dist/credits/catalog-runtime.js +74 -18
- package/dist/credits/catalog-runtime.js.map +1 -1
- package/dist/credits/lease-workflow.d.ts +3 -1
- package/dist/credits/lease-workflow.d.ts.map +1 -1
- package/dist/credits/lease-workflow.js +47 -38
- package/dist/credits/lease-workflow.js.map +1 -1
- package/dist/credits/low-balance-monitor.d.ts +2 -7
- package/dist/credits/low-balance-monitor.d.ts.map +1 -1
- package/dist/credits/low-balance-monitor.js +6 -57
- package/dist/credits/low-balance-monitor.js.map +1 -1
- package/dist/credits/postgres/repositories/analytics.d.ts +3 -3
- package/dist/credits/postgres/repositories/analytics.d.ts.map +1 -1
- package/dist/credits/postgres/repositories/analytics.js +25 -7
- package/dist/credits/postgres/repositories/analytics.js.map +1 -1
- package/dist/credits/postgres/repositories/balance.d.ts +28 -0
- package/dist/credits/postgres/repositories/balance.d.ts.map +1 -1
- package/dist/credits/postgres/repositories/balance.js +33 -14
- package/dist/credits/postgres/repositories/balance.js.map +1 -1
- package/dist/credits/postgres/repositories/bucket.d.ts.map +1 -1
- package/dist/credits/postgres/repositories/bucket.js +2 -2
- package/dist/credits/postgres/repositories/bucket.js.map +1 -1
- package/dist/credits/postgres/repositories/catalog.d.ts +2 -2
- package/dist/credits/postgres/repositories/catalog.d.ts.map +1 -1
- package/dist/credits/postgres/repositories/catalog.js +2 -12
- package/dist/credits/postgres/repositories/catalog.js.map +1 -1
- package/dist/credits/postgres/repositories/deduction.d.ts +2 -0
- package/dist/credits/postgres/repositories/deduction.d.ts.map +1 -1
- package/dist/credits/postgres/repositories/deduction.js +9 -2
- package/dist/credits/postgres/repositories/deduction.js.map +1 -1
- package/dist/credits/postgres/repositories/lease.d.ts +3 -0
- package/dist/credits/postgres/repositories/lease.d.ts.map +1 -1
- package/dist/credits/postgres/repositories/lease.js +46 -12
- package/dist/credits/postgres/repositories/lease.js.map +1 -1
- package/dist/credits/postgres/repositories/plan.d.ts +1 -1
- package/dist/credits/postgres/repositories/plan.d.ts.map +1 -1
- package/dist/credits/postgres/repositories/plan.js +4 -1
- package/dist/credits/postgres/repositories/plan.js.map +1 -1
- package/dist/credits/postgres/repositories/team.d.ts +1 -1
- package/dist/credits/postgres/repositories/team.d.ts.map +1 -1
- package/dist/credits/postgres/repositories/team.js +9 -5
- package/dist/credits/postgres/repositories/team.js.map +1 -1
- package/dist/credits/postgres/store.d.ts +29 -20
- package/dist/credits/postgres/store.d.ts.map +1 -1
- package/dist/credits/postgres/store.js +329 -250
- package/dist/credits/postgres/store.js.map +1 -1
- package/dist/credits/postgres/value-mappers.d.ts.map +1 -1
- package/dist/credits/postgres/value-mappers.js +4 -4
- package/dist/credits/postgres/value-mappers.js.map +1 -1
- package/dist/credits/rated-wire.d.ts +7 -0
- package/dist/credits/rated-wire.d.ts.map +1 -0
- package/dist/credits/rated-wire.js +70 -0
- package/dist/credits/rated-wire.js.map +1 -0
- package/dist/credits/service-errors.d.ts.map +1 -1
- package/dist/credits/service-errors.js +8 -1
- package/dist/credits/service-errors.js.map +1 -1
- package/dist/credits/service-types.d.ts +5 -7
- package/dist/credits/service-types.d.ts.map +1 -1
- package/dist/credits/service-types.js.map +1 -1
- package/dist/credits/service.d.ts +11 -9
- package/dist/credits/service.d.ts.map +1 -1
- package/dist/credits/service.js +156 -138
- package/dist/credits/service.js.map +1 -1
- package/dist/credits/store.d.ts +11 -3
- package/dist/credits/store.d.ts.map +1 -1
- package/dist/credits/store.js +9 -0
- package/dist/credits/store.js.map +1 -1
- package/dist/credits/types/account.d.ts +9 -1
- package/dist/credits/types/account.d.ts.map +1 -1
- package/dist/credits/types/account.js.map +1 -1
- package/dist/credits/types/admission.d.ts +1 -0
- package/dist/credits/types/admission.d.ts.map +1 -1
- package/dist/credits/types/admission.js.map +1 -1
- package/dist/credits/types/grants.d.ts +12 -0
- package/dist/credits/types/grants.d.ts.map +1 -1
- package/dist/credits/types/grants.js.map +1 -1
- package/dist/credits/types/ledger.d.ts +13 -0
- package/dist/credits/types/ledger.d.ts.map +1 -1
- package/dist/credits/types/ledger.js.map +1 -1
- package/dist/credits/types/quotas.d.ts +2 -1
- package/dist/credits/types/quotas.d.ts.map +1 -1
- package/dist/credits/types/quotas.js.map +1 -1
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +51 -34
- package/dist/engine.js.map +1 -1
- package/dist/errors.d.ts +12 -1
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +13 -0
- package/dist/errors.js.map +1 -1
- package/dist/expr/evaluator.d.ts.map +1 -1
- package/dist/expr/evaluator.js +29 -9
- package/dist/expr/evaluator.js.map +1 -1
- package/dist/expr/language.js +1 -1
- package/dist/expr/language.js.map +1 -1
- package/dist/expr.d.ts.map +1 -1
- package/dist/expr.js +5 -6
- package/dist/expr.js.map +1 -1
- package/dist/generated/pricing-config.schema.json +102 -202
- package/dist/generated/pricing-config.types.d.ts +7 -1
- package/dist/generated/pricing-config.types.d.ts.map +1 -1
- package/dist/generated/pricing-config.types.js.map +1 -1
- package/dist/index.d.ts +6 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -2
- package/dist/index.js.map +1 -1
- package/dist/load-config-file.d.ts.map +1 -1
- package/dist/load-config-file.js +38 -12
- package/dist/load-config-file.js.map +1 -1
- package/dist/metrics.d.ts +1 -1
- package/dist/metrics.d.ts.map +1 -1
- package/dist/metrics.js.map +1 -1
- package/dist/node.d.ts +1 -1
- package/dist/node.d.ts.map +1 -1
- package/dist/node.js.map +1 -1
- package/dist/pricing-decimal.d.ts +7 -0
- package/dist/pricing-decimal.d.ts.map +1 -0
- package/dist/pricing-decimal.js +43 -0
- package/dist/pricing-decimal.js.map +1 -0
- package/dist/providers/dodo/better-auth-core.d.ts +1 -1
- package/dist/providers/dodo/better-auth-core.d.ts.map +1 -1
- package/dist/providers/dodo/better-auth-core.js +2 -2
- package/dist/providers/dodo/better-auth-core.js.map +1 -1
- package/dist/providers/dodo/better-auth.d.ts.map +1 -1
- package/dist/providers/dodo/better-auth.js +16 -2
- package/dist/providers/dodo/better-auth.js.map +1 -1
- package/dist/providers/dodo/event-mapper.d.ts +1 -1
- package/dist/providers/dodo/event-mapper.d.ts.map +1 -1
- package/dist/providers/dodo/event-mapper.js +120 -18
- package/dist/providers/dodo/event-mapper.js.map +1 -1
- package/dist/providers/dodo/nextjs-core.d.ts +1 -1
- package/dist/providers/dodo/nextjs-core.d.ts.map +1 -1
- package/dist/providers/dodo/nextjs-core.js +2 -2
- package/dist/providers/dodo/nextjs-core.js.map +1 -1
- package/dist/providers/dodo/nextjs.d.ts.map +1 -1
- package/dist/providers/dodo/nextjs.js +16 -6
- package/dist/providers/dodo/nextjs.js.map +1 -1
- package/dist/providers/dodo/provider.d.ts +5 -2
- package/dist/providers/dodo/provider.d.ts.map +1 -1
- package/dist/providers/dodo/provider.js +33 -6
- package/dist/providers/dodo/provider.js.map +1 -1
- package/dist/providers/dodo/verified-payment-payload.d.ts +6 -0
- package/dist/providers/dodo/verified-payment-payload.d.ts.map +1 -0
- package/dist/providers/dodo/verified-payment-payload.js +42 -0
- package/dist/providers/dodo/verified-payment-payload.js.map +1 -0
- package/dist/providers/index.d.ts +1 -1
- package/dist/providers/index.d.ts.map +1 -1
- package/dist/providers/index.js.map +1 -1
- package/dist/providers/mock/provider.d.ts +4 -1
- package/dist/providers/mock/provider.d.ts.map +1 -1
- package/dist/providers/mock/provider.js +3 -1
- package/dist/providers/mock/provider.js.map +1 -1
- package/dist/providers/stripe/event-mapper.d.ts.map +1 -1
- package/dist/providers/stripe/event-mapper.js +48 -20
- package/dist/providers/stripe/event-mapper.js.map +1 -1
- package/dist/providers/stripe/provider.d.ts +4 -1
- package/dist/providers/stripe/provider.d.ts.map +1 -1
- package/dist/providers/stripe/provider.js +64 -23
- package/dist/providers/stripe/provider.js.map +1 -1
- package/dist/providers/types.d.ts +11 -1
- package/dist/providers/types.d.ts.map +1 -1
- package/dist/providers/types.js +9 -0
- package/dist/providers/types.js.map +1 -1
- package/dist/shared/postgres-client.d.ts +3 -0
- package/dist/shared/postgres-client.d.ts.map +1 -1
- package/dist/shared/postgres-client.js +61 -49
- package/dist/shared/postgres-client.js.map +1 -1
- package/dist/shared/provider-rejection.d.ts +3 -0
- package/dist/shared/provider-rejection.d.ts.map +1 -0
- package/dist/shared/provider-rejection.js +17 -0
- package/dist/shared/provider-rejection.js.map +1 -0
- package/dist/shared/timestamp.d.ts +2 -0
- package/dist/shared/timestamp.d.ts.map +1 -1
- package/dist/shared/timestamp.js +7 -0
- package/dist/shared/timestamp.js.map +1 -1
- package/dist/storage/adapters/clickhouse.d.ts +1 -1
- package/dist/storage/adapters/clickhouse.d.ts.map +1 -1
- package/dist/storage/adapters/clickhouse.js +18 -17
- package/dist/storage/adapters/clickhouse.js.map +1 -1
- package/dist/storage/adapters/s3.d.ts +5 -0
- package/dist/storage/adapters/s3.d.ts.map +1 -1
- package/dist/storage/adapters/s3.js +46 -14
- package/dist/storage/adapters/s3.js.map +1 -1
- package/dist/storage/diagnostics.d.ts +3 -7
- package/dist/storage/diagnostics.d.ts.map +1 -1
- package/dist/storage/diagnostics.js +6 -15
- package/dist/storage/diagnostics.js.map +1 -1
- package/dist/storage/outbox-worker.d.ts.map +1 -1
- package/dist/storage/outbox-worker.js +4 -3
- package/dist/storage/outbox-worker.js.map +1 -1
- package/dist/storage/ports.d.ts +3 -0
- package/dist/storage/ports.d.ts.map +1 -1
- package/dist/storage/ports.js.map +1 -1
- package/dist/storage/postgres-repository.d.ts +1 -3
- package/dist/storage/postgres-repository.d.ts.map +1 -1
- package/dist/storage/postgres-repository.js +10 -4
- package/dist/storage/postgres-repository.js.map +1 -1
- package/dist/storage/runtime.d.ts +15 -10
- package/dist/storage/runtime.d.ts.map +1 -1
- package/dist/storage/runtime.js +74 -68
- package/dist/storage/runtime.js.map +1 -1
- package/dist/telemetry/index.d.ts +1 -1
- package/dist/telemetry/index.js +1 -1
- package/dist/telemetry/index.js.map +1 -1
- package/package.json +73 -71
- package/dist/credits/queries.d.ts +0 -33
- package/dist/credits/queries.d.ts.map +0 -1
- package/dist/credits/queries.js +0 -71
- package/dist/credits/queries.js.map +0 -1
package/dist/commerce/service.js
CHANGED
|
@@ -5,8 +5,10 @@ import { loadConfigFromDict } from "../config.js";
|
|
|
5
5
|
import { offerIsAvailable } from "../config/availability.js";
|
|
6
6
|
import { resolveProviderId } from "../config/parse-commerce.js";
|
|
7
7
|
import { ProviderResponseError, StoreError } from "../errors.js";
|
|
8
|
+
import { ProviderOperationOutcomeUnknownError } from "../providers/types.js";
|
|
8
9
|
import { normalizeLogger } from "../shared/logger.js";
|
|
9
10
|
import { requireStableKey, scopedStableKey } from "../shared/idempotency.js";
|
|
11
|
+
import { isDefinitiveProviderRejection } from "../shared/provider-rejection.js";
|
|
10
12
|
import { normalizeProviderEnvironment } from "../providers/environment.js";
|
|
11
13
|
import { persistedDiagnosticSummary } from "../shared/diagnostics.js";
|
|
12
14
|
import { ActiveSubscriptionError, CheckoutCompletedError, CheckoutConflictError, CommerceResourceNotFoundError, CoreBillingDataUnavailableError, InvalidOfferQuantityError, ProviderCapabilityNotSupportedError, QuoteChangedError, UnknownOfferError, } from "./errors.js";
|
|
@@ -396,16 +398,21 @@ export class CommerceService {
|
|
|
396
398
|
}
|
|
397
399
|
const digestValue = {
|
|
398
400
|
accountId: input.accountId,
|
|
401
|
+
cancelUrl: input.cancelUrl,
|
|
399
402
|
checkoutKind: resolved.offer.type,
|
|
403
|
+
email: input.email ?? null,
|
|
404
|
+
metadata: Object.entries(metadata).sort(([left], [right]) => left < right ? -1 : left > right ? 1 : 0),
|
|
400
405
|
offerKey: resolved.offerKey,
|
|
401
406
|
provider: provider.provider,
|
|
402
407
|
quantity,
|
|
408
|
+
returnUrl: input.returnUrl,
|
|
403
409
|
};
|
|
404
410
|
if (input.region !== undefined)
|
|
405
411
|
digestValue.region = input.region;
|
|
406
|
-
const
|
|
407
|
-
.update(JSON.stringify(
|
|
412
|
+
const digest = (value) => createHash("sha256")
|
|
413
|
+
.update(JSON.stringify(value, Object.keys(value).sort()))
|
|
408
414
|
.digest("hex");
|
|
415
|
+
const requestDigest = digest(digestValue);
|
|
409
416
|
const expiresAt = () => new Date(Date.now() + (this.options.checkoutIntentTtlMs ?? DEFAULT_CHECKOUT_INTENT_TTL_MS)).toISOString();
|
|
410
417
|
const createIntent = () => {
|
|
411
418
|
const request = {
|
|
@@ -416,6 +423,7 @@ export class CommerceService {
|
|
|
416
423
|
checkoutKind: resolved.offer.type === "subscription" ? "subscription" : "credit_topup",
|
|
417
424
|
productKey: resolved.offerKey,
|
|
418
425
|
requestDigest,
|
|
426
|
+
providerCustomerId: customer?.providerCustomerId ?? null,
|
|
419
427
|
expiresAt: expiresAt(),
|
|
420
428
|
};
|
|
421
429
|
if (input.region !== undefined)
|
|
@@ -446,14 +454,11 @@ export class CommerceService {
|
|
|
446
454
|
if (intent.status !== "open") {
|
|
447
455
|
throw new CheckoutConflictError("This checkout operation is terminal; start a new checkout with a new operation key");
|
|
448
456
|
}
|
|
457
|
+
if (new Date(intent.expiresAt).getTime() <= Date.now()) {
|
|
458
|
+
await this.billing.updateCheckoutIntent(intent.id, { status: "expired" });
|
|
459
|
+
throw new CheckoutConflictError("This checkout operation expired; start a new checkout with a new operation key");
|
|
460
|
+
}
|
|
449
461
|
if (intent.checkoutUrl) {
|
|
450
|
-
const locallyExpired = new Date(intent.expiresAt).getTime() <= Date.now();
|
|
451
|
-
if (locallyExpired) {
|
|
452
|
-
await this.billing.updateCheckoutIntent(intent.id, {
|
|
453
|
-
status: "expired",
|
|
454
|
-
});
|
|
455
|
-
throw new CheckoutConflictError("This checkout operation expired; start a new checkout with a new operation key");
|
|
456
|
-
}
|
|
457
462
|
if (intent.providerSessionId && provider.getCheckoutSessionStatus) {
|
|
458
463
|
const providerState = await provider.getCheckoutSessionStatus(intent.providerSessionId);
|
|
459
464
|
if (providerState?.paymentStatus === "succeeded") {
|
|
@@ -489,8 +494,8 @@ export class CommerceService {
|
|
|
489
494
|
};
|
|
490
495
|
if (resolved.offer.type === "subscription" && resolved.offer.trial)
|
|
491
496
|
checkoutParams.trial = resolved.offer.trial;
|
|
492
|
-
if (
|
|
493
|
-
checkoutParams.customerId =
|
|
497
|
+
if (intent.providerCustomerId)
|
|
498
|
+
checkoutParams.customerId = intent.providerCustomerId;
|
|
494
499
|
if (input.email)
|
|
495
500
|
checkoutParams.email = input.email;
|
|
496
501
|
const session = await provider.createCheckoutSession(checkoutParams);
|
|
@@ -540,7 +545,10 @@ export class CommerceService {
|
|
|
540
545
|
if (!provider.cancelSubscription) {
|
|
541
546
|
throw new ProviderCapabilityNotSupportedError(provider.provider, "cancelSubscription");
|
|
542
547
|
}
|
|
543
|
-
await provider.cancelSubscription(subscription.providerSubscriptionId, input.operationKey
|
|
548
|
+
await provider.cancelSubscription(subscription.providerSubscriptionId, scopedStableKey(input.operationKey, "cancel-subscription", [
|
|
549
|
+
subscription.provider,
|
|
550
|
+
subscription.providerSubscriptionId,
|
|
551
|
+
]));
|
|
544
552
|
await this.billing.ingestBillingEvent({
|
|
545
553
|
provider: subscription.provider,
|
|
546
554
|
eventId: `cancel_${input.accountId}_${input.operationKey}`,
|
|
@@ -569,7 +577,10 @@ export class CommerceService {
|
|
|
569
577
|
if (!provider.reactivateSubscription) {
|
|
570
578
|
throw new ProviderCapabilityNotSupportedError(provider.provider, "reactivateSubscription");
|
|
571
579
|
}
|
|
572
|
-
await provider.reactivateSubscription(subscription.providerSubscriptionId, input.operationKey
|
|
580
|
+
await provider.reactivateSubscription(subscription.providerSubscriptionId, scopedStableKey(input.operationKey, "reactivate-subscription", [
|
|
581
|
+
subscription.provider,
|
|
582
|
+
subscription.providerSubscriptionId,
|
|
583
|
+
]));
|
|
573
584
|
await this.billing.ingestBillingEvent({
|
|
574
585
|
provider: subscription.provider,
|
|
575
586
|
eventId: `reactivate_${input.accountId}_${input.operationKey}`,
|
|
@@ -655,7 +666,8 @@ export class CommerceService {
|
|
|
655
666
|
interval,
|
|
656
667
|
intervalCount: pending.toOffer.intervalCount,
|
|
657
668
|
effectiveAt: pending.effectiveAt,
|
|
658
|
-
scheduled: pending.effective === "renewal",
|
|
669
|
+
scheduled: pending.effective === "renewal" && pending.errorMessage !== "provider_outcome_unknown",
|
|
670
|
+
providerOutcomeUnknown: pending.errorMessage === "provider_outcome_unknown",
|
|
659
671
|
providerOperationId: pending.providerOperationId ?? null,
|
|
660
672
|
};
|
|
661
673
|
}
|
|
@@ -789,8 +801,37 @@ export class CommerceService {
|
|
|
789
801
|
quoteFingerprint: quoteFingerprint(preview),
|
|
790
802
|
};
|
|
791
803
|
}
|
|
804
|
+
replayPlanChange(change, input) {
|
|
805
|
+
if (change.toOffer.offerKey !== input.offerKey ||
|
|
806
|
+
(change.region ?? null) !== (input.region?.trim().toUpperCase() ?? null) ||
|
|
807
|
+
change.quoteFingerprint !== input.quoteFingerprint) {
|
|
808
|
+
throw new CheckoutConflictError("Plan change operation key was reused with different parameters");
|
|
809
|
+
}
|
|
810
|
+
if (change.state === "failed" || change.state === "canceled") {
|
|
811
|
+
throw new CheckoutConflictError("Plan change operation is terminal; use a new operation key");
|
|
812
|
+
}
|
|
813
|
+
const applied = change.state === "applied";
|
|
814
|
+
const unknown = !applied && change.errorMessage === "provider_outcome_unknown";
|
|
815
|
+
return {
|
|
816
|
+
success: true,
|
|
817
|
+
pending: !applied,
|
|
818
|
+
unchanged: applied,
|
|
819
|
+
scheduled: !applied && change.effective === "renewal" && !unknown,
|
|
820
|
+
providerOutcomeUnknown: unknown,
|
|
821
|
+
effectiveAt: change.effectiveAt ?? undefined,
|
|
822
|
+
planId: change.toOffer.plan,
|
|
823
|
+
interval: change.toOffer.interval,
|
|
824
|
+
intervalCount: change.toOffer.intervalCount,
|
|
825
|
+
};
|
|
826
|
+
}
|
|
792
827
|
async confirmPlanChange(input) {
|
|
793
828
|
requireStableKey(input.operationKey, "operationKey");
|
|
829
|
+
const subscription = await this.billing.getActiveSubscription(input.accountId);
|
|
830
|
+
if (subscription) {
|
|
831
|
+
const prior = await this.billing.findBillingSubscriptionChange(subscription.provider, subscription.providerSubscriptionId, input.operationKey);
|
|
832
|
+
if (prior)
|
|
833
|
+
return this.replayPlanChange(prior, input);
|
|
834
|
+
}
|
|
794
835
|
const context = await this.planChangeContext(input);
|
|
795
836
|
if (context.classification === "unchanged") {
|
|
796
837
|
return {
|
|
@@ -801,6 +842,13 @@ export class CommerceService {
|
|
|
801
842
|
intervalCount: context.targetOffer.billingInterval.count,
|
|
802
843
|
};
|
|
803
844
|
}
|
|
845
|
+
const existing = await this.billing.getOpenBillingSubscriptionChange(context.subscription.provider, context.subscription.providerSubscriptionId);
|
|
846
|
+
if (existing) {
|
|
847
|
+
if (existing.idempotencyKey === input.operationKey) {
|
|
848
|
+
return this.replayPlanChange(existing, input);
|
|
849
|
+
}
|
|
850
|
+
throw new CheckoutConflictError("A plan change is already awaiting reconciliation");
|
|
851
|
+
}
|
|
804
852
|
const preview = await this.refreshPlanPreview(context);
|
|
805
853
|
const fingerprint = quoteFingerprint(preview);
|
|
806
854
|
const refreshed = {
|
|
@@ -819,13 +867,6 @@ export class CommerceService {
|
|
|
819
867
|
if (!context.provider.changePlan) {
|
|
820
868
|
throw new ProviderCapabilityNotSupportedError(context.provider.provider, "changePlan");
|
|
821
869
|
}
|
|
822
|
-
const existing = await this.billing.getOpenBillingSubscriptionChange(context.subscription.provider, context.subscription.providerSubscriptionId);
|
|
823
|
-
if (existing?.state === "scheduled" && existing.effective === "renewal") {
|
|
824
|
-
throw new CheckoutConflictError("Cancel the existing plan change before choosing another plan");
|
|
825
|
-
}
|
|
826
|
-
else if (existing) {
|
|
827
|
-
throw new CheckoutConflictError("A plan change is already awaiting payment");
|
|
828
|
-
}
|
|
829
870
|
const scheduled = context.policy.effective === "renewal";
|
|
830
871
|
const effectiveAt = scheduled ? preview.nextBillingDate : preview.effectiveAt;
|
|
831
872
|
if (!effectiveAt) {
|
|
@@ -838,12 +879,25 @@ export class CommerceService {
|
|
|
838
879
|
effectiveAt,
|
|
839
880
|
effective: context.policy.effective,
|
|
840
881
|
idempotencyKey: input.operationKey,
|
|
882
|
+
quoteFingerprint: input.quoteFingerprint,
|
|
841
883
|
prorationBehavior: context.policy.proration === "none" ? "none" : "invoice_immediately",
|
|
842
884
|
};
|
|
843
885
|
if (input.region !== undefined)
|
|
844
886
|
changeInput.region = input.region;
|
|
845
887
|
const change = await this.billing.createBillingSubscriptionChange(changeInput);
|
|
888
|
+
if (change.created === false)
|
|
889
|
+
return this.replayPlanChange(change, input);
|
|
890
|
+
if (change.created !== true)
|
|
891
|
+
throw new StoreError("Plan change creation did not establish ownership", {
|
|
892
|
+
indeterminate: true,
|
|
893
|
+
});
|
|
894
|
+
const providerChangeKey = scopedStableKey(change.id, "plan-change", [
|
|
895
|
+
context.subscription.provider,
|
|
896
|
+
context.subscription.providerSubscriptionId,
|
|
897
|
+
]);
|
|
846
898
|
let cancellationWasReactivated = false;
|
|
899
|
+
let providerOperationId = null;
|
|
900
|
+
let providerMutationStarted = false;
|
|
847
901
|
try {
|
|
848
902
|
if (context.subscription.cancelAtPeriodEnd) {
|
|
849
903
|
if (!context.provider.reactivateSubscription) {
|
|
@@ -852,9 +906,11 @@ export class CommerceService {
|
|
|
852
906
|
if (!context.provider.cancelSubscription) {
|
|
853
907
|
throw new ProviderCapabilityNotSupportedError(context.provider.provider, "cancelSubscription");
|
|
854
908
|
}
|
|
855
|
-
|
|
909
|
+
providerMutationStarted = true;
|
|
910
|
+
await context.provider.reactivateSubscription(context.subscription.providerSubscriptionId, scopedStableKey(providerChangeKey, "keep"));
|
|
856
911
|
cancellationWasReactivated = true;
|
|
857
912
|
}
|
|
913
|
+
providerMutationStarted = true;
|
|
858
914
|
const result = await context.provider.changePlan({
|
|
859
915
|
providerSubscriptionId: context.subscription.providerSubscriptionId,
|
|
860
916
|
productId: context.targetProductId,
|
|
@@ -865,20 +921,52 @@ export class CommerceService {
|
|
|
865
921
|
plan_slug: context.targetOffer.plan,
|
|
866
922
|
billing_interval: context.targetInterval,
|
|
867
923
|
},
|
|
868
|
-
idempotencyKey:
|
|
869
|
-
});
|
|
870
|
-
await this.billing.updateBillingSubscriptionChange(change.id, {
|
|
871
|
-
providerOperationId: result && "providerOperationId" in result ? (result.providerOperationId ?? null) : null,
|
|
924
|
+
idempotencyKey: providerChangeKey,
|
|
872
925
|
});
|
|
926
|
+
providerOperationId =
|
|
927
|
+
result && "providerOperationId" in result ? (result.providerOperationId ?? null) : null;
|
|
873
928
|
}
|
|
874
929
|
catch (error) {
|
|
875
|
-
|
|
930
|
+
const settled = await this.billing.findBillingSubscriptionChange(context.subscription.provider, context.subscription.providerSubscriptionId, input.operationKey);
|
|
931
|
+
if (settled?.state === "applied")
|
|
932
|
+
return this.replayPlanChange(settled, input);
|
|
933
|
+
if (error instanceof ProviderOperationOutcomeUnknownError) {
|
|
934
|
+
try {
|
|
935
|
+
await this.billing.updateBillingSubscriptionChange(change.id, {
|
|
936
|
+
state: scheduled ? "scheduled" : "awaiting_payment",
|
|
937
|
+
providerOperationId: error.providerOperationId,
|
|
938
|
+
errorMessage: "provider_outcome_unknown",
|
|
939
|
+
});
|
|
940
|
+
}
|
|
941
|
+
catch {
|
|
942
|
+
// A verified webhook may have settled the change concurrently.
|
|
943
|
+
}
|
|
944
|
+
throw error;
|
|
945
|
+
}
|
|
946
|
+
if (providerMutationStarted && !isDefinitiveProviderRejection(error)) {
|
|
947
|
+
// A webhook may already have settled the receipt. Never regress it.
|
|
948
|
+
try {
|
|
949
|
+
await this.billing.updateBillingSubscriptionChange(change.id, {
|
|
950
|
+
state: scheduled ? "scheduled" : "awaiting_payment",
|
|
951
|
+
errorMessage: "provider_outcome_unknown",
|
|
952
|
+
});
|
|
953
|
+
}
|
|
954
|
+
catch {
|
|
955
|
+
// Preserve the provider error; the original receipt remains the fence.
|
|
956
|
+
}
|
|
957
|
+
throw error;
|
|
958
|
+
}
|
|
959
|
+
const failure = error;
|
|
876
960
|
if (cancellationWasReactivated) {
|
|
877
961
|
try {
|
|
878
|
-
await context.provider.cancelSubscription(context.subscription.providerSubscriptionId, scopedStableKey(
|
|
962
|
+
await context.provider.cancelSubscription(context.subscription.providerSubscriptionId, scopedStableKey(providerChangeKey, "restore-cancellation"));
|
|
879
963
|
}
|
|
880
964
|
catch (compensationError) {
|
|
881
|
-
|
|
965
|
+
await this.billing.updateBillingSubscriptionChange(change.id, {
|
|
966
|
+
state: scheduled ? "scheduled" : "awaiting_payment",
|
|
967
|
+
errorMessage: "provider_outcome_unknown",
|
|
968
|
+
});
|
|
969
|
+
throw new AggregateError([error, compensationError], "Plan change failed and the original subscription cancellation could not be restored");
|
|
882
970
|
}
|
|
883
971
|
}
|
|
884
972
|
await this.billing.updateBillingSubscriptionChange(change.id, {
|
|
@@ -887,6 +975,20 @@ export class CommerceService {
|
|
|
887
975
|
});
|
|
888
976
|
throw failure;
|
|
889
977
|
}
|
|
978
|
+
// A local write failure after provider acceptance must leave the change
|
|
979
|
+
// open for webhook reconciliation rather than restoring cancellation.
|
|
980
|
+
try {
|
|
981
|
+
await this.billing.updateBillingSubscriptionChange(change.id, {
|
|
982
|
+
state: scheduled ? "scheduled" : "awaiting_payment",
|
|
983
|
+
providerOperationId,
|
|
984
|
+
});
|
|
985
|
+
}
|
|
986
|
+
catch (error) {
|
|
987
|
+
const settled = await this.billing.findBillingSubscriptionChange(context.subscription.provider, context.subscription.providerSubscriptionId, input.operationKey);
|
|
988
|
+
if (settled?.state === "applied")
|
|
989
|
+
return this.replayPlanChange(settled, input);
|
|
990
|
+
throw error;
|
|
991
|
+
}
|
|
890
992
|
return {
|
|
891
993
|
success: true,
|
|
892
994
|
pending: true,
|
|
@@ -910,7 +1012,7 @@ export class CommerceService {
|
|
|
910
1012
|
if (!provider.cancelScheduledPlanChange) {
|
|
911
1013
|
throw new ProviderCapabilityNotSupportedError(provider.provider, "cancelScheduledPlanChange");
|
|
912
1014
|
}
|
|
913
|
-
await provider.cancelScheduledPlanChange(subscription.providerSubscriptionId, change.providerOperationId, input.operationKey);
|
|
1015
|
+
await provider.cancelScheduledPlanChange(subscription.providerSubscriptionId, change.providerOperationId, scopedStableKey(input.operationKey, "cancel-plan-change", [subscription.provider, change.id]));
|
|
914
1016
|
await this.billing.updateBillingSubscriptionChange(change.id, {
|
|
915
1017
|
state: "canceled",
|
|
916
1018
|
});
|
|
@@ -1024,13 +1126,13 @@ export class CommerceService {
|
|
|
1024
1126
|
limit: 100,
|
|
1025
1127
|
includeRecordOnly: false,
|
|
1026
1128
|
}),
|
|
1027
|
-
this.billing.listBillingInvoices(accountId),
|
|
1129
|
+
this.billing.listBillingInvoices(accountId, { limit: 100 }),
|
|
1028
1130
|
]);
|
|
1029
1131
|
const transactions = transactionsResult.status === "fulfilled"
|
|
1030
1132
|
? transactionsResult.value.items.filter((entry) => entry.entryType !== "usage")
|
|
1031
1133
|
: [];
|
|
1032
1134
|
const usage = usageResult.status === "fulfilled" ? usageResult.value.items : [];
|
|
1033
|
-
const invoices = invoicesResult.status === "fulfilled" ? invoicesResult.value : [];
|
|
1135
|
+
const invoices = invoicesResult.status === "fulfilled" ? invoicesResult.value.items : [];
|
|
1034
1136
|
const documents = [
|
|
1035
1137
|
...invoices.flatMap((invoice) => {
|
|
1036
1138
|
return [
|
|
@@ -1158,15 +1260,18 @@ export class CommerceService {
|
|
|
1158
1260
|
}
|
|
1159
1261
|
async getInvoiceLink(input) {
|
|
1160
1262
|
let providerName;
|
|
1161
|
-
let
|
|
1263
|
+
let providerInvoiceId = null;
|
|
1264
|
+
let payment = null;
|
|
1162
1265
|
if (input.document.kind === "provider_invoice") {
|
|
1163
1266
|
const document = input.document;
|
|
1164
|
-
const owned =
|
|
1165
|
-
invoice.providerInvoiceId === document.providerDocumentId);
|
|
1267
|
+
const owned = await this.billing.hasBillingInvoice(input.accountId, document.provider, document.providerDocumentId);
|
|
1166
1268
|
if (!owned)
|
|
1167
1269
|
throw new CommerceResourceNotFoundError("Invoice not found");
|
|
1168
1270
|
providerName = document.provider;
|
|
1169
|
-
|
|
1271
|
+
providerInvoiceId = document.providerDocumentId;
|
|
1272
|
+
if (document.provider === "dodo") {
|
|
1273
|
+
payment = await this.billing.getOwnedBillingPaymentDocument(input.accountId, document.provider, document.providerDocumentId, document.providerDocumentId, { requireInvoiceLink: true });
|
|
1274
|
+
}
|
|
1170
1275
|
}
|
|
1171
1276
|
else {
|
|
1172
1277
|
const entry = await this.credits.getLedgerEntry(input.accountId, input.document.ledgerEntryId);
|
|
@@ -1176,14 +1281,33 @@ export class CommerceService {
|
|
|
1176
1281
|
if (!owned?.provider || !owned.providerDocumentId) {
|
|
1177
1282
|
throw new CommerceResourceNotFoundError("No provider document is associated with the ledger entry");
|
|
1178
1283
|
}
|
|
1284
|
+
const metadataPaymentId = z
|
|
1285
|
+
.string()
|
|
1286
|
+
.min(1)
|
|
1287
|
+
.safeParse(entry.metadata?.provider_payment_id).data;
|
|
1288
|
+
const paymentId = metadataPaymentId ?? owned.providerDocumentId;
|
|
1289
|
+
const invoiceOwned = await this.billing.hasBillingInvoice(input.accountId, owned.provider, owned.providerDocumentId);
|
|
1290
|
+
if (invoiceOwned && owned.provider === "dodo") {
|
|
1291
|
+
payment = await this.billing.getOwnedBillingPaymentDocument(input.accountId, owned.provider, paymentId, owned.providerDocumentId, { requireInvoiceLink: true });
|
|
1292
|
+
}
|
|
1293
|
+
else if (!invoiceOwned || metadataPaymentId) {
|
|
1294
|
+
payment = await this.billing.getOwnedBillingPaymentDocument(input.accountId, owned.provider, paymentId, owned.providerDocumentId);
|
|
1295
|
+
}
|
|
1296
|
+
if (!invoiceOwned && !payment)
|
|
1297
|
+
throw new CommerceResourceNotFoundError("Invoice not found");
|
|
1179
1298
|
providerName = owned.provider;
|
|
1180
|
-
|
|
1299
|
+
providerInvoiceId = invoiceOwned
|
|
1300
|
+
? owned.providerDocumentId
|
|
1301
|
+
: (payment?.providerInvoiceId ?? null);
|
|
1181
1302
|
}
|
|
1182
1303
|
const provider = await this.providers.get(providerName);
|
|
1183
1304
|
if (!provider.getInvoiceUrl) {
|
|
1184
1305
|
throw new ProviderCapabilityNotSupportedError(provider.provider, "getInvoiceUrl");
|
|
1185
1306
|
}
|
|
1186
|
-
const result = await provider.getInvoiceUrl(
|
|
1307
|
+
const result = await provider.getInvoiceUrl({
|
|
1308
|
+
providerInvoiceId,
|
|
1309
|
+
providerPaymentId: payment?.providerPaymentId ?? null,
|
|
1310
|
+
});
|
|
1187
1311
|
if (!result)
|
|
1188
1312
|
throw new CommerceResourceNotFoundError("No invoice URL is available");
|
|
1189
1313
|
return result;
|