@owlmeans/server-payment 0.1.20-rc.1 → 0.2.0-alpha.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 +52 -54
- package/agent-meta/manifest.json +2 -2
- package/agent-meta/skills/server-payment/SKILL.md +272 -330
- package/agent-meta/skills/server-payment/parts/reference.md +3 -3
- package/build/access/types.d.ts +7 -16
- package/build/access/types.d.ts.map +1 -1
- package/build/access.d.ts.map +1 -1
- package/build/access.js +6 -8
- package/build/access.js.map +1 -1
- package/build/actions/resync.d.ts +3 -3
- package/build/actions/resync.js +5 -5
- package/build/actions/resync.js.map +1 -1
- package/build/actions/webhook.d.ts +4 -1
- package/build/actions/webhook.d.ts.map +1 -1
- package/build/actions/webhook.js +8 -4
- package/build/actions/webhook.js.map +1 -1
- package/build/bootstrap/types.d.ts +13 -7
- package/build/bootstrap/types.d.ts.map +1 -1
- package/build/bootstrap.d.ts +4 -4
- package/build/bootstrap.d.ts.map +1 -1
- package/build/bootstrap.js +19 -10
- package/build/bootstrap.js.map +1 -1
- package/build/catalogue/types.d.ts +3 -3
- package/build/catalogue/types.d.ts.map +1 -1
- package/build/catalogue.d.ts.map +1 -1
- package/build/catalogue.js +4 -8
- package/build/catalogue.js.map +1 -1
- package/build/checkout/types.d.ts +21 -0
- package/build/checkout/types.d.ts.map +1 -0
- package/build/{plugins/stripe → checkout}/types.js.map +1 -1
- package/build/checkout.d.ts +6 -0
- package/build/checkout.d.ts.map +1 -0
- package/build/checkout.js +374 -0
- package/build/checkout.js.map +1 -0
- package/build/config.d.ts +10 -16
- package/build/config.d.ts.map +1 -1
- package/build/config.js +18 -59
- package/build/config.js.map +1 -1
- package/build/consts.d.ts +3 -49
- package/build/consts.d.ts.map +1 -1
- package/build/consts.js +3 -61
- package/build/consts.js.map +1 -1
- package/build/consumer/cancellation/types.d.ts +6 -4
- package/build/consumer/cancellation/types.d.ts.map +1 -1
- package/build/consumer/cancellation.d.ts.map +1 -1
- package/build/consumer/cancellation.js +11 -5
- package/build/consumer/cancellation.js.map +1 -1
- package/build/consumer/capture/types.d.ts +8 -12
- package/build/consumer/capture/types.d.ts.map +1 -1
- package/build/consumer/capture.d.ts.map +1 -1
- package/build/consumer/capture.js +48 -53
- package/build/consumer/capture.js.map +1 -1
- package/build/consumer/index.d.ts +4 -0
- package/build/consumer/index.d.ts.map +1 -1
- package/build/consumer/index.js +2 -0
- package/build/consumer/index.js.map +1 -1
- package/build/consumer/reconcile.d.ts.map +1 -1
- package/build/consumer/reconcile.js +82 -72
- package/build/consumer/reconcile.js.map +1 -1
- package/build/consumer/records/types.d.ts +4 -4
- package/build/consumer/records/types.d.ts.map +1 -1
- package/build/consumer/records.d.ts.map +1 -1
- package/build/consumer/records.js +16 -13
- package/build/consumer/records.js.map +1 -1
- package/build/consumer/service.d.ts +2 -2
- package/build/consumer/service.d.ts.map +1 -1
- package/build/consumer/service.js +28 -21
- package/build/consumer/service.js.map +1 -1
- package/build/consumer/types.d.ts +1 -4
- package/build/consumer/types.d.ts.map +1 -1
- package/build/consumer/types.local.d.ts +1 -9
- package/build/consumer/types.local.d.ts.map +1 -1
- package/build/consumer/withdrawal/types.d.ts +14 -12
- package/build/consumer/withdrawal/types.d.ts.map +1 -1
- package/build/consumer/withdrawal.d.ts.map +1 -1
- package/build/consumer/withdrawal.js +55 -71
- package/build/consumer/withdrawal.js.map +1 -1
- package/build/index.d.ts +12 -6
- package/build/index.d.ts.map +1 -1
- package/build/index.js +7 -4
- package/build/index.js.map +1 -1
- package/build/paygate/providers.d.ts +13 -0
- package/build/paygate/providers.d.ts.map +1 -0
- package/build/paygate/providers.js +38 -0
- package/build/paygate/providers.js.map +1 -0
- package/build/paygate/registry.d.ts +6 -0
- package/build/paygate/registry.d.ts.map +1 -0
- package/build/paygate/registry.js +37 -0
- package/build/paygate/registry.js.map +1 -0
- package/build/paygate/types.d.ts +577 -0
- package/build/paygate/types.d.ts.map +1 -0
- package/build/{plugins/session → paygate}/types.js.map +1 -1
- package/build/plan/types.d.ts +6 -1
- package/build/plan/types.d.ts.map +1 -1
- package/build/plan.d.ts.map +1 -1
- package/build/plan.js +16 -2
- package/build/plan.js.map +1 -1
- package/build/plugins/checkout-policy/types.d.ts +5 -3
- package/build/plugins/checkout-policy/types.d.ts.map +1 -1
- package/build/plugins/checkout-policy.d.ts.map +1 -1
- package/build/plugins/checkout-policy.js +5 -3
- package/build/plugins/checkout-policy.js.map +1 -1
- package/build/plugins/consts.local.d.ts +8 -10
- package/build/plugins/consts.local.d.ts.map +1 -1
- package/build/plugins/consts.local.js +2 -19
- package/build/plugins/consts.local.js.map +1 -1
- package/build/plugins/customer-lock/types.d.ts +2 -2
- package/build/plugins/customer-lock/types.d.ts.map +1 -1
- package/build/plugins/customer-lock.js +2 -2
- package/build/plugins/customer-lock.js.map +1 -1
- package/build/plugins/estimate/cache.js +1 -1
- package/build/plugins/estimate/cache.js.map +1 -1
- package/build/plugins/estimate/types.d.ts +10 -10
- package/build/plugins/estimate/types.d.ts.map +1 -1
- package/build/plugins/estimate.d.ts.map +1 -1
- package/build/plugins/estimate.js +48 -122
- package/build/plugins/estimate.js.map +1 -1
- package/build/plugins/events/types.d.ts +17 -10
- package/build/plugins/events/types.d.ts.map +1 -1
- package/build/plugins/events.d.ts +6 -5
- package/build/plugins/events.d.ts.map +1 -1
- package/build/plugins/events.js +162 -173
- package/build/plugins/events.js.map +1 -1
- package/build/plugins/fx/types.d.ts +10 -21
- package/build/plugins/fx/types.d.ts.map +1 -1
- package/build/plugins/fx.d.ts +1 -1
- package/build/plugins/fx.d.ts.map +1 -1
- package/build/plugins/fx.js +17 -54
- package/build/plugins/fx.js.map +1 -1
- package/build/plugins/portal/types.d.ts +8 -37
- package/build/plugins/portal/types.d.ts.map +1 -1
- package/build/plugins/portal.d.ts.map +1 -1
- package/build/plugins/portal.js +38 -208
- package/build/plugins/portal.js.map +1 -1
- package/build/plugins/refunds/types.d.ts +11 -11
- package/build/plugins/refunds/types.d.ts.map +1 -1
- package/build/plugins/refunds.d.ts.map +1 -1
- package/build/plugins/refunds.js +14 -39
- package/build/plugins/refunds.js.map +1 -1
- package/build/plugins/snapshot/types.d.ts +11 -0
- package/build/plugins/snapshot/types.d.ts.map +1 -0
- package/build/plugins/snapshot/types.js +2 -0
- package/build/plugins/snapshot/types.js.map +1 -0
- package/build/plugins/snapshot.d.ts +4 -0
- package/build/plugins/snapshot.d.ts.map +1 -0
- package/build/plugins/snapshot.js +19 -0
- package/build/plugins/snapshot.js.map +1 -0
- package/build/plugins/subscriptions/types.d.ts +17 -17
- package/build/plugins/subscriptions/types.d.ts.map +1 -1
- package/build/plugins/subscriptions.d.ts +4 -4
- package/build/plugins/subscriptions.d.ts.map +1 -1
- package/build/plugins/subscriptions.js +69 -77
- package/build/plugins/subscriptions.js.map +1 -1
- package/build/plugins/types.local.d.ts +9 -43
- package/build/plugins/types.local.d.ts.map +1 -1
- package/build/plugins/webhook-manager/types.d.ts +15 -43
- package/build/plugins/webhook-manager/types.d.ts.map +1 -1
- package/build/plugins/webhook-manager.d.ts +3 -3
- package/build/plugins/webhook-manager.d.ts.map +1 -1
- package/build/plugins/webhook-manager.js +21 -199
- package/build/plugins/webhook-manager.js.map +1 -1
- package/build/protocol.d.ts +1 -1
- package/build/protocol.d.ts.map +1 -1
- package/build/protocol.js +1 -1
- package/build/protocol.js.map +1 -1
- package/build/service.d.ts +11 -11
- package/build/service.d.ts.map +1 -1
- package/build/service.js +59 -23
- package/build/service.js.map +1 -1
- package/build/sync/types.d.ts +17 -13
- package/build/sync/types.d.ts.map +1 -1
- package/build/sync.d.ts.map +1 -1
- package/build/sync.js +22 -122
- package/build/sync.js.map +1 -1
- package/build/types.d.ts +46 -101
- package/build/types.d.ts.map +1 -1
- package/build/utils/types.d.ts +0 -8
- package/build/utils/types.d.ts.map +1 -1
- package/build/utils.d.ts +1 -4
- package/build/utils.d.ts.map +1 -1
- package/build/utils.js +1 -15
- package/build/utils.js.map +1 -1
- package/package.json +20 -22
- package/src/access/types.ts +7 -16
- package/src/access.ts +7 -15
- package/src/actions/resync.ts +5 -5
- package/src/actions/webhook.ts +8 -4
- package/src/bootstrap/types.ts +13 -7
- package/src/bootstrap.ts +23 -13
- package/src/catalogue/types.ts +3 -3
- package/src/catalogue.ts +4 -8
- package/src/checkout/types.ts +21 -0
- package/src/checkout.ts +414 -0
- package/src/config.ts +19 -66
- package/src/consts.ts +3 -69
- package/src/consumer/cancellation/types.ts +6 -4
- package/src/consumer/cancellation.ts +13 -7
- package/src/consumer/capture/types.ts +8 -12
- package/src/consumer/capture.ts +50 -59
- package/src/consumer/index.ts +4 -0
- package/src/consumer/reconcile.ts +69 -68
- package/src/consumer/records/types.ts +4 -4
- package/src/consumer/records.ts +19 -14
- package/src/consumer/service.ts +35 -22
- package/src/consumer/types.local.ts +1 -8
- package/src/consumer/types.ts +1 -4
- package/src/consumer/withdrawal/types.ts +14 -12
- package/src/consumer/withdrawal.ts +61 -77
- package/src/index.ts +12 -6
- package/src/paygate/providers.ts +48 -0
- package/src/paygate/registry.ts +53 -0
- package/src/paygate/types.ts +640 -0
- package/src/plan/types.ts +6 -1
- package/src/plan.ts +21 -3
- package/src/plugins/checkout-policy/types.ts +5 -3
- package/src/plugins/checkout-policy.ts +6 -5
- package/src/plugins/consts.local.ts +2 -26
- package/src/plugins/customer-lock/types.ts +2 -2
- package/src/plugins/customer-lock.ts +2 -2
- package/src/plugins/estimate/cache.ts +1 -1
- package/src/plugins/estimate/types.ts +10 -10
- package/src/plugins/estimate.ts +52 -128
- package/src/plugins/events/types.ts +17 -9
- package/src/plugins/events.ts +172 -190
- package/src/plugins/fx/types.ts +10 -23
- package/src/plugins/fx.ts +28 -64
- package/src/plugins/portal/types.ts +8 -38
- package/src/plugins/portal.ts +48 -229
- package/src/plugins/refunds/types.ts +11 -11
- package/src/plugins/refunds.ts +16 -40
- package/src/plugins/snapshot/types.ts +11 -0
- package/src/plugins/snapshot.ts +22 -0
- package/src/plugins/subscriptions/types.ts +17 -17
- package/src/plugins/subscriptions.ts +81 -81
- package/src/plugins/types.local.ts +9 -33
- package/src/plugins/webhook-manager/types.ts +15 -43
- package/src/plugins/webhook-manager.ts +24 -220
- package/src/protocol.ts +1 -1
- package/src/service.ts +66 -30
- package/src/sync/types.ts +17 -13
- package/src/sync.ts +25 -143
- package/src/types.ts +47 -97
- package/src/utils/types.ts +0 -9
- package/src/utils.ts +2 -20
- package/tests/capabilities.spec.ts +219 -0
- package/tests/entitlement.spec.ts +4 -4
- package/tests/gate-handlers.spec.ts +6 -6
- package/tests/gate.spec.ts +2 -2
- package/tests/grants.spec.ts +2 -2
- package/tests/internal-plan.spec.ts +2 -2
- package/tests/limit-gate.spec.ts +2 -2
- package/tests/paygates.spec.ts +132 -0
- package/tests/protocol.spec.ts +4 -5
- package/tests/usage.spec.ts +2 -2
- package/tests/webhook.spec.ts +137 -0
- package/build/plugins/session/types.d.ts +0 -34
- package/build/plugins/session/types.d.ts.map +0 -1
- package/build/plugins/session.d.ts +0 -4
- package/build/plugins/session.d.ts.map +0 -1
- package/build/plugins/session.js +0 -71
- package/build/plugins/session.js.map +0 -1
- package/build/plugins/status.d.ts +0 -8
- package/build/plugins/status.d.ts.map +0 -1
- package/build/plugins/status.js +0 -21
- package/build/plugins/status.js.map +0 -1
- package/build/plugins/stripe/types.d.ts +0 -20
- package/build/plugins/stripe/types.d.ts.map +0 -1
- package/build/plugins/stripe.d.ts +0 -6
- package/build/plugins/stripe.d.ts.map +0 -1
- package/build/plugins/stripe.js +0 -518
- package/build/plugins/stripe.js.map +0 -1
- package/src/plugins/session/types.ts +0 -30
- package/src/plugins/session.ts +0 -86
- package/src/plugins/status.ts +0 -22
- package/src/plugins/stripe/types.ts +0 -20
- package/src/plugins/stripe.ts +0 -571
- package/tests/checkout-consumer.spec.ts +0 -366
- package/tests/checkout-locks.spec.ts +0 -161
- package/tests/checkout-plugins.spec.ts +0 -164
- package/tests/checkout.spec.ts +0 -313
- package/tests/consumer-events.spec.ts +0 -215
- package/tests/consumer-fixtures.ts +0 -141
- package/tests/consumer-ops.spec.ts +0 -361
- package/tests/consumer-rights.integration.spec.ts +0 -147
- package/tests/consumer-rights.spec.ts +0 -606
- package/tests/context.ts +0 -110
- package/tests/estimate.spec.ts +0 -350
- package/tests/events.spec.ts +0 -367
- package/tests/fake-stripe.ts +0 -1004
- package/tests/gateway-options.spec.ts +0 -180
- package/tests/portal.spec.ts +0 -202
- package/tests/sync.spec.ts +0 -114
- package/tests/usage.integration.spec.ts +0 -165
- package/tests/webhook-manager.spec.ts +0 -149
- /package/build/{plugins/session → checkout}/types.js +0 -0
- /package/build/{plugins/stripe → paygate}/types.js +0 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/plugins/events/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/plugins/events/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAA;AAEnE,wFAAwF;AACxF,MAAM,WAAW,mBAAmB;IAClC;;;;;;;;;;;;OAYG;IACH,KAAK,EAAE,CAAC,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,YAAY,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;CAChE"}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import type Stripe from 'stripe';
|
|
2
1
|
import type { Context as ApiContext } from '@owlmeans/server-api';
|
|
3
|
-
import type {
|
|
2
|
+
import type { PaymentEventsHelper } from './events/types.js';
|
|
4
3
|
/**
|
|
5
|
-
* The
|
|
6
|
-
*
|
|
4
|
+
* The event dispatch of a context: one paygate event applied to the stores and told to the
|
|
5
|
+
* observers. A throw escapes so the paygate delivers the event again.
|
|
7
6
|
*/
|
|
8
|
-
export declare const
|
|
7
|
+
export declare const makePaymentEventsHelper: (ctx: ApiContext) => PaymentEventsHelper;
|
|
8
|
+
/** The paygate event dispatch of a context — one per context. */
|
|
9
|
+
export declare const paymentEventsOf: (target: ApiContext<import("@owlmeans/server-api").Config>) => PaymentEventsHelper;
|
|
9
10
|
//# sourceMappingURL=events.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"events.d.ts","sourceRoot":"","sources":["../../src/plugins/events.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"events.d.ts","sourceRoot":"","sources":["../../src/plugins/events.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,IAAI,UAAU,EAAE,MAAM,sBAAsB,CAAA;AAcjE,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAA;AAkB5D;;;GAGG;AACH,eAAO,MAAM,uBAAuB,QAAS,UAAU,KAAG,mBA2czD,CAAA;AAED,iEAAiE;AACjE,eAAO,MAAM,eAAe,oFAA8C,CAAA"}
|
package/build/plugins/events.js
CHANGED
|
@@ -1,18 +1,16 @@
|
|
|
1
1
|
import { CheckoutPricingMode, ENTITLING_STATUSES, PaygateError, SubscriptionStatus } from '@owlmeans/payment';
|
|
2
|
-
import {
|
|
2
|
+
import { memoHelper } from '@owlmeans/context';
|
|
3
|
+
import { GATEWAY_SERVICE } from '../consts.js';
|
|
3
4
|
import { log } from '../log.js';
|
|
4
|
-
import { DISPUTE_PHASES } from './consts.local.js';
|
|
5
5
|
import { paymentAccessOf } from '../access.js';
|
|
6
6
|
import { paymentUtils } from '../utils.js';
|
|
7
7
|
import { checkoutPluginsOf } from './checkout-plugins.js';
|
|
8
8
|
import { paymentTargetOf } from './refunds.js';
|
|
9
9
|
import { customerLockHelper } from './customer-lock.js';
|
|
10
|
-
import {
|
|
10
|
+
import { paygateSubscriptionsOf } from './subscriptions.js';
|
|
11
|
+
import { checkoutSnapshotHelper } from './snapshot.js';
|
|
11
12
|
import { consumerRecordsOf } from '../consumer/records.js';
|
|
12
13
|
import { captureOf } from '../consumer/capture.js';
|
|
13
|
-
import { stripeSessionHelper } from './session.js';
|
|
14
|
-
/** `invoice.subscription`, top-level on the pinned API version. */
|
|
15
|
-
const subscriptionIdOfInvoice = (invoice) => paymentUtils.idOf(invoice.subscription);
|
|
16
14
|
const integerMetadata = (metadata, key) => {
|
|
17
15
|
const raw = metadata[key];
|
|
18
16
|
const value = raw == null || raw.trim() === '' ? Number.NaN : Number(raw);
|
|
@@ -21,30 +19,31 @@ const integerMetadata = (metadata, key) => {
|
|
|
21
19
|
return value;
|
|
22
20
|
};
|
|
23
21
|
/**
|
|
24
|
-
* The
|
|
25
|
-
*
|
|
22
|
+
* The event dispatch of a context: one paygate event applied to the stores and told to the
|
|
23
|
+
* observers. A throw escapes so the paygate delivers the event again.
|
|
26
24
|
*/
|
|
27
|
-
export const
|
|
25
|
+
export const makePaymentEventsHelper = (ctx) => {
|
|
28
26
|
const access = paymentAccessOf(ctx);
|
|
29
|
-
const
|
|
27
|
+
const subscriptions = paygateSubscriptionsOf(ctx);
|
|
28
|
+
const lock = customerLockHelper.withCustomerLock;
|
|
30
29
|
/** The registered checkout plugins — none in a context without a gateway service. */
|
|
31
30
|
const registeredPlugins = () => ctx.hasService?.(GATEWAY_SERVICE) === true
|
|
32
31
|
? access.gateway().checkoutPlugins?.() ?? [] : [];
|
|
33
|
-
const upsertCustomer = async (customer) => {
|
|
34
|
-
await
|
|
32
|
+
const upsertCustomer = async (paygate, customer) => {
|
|
33
|
+
await lock(paygate.id, customer.id, async () => {
|
|
35
34
|
const resource = access.paygateCustomers();
|
|
36
|
-
const existing = await resource.loadByPgId(customer.id,
|
|
35
|
+
const existing = await resource.loadByPgId(customer.id, paygate.id);
|
|
37
36
|
const data = paymentUtils.compact({
|
|
38
37
|
email: customer.email ?? undefined, name: customer.name ?? undefined,
|
|
39
|
-
taxId: customer.
|
|
40
|
-
country: customer.
|
|
38
|
+
taxId: customer.taxId ?? undefined,
|
|
39
|
+
country: customer.country?.toUpperCase() ?? undefined,
|
|
41
40
|
currency: customer.currency?.toLowerCase() ?? undefined,
|
|
42
41
|
entityId: customer.metadata?.entityId ?? existing?.entityId ?? undefined,
|
|
43
42
|
profileId: customer.metadata?.profileId ?? existing?.profileId ?? undefined,
|
|
44
43
|
});
|
|
45
44
|
if (existing == null) {
|
|
46
45
|
try {
|
|
47
|
-
await resource.create({ paygate:
|
|
46
|
+
await resource.create({ paygate: paygate.id, externalId: customer.id, ...data });
|
|
48
47
|
}
|
|
49
48
|
catch (error) {
|
|
50
49
|
if (!paymentUtils.isDuplicateKey(error))
|
|
@@ -57,9 +56,9 @@ export const createEventHandler = (ctx, stripe) => {
|
|
|
57
56
|
}
|
|
58
57
|
});
|
|
59
58
|
};
|
|
60
|
-
const markCustomerDeleted = async (
|
|
59
|
+
const markCustomerDeleted = async (paygate, customerId) => {
|
|
61
60
|
const resource = access.paygateCustomers();
|
|
62
|
-
const existing = await resource.loadByPgId(
|
|
61
|
+
const existing = await resource.loadByPgId(customerId, paygate.id);
|
|
63
62
|
if (existing != null && existing.deletedAt == null) {
|
|
64
63
|
await resource.update({ ...existing, deletedAt: new Date() });
|
|
65
64
|
}
|
|
@@ -75,26 +74,26 @@ export const createEventHandler = (ctx, stripe) => {
|
|
|
75
74
|
amountMinor: Number.isSafeInteger(amount) && amount > 0 ? amount : undefined, at: new Date(),
|
|
76
75
|
}));
|
|
77
76
|
};
|
|
78
|
-
const fulfillPayment = async (session) => {
|
|
77
|
+
const fulfillPayment = async (paygate, session) => {
|
|
79
78
|
if (session.mode === 'subscription') {
|
|
80
|
-
await completeSubscriptionCheckout(session);
|
|
79
|
+
await completeSubscriptionCheckout(paygate, session);
|
|
81
80
|
return;
|
|
82
81
|
}
|
|
83
|
-
if (!
|
|
82
|
+
if (!checkoutSnapshotHelper.canFulfill(session))
|
|
84
83
|
return;
|
|
85
84
|
const metadata = session.metadata ?? {};
|
|
86
85
|
if (metadata.entityId == null || metadata.service == null || metadata.productSku == null) {
|
|
87
86
|
throw new PaygateError('metadata:owner');
|
|
88
87
|
}
|
|
89
|
-
await
|
|
88
|
+
await lock(paygate.id, session.customerId, async () => {
|
|
90
89
|
const ledger = access.fulfillments();
|
|
91
|
-
let stored = await ledger.byExternalId(session.id,
|
|
90
|
+
let stored = await ledger.byExternalId(session.id, paygate.id);
|
|
92
91
|
if (stored?.fulfilledAt != null)
|
|
93
92
|
return;
|
|
94
93
|
const amountMode = metadata.pricingMode === CheckoutPricingMode.Amount;
|
|
95
94
|
const base = {
|
|
96
95
|
entityId: metadata.entityId, productSku: metadata.productSku, planSku: metadata.planSku,
|
|
97
|
-
service: metadata.service, paygate:
|
|
96
|
+
service: metadata.service, paygate: paygate.id, externalId: session.id,
|
|
98
97
|
};
|
|
99
98
|
let completion;
|
|
100
99
|
let record;
|
|
@@ -106,7 +105,7 @@ export const createEventHandler = (ctx, stripe) => {
|
|
|
106
105
|
? chargedMinor : integerMetadata(metadata, 'sourceChargeAmountMinor');
|
|
107
106
|
const amountCurrency = (metadata.amountCurrency ?? currency)?.toLowerCase();
|
|
108
107
|
if (currency == null || session.currency?.toLowerCase() !== currency
|
|
109
|
-
|| amountCurrency == null || session.
|
|
108
|
+
|| amountCurrency == null || session.amountSubtotalMinor !== chargedMinor
|
|
110
109
|
|| amountMinor <= 0 || chargedMinor <= 0 || sourceChargedMinor < amountMinor
|
|
111
110
|
|| (amountCurrency === currency && sourceChargedMinor !== chargedMinor)) {
|
|
112
111
|
throw new PaygateError('amount-mismatch');
|
|
@@ -124,8 +123,7 @@ export const createEventHandler = (ctx, stripe) => {
|
|
|
124
123
|
if (metadata.currency != null && session.currency?.toLowerCase() !== metadata.currency.toLowerCase()) {
|
|
125
124
|
throw new PaygateError('currency-mismatch');
|
|
126
125
|
}
|
|
127
|
-
const
|
|
128
|
-
const units = lineItems.data.reduce((sum, line) => sum + (line.quantity ?? 0), 0);
|
|
126
|
+
const units = await paygate.checkout.units(session.id);
|
|
129
127
|
if (units <= 0)
|
|
130
128
|
throw new PaygateError('units');
|
|
131
129
|
record = { mode: CheckoutPricingMode.Quantity, units, ...(session.currency != null ? { currency: session.currency } : {}) };
|
|
@@ -133,18 +131,16 @@ export const createEventHandler = (ctx, stripe) => {
|
|
|
133
131
|
}
|
|
134
132
|
// The purchase — the withdrawal window and the contract — exists BEFORE the credits do; the
|
|
135
133
|
// billing country is locked with it.
|
|
136
|
-
const captured = await captureOf(ctx).capturePaymentPurchase(
|
|
134
|
+
const captured = await captureOf(ctx).capturePaymentPurchase(paygate, session, paymentUtils.compact({
|
|
137
135
|
netAmountMinor: completion.mode === 'amount' ? completion.amountMinor : undefined,
|
|
138
136
|
amountCurrency: completion.mode === 'amount' ? completion.amountCurrency : undefined,
|
|
139
137
|
units: completion.mode === 'quantity' ? completion.units : undefined,
|
|
140
138
|
}));
|
|
141
139
|
const evidence = captureOf(ctx).sessionEvidenceOf(session);
|
|
142
|
-
const identifiers = paymentUtils.compact({
|
|
143
|
-
paymentIntentId: paymentUtils.idOf(session.payment_intent), invoiceId: paymentUtils.idOf(session.invoice),
|
|
144
|
-
});
|
|
140
|
+
const identifiers = paymentUtils.compact({ paymentIntentId: session.paymentId, invoiceId: session.invoiceId });
|
|
145
141
|
const evidenceFields = paymentUtils.compact({
|
|
146
142
|
country: evidence.country, email: evidence.email, profileId: metadata.profileId,
|
|
147
|
-
amountTotalMinor: session.
|
|
143
|
+
amountTotalMinor: session.amountTotalMinor ?? undefined, amountTaxMinor: session.amountTaxMinor ?? undefined,
|
|
148
144
|
termsAccepted: evidence.termsAccepted, purchaseId: captured?.purchase.purchaseId,
|
|
149
145
|
});
|
|
150
146
|
if (stored == null) {
|
|
@@ -154,7 +150,7 @@ export const createEventHandler = (ctx, stripe) => {
|
|
|
154
150
|
catch (error) {
|
|
155
151
|
if (!paymentUtils.isDuplicateKey(error))
|
|
156
152
|
throw error;
|
|
157
|
-
stored = await ledger.byExternalId(session.id,
|
|
153
|
+
stored = await ledger.byExternalId(session.id, paygate.id);
|
|
158
154
|
if (stored == null || stored.fulfilledAt != null)
|
|
159
155
|
return;
|
|
160
156
|
}
|
|
@@ -178,42 +174,42 @@ export const createEventHandler = (ctx, stripe) => {
|
|
|
178
174
|
* refined with the buyer's own country and totals, the billing country is locked, the evidence is
|
|
179
175
|
* stored on the subscription row and the confirmation mailed.
|
|
180
176
|
*/
|
|
181
|
-
const completeSubscriptionCheckout = async (session) => {
|
|
182
|
-
if (session.
|
|
177
|
+
const completeSubscriptionCheckout = async (paygate, session) => {
|
|
178
|
+
if (session.payment !== 'paid' && session.payment !== 'not-required')
|
|
183
179
|
return;
|
|
184
|
-
const subscriptionId =
|
|
180
|
+
const subscriptionId = session.subscriptionId;
|
|
185
181
|
if (subscriptionId == null)
|
|
186
182
|
return;
|
|
187
|
-
await
|
|
188
|
-
let row = await access.subscriptions().byExternalId(subscriptionId,
|
|
183
|
+
await lock(paygate.id, session.customerId, async () => {
|
|
184
|
+
let row = await access.subscriptions().byExternalId(subscriptionId, paygate.id);
|
|
189
185
|
let subscription = null;
|
|
190
186
|
if (row?.propagated == null) {
|
|
191
|
-
subscription = await
|
|
187
|
+
subscription = await subscriptions.retrieveSubscription(paygate, subscriptionId);
|
|
192
188
|
if (subscription != null) {
|
|
193
|
-
row = (await
|
|
189
|
+
row = (await subscriptions.applySubscription(paygate, subscription, { source: 'webhook' })).record ?? row;
|
|
194
190
|
}
|
|
195
191
|
}
|
|
196
192
|
if (row == null) {
|
|
197
193
|
log.warn('Completed checkout names an unknown subscription', { sessionId: session.id, subscriptionId });
|
|
198
194
|
return;
|
|
199
195
|
}
|
|
200
|
-
let purchase = await access.purchases().byPurchaseId(consumerRecordsOf(ctx).purchaseIdOf(subscriptionId));
|
|
196
|
+
let purchase = await access.purchases().byPurchaseId(consumerRecordsOf(ctx).purchaseIdOf(paygate.id, subscriptionId));
|
|
201
197
|
if (purchase == null && ENTITLING_STATUSES.includes(row.status)) {
|
|
202
|
-
subscription = subscription ?? await
|
|
198
|
+
subscription = subscription ?? await subscriptions.retrieveSubscription(paygate, subscriptionId);
|
|
203
199
|
purchase = subscription != null
|
|
204
|
-
? (await captureOf(ctx).captureSubscriptionPurchase(
|
|
200
|
+
? (await captureOf(ctx).captureSubscriptionPurchase(paygate, subscription, row))?.purchase ?? null : null;
|
|
205
201
|
}
|
|
206
202
|
if (purchase != null) {
|
|
207
|
-
purchase = await captureOf(ctx).completeSubscriptionPurchase(
|
|
203
|
+
purchase = await captureOf(ctx).completeSubscriptionPurchase(paygate, session, purchase);
|
|
208
204
|
}
|
|
209
205
|
const evidence = captureOf(ctx).sessionEvidenceOf(session);
|
|
210
206
|
const metadata = session.metadata ?? {};
|
|
211
|
-
const current = await access.subscriptions().byExternalId(subscriptionId,
|
|
207
|
+
const current = await access.subscriptions().byExternalId(subscriptionId, paygate.id) ?? row;
|
|
212
208
|
await access.subscriptions().update(paymentUtils.compact({
|
|
213
209
|
...current,
|
|
214
210
|
checkoutSessionId: session.id,
|
|
215
211
|
purchaseId: purchase?.purchaseId ?? current.purchaseId,
|
|
216
|
-
firstInvoiceId: current.firstInvoiceId ??
|
|
212
|
+
firstInvoiceId: current.firstInvoiceId ?? session.invoiceId,
|
|
217
213
|
currency: current.currency ?? (evidence.currency !== '' ? evidence.currency : undefined),
|
|
218
214
|
country: evidence.country ?? current.country,
|
|
219
215
|
email: evidence.email ?? current.email,
|
|
@@ -230,25 +226,25 @@ export const createEventHandler = (ctx, stripe) => {
|
|
|
230
226
|
});
|
|
231
227
|
await settle(session, 'paid');
|
|
232
228
|
};
|
|
233
|
-
const checkoutFailed = async (session) => {
|
|
229
|
+
const checkoutFailed = async (paygate, session) => {
|
|
234
230
|
const metadata = session.metadata ?? {};
|
|
235
|
-
const customerId =
|
|
231
|
+
const customerId = session.customerId;
|
|
236
232
|
const entityId = metadata.entityId
|
|
237
|
-
?? (customerId != null ? (await access.paygateCustomers().loadByPgId(customerId,
|
|
233
|
+
?? (customerId != null ? (await access.paygateCustomers().loadByPgId(customerId, paygate.id))?.entityId : undefined);
|
|
238
234
|
if (entityId == null) {
|
|
239
235
|
log.warn('Failed checkout belongs to no known entity', { sessionId: session.id });
|
|
240
236
|
return;
|
|
241
237
|
}
|
|
242
238
|
if (session.mode === 'payment' && metadata.productSku != null && metadata.service != null) {
|
|
243
239
|
const ledger = access.fulfillments();
|
|
244
|
-
const stored = await ledger.byExternalId(session.id,
|
|
240
|
+
const stored = await ledger.byExternalId(session.id, paygate.id);
|
|
245
241
|
if (stored == null) {
|
|
246
242
|
try {
|
|
247
243
|
await ledger.create(paymentUtils.compact({
|
|
248
244
|
entityId, productSku: metadata.productSku, planSku: metadata.planSku, service: metadata.service,
|
|
249
|
-
paygate:
|
|
245
|
+
paygate: paygate.id, externalId: session.id,
|
|
250
246
|
mode: metadata.pricingMode === CheckoutPricingMode.Amount ? CheckoutPricingMode.Amount : CheckoutPricingMode.Quantity,
|
|
251
|
-
paymentIntentId:
|
|
247
|
+
paymentIntentId: session.paymentId, createdAt: new Date(), failedAt: new Date(),
|
|
252
248
|
}));
|
|
253
249
|
}
|
|
254
250
|
catch (error) {
|
|
@@ -262,82 +258,97 @@ export const createEventHandler = (ctx, stripe) => {
|
|
|
262
258
|
}
|
|
263
259
|
await access.observer().propagatePaymentFailed(paymentUtils.compact({
|
|
264
260
|
entityId, kind: 'checkout', externalId: session.id,
|
|
265
|
-
subscriptionId:
|
|
261
|
+
subscriptionId: session.subscriptionId, eventKey: `payment-failed:${session.id}:0`,
|
|
266
262
|
}), ctx);
|
|
267
263
|
log.info('Payment failed', paymentUtils.compact({
|
|
268
|
-
entityId, kind: 'checkout', sessionId: session.id, subscriptionId:
|
|
264
|
+
entityId, kind: 'checkout', sessionId: session.id, subscriptionId: session.subscriptionId,
|
|
269
265
|
}), { event: 'payment.failed' });
|
|
270
266
|
await settle(session, 'failed', entityId);
|
|
271
267
|
};
|
|
272
|
-
const purgeExpiredCheckout = async (session) => {
|
|
273
|
-
await access.fulfillments().purge({ paygate:
|
|
268
|
+
const purgeExpiredCheckout = async (paygate, session) => {
|
|
269
|
+
await access.fulfillments().purge({ paygate: paygate.id, externalId: session.id, fulfilledAt: null });
|
|
274
270
|
await settle(session, 'expired');
|
|
275
271
|
};
|
|
276
|
-
const
|
|
277
|
-
const subscription = event.
|
|
278
|
-
await
|
|
279
|
-
await
|
|
280
|
-
source: 'webhook', eventId: event.
|
|
272
|
+
const subscriptionChanged = async (paygate, event) => {
|
|
273
|
+
const subscription = event.subscription;
|
|
274
|
+
await lock(paygate.id, subscription.customerId, async () => {
|
|
275
|
+
await subscriptions.applySubscription(paygate, subscription, {
|
|
276
|
+
source: 'webhook', eventId: event.eventId, eventCreated: event.createdAt,
|
|
277
|
+
...(event.deleted ? { forced: SubscriptionStatus.Canceled } : {}),
|
|
278
|
+
...(event.trialEnding ? { trialEnding: true } : {}),
|
|
281
279
|
});
|
|
282
280
|
});
|
|
283
281
|
};
|
|
284
|
-
const reapplyInvoiceSubscription = async (
|
|
285
|
-
const subscriptionId =
|
|
286
|
-
if (subscriptionId == null)
|
|
282
|
+
const reapplyInvoiceSubscription = async (paygate, eventId, invoice, opts = {}) => {
|
|
283
|
+
const subscriptionId = invoice.subscriptionId;
|
|
284
|
+
if (subscriptionId == null || paygate.subscriptions == null)
|
|
287
285
|
return null;
|
|
288
|
-
const subscription = await
|
|
286
|
+
const subscription = await subscriptions.retrieveSubscription(paygate, subscriptionId);
|
|
289
287
|
if (subscription == null) {
|
|
290
|
-
return await
|
|
288
|
+
return await lock(paygate.id, invoice.customerId, async () => await subscriptions.cancelMissing(paygate, subscriptionId));
|
|
291
289
|
}
|
|
292
|
-
return await
|
|
290
|
+
return await lock(paygate.id, subscription.customerId, async () => await subscriptions.applySubscription(paygate, subscription, { source: 'webhook', eventId, ...opts }));
|
|
293
291
|
};
|
|
294
|
-
const
|
|
295
|
-
const
|
|
296
|
-
if (invoice.billing_reason === 'subscription_cycle') {
|
|
297
|
-
await reapplyInvoiceSubscription(event, invoice, { renewal: true, invoiceId: invoice.id });
|
|
298
|
-
return;
|
|
299
|
-
}
|
|
300
|
-
await refreshLatestInvoice(invoice, invoice.id);
|
|
301
|
-
};
|
|
302
|
-
const refreshLatestInvoice = async (invoice, latestInvoiceId) => {
|
|
303
|
-
const subscriptionId = subscriptionIdOfInvoice(invoice);
|
|
292
|
+
const refreshLatestInvoice = async (paygate, invoice, latestInvoiceId) => {
|
|
293
|
+
const subscriptionId = invoice.subscriptionId;
|
|
304
294
|
if (subscriptionId == null)
|
|
305
295
|
return;
|
|
306
|
-
await
|
|
307
|
-
const row = await access.subscriptions().byExternalId(subscriptionId,
|
|
296
|
+
await lock(paygate.id, invoice.customerId, async () => {
|
|
297
|
+
const row = await access.subscriptions().byExternalId(subscriptionId, paygate.id);
|
|
308
298
|
if (row == null)
|
|
309
299
|
return;
|
|
310
300
|
let latest = latestInvoiceId;
|
|
311
301
|
if (latest == null) {
|
|
312
|
-
const subscription = await
|
|
313
|
-
latest =
|
|
302
|
+
const subscription = await subscriptions.retrieveSubscription(paygate, subscriptionId);
|
|
303
|
+
latest = subscription?.latestInvoiceId;
|
|
314
304
|
}
|
|
315
305
|
if (latest != null && row.latestInvoiceId !== latest) {
|
|
316
306
|
await access.subscriptions().update({ ...row, latestInvoiceId: latest, updatedAt: new Date() });
|
|
317
307
|
}
|
|
318
308
|
});
|
|
319
309
|
};
|
|
320
|
-
const invoiceFailed = async (event) => {
|
|
321
|
-
const invoice = event.
|
|
322
|
-
const result = await reapplyInvoiceSubscription(event, invoice);
|
|
323
|
-
const customerId =
|
|
310
|
+
const invoiceFailed = async (paygate, event) => {
|
|
311
|
+
const invoice = event.invoice;
|
|
312
|
+
const result = await reapplyInvoiceSubscription(paygate, event.eventId, invoice);
|
|
313
|
+
const customerId = invoice.customerId;
|
|
324
314
|
const entityId = result?.record?.entityId
|
|
325
|
-
?? (customerId != null ? (await access.paygateCustomers().loadByPgId(customerId,
|
|
315
|
+
?? (customerId != null ? (await access.paygateCustomers().loadByPgId(customerId, paygate.id))?.entityId : undefined);
|
|
326
316
|
if (entityId == null) {
|
|
327
317
|
log.warn('Failed invoice belongs to no known entity', { invoiceId: invoice.id });
|
|
328
318
|
return;
|
|
329
319
|
}
|
|
330
|
-
const attempt = invoice.
|
|
320
|
+
const attempt = invoice.attempt ?? 0;
|
|
321
|
+
const actionRequired = event.change === 'action-required';
|
|
331
322
|
await access.observer().propagatePaymentFailed(paymentUtils.compact({
|
|
332
|
-
entityId, kind: 'invoice', externalId: invoice.id, subscriptionId:
|
|
333
|
-
invoiceId: invoice.id, attempt, actionRequired
|
|
334
|
-
nextAttemptAt:
|
|
323
|
+
entityId, kind: 'invoice', externalId: invoice.id, subscriptionId: invoice.subscriptionId,
|
|
324
|
+
invoiceId: invoice.id, attempt, actionRequired,
|
|
325
|
+
nextAttemptAt: invoice.nextAttemptAt, eventKey: `payment-failed:${invoice.id}:${attempt}`,
|
|
335
326
|
}), ctx);
|
|
336
327
|
log.info('Payment failed', paymentUtils.compact({
|
|
337
|
-
entityId, kind: 'invoice', invoiceId: invoice.id, subscriptionId:
|
|
338
|
-
actionRequired: event.type === 'invoice.payment_action_required',
|
|
328
|
+
entityId, kind: 'invoice', invoiceId: invoice.id, subscriptionId: invoice.subscriptionId, attempt, actionRequired,
|
|
339
329
|
}), { event: 'payment.failed' });
|
|
340
330
|
};
|
|
331
|
+
const invoiceChanged = async (paygate, event) => {
|
|
332
|
+
const invoice = event.invoice;
|
|
333
|
+
switch (event.change) {
|
|
334
|
+
case 'paid':
|
|
335
|
+
if (invoice.renewal) {
|
|
336
|
+
await reapplyInvoiceSubscription(paygate, event.eventId, invoice, { renewal: true, invoiceId: invoice.id });
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
339
|
+
await refreshLatestInvoice(paygate, invoice, invoice.id);
|
|
340
|
+
return;
|
|
341
|
+
case 'payment-failed':
|
|
342
|
+
case 'action-required':
|
|
343
|
+
await invoiceFailed(paygate, event);
|
|
344
|
+
return;
|
|
345
|
+
case 'uncollectible':
|
|
346
|
+
await reapplyInvoiceSubscription(paygate, event.eventId, invoice);
|
|
347
|
+
return;
|
|
348
|
+
case 'voided':
|
|
349
|
+
await refreshLatestInvoice(paygate, invoice);
|
|
350
|
+
}
|
|
351
|
+
};
|
|
341
352
|
const targetFields = (target) => target.kind === 'fulfillment'
|
|
342
353
|
? paymentUtils.compact({
|
|
343
354
|
entityId: target.record.entityId, target: target.kind, productSku: target.record.productSku,
|
|
@@ -350,8 +361,8 @@ export const createEventHandler = (ctx, stripe) => {
|
|
|
350
361
|
planSku: target.record.planSku, externalId: target.record.externalId,
|
|
351
362
|
});
|
|
352
363
|
/** A refund of a purchase's payment moves its refunded total; a whole refund closes its window. */
|
|
353
|
-
const refundPurchase = async (target, refundedTotal, paid) => {
|
|
354
|
-
const purchaseId = consumerRecordsOf(ctx).purchaseIdOf(target.record.externalId);
|
|
364
|
+
const refundPurchase = async (paygate, target, refundedTotal, paid) => {
|
|
365
|
+
const purchaseId = consumerRecordsOf(ctx).purchaseIdOf(paygate.id, target.record.externalId);
|
|
355
366
|
const purchase = await access.purchases().byPurchaseId(purchaseId);
|
|
356
367
|
if (purchase == null || (target.kind === 'subscription' && purchase.invoiceId != null && purchase.invoiceId !== target.invoiceId)) {
|
|
357
368
|
return;
|
|
@@ -361,34 +372,34 @@ export const createEventHandler = (ctx, stripe) => {
|
|
|
361
372
|
refundedAt: purchase.refundedAt ?? (paid != null && refundedTotal >= paid ? new Date() : undefined),
|
|
362
373
|
}));
|
|
363
374
|
};
|
|
364
|
-
const processRefund = async (refund, charge) => {
|
|
365
|
-
if (refund.
|
|
375
|
+
const processRefund = async (paygate, refund, charge) => {
|
|
376
|
+
if (!refund.succeeded)
|
|
366
377
|
return;
|
|
367
|
-
const target = await paymentTargetOf(ctx).resolvePaymentTarget(
|
|
368
|
-
paymentIntentId:
|
|
378
|
+
const target = await paymentTargetOf(ctx).resolvePaymentTarget(paygate, {
|
|
379
|
+
paymentIntentId: refund.paymentId, chargeId: refund.chargeId, charge,
|
|
369
380
|
});
|
|
370
381
|
if (target == null) {
|
|
371
382
|
log.warn('Refund matches no fulfillment or subscription', { refundId: refund.id });
|
|
372
383
|
return;
|
|
373
384
|
}
|
|
374
|
-
const chargeId =
|
|
375
|
-
const refunded = target.charge ?? (chargeId != null ? await paymentTargetOf(ctx).retrieveCharge(
|
|
376
|
-
const refundedTotal = Math.max(refunded?.
|
|
377
|
-
const paid = refunded?.
|
|
385
|
+
const chargeId = refund.chargeId;
|
|
386
|
+
const refunded = target.charge ?? (chargeId != null ? await paymentTargetOf(ctx).retrieveCharge(paygate, chargeId) : null);
|
|
387
|
+
const refundedTotal = Math.max(refunded?.refundedMinor ?? 0, refund.amountMinor);
|
|
388
|
+
const paid = refunded?.amountMinor;
|
|
378
389
|
if (target.kind === 'fulfillment') {
|
|
379
390
|
await access.fulfillments().update(paymentUtils.compact({
|
|
380
391
|
...target.record, refundedMinor: Math.max(target.record.refundedMinor ?? 0, refundedTotal),
|
|
381
392
|
refundedAt: new Date(), chargeId: target.record.chargeId ?? refunded?.id,
|
|
382
393
|
}));
|
|
383
394
|
}
|
|
384
|
-
await refundPurchase(target, refundedTotal, paid);
|
|
395
|
+
await refundPurchase(paygate, target, refundedTotal, paid);
|
|
385
396
|
const metadata = { ...(refund.metadata ?? {}) };
|
|
386
397
|
await access.observer().propagateRefund(paymentUtils.compact({
|
|
387
398
|
...targetFields(target),
|
|
388
399
|
refundId: refund.id,
|
|
389
|
-
paymentIntentId: target.paymentIntentId ??
|
|
400
|
+
paymentIntentId: target.paymentIntentId ?? refund.paymentId,
|
|
390
401
|
invoiceId: target.kind === 'subscription' ? target.invoiceId : target.record.invoiceId ?? undefined,
|
|
391
|
-
amountMinor: refund.
|
|
402
|
+
amountMinor: refund.amountMinor,
|
|
392
403
|
refundedTotalMinor: refundedTotal,
|
|
393
404
|
paidMinor: paid,
|
|
394
405
|
currency: refund.currency,
|
|
@@ -400,32 +411,21 @@ export const createEventHandler = (ctx, stripe) => {
|
|
|
400
411
|
}), ctx);
|
|
401
412
|
log.info('Payment refunded', paymentUtils.compact({
|
|
402
413
|
entityId: target.record.entityId, target: target.kind, externalId: target.record.externalId, refundId: refund.id,
|
|
403
|
-
amountMinor: refund.
|
|
414
|
+
amountMinor: refund.amountMinor, refundedTotalMinor: refundedTotal, paidMinor: paid, currency: refund.currency,
|
|
404
415
|
withdrawalId: metadata.withdrawalId != null && metadata.withdrawalId !== '' ? metadata.withdrawalId : undefined,
|
|
405
416
|
}), { event: 'payment.refunded' });
|
|
406
417
|
};
|
|
407
|
-
const
|
|
408
|
-
const
|
|
409
|
-
const
|
|
410
|
-
|
|
411
|
-
await processRefund(refund, charge);
|
|
412
|
-
}
|
|
413
|
-
};
|
|
414
|
-
const refundChanged = async (event) => {
|
|
415
|
-
await processRefund(event.data.object);
|
|
416
|
-
};
|
|
417
|
-
const disputeChanged = async (event) => {
|
|
418
|
-
const dispute = event.data.object;
|
|
419
|
-
const phase = DISPUTE_PHASES[event.type];
|
|
420
|
-
const target = await paymentTargetOf(ctx).resolvePaymentTarget(stripe, {
|
|
421
|
-
paymentIntentId: paymentUtils.idOf(dispute.payment_intent), chargeId: paymentUtils.idOf(dispute.charge),
|
|
418
|
+
const disputeChanged = async (paygate, event) => {
|
|
419
|
+
const { dispute, phase } = event;
|
|
420
|
+
const target = await paymentTargetOf(ctx).resolvePaymentTarget(paygate, {
|
|
421
|
+
paymentIntentId: dispute.paymentId, chargeId: dispute.chargeId,
|
|
422
422
|
});
|
|
423
423
|
if (target == null) {
|
|
424
424
|
log.warn('Dispute matches no fulfillment or subscription', { disputeId: dispute.id });
|
|
425
425
|
return;
|
|
426
426
|
}
|
|
427
|
-
const chargeId =
|
|
428
|
-
const charge = target.charge ?? (chargeId != null ? await paymentTargetOf(ctx).retrieveCharge(
|
|
427
|
+
const chargeId = dispute.chargeId;
|
|
428
|
+
const charge = target.charge ?? (chargeId != null ? await paymentTargetOf(ctx).retrieveCharge(paygate, chargeId) : null);
|
|
429
429
|
const marks = { disputedAt: target.record.disputedAt ?? new Date(), disputeStatus: dispute.status };
|
|
430
430
|
if (target.kind === 'fulfillment') {
|
|
431
431
|
await access.fulfillments().update({ ...target.record, ...marks });
|
|
@@ -435,61 +435,50 @@ export const createEventHandler = (ctx, stripe) => {
|
|
|
435
435
|
}
|
|
436
436
|
await access.observer().propagateDispute(paymentUtils.compact({
|
|
437
437
|
...targetFields(target), disputeId: dispute.id, phase, status: dispute.status,
|
|
438
|
-
amountMinor: dispute.
|
|
438
|
+
amountMinor: dispute.amountMinor, paidMinor: charge?.amountMinor, currency: dispute.currency,
|
|
439
|
+
eventKey: `dispute:${dispute.id}:${phase}`,
|
|
439
440
|
}), ctx);
|
|
440
441
|
log.info('Dispute changed', paymentUtils.compact({
|
|
441
442
|
entityId: target.record.entityId, target: target.kind, externalId: target.record.externalId,
|
|
442
|
-
disputeId: dispute.id, phase, status: dispute.status, amountMinor: dispute.
|
|
443
|
+
disputeId: dispute.id, phase, status: dispute.status, amountMinor: dispute.amountMinor, currency: dispute.currency,
|
|
443
444
|
}), { event: phase === 'opened' ? 'dispute.opened' : phase === 'closed' ? 'dispute.closed' : 'dispute.updated' });
|
|
444
445
|
};
|
|
445
|
-
const
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
'refund.updated': refundChanged,
|
|
479
|
-
'refund.failed': async () => undefined,
|
|
480
|
-
'charge.dispute.created': disputeChanged,
|
|
481
|
-
'charge.dispute.funds_withdrawn': disputeChanged,
|
|
482
|
-
'charge.dispute.funds_reinstated': disputeChanged,
|
|
483
|
-
'charge.dispute.closed': disputeChanged,
|
|
484
|
-
};
|
|
485
|
-
return {
|
|
486
|
-
handlers,
|
|
487
|
-
process: async (event) => {
|
|
488
|
-
const handler = handlers[event.type];
|
|
489
|
-
if (handler != null) {
|
|
490
|
-
await handler(event);
|
|
491
|
-
}
|
|
492
|
-
},
|
|
446
|
+
const apply = async (paygate, event) => {
|
|
447
|
+
switch (event.type) {
|
|
448
|
+
case 'customer':
|
|
449
|
+
if (event.change === 'deleted') {
|
|
450
|
+
await markCustomerDeleted(paygate, event.customer.id);
|
|
451
|
+
}
|
|
452
|
+
else {
|
|
453
|
+
await upsertCustomer(paygate, event.customer);
|
|
454
|
+
}
|
|
455
|
+
return;
|
|
456
|
+
case 'checkout':
|
|
457
|
+
if (event.outcome === 'completed') {
|
|
458
|
+
await fulfillPayment(paygate, event.session);
|
|
459
|
+
}
|
|
460
|
+
else if (event.outcome === 'failed') {
|
|
461
|
+
await checkoutFailed(paygate, event.session);
|
|
462
|
+
}
|
|
463
|
+
else {
|
|
464
|
+
await purgeExpiredCheckout(paygate, event.session);
|
|
465
|
+
}
|
|
466
|
+
return;
|
|
467
|
+
case 'subscription':
|
|
468
|
+
await subscriptionChanged(paygate, event);
|
|
469
|
+
return;
|
|
470
|
+
case 'invoice':
|
|
471
|
+
await invoiceChanged(paygate, event);
|
|
472
|
+
return;
|
|
473
|
+
case 'refund':
|
|
474
|
+
await processRefund(paygate, event.refund, event.charge);
|
|
475
|
+
return;
|
|
476
|
+
case 'dispute':
|
|
477
|
+
await disputeChanged(paygate, event);
|
|
478
|
+
}
|
|
493
479
|
};
|
|
480
|
+
return { apply };
|
|
494
481
|
};
|
|
482
|
+
/** The paygate event dispatch of a context — one per context. */
|
|
483
|
+
export const paymentEventsOf = memoHelper.oncePer(makePaymentEventsHelper);
|
|
495
484
|
//# sourceMappingURL=events.js.map
|