@voltro/plugin-economy 0.0.0-stage → 0.88.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.
Files changed (177) hide show
  1. package/CHANGELOG.md +24492 -0
  2. package/LICENSE +61 -0
  3. package/README.md +2719 -2
  4. package/SECURITY.md +56 -0
  5. package/THIRD-PARTY-NOTICES.md +9841 -0
  6. package/dist/amount.js +20 -0
  7. package/dist/appleIap.server.js +262 -0
  8. package/dist/appleIapNotifications.server.js +162 -0
  9. package/dist/appleIapTrust.server.js +30 -0
  10. package/dist/bundleValue.server.js +112 -0
  11. package/dist/canonicalDataJson.js +10 -0
  12. package/dist/capture.js +53 -0
  13. package/dist/codePort.fixture.js +123 -0
  14. package/dist/codeStore.server.js +439 -0
  15. package/dist/codeTables.entity.js +43 -0
  16. package/dist/codes.d.ts +150 -0
  17. package/dist/codes.js +82 -0
  18. package/dist/components.d.ts +397 -0
  19. package/dist/components.js +283 -0
  20. package/dist/consistency.server.js +115 -0
  21. package/dist/containerStore.server.js +687 -0
  22. package/dist/containerStore.testFixtures.js +75 -0
  23. package/dist/containerTables.entity.js +45 -0
  24. package/dist/containers.d.ts +311 -0
  25. package/dist/containers.fixture.js +128 -0
  26. package/dist/containers.js +214 -0
  27. package/dist/containers.server.js +39 -0
  28. package/dist/containersPort.fixture.js +74 -0
  29. package/dist/currency.js +32 -0
  30. package/dist/declarationFixtures.js +61 -0
  31. package/dist/economy-app/events/economy.refundReversed.event.ts +1 -0
  32. package/dist/economy-app/events/economy.refunded.event.ts +1 -0
  33. package/dist/economyPort.fixture.js +124 -0
  34. package/dist/errors.d.ts +14 -0
  35. package/dist/errors.js +21 -0
  36. package/dist/events.d.ts +46 -0
  37. package/dist/events.js +1 -0
  38. package/dist/googleIap.server.js +391 -0
  39. package/dist/googleIapNotifications.server.js +130 -0
  40. package/dist/googleIapSubscription.server.js +183 -0
  41. package/dist/iap.d.ts +309 -0
  42. package/dist/iap.js +303 -0
  43. package/dist/iap.testFixtures.js +144 -0
  44. package/dist/iapAccountTables.entity.js +39 -0
  45. package/dist/iapAccounts.server.js +200 -0
  46. package/dist/iapBinding.server.js +523 -0
  47. package/dist/iapCheckout.server.js +97 -0
  48. package/dist/iapCompletion.server.js +55 -0
  49. package/dist/iapCurrentState.server.js +107 -0
  50. package/dist/iapCurrentState.testFixtures.js +22 -0
  51. package/dist/iapCurrentStatePort.fixture.js +63 -0
  52. package/dist/iapEntitlements.d.ts +79 -0
  53. package/dist/iapEntitlements.js +57 -0
  54. package/dist/iapEntitlements.server.js +48 -0
  55. package/dist/iapEntitlementsBinding.server.js +105 -0
  56. package/dist/iapEntitlementsPort.fixture.js +80 -0
  57. package/dist/iapInstalledEntitlements.testFixtures.js +75 -0
  58. package/dist/iapInstalledNotifications.testFixtures.js +93 -0
  59. package/dist/iapInventoryRefunds.server.js +261 -0
  60. package/dist/iapNotificationCustodyPort.fixture.js +67 -0
  61. package/dist/iapNotificationPort.fixture.js +73 -0
  62. package/dist/iapNotificationProcessor.server.js +134 -0
  63. package/dist/iapNotificationProcessor.testFixtures.js +63 -0
  64. package/dist/iapNotificationProcessorPort.fixture.js +55 -0
  65. package/dist/iapNotificationSchedule.server.js +69 -0
  66. package/dist/iapNotificationStore.server.js +352 -0
  67. package/dist/iapNotificationStore.testFixtures.js +31 -0
  68. package/dist/iapNotificationTables.entity.js +48 -0
  69. package/dist/iapNotificationTarget.server.js +36 -0
  70. package/dist/iapNotificationTarget.testFixtures.js +29 -0
  71. package/dist/iapNotifications.d.ts +644 -0
  72. package/dist/iapNotifications.js +6 -0
  73. package/dist/iapNotifications.server.js +95 -0
  74. package/dist/iapNotificationsBinding.server.js +232 -0
  75. package/dist/iapNotificationsSystemPort.fixture.js +50 -0
  76. package/dist/iapPlugin.testFixtures.js +59 -0
  77. package/dist/iapPort.fixture.js +74 -0
  78. package/dist/iapProviderCapture.server.js +17 -0
  79. package/dist/iapRecovery.d.ts +146 -0
  80. package/dist/iapRecovery.js +89 -0
  81. package/dist/iapRefundEventDelivery.server.js +82 -0
  82. package/dist/iapRefundEvents.js +44 -0
  83. package/dist/iapRefundEventsPort.fixture.js +59 -0
  84. package/dist/iapRefundPort.fixture.js +54 -0
  85. package/dist/iapRefundTables.entity.js +55 -0
  86. package/dist/iapRefunds.d.ts +755 -0
  87. package/dist/iapRefunds.js +6 -0
  88. package/dist/iapRefunds.server.js +553 -0
  89. package/dist/iapRefunds.testFixtures.js +64 -0
  90. package/dist/iapSpendAdmission.server.js +98 -0
  91. package/dist/iapStore.server.js +740 -0
  92. package/dist/iapStore.testFixtures.js +127 -0
  93. package/dist/iapTables.entity.js +80 -0
  94. package/dist/iapVerification.server.js +120 -0
  95. package/dist/index.d.ts +2984 -0
  96. package/dist/index.js +15 -0
  97. package/dist/intentionRecovery.js +133 -0
  98. package/dist/ledger.js +77 -0
  99. package/dist/loyalty.d.ts +274 -0
  100. package/dist/loyalty.fixture.js +92 -0
  101. package/dist/loyalty.js +311 -0
  102. package/dist/loyaltyBinding.server.js +203 -0
  103. package/dist/loyaltyPort.fixture.js +86 -0
  104. package/dist/loyaltyShopSource.server.js +90 -0
  105. package/dist/loyaltyStore.server.js +552 -0
  106. package/dist/loyaltyStore.testFixtures.js +82 -0
  107. package/dist/loyaltyTables.entity.js +49 -0
  108. package/dist/offerMerchandising.fixture.js +114 -0
  109. package/dist/offerMerchandising.js +282 -0
  110. package/dist/offerPolicies.d.ts +441 -0
  111. package/dist/offerPolicies.fixture.js +53 -0
  112. package/dist/offerPolicies.js +267 -0
  113. package/dist/piggyBanks.d.ts +272 -0
  114. package/dist/piggyBanks.fixture.js +70 -0
  115. package/dist/piggyBanks.js +200 -0
  116. package/dist/piggyBinding.server.js +204 -0
  117. package/dist/piggyPort.fixture.js +72 -0
  118. package/dist/piggyStore.server.js +610 -0
  119. package/dist/piggyStore.testFixtures.js +97 -0
  120. package/dist/piggyTables.entity.js +40 -0
  121. package/dist/plugin.js +1047 -0
  122. package/dist/port.js +12 -0
  123. package/dist/price.d.ts +99 -0
  124. package/dist/price.js +143 -0
  125. package/dist/purchaseRecovery.js +55 -0
  126. package/dist/reports.server.js +114 -0
  127. package/dist/rpc.d.ts +3201 -0
  128. package/dist/rpc.js +1200 -0
  129. package/dist/sanctionsTradeAuthority.server.js +79 -0
  130. package/dist/server.d.ts +3856 -0
  131. package/dist/server.js +43 -0
  132. package/dist/shop.d.ts +279 -0
  133. package/dist/shop.js +195 -0
  134. package/dist/shop.testFixtures.js +113 -0
  135. package/dist/shopPort.fixture.js +71 -0
  136. package/dist/shopPresentation.js +67 -0
  137. package/dist/shopStore.server.js +623 -0
  138. package/dist/shopTables.entity.js +62 -0
  139. package/dist/shopUiCopy.js +56 -0
  140. package/dist/spendLimits.d.ts +140 -0
  141. package/dist/spendLimits.fixture.js +52 -0
  142. package/dist/spendLimits.js +154 -0
  143. package/dist/spendLimitsPort.fixture.js +119 -0
  144. package/dist/subscriptionBinding.server.js +210 -0
  145. package/dist/subscriptionStore.server.js +642 -0
  146. package/dist/subscriptionStore.testFixtures.js +71 -0
  147. package/dist/subscriptionTables.entity.js +36 -0
  148. package/dist/subscriptions.d.ts +394 -0
  149. package/dist/subscriptions.fixture.js +96 -0
  150. package/dist/subscriptions.js +212 -0
  151. package/dist/subscriptionsPort.fixture.js +75 -0
  152. package/dist/systemFixtures.js +88 -0
  153. package/dist/tables.entity.js +82 -0
  154. package/dist/testing.d.ts +4615 -0
  155. package/dist/testing.js +29 -0
  156. package/dist/tradeAuthority.server.js +171 -0
  157. package/dist/tradeBinding.server.js +200 -0
  158. package/dist/tradeComponents.js +341 -0
  159. package/dist/tradeEscrow.server.js +231 -0
  160. package/dist/tradeEscrow.testFixtures.js +132 -0
  161. package/dist/tradeExpiry.server.js +42 -0
  162. package/dist/tradeRecovery.js +47 -0
  163. package/dist/tradeStore.server.js +749 -0
  164. package/dist/tradeStore.testFixtures.js +72 -0
  165. package/dist/tradeTables.entity.js +65 -0
  166. package/dist/tradeUiCopy.js +78 -0
  167. package/dist/trades.d.ts +246 -0
  168. package/dist/trades.fixture.js +102 -0
  169. package/dist/trades.js +288 -0
  170. package/dist/tradesPort.fixture.js +93 -0
  171. package/dist/walletEscrow.js +105 -0
  172. package/dist/walletEscrow.server.js +375 -0
  173. package/dist/walletKeys.server.js +4 -0
  174. package/dist/walletStore.js +570 -0
  175. package/dist/web.d.ts +2257 -0
  176. package/dist/web.js +8 -0
  177. package/package.json +161 -4
