@fantastic.dev/better-stripe 0.59.0 → 0.60.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/README.md +5 -1
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +676 -50
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts +322 -9
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +644 -18
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutSyncTestHelpers.d.ts +338 -25
- package/dist/client/connect/payoutSyncTestHelpers.d.ts.map +1 -1
- package/dist/client/index.d.ts +4 -0
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/paymentSource/resolver.d.ts +12 -1
- package/dist/client/paymentSource/resolver.d.ts.map +1 -1
- package/dist/client/paymentSource/resolver.fixtures.d.ts +220 -0
- package/dist/client/paymentSource/resolver.fixtures.d.ts.map +1 -0
- package/dist/client/paymentSource/resolver.fixtures.js +189 -0
- package/dist/client/paymentSource/resolver.fixtures.js.map +1 -0
- package/dist/client/paymentSource/resolver.js +28 -3
- package/dist/client/paymentSource/resolver.js.map +1 -1
- package/dist/client/settlement/execution.d.ts +25 -0
- package/dist/client/settlement/execution.d.ts.map +1 -1
- package/dist/client/settlement/execution.js +2 -0
- package/dist/client/settlement/execution.js.map +1 -1
- package/dist/client/usage/catalogPrices.d.ts +1 -1
- package/dist/client/usage/catalogPrices.d.ts.map +1 -1
- package/dist/client/usage/catalogPrices.js +4 -4
- package/dist/client/usage/catalogPrices.js.map +1 -1
- package/dist/client/usage/client.d.ts +2 -2
- package/dist/client/usage/client.js +2 -2
- package/dist/client/usage/creditCapacity.d.ts.map +1 -1
- package/dist/client/usage/creditCapacity.js +7 -1
- package/dist/client/usage/creditCapacity.js.map +1 -1
- package/dist/client/usage/creditClient.d.ts +3 -0
- package/dist/client/usage/creditClient.d.ts.map +1 -1
- package/dist/client/usage/creditDelivery.d.ts.map +1 -1
- package/dist/client/usage/creditDelivery.js +4 -1
- package/dist/client/usage/creditDelivery.js.map +1 -1
- package/dist/client/usage/creditLifecycle.d.ts.map +1 -1
- package/dist/client/usage/creditLifecycle.js +2 -1
- package/dist/client/usage/creditLifecycle.js.map +1 -1
- package/dist/client/usage/creditNative.d.ts +3 -0
- package/dist/client/usage/creditNative.d.ts.map +1 -1
- package/dist/client/usage/creditNative.js +3 -0
- package/dist/client/usage/creditNative.js.map +1 -1
- package/dist/client/usage/creditProvider.d.ts.map +1 -1
- package/dist/client/usage/creditProvider.js +2 -1
- package/dist/client/usage/creditProvider.js.map +1 -1
- package/dist/client/usage/creditRead.d.ts +1 -0
- package/dist/client/usage/creditRead.d.ts.map +1 -1
- package/dist/client/usage/creditReconciliation.d.ts.map +1 -1
- package/dist/client/usage/creditReconciliation.js +11 -3
- package/dist/client/usage/creditReconciliation.js.map +1 -1
- package/dist/client/usage/creditRevenue.d.ts +22 -0
- package/dist/client/usage/creditRevenue.d.ts.map +1 -0
- package/dist/client/usage/creditRevenue.js +118 -0
- package/dist/client/usage/creditRevenue.js.map +1 -0
- package/dist/client/usage/creditSettlement.d.ts +29 -0
- package/dist/client/usage/creditSettlement.d.ts.map +1 -0
- package/dist/client/usage/creditSettlement.fixtures.d.ts +216 -0
- package/dist/client/usage/creditSettlement.fixtures.d.ts.map +1 -0
- package/dist/client/usage/creditSettlement.fixtures.js +59 -0
- package/dist/client/usage/creditSettlement.fixtures.js.map +1 -0
- package/dist/client/usage/creditSettlement.js +70 -0
- package/dist/client/usage/creditSettlement.js.map +1 -0
- package/dist/client/usage/creditTransport.fixtures.d.ts +8 -1
- package/dist/client/usage/creditTransport.fixtures.d.ts.map +1 -1
- package/dist/client/usage/creditTransport.fixtures.js +27 -5
- package/dist/client/usage/creditTransport.fixtures.js.map +1 -1
- package/dist/client/usage/index.d.ts +4 -0
- package/dist/client/usage/index.d.ts.map +1 -1
- package/dist/client/usage/invoiceSale.d.ts +22 -0
- package/dist/client/usage/invoiceSale.d.ts.map +1 -0
- package/dist/client/usage/invoiceSale.fixtures.d.ts +285 -0
- package/dist/client/usage/invoiceSale.fixtures.d.ts.map +1 -0
- package/dist/client/usage/invoiceSale.fixtures.js +104 -0
- package/dist/client/usage/invoiceSale.fixtures.js.map +1 -0
- package/dist/client/usage/invoiceSale.js +117 -0
- package/dist/client/usage/invoiceSale.js.map +1 -0
- package/dist/client/usage/observationClient.d.ts +2 -2
- package/dist/client/usage/observationClient.d.ts.map +1 -1
- package/dist/client/usage/settlement.d.ts +32 -0
- package/dist/client/usage/settlement.d.ts.map +1 -0
- package/dist/client/usage/settlement.js +60 -0
- package/dist/client/usage/settlement.js.map +1 -0
- package/dist/client/usage/settlementClient.d.ts +51 -0
- package/dist/client/usage/settlementClient.d.ts.map +1 -0
- package/dist/client/usage/settlementClient.js +35 -0
- package/dist/client/usage/settlementClient.js.map +1 -0
- package/dist/client/usage/subscriptionCancellation.d.ts.map +1 -1
- package/dist/client/usage/subscriptionCancellation.js +3 -2
- package/dist/client/usage/subscriptionCancellation.js.map +1 -1
- package/dist/client/usage/subscriptionCheckout.d.ts.map +1 -1
- package/dist/client/usage/subscriptionCheckout.js +3 -2
- package/dist/client/usage/subscriptionCheckout.js.map +1 -1
- package/dist/client/usage/subscriptionLifecycle.d.ts +1 -0
- package/dist/client/usage/subscriptionLifecycle.d.ts.map +1 -1
- package/dist/client/usage/subscriptionLifecycle.js +3 -2
- package/dist/client/usage/subscriptionLifecycle.js.map +1 -1
- package/dist/client/usage/subscriptionProvider.d.ts +2 -0
- package/dist/client/usage/subscriptionProvider.d.ts.map +1 -1
- package/dist/client/usage/subscriptionProvider.js +9 -2
- package/dist/client/usage/subscriptionProvider.js.map +1 -1
- package/dist/component/_generated/component.d.ts +146 -0
- package/dist/component/_generated/component.d.ts.map +1 -1
- package/dist/component/balanceFunding/internal/actions.d.ts +11 -11
- package/dist/component/balanceFunding/mutations.d.ts +27 -27
- package/dist/component/earnings/triggers.d.ts +172 -12
- package/dist/component/earnings/triggers.d.ts.map +1 -1
- package/dist/component/financialObservations/settlementHelpers.d.ts.map +1 -1
- package/dist/component/financialObservations/settlementHelpers.js +2 -0
- package/dist/component/financialObservations/settlementHelpers.js.map +1 -1
- package/dist/component/recoveryWorkflow/helpers.d.ts +25 -0
- package/dist/component/recoveryWorkflow/helpers.d.ts.map +1 -1
- package/dist/component/recoveryWorkflow/mutations.d.ts +25 -0
- package/dist/component/recoveryWorkflow/mutations.d.ts.map +1 -1
- package/dist/component/recoveryWorkflow/queries.d.ts +50 -0
- package/dist/component/recoveryWorkflow/queries.d.ts.map +1 -1
- package/dist/component/recoveryWorkflow/schema.d.ts +566 -10
- package/dist/component/recoveryWorkflow/schema.d.ts.map +1 -1
- package/dist/component/schema.d.ts +322 -9
- package/dist/component/settlement/mutations.d.ts +50 -0
- package/dist/component/settlement/mutations.d.ts.map +1 -1
- package/dist/component/settlement/mutations.js +8 -1
- package/dist/component/settlement/mutations.js.map +1 -1
- package/dist/component/settlement/queries.d.ts +25 -0
- package/dist/component/settlement/queries.d.ts.map +1 -1
- package/dist/component/settlement/schema.d.ts +128 -1
- package/dist/component/settlement/schema.d.ts.map +1 -1
- package/dist/component/settlement/schema.js +22 -0
- package/dist/component/settlement/schema.js.map +1 -1
- package/dist/component/usage/allowance.fixtures.d.ts +4212 -0
- package/dist/component/usage/allowance.fixtures.d.ts.map +1 -0
- package/dist/component/usage/allowance.fixtures.js +67 -0
- package/dist/component/usage/allowance.fixtures.js.map +1 -0
- package/dist/component/usageCredits/accounting.fixtures.d.ts +4235 -0
- package/dist/component/usageCredits/accounting.fixtures.d.ts.map +1 -0
- package/dist/component/usageCredits/accounting.fixtures.js +89 -0
- package/dist/component/usageCredits/accounting.fixtures.js.map +1 -0
- package/dist/component/usageCredits/helpers.d.ts +7 -0
- package/dist/component/usageCredits/helpers.d.ts.map +1 -1
- package/dist/component/usageCredits/helpers.js +5 -0
- package/dist/component/usageCredits/helpers.js.map +1 -1
- package/dist/component/usageCredits/mutations.d.ts +6 -0
- package/dist/component/usageCredits/mutations.d.ts.map +1 -1
- package/dist/component/usageCredits/mutations.js +70 -7
- package/dist/component/usageCredits/mutations.js.map +1 -1
- package/dist/component/usageCredits/purchase.d.ts +1 -0
- package/dist/component/usageCredits/purchase.d.ts.map +1 -1
- package/dist/component/usageCredits/queries.d.ts +10 -0
- package/dist/component/usageCredits/queries.d.ts.map +1 -1
- package/dist/component/usageCredits/schema.d.ts +24 -4
- package/dist/component/usageCredits/schema.d.ts.map +1 -1
- package/dist/component/usageCredits/schema.js +4 -0
- package/dist/component/usageCredits/schema.js.map +1 -1
- package/dist/component/usageSubscriptions/history.d.ts.map +1 -1
- package/dist/component/usageSubscriptions/history.js +3 -1
- package/dist/component/usageSubscriptions/history.js.map +1 -1
- package/dist/component/usageSubscriptions/mutations.d.ts +5 -0
- package/dist/component/usageSubscriptions/mutations.d.ts.map +1 -1
- package/dist/component/usageSubscriptions/mutations.js +19 -0
- package/dist/component/usageSubscriptions/mutations.js.map +1 -1
- package/dist/component/usageSubscriptions/queries.d.ts +2 -0
- package/dist/component/usageSubscriptions/queries.d.ts.map +1 -1
- package/dist/component/usageSubscriptions/schema.d.ts +7 -2
- package/dist/component/usageSubscriptions/schema.d.ts.map +1 -1
- package/dist/component/usageSubscriptions/schema.js +1 -0
- package/dist/component/usageSubscriptions/schema.js.map +1 -1
- package/docs/README.md +4 -0
- package/docs/settlement.md +4 -0
- package/docs/usage-credits.md +6 -1
- package/docs/usage-security.md +130 -0
- package/docs/usage-settlement.md +162 -0
- package/package.json +3 -1
- package/src/client/index.ts +9 -0
- package/src/client/paymentSource/resolver.fixtures.ts +216 -0
- package/src/client/paymentSource/resolver.test.ts +11 -207
- package/src/client/paymentSource/resolver.ts +54 -2
- package/src/client/settlement/execution.ts +24 -0
- package/src/client/usage/catalogPrices.ts +4 -2
- package/src/client/usage/client.ts +2 -2
- package/src/client/usage/creditBackingIsolation.test.ts +110 -0
- package/src/client/usage/creditCapacity.test.ts +80 -1
- package/src/client/usage/creditCapacity.ts +10 -1
- package/src/client/usage/creditDelivery.ts +4 -0
- package/src/client/usage/creditLifecycle.ts +2 -0
- package/src/client/usage/creditNative.ts +5 -0
- package/src/client/usage/creditProofBounds.test.ts +187 -0
- package/src/client/usage/creditProvider.test.ts +74 -0
- package/src/client/usage/creditProvider.ts +2 -0
- package/src/client/usage/creditReconciliation.ts +18 -3
- package/src/client/usage/creditRevenue.test.ts +105 -0
- package/src/client/usage/creditRevenue.ts +163 -0
- package/src/client/usage/creditSettlement.fixtures.ts +70 -0
- package/src/client/usage/creditSettlement.test.ts +66 -0
- package/src/client/usage/creditSettlement.ts +94 -0
- package/src/client/usage/creditSettlementRecovery.test.ts +173 -0
- package/src/client/usage/creditTransport.fixtures.ts +31 -5
- package/src/client/usage/index.ts +9 -0
- package/src/client/usage/invoiceSale.fixtures.ts +103 -0
- package/src/client/usage/invoiceSale.test.ts +184 -0
- package/src/client/usage/invoiceSale.ts +155 -0
- package/src/client/usage/settlement.test.ts +179 -0
- package/src/client/usage/settlement.ts +81 -0
- package/src/client/usage/settlementClient.ts +84 -0
- package/src/client/usage/subscriptionCancellation.ts +3 -2
- package/src/client/usage/subscriptionCheckout.ts +7 -2
- package/src/client/usage/subscriptionClient.test.ts +60 -1
- package/src/client/usage/subscriptionLifecycle.ts +3 -2
- package/src/client/usage/subscriptionProvider.ts +27 -3
- package/src/component/_generated/component.ts +150 -1
- package/src/component/financialObservations/settlementHelpers.ts +2 -0
- package/src/component/settlement/mutations.ts +10 -1
- package/src/component/settlement/schema.ts +26 -0
- package/src/component/settlement/settlement-claims.test.ts +54 -0
- package/src/component/usage/allowance.fixtures.ts +77 -0
- package/src/component/usage/allowance.test.ts +3 -66
- package/src/component/usage/allowanceBounds.test.ts +79 -0
- package/src/component/usage/historyIsolation.test.ts +124 -0
- package/src/component/usageCredits/accounting.fixtures.ts +96 -0
- package/src/component/usageCredits/accounting.test.ts +176 -82
- package/src/component/usageCredits/freshness.test.ts +33 -0
- package/src/component/usageCredits/helpers.ts +8 -0
- package/src/component/usageCredits/mutations.ts +83 -5
- package/src/component/usageCredits/quotas.test.ts +63 -0
- package/src/component/usageCredits/schema.ts +10 -0
- package/src/component/usageSubscriptions/history.test.ts +57 -0
- package/src/component/usageSubscriptions/history.ts +3 -1
- package/src/component/usageSubscriptions/mutations.ts +23 -0
- package/src/component/usageSubscriptions/schema.ts +1 -0
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { expect, it } from "vitest";
|
|
2
|
+
import { componentsGeneric } from "convex/server";
|
|
3
|
+
import { initConvexTest } from "../setup.test.js";
|
|
4
|
+
import { schema, modules } from "../../test.js";
|
|
5
|
+
import type { Component, RunCtx } from "../helpers.js";
|
|
6
|
+
import { fixture } from "./invoiceSale.fixtures.js";
|
|
7
|
+
import { settleUsageInvoice } from "./settlement.js";
|
|
8
|
+
function setup() {
|
|
9
|
+
const f = fixture(),
|
|
10
|
+
t = initConvexTest();
|
|
11
|
+
t.registerComponent("betterStripe", schema, modules);
|
|
12
|
+
const component = componentsGeneric().betterStripe as unknown as Component;
|
|
13
|
+
const native: Record<string, unknown>[] = [];
|
|
14
|
+
f.extra.set("/v1/charges/ch_source", {
|
|
15
|
+
...f.payment.latest_charge,
|
|
16
|
+
created: 1200,
|
|
17
|
+
});
|
|
18
|
+
f.extra.set("/v1/transfers", (method: string, raw: string) => {
|
|
19
|
+
if (method === "GET")
|
|
20
|
+
return { object: "list", data: native, has_more: false };
|
|
21
|
+
const body = new URLSearchParams(raw);
|
|
22
|
+
const transfer = {
|
|
23
|
+
id: `tr_${native.length}`,
|
|
24
|
+
object: "transfer",
|
|
25
|
+
amount: Number(body.get("amount")),
|
|
26
|
+
currency: body.get("currency"),
|
|
27
|
+
destination: body.get("destination"),
|
|
28
|
+
source_transaction: body.get("source_transaction"),
|
|
29
|
+
transfer_group: body.get("transfer_group"),
|
|
30
|
+
metadata: {
|
|
31
|
+
bsRole: body.get("metadata[bsRole]"),
|
|
32
|
+
bsSourceCharge: body.get("metadata[bsSourceCharge]"),
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
native.push(transfer);
|
|
36
|
+
return transfer;
|
|
37
|
+
});
|
|
38
|
+
const input = {
|
|
39
|
+
...f.input,
|
|
40
|
+
policy: { creatorAccountId: "acct_seller", feeBasisPoints: 900 },
|
|
41
|
+
};
|
|
42
|
+
return { ...f, t, component, native, input };
|
|
43
|
+
}
|
|
44
|
+
it("settles actual paid metered proceeds once using the existing immutable allocation and transfer ledger", async () => {
|
|
45
|
+
const f = setup();
|
|
46
|
+
const first = await f.t.action((ctx) =>
|
|
47
|
+
settleUsageInvoice(f.sdk, f.component, ctx as RunCtx, f.input),
|
|
48
|
+
);
|
|
49
|
+
expect(first).toMatchObject({
|
|
50
|
+
status: "settled",
|
|
51
|
+
gross: 1000,
|
|
52
|
+
processingFee: 50,
|
|
53
|
+
platformNet: 40,
|
|
54
|
+
usageSource: {
|
|
55
|
+
meterId: "mtr_calls",
|
|
56
|
+
invoicePaymentId: "inpay_source",
|
|
57
|
+
stripeProductId: "prod_original",
|
|
58
|
+
},
|
|
59
|
+
transfers: [
|
|
60
|
+
{ destinationAccountId: "acct_seller", amount: 910, role: "store" },
|
|
61
|
+
],
|
|
62
|
+
});
|
|
63
|
+
expect(
|
|
64
|
+
await f.t.action((ctx) =>
|
|
65
|
+
settleUsageInvoice(f.sdk, f.component, ctx as RunCtx, {
|
|
66
|
+
...f.input,
|
|
67
|
+
policy: { ...f.input.policy, feeBasisPoints: 9900 },
|
|
68
|
+
}),
|
|
69
|
+
),
|
|
70
|
+
).toEqual(first);
|
|
71
|
+
expect(f.native).toHaveLength(1);
|
|
72
|
+
});
|
|
73
|
+
it("never creates transfers from a zero invoice or a prepaid consumption invoice", async () => {
|
|
74
|
+
for (const prepaid of [false, true]) {
|
|
75
|
+
const f = setup();
|
|
76
|
+
if (!prepaid) f.invoice.amount_paid = 0;
|
|
77
|
+
const input = prepaid
|
|
78
|
+
? {
|
|
79
|
+
...f.input,
|
|
80
|
+
binding: {
|
|
81
|
+
...f.input.binding,
|
|
82
|
+
plan: {
|
|
83
|
+
...f.input.binding.plan,
|
|
84
|
+
model: "prepaid" as const,
|
|
85
|
+
insufficientBalance: "reject" as const,
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
}
|
|
89
|
+
: f.input;
|
|
90
|
+
expect(
|
|
91
|
+
await f.t.action((ctx) =>
|
|
92
|
+
settleUsageInvoice(f.sdk, f.component, ctx as RunCtx, input),
|
|
93
|
+
),
|
|
94
|
+
).toMatchObject({ status: "rejected" });
|
|
95
|
+
expect(f.native).toHaveLength(0);
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("keeps a zero initial metered invoice from consuming the one-time affiliate commission", async () => {
|
|
100
|
+
const f = setup();
|
|
101
|
+
const paid = {
|
|
102
|
+
...f.invoice,
|
|
103
|
+
created: 1200,
|
|
104
|
+
status_transitions: { finalized_at: 1200, paid_at: 1250 },
|
|
105
|
+
};
|
|
106
|
+
f.extra.set("/v1/invoices", {
|
|
107
|
+
object: "list",
|
|
108
|
+
has_more: false,
|
|
109
|
+
data: [
|
|
110
|
+
{
|
|
111
|
+
...paid,
|
|
112
|
+
id: "in_zero",
|
|
113
|
+
created: 600,
|
|
114
|
+
amount_paid: 0,
|
|
115
|
+
status_transitions: { finalized_at: 600, paid_at: 600 },
|
|
116
|
+
},
|
|
117
|
+
paid,
|
|
118
|
+
],
|
|
119
|
+
});
|
|
120
|
+
const result = await f.t.action((ctx) =>
|
|
121
|
+
settleUsageInvoice(f.sdk, f.component, ctx as RunCtx, {
|
|
122
|
+
...f.input,
|
|
123
|
+
policy: {
|
|
124
|
+
...f.input.policy,
|
|
125
|
+
commissionTerm: "one_time",
|
|
126
|
+
affiliate: {
|
|
127
|
+
destinationAccountId: "acct_affiliate",
|
|
128
|
+
basisPoints: 2000,
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
}),
|
|
132
|
+
);
|
|
133
|
+
expect(result).toMatchObject({
|
|
134
|
+
status: "settled",
|
|
135
|
+
transfers: [
|
|
136
|
+
{ destinationAccountId: "acct_seller", role: "store", amount: 728 },
|
|
137
|
+
{
|
|
138
|
+
destinationAccountId: "acct_affiliate",
|
|
139
|
+
role: "affiliate",
|
|
140
|
+
amount: 182,
|
|
141
|
+
},
|
|
142
|
+
],
|
|
143
|
+
});
|
|
144
|
+
expect(f.native).toHaveLength(2);
|
|
145
|
+
});
|
|
146
|
+
it("recovers a native transfer whose ledger write failed without paying the metered invoice twice", async () => {
|
|
147
|
+
const f = setup();
|
|
148
|
+
let fail = true;
|
|
149
|
+
const settle = () =>
|
|
150
|
+
f.t.action((ctx) =>
|
|
151
|
+
settleUsageInvoice(
|
|
152
|
+
f.sdk,
|
|
153
|
+
f.component,
|
|
154
|
+
{
|
|
155
|
+
...ctx,
|
|
156
|
+
runMutation: async (ref, args) => {
|
|
157
|
+
if (
|
|
158
|
+
fail &&
|
|
159
|
+
String(
|
|
160
|
+
ref[Symbol.for("toReferencePath") as keyof typeof ref],
|
|
161
|
+
).includes("upsertTransfer")
|
|
162
|
+
) {
|
|
163
|
+
fail = false;
|
|
164
|
+
throw new Error("receipt storage unavailable");
|
|
165
|
+
}
|
|
166
|
+
return ctx.runMutation(ref, args);
|
|
167
|
+
},
|
|
168
|
+
} as RunCtx,
|
|
169
|
+
f.input,
|
|
170
|
+
),
|
|
171
|
+
);
|
|
172
|
+
await expect(settle()).rejects.toThrow("receipt storage unavailable");
|
|
173
|
+
expect(f.native).toHaveLength(1);
|
|
174
|
+
expect(await settle()).toMatchObject({
|
|
175
|
+
status: "settled",
|
|
176
|
+
usageSource: { invoicePaymentId: "inpay_source" },
|
|
177
|
+
});
|
|
178
|
+
expect(f.native).toHaveLength(1);
|
|
179
|
+
});
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { getUsageInvoiceSale } from "./invoiceSale.js";
|
|
2
|
+
import { allocateSale } from "../settlement/allocation.js";
|
|
3
|
+
import { selectCommissionInvoice } from "../settlement/commissions.js";
|
|
4
|
+
import {
|
|
5
|
+
executeResolvedTransfers,
|
|
6
|
+
type ResolvedSettlement,
|
|
7
|
+
} from "../settlement/execution.js";
|
|
8
|
+
import { componentRef } from "../webhooks/helpers.js";
|
|
9
|
+
import type Stripe from "stripe";
|
|
10
|
+
import type { Component, RunCtx } from "../helpers.js";
|
|
11
|
+
import type { SaleAllocationOptions } from "../settlement/allocation.js";
|
|
12
|
+
import type { CommissionTerm } from "../settlement/commissions.js";
|
|
13
|
+
import type { UsageInvoiceSaleOptions } from "./invoiceSale.js";
|
|
14
|
+
export type UsageInvoiceSettlementOptions = UsageInvoiceSaleOptions & {
|
|
15
|
+
policy: Omit<SaleAllocationOptions, "gross" | "processingFee"> & {
|
|
16
|
+
commissionTerm?: CommissionTerm;
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
export async function settleUsageInvoice(
|
|
20
|
+
stripe: Stripe,
|
|
21
|
+
component: Component,
|
|
22
|
+
ctx: RunCtx,
|
|
23
|
+
input: UsageInvoiceSettlementOptions,
|
|
24
|
+
) {
|
|
25
|
+
const sale = await getUsageInvoiceSale(stripe, input);
|
|
26
|
+
if (sale.status !== "ready") return sale;
|
|
27
|
+
const source = sale.value;
|
|
28
|
+
const prior = (await ctx.runQuery(
|
|
29
|
+
componentRef(component, "settlement/queries/getSettlementAllocation"),
|
|
30
|
+
{ sourceChargeId: source.chargeId },
|
|
31
|
+
)) as ResolvedSettlement | null;
|
|
32
|
+
if (
|
|
33
|
+
prior &&
|
|
34
|
+
(prior.gross !== source.gross ||
|
|
35
|
+
prior.processingFee !== source.processingFee ||
|
|
36
|
+
prior.currency !== source.currency)
|
|
37
|
+
)
|
|
38
|
+
throw new Error(
|
|
39
|
+
"Retained allocation conflicts with verified invoice proceeds",
|
|
40
|
+
);
|
|
41
|
+
let affiliate = input.policy.affiliate;
|
|
42
|
+
if (affiliate) {
|
|
43
|
+
const commission = await selectCommissionInvoice(stripe, component, ctx, {
|
|
44
|
+
subscriptionId: input.subscription.subscriptionId,
|
|
45
|
+
invoiceId: source.invoiceId,
|
|
46
|
+
affiliateAccountId: affiliate.destinationAccountId,
|
|
47
|
+
term: input.policy.commissionTerm ?? "forever",
|
|
48
|
+
});
|
|
49
|
+
if (commission.status !== "ready") return commission;
|
|
50
|
+
if (!commission.eligible) affiliate = undefined;
|
|
51
|
+
}
|
|
52
|
+
const allocation = allocateSale({
|
|
53
|
+
...input.policy,
|
|
54
|
+
affiliate,
|
|
55
|
+
gross: source.gross,
|
|
56
|
+
processingFee: source.processingFee,
|
|
57
|
+
});
|
|
58
|
+
return executeResolvedTransfers(stripe, component, ctx, {
|
|
59
|
+
sourceChargeId: source.chargeId,
|
|
60
|
+
paymentIntentId: source.paymentIntentId,
|
|
61
|
+
sourceInvoiceId: source.invoiceId,
|
|
62
|
+
usageSource: {
|
|
63
|
+
stripeAccountId: input.scope.stripeAccountId,
|
|
64
|
+
customerAccount: source.customerAccountId,
|
|
65
|
+
livemode: source.livemode,
|
|
66
|
+
meterId: source.meterId,
|
|
67
|
+
stripePriceId: source.stripePriceId,
|
|
68
|
+
stripeProductId: source.stripeProductId,
|
|
69
|
+
subscriptionId: input.subscription.subscriptionId,
|
|
70
|
+
subscriptionItemId: source.subscriptionItemId,
|
|
71
|
+
invoicePaymentId: source.invoicePaymentId,
|
|
72
|
+
period: source.period,
|
|
73
|
+
model: source.model,
|
|
74
|
+
},
|
|
75
|
+
gross: source.gross,
|
|
76
|
+
currency: source.currency,
|
|
77
|
+
processingFee: source.processingFee,
|
|
78
|
+
platformNet: allocation.platformNet,
|
|
79
|
+
transfers: allocation.transfers,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import {
|
|
2
|
+
settleCreditPurchase,
|
|
3
|
+
type UsageCreditSettlementOptions,
|
|
4
|
+
} from "./creditSettlement.js";
|
|
5
|
+
import Stripe from "stripe";
|
|
6
|
+
import type { RunCtx } from "../helpers.js";
|
|
7
|
+
import type { StripeApiVersion } from "../stripe-types.js";
|
|
8
|
+
import { UsageCorrectionClient } from "./correctionClient.js";
|
|
9
|
+
import {
|
|
10
|
+
getUsageInvoiceSale,
|
|
11
|
+
type UsageInvoiceSaleOptions,
|
|
12
|
+
} from "./invoiceSale.js";
|
|
13
|
+
import {
|
|
14
|
+
settleUsageInvoice,
|
|
15
|
+
type UsageInvoiceSettlementOptions,
|
|
16
|
+
} from "./settlement.js";
|
|
17
|
+
import { getCreditPurchaseRevenue } from "./creditRevenue.js";
|
|
18
|
+
import {
|
|
19
|
+
creditBinding,
|
|
20
|
+
creditPurchaseTerms,
|
|
21
|
+
type UsageCreditPurchaseOptions,
|
|
22
|
+
} from "./creditTerms.js";
|
|
23
|
+
import { observationOptions } from "./observationTypes.js";
|
|
24
|
+
import type { UsageDeliveryProviderOptions } from "./deliveryProvider.js";
|
|
25
|
+
function provider(version: string, options: UsageDeliveryProviderOptions) {
|
|
26
|
+
if (!options.stripeSecretKey)
|
|
27
|
+
throw new Error("Native usage settlement credentials unavailable");
|
|
28
|
+
return new Stripe(options.stripeSecretKey, {
|
|
29
|
+
apiVersion: version as StripeApiVersion,
|
|
30
|
+
maxNetworkRetries: 0,
|
|
31
|
+
timeout: 15000,
|
|
32
|
+
...(options.httpClient ? { httpClient: options.httpClient } : {}),
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
/** Trusted host authorizes original seller/product and recipient policies before any settlement. */
|
|
36
|
+
export class UsageSettlementClient extends UsageCorrectionClient {
|
|
37
|
+
async getInvoiceSale(
|
|
38
|
+
_ctx: RunCtx,
|
|
39
|
+
input: UsageInvoiceSaleOptions,
|
|
40
|
+
options: UsageDeliveryProviderOptions,
|
|
41
|
+
) {
|
|
42
|
+
return getUsageInvoiceSale(
|
|
43
|
+
provider(observationOptions(input).binding.apiVersion, options),
|
|
44
|
+
input,
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
async settleInvoice(
|
|
48
|
+
ctx: RunCtx,
|
|
49
|
+
input: UsageInvoiceSettlementOptions,
|
|
50
|
+
options: UsageDeliveryProviderOptions,
|
|
51
|
+
) {
|
|
52
|
+
return settleUsageInvoice(
|
|
53
|
+
provider(observationOptions(input).binding.apiVersion, options),
|
|
54
|
+
this.component,
|
|
55
|
+
ctx,
|
|
56
|
+
input,
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
async getCreditPurchaseRevenue(
|
|
60
|
+
ctx: RunCtx,
|
|
61
|
+
input: UsageCreditPurchaseOptions,
|
|
62
|
+
options: UsageDeliveryProviderOptions,
|
|
63
|
+
) {
|
|
64
|
+
return getCreditPurchaseRevenue(
|
|
65
|
+
provider(creditPurchaseTerms(input).binding.apiVersion, options),
|
|
66
|
+
this.component,
|
|
67
|
+
ctx,
|
|
68
|
+
input,
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
async settleCreditPurchase(
|
|
72
|
+
ctx: RunCtx,
|
|
73
|
+
input: UsageCreditSettlementOptions,
|
|
74
|
+
options: UsageDeliveryProviderOptions,
|
|
75
|
+
) {
|
|
76
|
+
const { policy: _policy, ...purchase } = input;
|
|
77
|
+
return settleCreditPurchase(
|
|
78
|
+
provider(creditBinding(purchase).binding.apiVersion, options),
|
|
79
|
+
this.component,
|
|
80
|
+
ctx,
|
|
81
|
+
input,
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
} from "./subscriptionTerms.js";
|
|
12
12
|
import {
|
|
13
13
|
subscriptionCall,
|
|
14
|
+
beginSubscriptionObservation,
|
|
14
15
|
UsageSubscriptionError,
|
|
15
16
|
verifySubscriptionRealm,
|
|
16
17
|
} from "./subscriptionProvider.js";
|
|
@@ -21,8 +22,8 @@ export async function changeManagedCancellation(
|
|
|
21
22
|
row: Doc<"usageSubscriptionIntents">,
|
|
22
23
|
input: { operationId: string; cancelAtPeriodEnd: boolean },
|
|
23
24
|
): Promise<UsageSubscriptionView> {
|
|
24
|
-
const terms = subscriptionTerms(JSON.parse(row.termsJson))
|
|
25
|
-
|
|
25
|
+
const terms = subscriptionTerms(JSON.parse(row.termsJson));
|
|
26
|
+
const startedAt = await beginSubscriptionObservation(component, ctx, row.key);
|
|
26
27
|
return subscriptionCall(async () => {
|
|
27
28
|
if (!row.providerId)
|
|
28
29
|
throw new UsageSubscriptionError(
|
|
@@ -15,6 +15,7 @@ import {
|
|
|
15
15
|
verifySubscriptionPrices,
|
|
16
16
|
discoverSubscription,
|
|
17
17
|
saveSubscription,
|
|
18
|
+
beginSubscriptionObservation,
|
|
18
19
|
} from "./subscriptionProvider.js";
|
|
19
20
|
export function managedCheckoutParams(
|
|
20
21
|
terms: UsageSubscriptionTerms,
|
|
@@ -79,12 +80,16 @@ export async function ensureManagedCheckout(
|
|
|
79
80
|
ctx: RunCtx,
|
|
80
81
|
input: UsageSubscriptionOptions,
|
|
81
82
|
) {
|
|
82
|
-
const terms = subscriptionTerms(input)
|
|
83
|
-
startedAt = Date.now();
|
|
83
|
+
const terms = subscriptionTerms(input);
|
|
84
84
|
const params = managedCheckoutParams(terms, "pending");
|
|
85
85
|
return subscriptionCall(async () => {
|
|
86
86
|
await verifySubscriptionRealm(stripe, terms);
|
|
87
87
|
const row = await prepareSubscription(component, ctx, terms, "checkout");
|
|
88
|
+
const startedAt = await beginSubscriptionObservation(
|
|
89
|
+
component,
|
|
90
|
+
ctx,
|
|
91
|
+
row.key,
|
|
92
|
+
);
|
|
88
93
|
await discoverSubscription(stripe, terms, row.key);
|
|
89
94
|
let session = row.checkoutId
|
|
90
95
|
? await stripe.checkout.sessions.retrieve(row.checkoutId)
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { expect, it } from "vitest";
|
|
1
|
+
import { afterEach, expect, it, vi } from "vitest";
|
|
2
2
|
import { BetterStripe } from "../index.js";
|
|
3
3
|
import type { RunCtx } from "../helpers.js";
|
|
4
4
|
import { fixture } from "./subscriptionTransport.fixtures.js";
|
|
@@ -314,3 +314,62 @@ it("records terminal cancellation refusal without leaving a lock that prevents c
|
|
|
314
314
|
),
|
|
315
315
|
).toMatchObject({ retired: true });
|
|
316
316
|
});
|
|
317
|
+
|
|
318
|
+
afterEach(() => vi.useRealTimers());
|
|
319
|
+
it("orders concurrent same-clock native refreshes and returns the retained newer projection", async () => {
|
|
320
|
+
vi.useFakeTimers();
|
|
321
|
+
vi.setSystemTime(700000);
|
|
322
|
+
let block = false,
|
|
323
|
+
signal!: () => void,
|
|
324
|
+
release!: () => void;
|
|
325
|
+
const entered = new Promise<void>((resolve) => {
|
|
326
|
+
signal = resolve;
|
|
327
|
+
}),
|
|
328
|
+
gate = new Promise<void>((resolve) => {
|
|
329
|
+
release = resolve;
|
|
330
|
+
});
|
|
331
|
+
const f = fixture(false, false, async (input, init) => {
|
|
332
|
+
if (
|
|
333
|
+
block &&
|
|
334
|
+
new URL(input).pathname === "/v1/subscriptions/sub_calls" &&
|
|
335
|
+
(init?.method ?? "GET") === "GET"
|
|
336
|
+
) {
|
|
337
|
+
block = false;
|
|
338
|
+
signal();
|
|
339
|
+
await gate;
|
|
340
|
+
return new Response(JSON.stringify(nativeSubscription()), {
|
|
341
|
+
headers: { "content-type": "application/json" },
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
});
|
|
345
|
+
const client = new BetterStripe(f.component),
|
|
346
|
+
options = {
|
|
347
|
+
stripeSecretKey: "sk_test_subscription",
|
|
348
|
+
httpClient: f.httpClient,
|
|
349
|
+
};
|
|
350
|
+
await f.t.action((ctx) =>
|
|
351
|
+
client.usage.ensureSubscription(ctx as RunCtx, request, options),
|
|
352
|
+
);
|
|
353
|
+
block = true;
|
|
354
|
+
const older = f.t.action((ctx) =>
|
|
355
|
+
client.usage.observeSubscription(ctx as RunCtx, ref, options),
|
|
356
|
+
);
|
|
357
|
+
await entered;
|
|
358
|
+
f.state.native!.items.data.forEach((item) => {
|
|
359
|
+
item.current_period_start = 1200;
|
|
360
|
+
item.current_period_end = 1800;
|
|
361
|
+
});
|
|
362
|
+
const newer = await f.t.action((ctx) =>
|
|
363
|
+
client.usage.observeSubscription(ctx as RunCtx, ref, options),
|
|
364
|
+
);
|
|
365
|
+
release();
|
|
366
|
+
expect(newer.bindings[0].subscription.period.start).toBe(1200);
|
|
367
|
+
expect((await older).bindings[0].subscription.period.start).toBe(1200);
|
|
368
|
+
expect(
|
|
369
|
+
await f.t.query((ctx) => client.usage.getSubscription(ctx as RunCtx, ref)),
|
|
370
|
+
).toMatchObject({
|
|
371
|
+
value: {
|
|
372
|
+
bindings: [{ subscription: { period: { start: 1200, end: 1800 } } }],
|
|
373
|
+
},
|
|
374
|
+
});
|
|
375
|
+
});
|
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
} from "./subscriptionTerms.js";
|
|
12
12
|
import {
|
|
13
13
|
saveSubscription,
|
|
14
|
+
beginSubscriptionObservation,
|
|
14
15
|
subscriptionCall,
|
|
15
16
|
UsageSubscriptionError,
|
|
16
17
|
verifySubscriptionRealm,
|
|
@@ -51,8 +52,8 @@ export async function refreshManagedSubscription(
|
|
|
51
52
|
ctx: RunCtx,
|
|
52
53
|
row: Doc<"usageSubscriptionIntents">,
|
|
53
54
|
) {
|
|
54
|
-
const terms = subscriptionTerms(JSON.parse(row.termsJson))
|
|
55
|
-
|
|
55
|
+
const terms = subscriptionTerms(JSON.parse(row.termsJson));
|
|
56
|
+
const startedAt = await beginSubscriptionObservation(component, ctx, row.key);
|
|
56
57
|
return subscriptionCall(async () => {
|
|
57
58
|
if (!row.providerId)
|
|
58
59
|
throw new UsageSubscriptionError(
|
|
@@ -159,6 +159,17 @@ export async function discoverSubscription(
|
|
|
159
159
|
"Native subscription inventory exceeds bounded history",
|
|
160
160
|
);
|
|
161
161
|
}
|
|
162
|
+
export function beginSubscriptionObservation(
|
|
163
|
+
component: Component,
|
|
164
|
+
ctx: RunCtx,
|
|
165
|
+
key: string,
|
|
166
|
+
) {
|
|
167
|
+
return runMutationOrThrow(
|
|
168
|
+
ctx,
|
|
169
|
+
componentRef(component, "usageSubscriptions/mutations/beginObservation"),
|
|
170
|
+
{ key },
|
|
171
|
+
) as Promise<number>;
|
|
172
|
+
}
|
|
162
173
|
export async function saveSubscription(
|
|
163
174
|
component: Component,
|
|
164
175
|
ctx: RunCtx,
|
|
@@ -174,7 +185,16 @@ export async function saveSubscription(
|
|
|
174
185
|
componentRef(component, "usageSubscriptions/mutations/save"),
|
|
175
186
|
{ key, nativeJson: JSON.stringify(evidence), startedAt },
|
|
176
187
|
);
|
|
177
|
-
|
|
188
|
+
const retained = (await ctx.runQuery(
|
|
189
|
+
componentRef(component, "usageSubscriptions/queries/lookup"),
|
|
190
|
+
{ key },
|
|
191
|
+
)) as Doc<"usageSubscriptionIntents"> | null;
|
|
192
|
+
if (!retained?.viewJson || retained.providerId !== view.subscriptionId)
|
|
193
|
+
throw new UsageSubscriptionError(
|
|
194
|
+
"incompatible_terms",
|
|
195
|
+
"Retained subscription projection unavailable",
|
|
196
|
+
);
|
|
197
|
+
return JSON.parse(retained.viewJson) as ReturnType<typeof subscriptionView>;
|
|
178
198
|
}
|
|
179
199
|
export async function prepareSubscription(
|
|
180
200
|
component: Component,
|
|
@@ -194,8 +214,7 @@ export async function ensureManagedSubscription(
|
|
|
194
214
|
ctx: RunCtx,
|
|
195
215
|
input: UsageSubscriptionOptions,
|
|
196
216
|
) {
|
|
197
|
-
const terms = subscriptionTerms(input)
|
|
198
|
-
startedAt = Date.now();
|
|
217
|
+
const terms = subscriptionTerms(input);
|
|
199
218
|
return subscriptionCall(async () => {
|
|
200
219
|
if (terms.collection.method === "checkout")
|
|
201
220
|
throw new UsageSubscriptionError(
|
|
@@ -204,6 +223,11 @@ export async function ensureManagedSubscription(
|
|
|
204
223
|
);
|
|
205
224
|
await verifySubscriptionRealm(stripe, terms);
|
|
206
225
|
const row = await prepareSubscription(component, ctx, terms, "direct");
|
|
226
|
+
const startedAt = await beginSubscriptionObservation(
|
|
227
|
+
component,
|
|
228
|
+
ctx,
|
|
229
|
+
row.key,
|
|
230
|
+
);
|
|
207
231
|
if (!row.providerId) await verifySubscriptionPrices(stripe, terms);
|
|
208
232
|
let sub = row.providerId
|
|
209
233
|
? await stripe.subscriptions.retrieve(row.providerId)
|