@wopr-network/platform-core 1.14.8 → 1.16.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/dist/account/deletion-executor-repository.d.ts +2 -2
- package/dist/account/deletion-executor-repository.js +5 -5
- package/dist/{monetization/payram → billing/crypto}/cents-credits-boundary.test.js +14 -17
- package/dist/billing/crypto/charge-store.d.ts +68 -0
- package/dist/billing/crypto/charge-store.js +109 -0
- package/dist/billing/crypto/charge-store.test.js +120 -0
- package/dist/billing/crypto/checkout.d.ts +18 -0
- package/dist/billing/crypto/checkout.js +35 -0
- package/dist/billing/crypto/checkout.test.js +71 -0
- package/dist/billing/crypto/client.d.ts +39 -0
- package/dist/billing/crypto/client.js +72 -0
- package/dist/billing/crypto/client.test.js +100 -0
- package/dist/billing/crypto/evm/__tests__/address-gen.test.js +54 -0
- package/dist/billing/crypto/evm/__tests__/checkout.test.js +54 -0
- package/dist/billing/crypto/evm/__tests__/config.test.js +52 -0
- package/dist/billing/crypto/evm/__tests__/settler.test.js +196 -0
- package/dist/billing/crypto/evm/__tests__/watcher.test.js +109 -0
- package/dist/billing/crypto/evm/address-gen.d.ts +8 -0
- package/dist/billing/crypto/evm/address-gen.js +29 -0
- package/dist/billing/crypto/evm/checkout.d.ts +26 -0
- package/dist/billing/crypto/evm/checkout.js +57 -0
- package/dist/billing/crypto/evm/config.d.ts +13 -0
- package/dist/billing/crypto/evm/config.js +46 -0
- package/dist/billing/crypto/evm/index.d.ts +9 -0
- package/dist/billing/crypto/evm/index.js +5 -0
- package/dist/billing/crypto/evm/settler.d.ts +23 -0
- package/dist/billing/crypto/evm/settler.js +60 -0
- package/dist/billing/crypto/evm/types.d.ts +40 -0
- package/dist/billing/crypto/evm/types.js +1 -0
- package/dist/billing/crypto/evm/watcher.d.ts +31 -0
- package/dist/billing/crypto/evm/watcher.js +91 -0
- package/dist/billing/crypto/index.d.ts +10 -0
- package/dist/billing/crypto/index.js +6 -0
- package/dist/billing/crypto/types.d.ts +61 -0
- package/dist/billing/crypto/types.js +24 -0
- package/dist/billing/crypto/webhook.d.ts +34 -0
- package/dist/billing/crypto/webhook.js +107 -0
- package/dist/billing/crypto/webhook.test.d.ts +1 -0
- package/dist/billing/crypto/webhook.test.js +266 -0
- package/dist/billing/index.d.ts +1 -1
- package/dist/billing/index.js +2 -2
- package/dist/billing/payment-processor.d.ts +3 -3
- package/dist/credits/credit-ledger.d.ts +3 -3
- package/dist/credits/credit-ledger.js +3 -3
- package/dist/db/schema/credits.js +1 -1
- package/dist/db/schema/{payram.d.ts → crypto.d.ts} +85 -13
- package/dist/db/schema/crypto.js +32 -0
- package/dist/db/schema/index.d.ts +1 -1
- package/dist/db/schema/index.js +1 -1
- package/dist/monetization/crypto/__tests__/webhook.test.d.ts +1 -0
- package/dist/monetization/crypto/__tests__/webhook.test.js +249 -0
- package/dist/monetization/crypto/index.d.ts +4 -0
- package/dist/monetization/crypto/index.js +2 -0
- package/dist/monetization/crypto/webhook.d.ts +24 -0
- package/dist/monetization/crypto/webhook.js +88 -0
- package/dist/monetization/index.d.ts +3 -3
- package/dist/monetization/index.js +1 -1
- package/dist/monetization/repository-types.d.ts +1 -1
- package/dist/observability/pagerduty.test.js +1 -0
- package/docs/superpowers/plans/2026-03-14-stablecoin-phase1.md +1413 -0
- package/drizzle/migrations/0004_crypto_charges.sql +25 -0
- package/drizzle/migrations/0005_stablecoin_columns.sql +7 -0
- package/drizzle/migrations/meta/_journal.json +14 -0
- package/package.json +4 -3
- package/src/account/deletion-executor-repository.ts +6 -6
- package/src/billing/{payram → crypto}/cents-credits-boundary.test.ts +14 -17
- package/src/billing/crypto/charge-store.test.ts +142 -0
- package/src/billing/crypto/charge-store.ts +166 -0
- package/src/billing/crypto/checkout.test.ts +93 -0
- package/src/billing/crypto/checkout.ts +48 -0
- package/src/billing/crypto/client.test.ts +132 -0
- package/src/billing/crypto/client.ts +86 -0
- package/src/billing/crypto/evm/__tests__/address-gen.test.ts +63 -0
- package/src/billing/crypto/evm/__tests__/checkout.test.ts +83 -0
- package/src/billing/crypto/evm/__tests__/config.test.ts +63 -0
- package/src/billing/crypto/evm/__tests__/settler.test.ts +218 -0
- package/src/billing/crypto/evm/__tests__/watcher.test.ts +128 -0
- package/src/billing/crypto/evm/address-gen.ts +29 -0
- package/src/billing/crypto/evm/checkout.ts +82 -0
- package/src/billing/crypto/evm/config.ts +50 -0
- package/src/billing/crypto/evm/index.ts +16 -0
- package/src/billing/crypto/evm/settler.ts +79 -0
- package/src/billing/crypto/evm/types.ts +45 -0
- package/src/billing/crypto/evm/watcher.ts +126 -0
- package/src/billing/crypto/index.ts +16 -0
- package/src/billing/crypto/types.ts +83 -0
- package/src/billing/crypto/webhook.test.ts +340 -0
- package/src/billing/crypto/webhook.ts +136 -0
- package/src/billing/index.ts +2 -2
- package/src/billing/payment-processor.ts +3 -3
- package/src/credits/credit-ledger.ts +3 -3
- package/src/db/schema/credits.ts +1 -1
- package/src/db/schema/crypto.ts +37 -0
- package/src/db/schema/index.ts +1 -1
- package/src/monetization/crypto/__tests__/webhook.test.ts +327 -0
- package/src/monetization/crypto/index.ts +23 -0
- package/src/monetization/crypto/webhook.ts +115 -0
- package/src/monetization/index.ts +23 -21
- package/src/monetization/repository-types.ts +2 -2
- package/src/observability/pagerduty.test.ts +1 -0
- package/dist/billing/payram/cents-credits-boundary.test.js +0 -75
- package/dist/billing/payram/charge-store.d.ts +0 -41
- package/dist/billing/payram/charge-store.js +0 -72
- package/dist/billing/payram/charge-store.test.js +0 -64
- package/dist/billing/payram/checkout.d.ts +0 -15
- package/dist/billing/payram/checkout.js +0 -24
- package/dist/billing/payram/checkout.test.js +0 -74
- package/dist/billing/payram/client.d.ts +0 -7
- package/dist/billing/payram/client.js +0 -15
- package/dist/billing/payram/client.test.js +0 -52
- package/dist/billing/payram/index.d.ts +0 -8
- package/dist/billing/payram/index.js +0 -4
- package/dist/billing/payram/types.d.ts +0 -40
- package/dist/billing/payram/webhook.d.ts +0 -19
- package/dist/billing/payram/webhook.js +0 -71
- package/dist/billing/payram/webhook.test.d.ts +0 -7
- package/dist/billing/payram/webhook.test.js +0 -249
- package/dist/db/schema/payram.js +0 -21
- package/dist/monetization/payram/charge-store.test.js +0 -64
- package/dist/monetization/payram/checkout.test.js +0 -73
- package/dist/monetization/payram/client.test.js +0 -52
- package/dist/monetization/payram/index.d.ts +0 -4
- package/dist/monetization/payram/index.js +0 -2
- package/dist/monetization/payram/webhook.d.ts +0 -17
- package/dist/monetization/payram/webhook.js +0 -71
- package/dist/monetization/payram/webhook.test.d.ts +0 -7
- package/dist/monetization/payram/webhook.test.js +0 -247
- package/src/billing/payram/charge-store.test.ts +0 -84
- package/src/billing/payram/charge-store.ts +0 -109
- package/src/billing/payram/checkout.test.ts +0 -99
- package/src/billing/payram/checkout.ts +0 -40
- package/src/billing/payram/client.test.ts +0 -62
- package/src/billing/payram/client.ts +0 -21
- package/src/billing/payram/index.ts +0 -14
- package/src/billing/payram/types.ts +0 -44
- package/src/billing/payram/webhook.test.ts +0 -320
- package/src/billing/payram/webhook.ts +0 -94
- package/src/db/schema/payram.ts +0 -26
- package/src/monetization/payram/cents-credits-boundary.test.ts +0 -84
- package/src/monetization/payram/charge-store.test.ts +0 -84
- package/src/monetization/payram/checkout.test.ts +0 -98
- package/src/monetization/payram/client.test.ts +0 -62
- package/src/monetization/payram/index.ts +0 -20
- package/src/monetization/payram/webhook.test.ts +0 -327
- package/src/monetization/payram/webhook.ts +0 -97
- /package/dist/billing/{payram → crypto}/cents-credits-boundary.test.d.ts +0 -0
- /package/dist/billing/{payram → crypto}/charge-store.test.d.ts +0 -0
- /package/dist/billing/{payram → crypto}/checkout.test.d.ts +0 -0
- /package/dist/billing/{payram → crypto}/client.test.d.ts +0 -0
- /package/dist/billing/{payram/types.js → crypto/evm/__tests__/address-gen.test.d.ts} +0 -0
- /package/dist/{monetization/payram → billing/crypto/evm/__tests__}/checkout.test.d.ts +0 -0
- /package/dist/{monetization/payram/cents-credits-boundary.test.d.ts → billing/crypto/evm/__tests__/config.test.d.ts} +0 -0
- /package/dist/{monetization/payram/charge-store.test.d.ts → billing/crypto/evm/__tests__/settler.test.d.ts} +0 -0
- /package/dist/{monetization/payram/client.test.d.ts → billing/crypto/evm/__tests__/watcher.test.d.ts} +0 -0
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
-- Replace payram_charges with crypto_charges (BTCPay Server).
|
|
2
|
+
-- payram_charges existed only in the initial schema (0000) and was never used
|
|
3
|
+
-- in production — no data migration is needed. The table is dropped and replaced
|
|
4
|
+
-- with crypto_charges which has the same column structure but uses BTCPay-specific
|
|
5
|
+
-- naming (reference_id = BTCPay invoice ID).
|
|
6
|
+
--> statement-breakpoint
|
|
7
|
+
DROP TABLE IF EXISTS "payram_charges";
|
|
8
|
+
--> statement-breakpoint
|
|
9
|
+
CREATE TABLE IF NOT EXISTS "crypto_charges" (
|
|
10
|
+
"reference_id" text PRIMARY KEY NOT NULL,
|
|
11
|
+
"tenant_id" text NOT NULL,
|
|
12
|
+
"amount_usd_cents" integer NOT NULL,
|
|
13
|
+
"status" text DEFAULT 'New' NOT NULL,
|
|
14
|
+
"currency" text,
|
|
15
|
+
"filled_amount" text,
|
|
16
|
+
"created_at" text DEFAULT (now()) NOT NULL,
|
|
17
|
+
"updated_at" text DEFAULT (now()) NOT NULL,
|
|
18
|
+
"credited_at" text
|
|
19
|
+
);
|
|
20
|
+
--> statement-breakpoint
|
|
21
|
+
CREATE INDEX IF NOT EXISTS "idx_crypto_charges_tenant" ON "crypto_charges" USING btree ("tenant_id");
|
|
22
|
+
--> statement-breakpoint
|
|
23
|
+
CREATE INDEX IF NOT EXISTS "idx_crypto_charges_status" ON "crypto_charges" USING btree ("status");
|
|
24
|
+
--> statement-breakpoint
|
|
25
|
+
CREATE INDEX IF NOT EXISTS "idx_crypto_charges_created" ON "crypto_charges" USING btree ("created_at");
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
ALTER TABLE "crypto_charges" ADD COLUMN "chain" text;--> statement-breakpoint
|
|
2
|
+
ALTER TABLE "crypto_charges" ADD COLUMN "token" text;--> statement-breakpoint
|
|
3
|
+
ALTER TABLE "crypto_charges" ADD COLUMN "deposit_address" text;--> statement-breakpoint
|
|
4
|
+
ALTER TABLE "crypto_charges" ADD COLUMN "derivation_index" integer;--> statement-breakpoint
|
|
5
|
+
CREATE INDEX "idx_crypto_charges_deposit_address" ON "crypto_charges" USING btree ("deposit_address");--> statement-breakpoint
|
|
6
|
+
CREATE UNIQUE INDEX "uq_crypto_charges_deposit_address" ON "crypto_charges" ("deposit_address") WHERE "deposit_address" IS NOT NULL;--> statement-breakpoint
|
|
7
|
+
CREATE UNIQUE INDEX "uq_crypto_charges_derivation_index" ON "crypto_charges" ("derivation_index") WHERE "derivation_index" IS NOT NULL;
|
|
@@ -29,6 +29,20 @@
|
|
|
29
29
|
"when": 1741881600000,
|
|
30
30
|
"tag": "0003_double_entry_ledger",
|
|
31
31
|
"breakpoints": true
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
"idx": 4,
|
|
35
|
+
"version": "7",
|
|
36
|
+
"when": 1741968000000,
|
|
37
|
+
"tag": "0004_crypto_charges",
|
|
38
|
+
"breakpoints": true
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
"idx": 5,
|
|
42
|
+
"version": "7",
|
|
43
|
+
"when": 1742054400000,
|
|
44
|
+
"tag": "0005_stablecoin_columns",
|
|
45
|
+
"breakpoints": true
|
|
32
46
|
}
|
|
33
47
|
]
|
|
34
48
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wopr-network/platform-core",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.16.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -87,7 +87,6 @@
|
|
|
87
87
|
"dockerode": ">=4",
|
|
88
88
|
"drizzle-orm": ">=0.45",
|
|
89
89
|
"hono": ">=4",
|
|
90
|
-
"payram": ">=1",
|
|
91
90
|
"pg": ">=8",
|
|
92
91
|
"resend": ">=4",
|
|
93
92
|
"stripe": ">=17",
|
|
@@ -111,7 +110,6 @@
|
|
|
111
110
|
"drizzle-orm": "^0.45.1",
|
|
112
111
|
"hono": "^4.12.7",
|
|
113
112
|
"lru-cache": "^11.2.6",
|
|
114
|
-
"payram": "^1.0.1",
|
|
115
113
|
"pg": "^8.20.0",
|
|
116
114
|
"resend": "^6.9.3",
|
|
117
115
|
"stripe": "^20.4.1",
|
|
@@ -130,7 +128,10 @@
|
|
|
130
128
|
},
|
|
131
129
|
"packageManager": "pnpm@10.31.0",
|
|
132
130
|
"dependencies": {
|
|
131
|
+
"@scure/base": "^2.0.0",
|
|
132
|
+
"@scure/bip32": "^2.0.1",
|
|
133
133
|
"js-yaml": "^4.1.1",
|
|
134
|
+
"viem": "^2.47.4",
|
|
134
135
|
"yaml": "^2.8.2"
|
|
135
136
|
}
|
|
136
137
|
}
|
|
@@ -10,11 +10,11 @@ import {
|
|
|
10
10
|
botInstances,
|
|
11
11
|
creditBalances,
|
|
12
12
|
creditTransactions,
|
|
13
|
+
cryptoCharges,
|
|
13
14
|
emailNotifications,
|
|
14
15
|
meterEvents,
|
|
15
16
|
notificationPreferences,
|
|
16
17
|
notificationQueue,
|
|
17
|
-
payramCharges,
|
|
18
18
|
snapshots,
|
|
19
19
|
stripeUsageReports,
|
|
20
20
|
tenantCustomers,
|
|
@@ -55,7 +55,7 @@ export interface IDeletionExecutorRepository {
|
|
|
55
55
|
listSnapshotS3Keys(tenantId: string): Promise<{ id: string; s3Key: string | null }[]>;
|
|
56
56
|
deleteSnapshots(tenantId: string): Promise<number>;
|
|
57
57
|
deleteBackupStatus(tenantId: string): Promise<number>;
|
|
58
|
-
|
|
58
|
+
deleteCryptoCharges(tenantId: string): Promise<number>;
|
|
59
59
|
deleteTenantStatus(tenantId: string): Promise<number>;
|
|
60
60
|
deleteUserRolesByUser(tenantId: string): Promise<number>;
|
|
61
61
|
deleteUserRolesByTenant(tenantId: string): Promise<number>;
|
|
@@ -259,11 +259,11 @@ export class DrizzleDeletionExecutorRepository implements IDeletionExecutorRepos
|
|
|
259
259
|
return result.length;
|
|
260
260
|
}
|
|
261
261
|
|
|
262
|
-
async
|
|
262
|
+
async deleteCryptoCharges(tenantId: string): Promise<number> {
|
|
263
263
|
const result = await this.db
|
|
264
|
-
.delete(
|
|
265
|
-
.where(eq(
|
|
266
|
-
.returning({ referenceId:
|
|
264
|
+
.delete(cryptoCharges)
|
|
265
|
+
.where(eq(cryptoCharges.tenantId, tenantId))
|
|
266
|
+
.returning({ referenceId: cryptoCharges.referenceId });
|
|
267
267
|
return result.length;
|
|
268
268
|
}
|
|
269
269
|
|
|
@@ -1,20 +1,21 @@
|
|
|
1
1
|
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { Credit } from "../../credits/credit.js";
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
|
-
* Regression tests for
|
|
5
|
+
* Regression tests for crypto cents/credits boundary.
|
|
5
6
|
*
|
|
6
7
|
* Verifies that the USD-to-cents conversion in checkout and
|
|
7
8
|
* the cents-to-credits flow in the webhook maintain correct units.
|
|
8
9
|
*
|
|
9
10
|
* If any of these tests fail after a rename/refactor, a _cents field was
|
|
10
11
|
* incorrectly changed to store Credit raw units (nanodollars) instead of
|
|
11
|
-
* USD cents. See src/
|
|
12
|
+
* USD cents. See src/credits/credit-ledger.ts for naming convention.
|
|
12
13
|
*/
|
|
13
|
-
describe("
|
|
14
|
-
it("USD to cents conversion is correct (mirrors checkout.ts pattern)", () => {
|
|
15
|
-
// This mirrors the conversion in
|
|
14
|
+
describe("Crypto cents/credits boundary", () => {
|
|
15
|
+
it("USD to cents conversion is correct (mirrors checkout.ts Credit.fromDollars pattern)", () => {
|
|
16
|
+
// This mirrors the conversion in crypto/checkout.ts: Credit.fromDollars(amountUsd).toCentsRounded()
|
|
16
17
|
const amountUsd = 25;
|
|
17
|
-
const amountUsdCents =
|
|
18
|
+
const amountUsdCents = Credit.fromDollars(amountUsd).toCentsRounded();
|
|
18
19
|
expect(amountUsdCents).toBe(2500);
|
|
19
20
|
// Must NOT be nanodollar scale
|
|
20
21
|
expect(amountUsdCents).toBeLessThan(1_000_000);
|
|
@@ -22,7 +23,7 @@ describe("WOP-1058: PayRam cents/credits boundary", () => {
|
|
|
22
23
|
|
|
23
24
|
it("minimum payment amount converts to valid cents", () => {
|
|
24
25
|
const MIN_PAYMENT_USD = 10;
|
|
25
|
-
const cents =
|
|
26
|
+
const cents = Credit.fromDollars(MIN_PAYMENT_USD).toCentsRounded();
|
|
26
27
|
expect(cents).toBe(1000);
|
|
27
28
|
expect(Number.isInteger(cents)).toBe(true);
|
|
28
29
|
// Sanity: $10 is 1000 cents, NOT 10_000_000_000 nanodollars
|
|
@@ -30,16 +31,13 @@ describe("WOP-1058: PayRam cents/credits boundary", () => {
|
|
|
30
31
|
});
|
|
31
32
|
|
|
32
33
|
it("fractional USD amounts round correctly to cents", () => {
|
|
33
|
-
// Edge case: floating point conversion
|
|
34
34
|
const amountUsd = 10.99;
|
|
35
|
-
const cents =
|
|
35
|
+
const cents = Credit.fromDollars(amountUsd).toCentsRounded();
|
|
36
36
|
expect(cents).toBe(1099);
|
|
37
37
|
expect(cents).toBeLessThan(1_000_000);
|
|
38
38
|
});
|
|
39
39
|
|
|
40
40
|
it("amountUsdCents stored in charge record equals USD * 100 (not nanodollars)", () => {
|
|
41
|
-
// The core invariant: payram/checkout.ts stores Math.round(amountUsd * 100)
|
|
42
|
-
// as amountUsdCents. This test proves the conversion stays at cent scale.
|
|
43
41
|
const testCases: Array<{ usd: number; expectedCents: number }> = [
|
|
44
42
|
{ usd: 10, expectedCents: 1000 },
|
|
45
43
|
{ usd: 25, expectedCents: 2500 },
|
|
@@ -48,18 +46,18 @@ describe("WOP-1058: PayRam cents/credits boundary", () => {
|
|
|
48
46
|
];
|
|
49
47
|
|
|
50
48
|
for (const { usd, expectedCents } of testCases) {
|
|
51
|
-
const amountUsdCents =
|
|
49
|
+
const amountUsdCents = Credit.fromDollars(usd).toCentsRounded();
|
|
52
50
|
expect(amountUsdCents).toBe(expectedCents);
|
|
53
51
|
// CREDIT SCALE = 1_000_000_000. If this value approaches that, unit confusion occurred.
|
|
54
52
|
expect(amountUsdCents).toBeLessThan(1_000_000);
|
|
55
53
|
}
|
|
56
54
|
});
|
|
57
55
|
|
|
58
|
-
it("creditedCents in webhook equals amountUsdCents from charge store (1:1 for
|
|
59
|
-
//
|
|
60
|
-
// The credited amount always equals the stored USD cents — no bonus tiers for
|
|
56
|
+
it("creditedCents in webhook equals amountUsdCents from charge store (1:1 for crypto)", () => {
|
|
57
|
+
// crypto/webhook.ts: const creditCents = charge.amountUsdCents;
|
|
58
|
+
// The credited amount always equals the stored USD cents — no bonus tiers for crypto.
|
|
61
59
|
const chargeAmountUsdCents = 2500; // $25.00
|
|
62
|
-
const creditCents = chargeAmountUsdCents; // 1:1 for
|
|
60
|
+
const creditCents = chargeAmountUsdCents; // 1:1 for crypto
|
|
63
61
|
expect(creditCents).toBe(2500);
|
|
64
62
|
// creditedCents must be 2500 (cents), not 25_000_000_000 (nanodollars)
|
|
65
63
|
expect(creditCents).toBeLessThan(1_000_000);
|
|
@@ -68,7 +66,6 @@ describe("WOP-1058: PayRam cents/credits boundary", () => {
|
|
|
68
66
|
it("cents-to-nanodollar scale difference is preserved as a sanity constant", () => {
|
|
69
67
|
// Credit.SCALE = 1_000_000_000 nanodollars per dollar
|
|
70
68
|
// 1 USD cent = 10_000_000 nanodollars (SCALE / 100)
|
|
71
|
-
// This test documents the relationship so future developers understand the gap.
|
|
72
69
|
const CREDIT_SCALE = 1_000_000_000;
|
|
73
70
|
const CENTS_PER_DOLLAR = 100;
|
|
74
71
|
const NANODOLLARS_PER_CENT = CREDIT_SCALE / CENTS_PER_DOLLAR;
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import type { PGlite } from "@electric-sql/pglite";
|
|
2
|
+
import { afterAll, beforeAll, beforeEach, describe, expect, it } from "vitest";
|
|
3
|
+
import type { PlatformDb } from "../../db/index.js";
|
|
4
|
+
import { beginTestTransaction, createTestDb, endTestTransaction, rollbackTestTransaction } from "../../test/db.js";
|
|
5
|
+
import { CryptoChargeRepository } from "./charge-store.js";
|
|
6
|
+
|
|
7
|
+
describe("CryptoChargeRepository", () => {
|
|
8
|
+
let pool: PGlite;
|
|
9
|
+
let db: PlatformDb;
|
|
10
|
+
let store: CryptoChargeRepository;
|
|
11
|
+
|
|
12
|
+
beforeAll(async () => {
|
|
13
|
+
({ db, pool } = await createTestDb());
|
|
14
|
+
await beginTestTransaction(pool);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
afterAll(async () => {
|
|
18
|
+
await endTestTransaction(pool);
|
|
19
|
+
await pool.close();
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
beforeEach(async () => {
|
|
23
|
+
await rollbackTestTransaction(pool);
|
|
24
|
+
store = new CryptoChargeRepository(db);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("create() stores a charge with New status", async () => {
|
|
28
|
+
await store.create("inv-001", "tenant-1", 2500);
|
|
29
|
+
|
|
30
|
+
const charge = await store.getByReferenceId("inv-001");
|
|
31
|
+
expect(charge).not.toBeNull();
|
|
32
|
+
expect(charge?.referenceId).toBe("inv-001");
|
|
33
|
+
expect(charge?.tenantId).toBe("tenant-1");
|
|
34
|
+
expect(charge?.amountUsdCents).toBe(2500);
|
|
35
|
+
expect(charge?.status).toBe("New");
|
|
36
|
+
expect(charge?.creditedAt).toBeNull();
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("getByReferenceId() returns null when not found", async () => {
|
|
40
|
+
const charge = await store.getByReferenceId("inv-nonexistent");
|
|
41
|
+
expect(charge).toBeNull();
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("updateStatus() updates status, currency and filled_amount", async () => {
|
|
45
|
+
await store.create("inv-002", "tenant-2", 5000);
|
|
46
|
+
await store.updateStatus("inv-002", "Settled", "BTC", "0.00025");
|
|
47
|
+
|
|
48
|
+
const charge = await store.getByReferenceId("inv-002");
|
|
49
|
+
expect(charge?.status).toBe("Settled");
|
|
50
|
+
expect(charge?.currency).toBe("BTC");
|
|
51
|
+
expect(charge?.filledAmount).toBe("0.00025");
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("updateStatus() handles partial updates (no currency)", async () => {
|
|
55
|
+
await store.create("inv-003", "tenant-3", 1000);
|
|
56
|
+
await store.updateStatus("inv-003", "Processing");
|
|
57
|
+
|
|
58
|
+
const charge = await store.getByReferenceId("inv-003");
|
|
59
|
+
expect(charge?.status).toBe("Processing");
|
|
60
|
+
expect(charge?.currency).toBeNull();
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("isCredited() returns false before markCredited", async () => {
|
|
64
|
+
await store.create("inv-004", "tenant-4", 1500);
|
|
65
|
+
expect(await store.isCredited("inv-004")).toBe(false);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("markCredited() sets creditedAt", async () => {
|
|
69
|
+
await store.create("inv-005", "tenant-5", 3000);
|
|
70
|
+
await store.markCredited("inv-005");
|
|
71
|
+
|
|
72
|
+
const charge = await store.getByReferenceId("inv-005");
|
|
73
|
+
expect(charge?.creditedAt).not.toBeNull();
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("isCredited() returns true after markCredited", async () => {
|
|
77
|
+
await store.create("inv-006", "tenant-6", 2000);
|
|
78
|
+
await store.markCredited("inv-006");
|
|
79
|
+
expect(await store.isCredited("inv-006")).toBe(true);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
describe("stablecoin charges", () => {
|
|
83
|
+
it("creates a stablecoin charge with chain/token/address", async () => {
|
|
84
|
+
await store.createStablecoinCharge({
|
|
85
|
+
referenceId: "sc:base:usdc:0x123",
|
|
86
|
+
tenantId: "tenant-1",
|
|
87
|
+
amountUsdCents: 1000,
|
|
88
|
+
chain: "base",
|
|
89
|
+
token: "USDC",
|
|
90
|
+
depositAddress: "0xabc123",
|
|
91
|
+
derivationIndex: 42,
|
|
92
|
+
});
|
|
93
|
+
const charge = await store.getByReferenceId("sc:base:usdc:0x123");
|
|
94
|
+
expect(charge).not.toBeNull();
|
|
95
|
+
expect(charge?.chain).toBe("base");
|
|
96
|
+
expect(charge?.token).toBe("USDC");
|
|
97
|
+
expect(charge?.depositAddress).toBe("0xabc123");
|
|
98
|
+
expect(charge?.derivationIndex).toBe(42);
|
|
99
|
+
expect(charge?.amountUsdCents).toBe(1000);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it("looks up charge by deposit address", async () => {
|
|
103
|
+
await store.createStablecoinCharge({
|
|
104
|
+
referenceId: "sc:base:usdc:0x456",
|
|
105
|
+
tenantId: "tenant-2",
|
|
106
|
+
amountUsdCents: 5000,
|
|
107
|
+
chain: "base",
|
|
108
|
+
token: "USDC",
|
|
109
|
+
depositAddress: "0xdef456",
|
|
110
|
+
derivationIndex: 43,
|
|
111
|
+
});
|
|
112
|
+
const charge = await store.getByDepositAddress("0xdef456");
|
|
113
|
+
expect(charge).not.toBeNull();
|
|
114
|
+
expect(charge?.tenantId).toBe("tenant-2");
|
|
115
|
+
expect(charge?.amountUsdCents).toBe(5000);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("returns null for unknown deposit address", async () => {
|
|
119
|
+
const charge = await store.getByDepositAddress("0xnonexistent");
|
|
120
|
+
expect(charge).toBeNull();
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("gets next derivation index (0 when empty)", async () => {
|
|
124
|
+
const idx = await store.getNextDerivationIndex();
|
|
125
|
+
expect(idx).toBe(0);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it("gets next derivation index (max + 1)", async () => {
|
|
129
|
+
await store.createStablecoinCharge({
|
|
130
|
+
referenceId: "sc:idx-test",
|
|
131
|
+
tenantId: "t",
|
|
132
|
+
amountUsdCents: 100,
|
|
133
|
+
chain: "base",
|
|
134
|
+
token: "USDC",
|
|
135
|
+
depositAddress: "0xidxtest",
|
|
136
|
+
derivationIndex: 5,
|
|
137
|
+
});
|
|
138
|
+
const idx = await store.getNextDerivationIndex();
|
|
139
|
+
expect(idx).toBe(6);
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
});
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { eq, sql } from "drizzle-orm";
|
|
2
|
+
import type { PlatformDb } from "../../db/index.js";
|
|
3
|
+
import { cryptoCharges } from "../../db/schema/crypto.js";
|
|
4
|
+
import type { CryptoPaymentState } from "./types.js";
|
|
5
|
+
|
|
6
|
+
export interface CryptoChargeRecord {
|
|
7
|
+
referenceId: string;
|
|
8
|
+
tenantId: string;
|
|
9
|
+
amountUsdCents: number;
|
|
10
|
+
status: string;
|
|
11
|
+
currency: string | null;
|
|
12
|
+
filledAmount: string | null;
|
|
13
|
+
creditedAt: string | null;
|
|
14
|
+
createdAt: string;
|
|
15
|
+
updatedAt: string;
|
|
16
|
+
chain: string | null;
|
|
17
|
+
token: string | null;
|
|
18
|
+
depositAddress: string | null;
|
|
19
|
+
derivationIndex: number | null;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface StablecoinChargeInput {
|
|
23
|
+
referenceId: string;
|
|
24
|
+
tenantId: string;
|
|
25
|
+
amountUsdCents: number;
|
|
26
|
+
chain: string;
|
|
27
|
+
token: string;
|
|
28
|
+
depositAddress: string;
|
|
29
|
+
derivationIndex: number;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface ICryptoChargeRepository {
|
|
33
|
+
create(referenceId: string, tenantId: string, amountUsdCents: number): Promise<void>;
|
|
34
|
+
getByReferenceId(referenceId: string): Promise<CryptoChargeRecord | null>;
|
|
35
|
+
updateStatus(
|
|
36
|
+
referenceId: string,
|
|
37
|
+
status: CryptoPaymentState,
|
|
38
|
+
currency?: string,
|
|
39
|
+
filledAmount?: string,
|
|
40
|
+
): Promise<void>;
|
|
41
|
+
markCredited(referenceId: string): Promise<void>;
|
|
42
|
+
isCredited(referenceId: string): Promise<boolean>;
|
|
43
|
+
createStablecoinCharge(input: StablecoinChargeInput): Promise<void>;
|
|
44
|
+
getByDepositAddress(address: string): Promise<CryptoChargeRecord | null>;
|
|
45
|
+
getNextDerivationIndex(): Promise<number>;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Manages crypto charge records in PostgreSQL.
|
|
50
|
+
*
|
|
51
|
+
* Each charge maps a BTCPay invoice ID to a tenant and tracks
|
|
52
|
+
* the payment lifecycle (New → Processing → Settled/Expired/Invalid).
|
|
53
|
+
*
|
|
54
|
+
* amountUsdCents stores the requested amount in USD cents (integer).
|
|
55
|
+
* This is NOT nanodollars — Credit.fromCents() handles the conversion
|
|
56
|
+
* when crediting the ledger in the webhook handler.
|
|
57
|
+
*/
|
|
58
|
+
export class DrizzleCryptoChargeRepository implements ICryptoChargeRepository {
|
|
59
|
+
constructor(private readonly db: PlatformDb) {}
|
|
60
|
+
|
|
61
|
+
/** Create a new charge record when an invoice is created. */
|
|
62
|
+
async create(referenceId: string, tenantId: string, amountUsdCents: number): Promise<void> {
|
|
63
|
+
await this.db.insert(cryptoCharges).values({
|
|
64
|
+
referenceId,
|
|
65
|
+
tenantId,
|
|
66
|
+
amountUsdCents,
|
|
67
|
+
status: "New",
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Get a charge by reference ID. Returns null if not found. */
|
|
72
|
+
async getByReferenceId(referenceId: string): Promise<CryptoChargeRecord | null> {
|
|
73
|
+
const row = (await this.db.select().from(cryptoCharges).where(eq(cryptoCharges.referenceId, referenceId)))[0];
|
|
74
|
+
if (!row) return null;
|
|
75
|
+
return this.toRecord(row);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
private toRecord(row: typeof cryptoCharges.$inferSelect): CryptoChargeRecord {
|
|
79
|
+
return {
|
|
80
|
+
referenceId: row.referenceId,
|
|
81
|
+
tenantId: row.tenantId,
|
|
82
|
+
amountUsdCents: row.amountUsdCents,
|
|
83
|
+
status: row.status,
|
|
84
|
+
currency: row.currency ?? null,
|
|
85
|
+
filledAmount: row.filledAmount ?? null,
|
|
86
|
+
creditedAt: row.creditedAt ?? null,
|
|
87
|
+
createdAt: row.createdAt,
|
|
88
|
+
updatedAt: row.updatedAt,
|
|
89
|
+
chain: row.chain ?? null,
|
|
90
|
+
token: row.token ?? null,
|
|
91
|
+
depositAddress: row.depositAddress ?? null,
|
|
92
|
+
derivationIndex: row.derivationIndex ?? null,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Update charge status and payment details from webhook. */
|
|
97
|
+
async updateStatus(
|
|
98
|
+
referenceId: string,
|
|
99
|
+
status: CryptoPaymentState,
|
|
100
|
+
currency?: string,
|
|
101
|
+
filledAmount?: string,
|
|
102
|
+
): Promise<void> {
|
|
103
|
+
await this.db
|
|
104
|
+
.update(cryptoCharges)
|
|
105
|
+
.set({
|
|
106
|
+
status,
|
|
107
|
+
currency,
|
|
108
|
+
filledAmount,
|
|
109
|
+
updatedAt: sql`now()`,
|
|
110
|
+
})
|
|
111
|
+
.where(eq(cryptoCharges.referenceId, referenceId));
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Mark a charge as credited (idempotency flag). */
|
|
115
|
+
async markCredited(referenceId: string): Promise<void> {
|
|
116
|
+
await this.db
|
|
117
|
+
.update(cryptoCharges)
|
|
118
|
+
.set({
|
|
119
|
+
creditedAt: sql`now()`,
|
|
120
|
+
updatedAt: sql`now()`,
|
|
121
|
+
})
|
|
122
|
+
.where(eq(cryptoCharges.referenceId, referenceId));
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** Check if a charge has already been credited (for idempotency). */
|
|
126
|
+
async isCredited(referenceId: string): Promise<boolean> {
|
|
127
|
+
const row = (
|
|
128
|
+
await this.db
|
|
129
|
+
.select({ creditedAt: cryptoCharges.creditedAt })
|
|
130
|
+
.from(cryptoCharges)
|
|
131
|
+
.where(eq(cryptoCharges.referenceId, referenceId))
|
|
132
|
+
)[0];
|
|
133
|
+
return row?.creditedAt != null;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Create a stablecoin charge with chain/token/deposit address. */
|
|
137
|
+
async createStablecoinCharge(input: StablecoinChargeInput): Promise<void> {
|
|
138
|
+
await this.db.insert(cryptoCharges).values({
|
|
139
|
+
referenceId: input.referenceId,
|
|
140
|
+
tenantId: input.tenantId,
|
|
141
|
+
amountUsdCents: input.amountUsdCents,
|
|
142
|
+
status: "New",
|
|
143
|
+
chain: input.chain,
|
|
144
|
+
token: input.token,
|
|
145
|
+
depositAddress: input.depositAddress,
|
|
146
|
+
derivationIndex: input.derivationIndex,
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** Look up a charge by its deposit address. */
|
|
151
|
+
async getByDepositAddress(address: string): Promise<CryptoChargeRecord | null> {
|
|
152
|
+
const row = (await this.db.select().from(cryptoCharges).where(eq(cryptoCharges.depositAddress, address)))[0];
|
|
153
|
+
if (!row) return null;
|
|
154
|
+
return this.toRecord(row);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Get the next available HD derivation index (max + 1, or 0 if empty). */
|
|
158
|
+
async getNextDerivationIndex(): Promise<number> {
|
|
159
|
+
const result = await this.db
|
|
160
|
+
.select({ maxIdx: sql<number>`coalesce(max(${cryptoCharges.derivationIndex}), -1)` })
|
|
161
|
+
.from(cryptoCharges);
|
|
162
|
+
return (result[0]?.maxIdx ?? -1) + 1;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export { DrizzleCryptoChargeRepository as CryptoChargeRepository };
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import type { PGlite } from "@electric-sql/pglite";
|
|
2
|
+
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
|
3
|
+
import type { PlatformDb } from "../../db/index.js";
|
|
4
|
+
import { beginTestTransaction, createTestDb, endTestTransaction, rollbackTestTransaction } from "../../test/db.js";
|
|
5
|
+
import { CryptoChargeRepository } from "./charge-store.js";
|
|
6
|
+
import { createCryptoCheckout, MIN_PAYMENT_USD } from "./checkout.js";
|
|
7
|
+
import type { BTCPayClient } from "./client.js";
|
|
8
|
+
|
|
9
|
+
function createMockClient(overrides: { createInvoice?: ReturnType<typeof vi.fn> } = {}): BTCPayClient {
|
|
10
|
+
return {
|
|
11
|
+
createInvoice:
|
|
12
|
+
overrides.createInvoice ??
|
|
13
|
+
vi.fn().mockResolvedValue({
|
|
14
|
+
id: "inv-mock-001",
|
|
15
|
+
checkoutLink: "https://btcpay.example.com/i/inv-mock-001",
|
|
16
|
+
}),
|
|
17
|
+
} as unknown as BTCPayClient;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
describe("createCryptoCheckout", () => {
|
|
21
|
+
let pool: PGlite;
|
|
22
|
+
let db: PlatformDb;
|
|
23
|
+
let chargeStore: CryptoChargeRepository;
|
|
24
|
+
let client: BTCPayClient;
|
|
25
|
+
|
|
26
|
+
beforeAll(async () => {
|
|
27
|
+
({ db, pool } = await createTestDb());
|
|
28
|
+
await beginTestTransaction(pool);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
afterAll(async () => {
|
|
32
|
+
await endTestTransaction(pool);
|
|
33
|
+
await pool.close();
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
beforeEach(async () => {
|
|
37
|
+
await rollbackTestTransaction(pool);
|
|
38
|
+
chargeStore = new CryptoChargeRepository(db);
|
|
39
|
+
client = createMockClient();
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("rejects amounts below $10 minimum", async () => {
|
|
43
|
+
await expect(createCryptoCheckout(client, chargeStore, { tenant: "t-1", amountUsd: 5 })).rejects.toThrow(
|
|
44
|
+
`Minimum payment amount is $${MIN_PAYMENT_USD}`,
|
|
45
|
+
);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("rejects amounts of exactly $0", async () => {
|
|
49
|
+
await expect(createCryptoCheckout(client, chargeStore, { tenant: "t-1", amountUsd: 0 })).rejects.toThrow();
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("calls client.createInvoice with correct params", async () => {
|
|
53
|
+
const createInvoice = vi.fn().mockResolvedValue({
|
|
54
|
+
id: "inv-abc",
|
|
55
|
+
checkoutLink: "https://btcpay.example.com/i/inv-abc",
|
|
56
|
+
});
|
|
57
|
+
const mockClient = createMockClient({ createInvoice });
|
|
58
|
+
|
|
59
|
+
await createCryptoCheckout(mockClient, chargeStore, { tenant: "t-test", amountUsd: 25 });
|
|
60
|
+
|
|
61
|
+
expect(createInvoice).toHaveBeenCalledOnce();
|
|
62
|
+
const args = createInvoice.mock.calls[0][0];
|
|
63
|
+
expect(args.amountUsd).toBe(25);
|
|
64
|
+
expect(args.buyerEmail).toContain("t-test@");
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("stores the charge with correct amountUsdCents (converts from USD)", async () => {
|
|
68
|
+
const createInvoice = vi.fn().mockResolvedValue({
|
|
69
|
+
id: "inv-store-test",
|
|
70
|
+
checkoutLink: "https://btcpay.example.com/i/inv-store-test",
|
|
71
|
+
});
|
|
72
|
+
const mockClient = createMockClient({ createInvoice });
|
|
73
|
+
|
|
74
|
+
await createCryptoCheckout(mockClient, chargeStore, { tenant: "t-2", amountUsd: 25 });
|
|
75
|
+
|
|
76
|
+
const charge = await chargeStore.getByReferenceId("inv-store-test");
|
|
77
|
+
expect(charge).not.toBeNull();
|
|
78
|
+
expect(charge?.tenantId).toBe("t-2");
|
|
79
|
+
expect(charge?.amountUsdCents).toBe(2500); // $25.00 = 2500 cents
|
|
80
|
+
expect(charge?.status).toBe("New");
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("returns referenceId and url", async () => {
|
|
84
|
+
const result = await createCryptoCheckout(client, chargeStore, { tenant: "t-3", amountUsd: 10 });
|
|
85
|
+
|
|
86
|
+
expect(result.referenceId).toBe("inv-mock-001");
|
|
87
|
+
expect(result.url).toBe("https://btcpay.example.com/i/inv-mock-001");
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("accepts exactly $10 (minimum boundary)", async () => {
|
|
91
|
+
await expect(createCryptoCheckout(client, chargeStore, { tenant: "t-4", amountUsd: 10 })).resolves.not.toBeNull();
|
|
92
|
+
});
|
|
93
|
+
});
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import crypto from "node:crypto";
|
|
2
|
+
import { Credit } from "../../credits/credit.js";
|
|
3
|
+
import type { ICryptoChargeRepository } from "./charge-store.js";
|
|
4
|
+
import type { BTCPayClient } from "./client.js";
|
|
5
|
+
import type { CryptoCheckoutOpts } from "./types.js";
|
|
6
|
+
|
|
7
|
+
/** Minimum payment amount in USD. */
|
|
8
|
+
export const MIN_PAYMENT_USD = 10;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Create a BTCPay invoice and store the charge record.
|
|
12
|
+
*
|
|
13
|
+
* Returns the BTCPay-hosted checkout page URL and invoice ID.
|
|
14
|
+
* The user is redirected to checkoutLink to complete the crypto payment.
|
|
15
|
+
*
|
|
16
|
+
* NOTE: amountUsd is converted to cents (integer) for the charge store.
|
|
17
|
+
* The charge store holds USD cents, NOT nanodollars.
|
|
18
|
+
*/
|
|
19
|
+
export async function createCryptoCheckout(
|
|
20
|
+
client: BTCPayClient,
|
|
21
|
+
chargeStore: ICryptoChargeRepository,
|
|
22
|
+
opts: CryptoCheckoutOpts,
|
|
23
|
+
): Promise<{ referenceId: string; url: string }> {
|
|
24
|
+
if (opts.amountUsd < MIN_PAYMENT_USD) {
|
|
25
|
+
throw new Error(`Minimum payment amount is $${MIN_PAYMENT_USD}`);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const orderId = `crypto:${opts.tenant}:${crypto.randomUUID()}`;
|
|
29
|
+
|
|
30
|
+
const invoice = await client.createInvoice({
|
|
31
|
+
amountUsd: opts.amountUsd,
|
|
32
|
+
orderId,
|
|
33
|
+
buyerEmail: `${opts.tenant}@${process.env.PLATFORM_DOMAIN ?? "wopr.bot"}`,
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// Store the charge record for webhook correlation.
|
|
37
|
+
// amountUsdCents = USD * 100 (cents, NOT nanodollars).
|
|
38
|
+
// Credit.fromDollars() handles the float → integer boundary safely via Math.round
|
|
39
|
+
// on the nanodollar scale, then toCentsRounded() converts back to integer cents.
|
|
40
|
+
// This avoids direct floating-point multiplication for the cents conversion.
|
|
41
|
+
const amountUsdCents = Credit.fromDollars(opts.amountUsd).toCentsRounded();
|
|
42
|
+
await chargeStore.create(invoice.id, opts.tenant, amountUsdCents);
|
|
43
|
+
|
|
44
|
+
return {
|
|
45
|
+
referenceId: invoice.id,
|
|
46
|
+
url: invoice.checkoutLink,
|
|
47
|
+
};
|
|
48
|
+
}
|