@@ -0,0 +1,93 @@
1
+ import { economyError as e } from "./errors.js";
2
+ import { iapNotificationTables as t } from "./iapNotificationTables.entity.js";
3
+ import { iapCurrentStatePort as n } from "./iapCurrentState.server.js";
4
+ import { iapRefundTables as r } from "./iapRefundTables.entity.js";
5
+ import { economyPlugin as i } from "./plugin.js";
6
+ import { iapFixtureLifecycle as a, iapPluginFixture as o } from "./iapPlugin.testFixtures.js";
7
+ import { iapSqlNoticeFixture as s } from "./iapNotificationTarget.testFixtures.js";
8
+ import { inventoryIapRefundAssets as c } from "./iapInventoryRefunds.server.js";
9
+ import { Effect as l } from "effect";
10
+ import { randomBytes as u } from "node:crypto";
11
+ import { ensureTableRegistered as d } from "@voltro/database";
12
+ import { makeFieldCipher as f } from "@voltro/runtime";
13
+ // src/iapInstalledNotifications.testFixtures.ts
14
+ var p = async (p, m = "test.installed.notices", h) => {
15
+ let g = await o(p, void 0, m), _ = g.base;
16
+ await l.runPromise(g.close), [...t, ...r].forEach(d);
17
+ let v = s(_), y = u(24).toString("base64url"), b = f(u(32).toString("base64url")), x = _.at + 1, S = 1, C = 0, w = 0, T = !1, E, D = {
18
+ ...g.options.providers[0],
19
+ createNotifications: () => ({
20
+ initialize: () => l.sync(() => {
21
+ C++;
22
+ }),
23
+ verify: (t) => t.body === y ? l.succeed(v.evidence) : l.fail(e("forbidden"))
24
+ }),
25
+ createCurrentState: (t) => n({
26
+ realMoney: !0,
27
+ channel: "ios",
28
+ environment: t.environment,
29
+ applicationId: t.applicationId,
30
+ accounts: t.accounts,
31
+ now: t.now
32
+ }, {
33
+ initialize: () => l.sync(() => {
34
+ C++;
35
+ }),
36
+ read: (t) => l.suspend(() => (w++, T ? l.fail(e("unavailable")) : (E ? E() : l.void).pipe(l.map(() => ({
37
+ orderId: t.orderId,
38
+ channel: "ios",
39
+ state: S === 0 ? "active" : "revoked",
40
+ refundNumerator: S,
41
+ refundDenominator: 1,
42
+ observedAt: t.at,
43
+ revokedAt: null
44
+ })))))
45
+ })
46
+ }, O = {
47
+ ...g.options,
48
+ now: () => x,
49
+ providers: [D],
50
+ notifications: {
51
+ version: "notices.fixture.v1",
52
+ bodyRetentionMs: 1e3,
53
+ debtPolicy: "blockSpend",
54
+ assets: c(p, {
55
+ inventory: _.inventory,
56
+ disposition: "requireAll"
57
+ }),
58
+ assetsVersion: "items.fixture.v1",
59
+ cipher: { read: () => b },
60
+ ...h ? { onChange: h } : {}
61
+ }
62
+ }, k = i({
63
+ currencies: _.currencies,
64
+ realMoney: !0,
65
+ now: () => x,
66
+ iap: O
67
+ });
68
+ return await k.bindDataStore?.(p), {
69
+ base: _,
70
+ store: p,
71
+ plugin: k,
72
+ options: O,
73
+ body: y,
74
+ notice: v,
75
+ activate: () => k.onActivate(a),
76
+ close: () => k.onDeactivate(a),
77
+ ready: () => C,
78
+ reads: () => w,
79
+ failProvider: (e) => {
80
+ T = e;
81
+ },
82
+ beforeRead: (e) => {
83
+ E = e;
84
+ },
85
+ advance: (e) => {
86
+ x += e;
87
+ },
88
+ currentRefund: (e) => {
89
+ S = e, x++;
90
+ }
91
+ };
92
+ };
93
+ export { p as iapInstalledNotificationsFixture };
@@ -0,0 +1,261 @@
1
+ import { EconomyError as e, economyError as t } from "./errors.js";
2
+ import { captureBoundMethods as n, dataList as r, dataObject as i, identifier as a } from "./capture.js";
3
+ import { shopInteger as o } from "./shop.js";
4
+ import { canonicalDataJson as s } from "./canonicalDataJson.js";
5
+ import { Effect as c, Result as l } from "effect";
6
+ import { systemSubject as u } from "@voltro/protocol";
7
+ import { createHash as d } from "node:crypto";
8
+ import { and as f, eq as p, queryFor as m } from "@voltro/database";
9
+ import { prepareSqlMutation as h, prepareSqlMutationGroup as g } from "@voltro/database/sql";
10
+ import { inventoryItemsTable as _, inventoryLedgerTable as v, inventoryOperationsTable as y, inventorySubjectsTable as b } from "@voltro/plugin-inventory/server";
11
+ // src/iapInventoryRefunds.server.ts
12
+ var x = (n) => n instanceof e ? n : t("unavailable"), S = async (e) => {
13
+ let n = await c.runPromise(e.pipe(c.result));
14
+ if (l.isFailure(n)) throw t(n.failure.code === "erased" ? "erased" : "unavailable");
15
+ return n.success;
16
+ }, C = (e) => d("sha256").update(s(JSON.parse(JSON.stringify(e)))).digest("hex"), w = (e) => {
17
+ let n = i(e, [
18
+ "operationId",
19
+ "kind",
20
+ "changes",
21
+ "at"
22
+ ], "invalid_record");
23
+ if (n.kind !== "consume" && n.kind !== "grant") throw t("invalid_record");
24
+ let s = Object.freeze(r(n.changes, 4096, "invalid_record").map((e) => {
25
+ let r = i(e, ["instanceId", "delta"], "invalid_record"), s = o(r.delta, -1e6, 1e6);
26
+ if (s === 0 || n.kind === "consume" && s > 0 || n.kind === "grant" && s < 0) throw t("invalid_record");
27
+ return Object.freeze({
28
+ instanceId: a(r.instanceId),
29
+ delta: s
30
+ });
31
+ }));
32
+ if (new Set(s.map((e) => e.instanceId)).size !== s.length) throw t("invalid_record");
33
+ return Object.freeze({
34
+ operationId: a(n.operationId),
35
+ kind: n.kind,
36
+ changes: s,
37
+ at: o(n.at, 0, 864e13)
38
+ });
39
+ }, T = (e) => {
40
+ let n = i(e, [
41
+ "disposition",
42
+ "items",
43
+ "operations",
44
+ "targets"
45
+ ], "invalid_record");
46
+ if (n.disposition !== "requireAll" && n.disposition !== "recoverRemaining") throw t("invalid_record");
47
+ let s = Object.freeze(r(n.items, 4096, "invalid_record").map((e) => {
48
+ let s = i(e, [
49
+ "grantId",
50
+ "instanceId",
51
+ "itemId",
52
+ "original",
53
+ "requested",
54
+ "recovered",
55
+ "unrecoverable",
56
+ "candidates"
57
+ ], "invalid_record"), c = o(s.original, 1, 1e6), l = o(s.requested, 0, c), u = o(s.recovered, 0, l), d = o(s.unrecoverable, 0, l);
58
+ if (u + d !== l || n.disposition === "requireAll" && d !== 0) throw t("invalid_record");
59
+ let f = Object.freeze(r(s.candidates, 4096, "invalid_record").map((e) => a(e)));
60
+ if (!f.length || new Set(f).size !== f.length) throw t("invalid_record");
61
+ return Object.freeze({
62
+ candidates: f,
63
+ grantId: a(s.grantId),
64
+ instanceId: a(s.instanceId),
65
+ itemId: a(s.itemId),
66
+ original: c,
67
+ requested: l,
68
+ recovered: u,
69
+ unrecoverable: d
70
+ });
71
+ }));
72
+ if (new Set(s.map((e) => e.grantId + ":" + e.instanceId)).size !== s.length) throw t("invalid_record");
73
+ let c = Object.freeze(r(n.operations, 4096, "invalid_record").map(w));
74
+ if (new Set(c.map((e) => e.operationId)).size !== c.length) throw t("invalid_record");
75
+ let l = Object.freeze(r(n.targets, 4096, "invalid_record").map((e) => o(e, 0, s.length - 1)));
76
+ if (l.length !== c.length) throw t("invalid_record");
77
+ return Object.freeze({
78
+ disposition: n.disposition,
79
+ items: s,
80
+ operations: c,
81
+ targets: l
82
+ });
83
+ }, E = (e, r) => {
84
+ let l = i(r, ["inventory", "disposition"]), E = n(l.inventory, ["prepareConsume", "prepareGrant"]);
85
+ if (l.disposition !== "requireAll" && l.disposition !== "recoverRemaining") throw t("invalid_input");
86
+ let D = l.disposition, O = u("economy.iap-item-refunds"), k = async (e, n, r = !0) => {
87
+ let { original: i } = n, a = i.scope, o = (await e.query(m(b).where(f(p("tenantId", a.tenantId), p("ownerKind", "player"), p("ownerId", a.ownerId))).limit(2).descriptor))[0];
88
+ if (!o) throw t("erased");
89
+ if (r && await e.updateMany(b.tableName, { admission: 0 }, { where: p("id", o.id) }) !== 1) throw t("unavailable");
90
+ let c = (await e.query(m(b).where(p("id", o.id)).limit(1).descriptor))[0];
91
+ if (!c || c.erased !== !1 || c.ownerId !== a.ownerId || c.tenantId !== a.tenantId || c.ownerKind !== "player") throw t("erased");
92
+ let l = [];
93
+ for (let n of i.receipt.grants) {
94
+ let r = (await e.query(m(y).where(p("id", n.operationId)).limit(1).descriptor))[0];
95
+ if (!r || r.tenantId !== a.tenantId || r.ownerKey !== o.id || r.state !== "committed" || s(r.receipt) !== s(n)) throw t("invalid_record");
96
+ let c = await e.query(m(v).where(f(p("tenantId", a.tenantId), p("operationId", n.operationId))).limit(n.changes.length + 1).descriptor);
97
+ if (c.length !== n.changes.length) throw t("invalid_record");
98
+ for (let r of n.changes) {
99
+ let o = c.find((e) => e.instanceId === r.instanceId);
100
+ if (!o || o.ownerId !== a.ownerId || o.ownerKind !== "player" || o.delta !== r.delta || o.reason !== "iap.fulfill" || o.refKind !== "iap-order" || o.refId !== i.orderId || o.after === null) throw t("invalid_record");
101
+ let s = (await e.query(m(_).where(p("id", r.instanceId)).limit(1).descriptor))[0];
102
+ l.push({
103
+ grantId: n.operationId,
104
+ original: o,
105
+ current: s?.tenantId === a.tenantId && s.ownerId === a.ownerId && s.ownerKind === "player" ? s : null,
106
+ delta: r.delta
107
+ });
108
+ }
109
+ }
110
+ if (l.length > 4096) throw t("limit_exceeded");
111
+ return l;
112
+ }, A = (e, n) => {
113
+ let r = T(n), i = e.previous?.assets === null || !e.previous ? null : T(e.previous.assets);
114
+ if (r.disposition !== D || r.items.length !== e.original.receipt.grants.reduce((e, t) => e + t.changes.length, 0)) throw t("invalid_record");
115
+ let a = 0, o = 0;
116
+ for (let [n, c] of r.items.entries()) {
117
+ let l = e.original.receipt.grants.find((e) => e.operationId === c.grantId)?.changes.find((e) => e.instanceId === c.instanceId), u = e.original.receipt.grants.findIndex((e) => e.operationId === c.grantId);
118
+ if (e.original.product.offer.items[u]?.itemId !== c.itemId || !l || l.delta !== c.original || c.requested !== Number(BigInt(c.original) * BigInt(e.state.refundNumerator) / BigInt(e.state.refundDenominator))) throw t("invalid_record");
119
+ let d = i?.items.find((e) => e.grantId === c.grantId && e.instanceId === c.instanceId), f = new Set(d?.candidates ?? [c.instanceId]);
120
+ for (let [e, t] of r.operations.entries()) if (r.targets[e] === n && t.kind === "grant") for (let e of t.changes) f.add(e.instanceId);
121
+ if (s([...f].sort()) !== s([...c.candidates].sort())) throw t("invalid_record");
122
+ let p = c.recovered - (d?.recovered ?? 0);
123
+ if (p !== r.operations.reduce((e, t, i) => r.targets[i] === n ? e + t.changes.reduce((e, t) => e - t.delta, 0) : e, 0)) throw t("invalid_record");
124
+ p > 0 ? a += p : o -= p;
125
+ }
126
+ let c = r.operations.filter((e) => e.kind === "consume").reduce((e, t) => e + t.changes.reduce((e, t) => e - t.delta, 0), 0), l = r.operations.filter((e) => e.kind === "grant").reduce((e, t) => e + t.changes.reduce((e, t) => e + t.delta, 0), 0);
127
+ if (a !== c || o !== l) throw t("invalid_record");
128
+ return r;
129
+ };
130
+ return Object.freeze({
131
+ prepare: (n) => c.tryPromise({
132
+ catch: x,
133
+ try: async () => {
134
+ let r = n.previous?.assets === null || !n.previous ? null : T(n.previous.assets), c = async (e) => {
135
+ let t = await k(e, n), i = [];
136
+ for (let a of t) {
137
+ let t = r?.items.find((e) => e.grantId === a.grantId && e.instanceId === a.original.instanceId), o = [];
138
+ for (let r of t?.candidates ?? [String(a.original.instanceId)]) {
139
+ let t = (await e.query(m(_).where(p("id", r)).limit(1).descriptor))[0];
140
+ o.push(t?.tenantId === n.original.scope.tenantId && t.ownerId === n.original.scope.ownerId && t.ownerKind === "player" ? t : null);
141
+ }
142
+ i.push({
143
+ ...a,
144
+ positions: o
145
+ });
146
+ }
147
+ return i;
148
+ }, l = await e.transactional(c), u = [], f = [], v = [], y = h(e, async (e) => {
149
+ if (C(await c(e)) !== C(l)) throw t("conflict");
150
+ return null;
151
+ });
152
+ f.push(y);
153
+ for (let e of l) {
154
+ let c = e.grantId, l = a(e.original.instanceId), p = a(e.original.itemId), m = r?.items.find((e) => e.grantId === c && e.instanceId === l), h = Number(BigInt(e.delta) * BigInt(n.state.refundNumerator) / BigInt(n.state.refundDenominator)), g = h - (m?.requested ?? 0), _ = m?.recovered ?? 0, y = "ifa_" + d("sha256").update(s([
155
+ n.original.orderId,
156
+ n.sequence,
157
+ c,
158
+ l
159
+ ])).digest("hex").slice(0, 56), b = {
160
+ tenantId: n.original.scope.tenantId,
161
+ owner: {
162
+ ownerKind: "player",
163
+ ownerId: n.original.scope.ownerId
164
+ },
165
+ requestId: y,
166
+ ref: {
167
+ kind: "iap-order",
168
+ id: n.original.orderId
169
+ }
170
+ };
171
+ if (g > 0) {
172
+ let n = i(e.original.after, [
173
+ "id",
174
+ "itemId",
175
+ "schemaVersion",
176
+ "qty",
177
+ "attributes",
178
+ "acquiredAt",
179
+ "expiresAt",
180
+ "locked",
181
+ "source"
182
+ ], "invalid_record"), r = g;
183
+ for (let t of e.positions) {
184
+ if (!r) break;
185
+ let e = t && t.itemId === p && t.locked === !1 && s(t.attributes) === s(n.attributes) ? o(t.qty, 1, 1e6) : 0, i = Math.min(r, e);
186
+ i && (f.push(await S(E.prepareConsume(O, {
187
+ ...b,
188
+ requestId: b.requestId + "." + v.length,
189
+ instanceId: a(t.id),
190
+ qty: i,
191
+ reason: "iap.refund"
192
+ }))), v.push(u.length)), _ += i, r -= i;
193
+ }
194
+ if (D === "requireAll" && r) throw t("unavailable");
195
+ } else if (g < 0) {
196
+ let t = Math.max(0, _ - h);
197
+ if (t) {
198
+ let n = i(e.original.after, [
199
+ "id",
200
+ "itemId",
201
+ "schemaVersion",
202
+ "qty",
203
+ "attributes",
204
+ "acquiredAt",
205
+ "expiresAt",
206
+ "locked",
207
+ "source"
208
+ ], "invalid_record");
209
+ f.push(await S(E.prepareGrant(O, {
210
+ ...b,
211
+ itemId: p,
212
+ qty: t,
213
+ attributes: n.attributes,
214
+ reason: "iap.refund-reversed"
215
+ }))), v.push(u.length);
216
+ }
217
+ _ -= t;
218
+ }
219
+ u.push(Object.freeze({
220
+ candidates: m?.candidates ?? [l],
221
+ grantId: c,
222
+ instanceId: l,
223
+ itemId: p,
224
+ original: e.delta,
225
+ requested: h,
226
+ recovered: _,
227
+ unrecoverable: h - _
228
+ }));
229
+ }
230
+ return g(e, f, async (e, t) => {
231
+ let r = t.slice(1).map(w), i = u.map((e, t) => ({
232
+ ...e,
233
+ candidates: [.../* @__PURE__ */ new Set([...e.candidates, ...r.flatMap((e, n) => e.kind === "grant" && v[n] === t ? e.changes.map((e) => e.instanceId) : [])])]
234
+ }));
235
+ return A(n, {
236
+ disposition: D,
237
+ items: i,
238
+ operations: r,
239
+ targets: v
240
+ });
241
+ });
242
+ }
243
+ }),
244
+ validate: A,
245
+ audit: async (e, n, r) => {
246
+ let i = A(n, r), a = n.original.scope;
247
+ await k(e, n, !1);
248
+ for (let [r, o] of i.operations.entries()) {
249
+ let c = (await e.query(m(y).where(p("id", o.operationId)).limit(1).descriptor))[0];
250
+ if (!c || c.state !== "committed" || c.tenantId !== a.tenantId || s(c.receipt) !== s(o)) throw t("invalid_record");
251
+ let l = await e.query(m(v).where(f(p("tenantId", a.tenantId), p("operationId", o.operationId))).limit(o.changes.length + 1).descriptor);
252
+ if (l.length !== o.changes.length) throw t("invalid_record");
253
+ for (let e of o.changes) {
254
+ let s = l.find((t) => t.instanceId === e.instanceId);
255
+ if (!s || s.itemId !== i.items[i.targets[r]].itemId || s.delta !== e.delta || s.ownerId !== a.ownerId || s.ownerKind !== "player" || s.reason !== (o.kind === "consume" ? "iap.refund" : "iap.refund-reversed") || s.refKind !== "iap-order" || s.refId !== n.original.orderId) throw t("invalid_record");
256
+ }
257
+ }
258
+ }
259
+ });
260
+ };
261
+ export { E as inventoryIapRefundAssets };
@@ -0,0 +1,67 @@
1
+ import { economyError as e } from "./errors.js";
2
+ import { iapNotificationNotice as t } from "./iapNotifications.server.js";
3
+ import { IapNotificationCustodyPort as n } from "./iapNotificationStore.server.js";
4
+ import { iapNotificationStoreFixture as r } from "./iapNotificationStore.testFixtures.js";
5
+ import { Effect as i, Layer as a } from "effect";
6
+ import { randomBytes as o } from "node:crypto";
7
+ import { InMemoryDataStore as s, makeFieldCipher as c } from "@voltro/runtime";
8
+ // src/iapNotificationCustodyPort.fixture.ts
9
+ var l = /* @__PURE__ */ new WeakMap(), u = i.tryPromise({
10
+ try: async () => {
11
+ let e = c(o(32).toString("base64url")), t = { value: !0 };
12
+ return {
13
+ ...await r(new s(), "fixture.notice", { cipher: { read: () => t.value ? e : void 0 } }),
14
+ alive: t
15
+ };
16
+ },
17
+ catch: () => e("unavailable")
18
+ }).pipe(i.map((t) => {
19
+ let n = !0, r = (t) => i.suspend(() => n ? t : i.fail(e("unavailable"))), a = Object.freeze({
20
+ initialize: () => r(t.custody.initialize()),
21
+ retain: (e) => r(t.custody.retain(e)),
22
+ restore: (e) => r(t.custody.restore(e)),
23
+ markProcessed: (e, n) => r(t.custody.markProcessed(e, n)),
24
+ pending: () => r(t.custody.pending()),
25
+ prune: () => r(t.custody.prune()),
26
+ exportSubject: (e) => r(t.custody.exportSubject(e)),
27
+ eraseSubject: (e) => r(t.custody.eraseSubject(e))
28
+ });
29
+ return l.set(a, {
30
+ evidence: t.n.evidence,
31
+ orderId: t.original.orderId
32
+ }), Object.freeze({
33
+ service: a,
34
+ evidence: t.n.evidence,
35
+ privacy: t.privacy,
36
+ close: i.sync(() => {
37
+ n = !1, t.alive.value = !1;
38
+ })
39
+ });
40
+ })), d = a.effect(n, i.acquireRelease(u, (e) => e.close).pipe(i.map((e) => e.service))), f = Object.freeze({
41
+ kind: "port",
42
+ tag: n,
43
+ fakeLayer: d,
44
+ probe: {
45
+ run: () => i.scoped(i.gen(function* () {
46
+ let r = yield* n, a = l.get(r);
47
+ if (!a) return yield* i.fail(e("unavailable"));
48
+ yield* r.initialize();
49
+ let o = yield* r.retain(a.evidence), s = yield* r.retain(a.evidence), c = yield* r.restore(o.id), u = yield* r.pending();
50
+ return {
51
+ state: o.state,
52
+ actualOrder: o.orderId === a.orderId,
53
+ stableIdentity: o.id === s.id,
54
+ restoredChannel: t(c).channel,
55
+ pending: u.length
56
+ };
57
+ }).pipe(i.provide(d))),
58
+ expected: {
59
+ state: "pending",
60
+ actualOrder: !0,
61
+ stableIdentity: !0,
62
+ restoredChannel: "ios",
63
+ pending: 1
64
+ }
65
+ }
66
+ });
67
+ export { d as iapNotificationCustodyFakeLayer, u as iapNotificationCustodyPortFixture, f as iapNotificationCustodyPortSystemFixture };
@@ -0,0 +1,73 @@
1
+ import { EconomyError as e, economyError as t } from "./errors.js";
2
+ import { dataObject as n } from "./capture.js";
3
+ import { IapNotificationPort as r, captureNotificationInput as i, iapNotificationNotice as a, issueVerifiedIapNotification as o } from "./iapNotifications.server.js";
4
+ import { Effect as s, Layer as c } from "effect";
5
+ import { randomUUID as l } from "node:crypto";
6
+ // src/iapNotificationPort.fixture.ts
7
+ var u = s.sync(() => {
8
+ let r = l(), a = Date.now(), c = !1, u = !1, d = Object.freeze({
9
+ initialize: () => s.suspend(() => u ? s.fail(t("unavailable")) : s.sync(() => {
10
+ c = !0;
11
+ })),
12
+ verify: (l) => {
13
+ let d;
14
+ try {
15
+ d = i(l, 2048);
16
+ } catch (n) {
17
+ return s.fail(n instanceof e ? n : t("invalid_input"));
18
+ }
19
+ return s.try({
20
+ try: () => {
21
+ if (!c || u) throw t("unavailable");
22
+ if (n(JSON.parse(d.body), ["event"], "invalid_record").event !== "fixture") throw t("forbidden");
23
+ return o({
24
+ channel: "ios",
25
+ applicationId: "fixture.application",
26
+ environment: "sandbox",
27
+ noticeId: r,
28
+ sourceType: "fixture.notification",
29
+ kind: "test",
30
+ occurredAt: a,
31
+ transactionId: null,
32
+ originalTransactionId: null,
33
+ storeProductId: null
34
+ }, {});
35
+ },
36
+ catch: (n) => n instanceof e ? n : t("invalid_record")
37
+ });
38
+ }
39
+ });
40
+ return Object.freeze({
41
+ service: d,
42
+ close: s.sync(() => {
43
+ c = !1, u = !0;
44
+ })
45
+ });
46
+ }).pipe(s.tap((e) => e.service.initialize())), d = c.effect(r, s.acquireRelease(u, (e) => e.close).pipe(s.map((e) => e.service))), f = Object.freeze({
47
+ kind: "port",
48
+ tag: r,
49
+ fakeLayer: d,
50
+ probe: {
51
+ run: () => s.scoped(s.gen(function* () {
52
+ let e = yield* (yield* r).verify({ body: JSON.stringify({ event: "fixture" }) }), t = a(e), n = !1;
53
+ try {
54
+ a({ ...e });
55
+ } catch {
56
+ n = !0;
57
+ }
58
+ return {
59
+ kind: t.kind,
60
+ channel: t.channel,
61
+ copiedRefused: n,
62
+ noPurchaseAuthority: t.transactionId === null
63
+ };
64
+ }).pipe(s.provide(d))),
65
+ expected: {
66
+ kind: "test",
67
+ channel: "ios",
68
+ copiedRefused: !0,
69
+ noPurchaseAuthority: !0
70
+ }
71
+ }
72
+ });
73
+ export { d as iapNotificationFakeLayer, u as iapNotificationPortFixture, f as iapNotificationPortSystemFixture };
@@ -0,0 +1,134 @@
1
+ import { EconomyError as e, economyError as t } from "./errors.js";
2
+ import { captureBoundMethods as n, dataObject as r, identifier as i } from "./capture.js";
3
+ import { shopInteger as a } from "./shop.js";
4
+ import { captureIapNotificationService as o, readVerifiedIapNotification as s } from "./iapNotifications.server.js";
5
+ import { captureIapNotificationTargetOptions as c, readAcceptedIapNotificationTarget as l } from "./iapNotificationTarget.server.js";
6
+ import { captureIapCurrentStateService as u } from "./iapCurrentState.server.js";
7
+ import { captureIapRefundService as d } from "./iapRefunds.server.js";
8
+ import { captureIapNotificationCustodyService as f } from "./iapNotificationStore.server.js";
9
+ import { Context as p, Effect as m, Result as h } from "effect";
10
+ import { TextDecoder as g } from "node:util";
11
+ // src/iapNotificationProcessor.server.ts
12
+ var _ = class extends p.Service()("@voltro/IapNotificationProcessorPort") {}, v = (e) => n(e, [
13
+ "initialize",
14
+ "ingest",
15
+ "process",
16
+ "pending",
17
+ "prune",
18
+ "exportSubject",
19
+ "eraseSubject"
20
+ ]), y = (e) => {
21
+ let a = r(e, [
22
+ "deployment",
23
+ "notifications",
24
+ "current",
25
+ "custody",
26
+ "refunds",
27
+ "orders",
28
+ "changed"
29
+ ]);
30
+ if (a.changed !== void 0 && typeof a.changed != "function") throw t("invalid_input");
31
+ let p = a.changed, h = c(a.deployment), g = o(a.notifications), _ = u(a.current), v = f(a.custody), y = d(a.refunds), b = n(a.orders, ["prepareNotification"]), x = (e) => p === void 0 ? m.succeed(null) : l(e).pipe(m.map((e) => ({
32
+ tenantId: e.scope.tenantId,
33
+ ownerId: e.scope.ownerId,
34
+ offerId: e.receipt.offerId,
35
+ offerVersion: e.receipt.offerVersion
36
+ }))), S = (e, t) => p === void 0 || e === null ? m.void : m.suspend(() => p(Object.freeze({
37
+ ...e,
38
+ orderId: t.orderId,
39
+ state: t.state
40
+ }))).pipe(m.catchCause(() => m.void)), C = (e) => m.try({
41
+ try: () => i(e),
42
+ catch: () => t("invalid_input")
43
+ }).pipe(m.flatMap((e) => v.restore(e).pipe(m.flatMap((e) => b.prepareNotification(h, e)), m.flatMap((t) => x(t).pipe(m.flatMap((n) => _.read(t).pipe(m.flatMap((e) => y.reconcile(e)), m.flatMap((t) => v.markProcessed(e, t).pipe(m.tap(() => S(n, t)))))))))), m.catchDefect(() => m.fail(t("unavailable"))));
44
+ return Object.freeze({
45
+ initialize: () => m.gen(function* () {
46
+ yield* v.initialize(), yield* g.initialize(), yield* _.initialize();
47
+ }).pipe(m.catchDefect(() => m.fail(t("unavailable")))),
48
+ ingest: (e) => g.verify(e).pipe(m.flatMap((e) => s(e).notice.kind === "test" ? m.succeed(null) : v.retain(e).pipe(m.flatMap((e) => e.state === "processed" ? m.succeed(e) : e.state === "pending" ? C(e.id) : m.fail(t(e.state === "erased" ? "erased" : "unavailable"))))), m.catchDefect(() => m.fail(t("unavailable")))),
49
+ process: C,
50
+ pending: v.pending,
51
+ prune: v.prune,
52
+ exportSubject: (e) => m.gen(function* () {
53
+ return {
54
+ notifications: yield* v.exportSubject(e),
55
+ refunds: yield* y.exportSubject(e)
56
+ };
57
+ }),
58
+ eraseSubject: (e) => v.eraseSubject(e).pipe(m.flatMap(() => y.eraseSubject(e)))
59
+ });
60
+ }, b = (n) => {
61
+ let i = r(n, [
62
+ "path",
63
+ "maxBodyBytes",
64
+ "processor",
65
+ "deliver"
66
+ ]);
67
+ if (typeof i.path != "string" || !/^\/(?:[A-Za-z0-9_-]+\/)*[A-Za-z0-9_-]+$/.test(i.path) || i.path.length > 240) throw t("invalid_input");
68
+ let o = i.path, s = a(i.maxBodyBytes, 1024, 1048576), c = v(i.processor);
69
+ if (i.deliver !== void 0 && typeof i.deliver != "function") throw t("invalid_input");
70
+ let l = i.deliver;
71
+ return Object.freeze({
72
+ path: o,
73
+ method: "POST",
74
+ match: "exact",
75
+ answersOtherMethods: !0,
76
+ originGuard: "exempt",
77
+ maxBodyBytes: s,
78
+ handle: async (n) => {
79
+ let r = {
80
+ "cache-control": "no-store",
81
+ "x-content-type-options": "nosniff"
82
+ };
83
+ if (n.path !== o) return {
84
+ status: 404,
85
+ headers: r
86
+ };
87
+ if (n.method !== "POST") return {
88
+ status: 405,
89
+ headers: {
90
+ ...r,
91
+ allow: "POST"
92
+ }
93
+ };
94
+ if (n.rawBody.byteLength > s) return {
95
+ status: 413,
96
+ headers: r
97
+ };
98
+ if (n.query || n.headers["content-encoding"] && n.headers["content-encoding"] !== "identity") return {
99
+ status: 400,
100
+ headers: r
101
+ };
102
+ let i;
103
+ try {
104
+ i = new g("utf-8", { fatal: !0 }).decode(n.rawBody);
105
+ } catch {
106
+ return {
107
+ status: 400,
108
+ headers: r
109
+ };
110
+ }
111
+ if (!i.length) return {
112
+ status: 400,
113
+ headers: r
114
+ };
115
+ let a = {
116
+ body: i,
117
+ ...n.headers.authorization === void 0 ? {} : { authorization: n.headers.authorization },
118
+ headers: Object.fromEntries(Object.entries(n.headers).filter((e) => e[0] !== "authorization" && typeof e[1] == "string").slice(0, 64))
119
+ }, u = (l ? l(a) : m.succeed(!1)).pipe(m.flatMap((e) => e ? m.void : m.asVoid(c.ingest(a)))), d = await m.runPromise(u.pipe(m.result)).catch(() => h.fail(t("unavailable")));
120
+ if (h.isSuccess(d)) return {
121
+ status: 204,
122
+ headers: r
123
+ };
124
+ let f = d.failure;
125
+ return {
126
+ status: f instanceof e && f.code === "forbidden" ? 401 : f instanceof e && f.code === "limit_exceeded" ? 413 : f instanceof e && f.code === "invalid_input" ? 400 : 503,
127
+ headers: r,
128
+ body: "IAP notification refused",
129
+ contentType: "text/plain; charset=utf-8"
130
+ };
131
+ }
132
+ });
133
+ };
134
+ export { _ as IapNotificationProcessorPort, v as captureIapNotificationProcessorService, b as iapNotificationHttpRoute, y as iapNotificationProcessor };