@wopr-network/platform-core 1.14.7 → 1.15.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/.env.example +10 -0
- 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/{payram → crypto}/charge-store.d.ts +17 -13
- package/dist/billing/{payram → crypto}/charge-store.js +21 -18
- package/dist/billing/crypto/charge-store.test.js +64 -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/index.d.ts +9 -0
- package/dist/billing/crypto/index.js +5 -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.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/config/provider-endpoints.d.ts +4 -0
- package/dist/config/provider-endpoints.js +10 -6
- package/dist/credits/credit-ledger.d.ts +3 -3
- package/dist/credits/credit-ledger.js +3 -3
- package/dist/db/index.d.ts +1 -1
- package/dist/db/index.js +1 -1
- package/dist/db/schema/credits.js +1 -1
- package/dist/db/schema/{payram.d.ts → crypto.d.ts} +17 -13
- package/dist/db/schema/crypto.js +25 -0
- package/dist/db/schema/index.d.ts +1 -1
- package/dist/db/schema/index.js +1 -1
- package/dist/fleet/node-repository.d.ts +1 -3
- 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/dist/security/key-validation.test.js +65 -8
- package/drizzle/migrations/0004_crypto_charges.sql +25 -0
- package/drizzle/migrations/meta/_journal.json +7 -0
- package/package.json +1 -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 +81 -0
- package/src/billing/{payram → crypto}/charge-store.ts +28 -25
- 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/index.ts +15 -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/config/provider-endpoints.ts +10 -6
- package/src/credits/credit-ledger.ts +3 -3
- package/src/db/index.ts +1 -2
- package/src/db/schema/credits.ts +1 -1
- package/src/db/schema/crypto.ts +30 -0
- package/src/db/schema/index.ts +1 -1
- package/src/fleet/node-repository.ts +8 -3
- 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/src/security/key-validation.test.ts +74 -8
- package/dist/billing/payram/cents-credits-boundary.test.js +0 -75
- 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.d.ts +0 -1
- package/dist/monetization/payram/charge-store.test.js +0 -64
- package/dist/monetization/payram/checkout.test.d.ts +0 -1
- package/dist/monetization/payram/checkout.test.js +0 -73
- package/dist/monetization/payram/client.test.d.ts +0 -1
- 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/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/webhook.test.d.ts} +0 -0
- /package/dist/monetization/{payram/cents-credits-boundary.test.d.ts → crypto/__tests__/webhook.test.d.ts} +0 -0
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
/**
|
|
3
|
-
* Regression tests for PayRam cents/credits boundary (WOP-1058).
|
|
4
|
-
*
|
|
5
|
-
* Verifies that the USD-to-cents conversion in checkout and
|
|
6
|
-
* the cents-to-credits flow in the webhook maintain correct units.
|
|
7
|
-
*
|
|
8
|
-
* If any of these tests fail after a rename/refactor, a _cents field was
|
|
9
|
-
* incorrectly changed to store Credit raw units (nanodollars) instead of
|
|
10
|
-
* USD cents. See src/monetization/credits/credit-ledger.ts for naming convention.
|
|
11
|
-
*/
|
|
12
|
-
describe("WOP-1058: PayRam cents/credits boundary", () => {
|
|
13
|
-
it("USD to cents conversion is correct (mirrors checkout.ts pattern)", () => {
|
|
14
|
-
// This mirrors the conversion in payram/checkout.ts: Math.round(opts.amountUsd * 100)
|
|
15
|
-
const amountUsd = 25;
|
|
16
|
-
const amountUsdCents = Math.round(amountUsd * 100);
|
|
17
|
-
expect(amountUsdCents).toBe(2500);
|
|
18
|
-
// Must NOT be nanodollar scale
|
|
19
|
-
expect(amountUsdCents).toBeLessThan(1_000_000);
|
|
20
|
-
});
|
|
21
|
-
it("minimum payment amount converts to valid cents", () => {
|
|
22
|
-
const MIN_PAYMENT_USD = 10;
|
|
23
|
-
const cents = Math.round(MIN_PAYMENT_USD * 100);
|
|
24
|
-
expect(cents).toBe(1000);
|
|
25
|
-
expect(Number.isInteger(cents)).toBe(true);
|
|
26
|
-
// Sanity: $10 is 1000 cents, NOT 10_000_000_000 nanodollars
|
|
27
|
-
expect(cents).toBeLessThan(1_000_000);
|
|
28
|
-
});
|
|
29
|
-
it("fractional USD amounts round correctly to cents", () => {
|
|
30
|
-
// Edge case: floating point conversion
|
|
31
|
-
const amountUsd = 10.99;
|
|
32
|
-
const cents = Math.round(amountUsd * 100);
|
|
33
|
-
expect(cents).toBe(1099);
|
|
34
|
-
expect(cents).toBeLessThan(1_000_000);
|
|
35
|
-
});
|
|
36
|
-
it("amountUsdCents stored in charge record equals USD * 100 (not nanodollars)", () => {
|
|
37
|
-
// The core invariant: payram/checkout.ts stores Math.round(amountUsd * 100)
|
|
38
|
-
// as amountUsdCents. This test proves the conversion stays at cent scale.
|
|
39
|
-
const testCases = [
|
|
40
|
-
{ usd: 10, expectedCents: 1000 },
|
|
41
|
-
{ usd: 25, expectedCents: 2500 },
|
|
42
|
-
{ usd: 50, expectedCents: 5000 },
|
|
43
|
-
{ usd: 100, expectedCents: 10000 },
|
|
44
|
-
];
|
|
45
|
-
for (const { usd, expectedCents } of testCases) {
|
|
46
|
-
const amountUsdCents = Math.round(usd * 100);
|
|
47
|
-
expect(amountUsdCents).toBe(expectedCents);
|
|
48
|
-
// CREDIT SCALE = 1_000_000_000. If this value approaches that, unit confusion occurred.
|
|
49
|
-
expect(amountUsdCents).toBeLessThan(1_000_000);
|
|
50
|
-
}
|
|
51
|
-
});
|
|
52
|
-
it("creditedCents in webhook equals amountUsdCents from charge store (1:1 for PayRam)", () => {
|
|
53
|
-
// payram/webhook.ts: const creditCents = charge.amountUsdCents;
|
|
54
|
-
// The credited amount always equals the stored USD cents — no bonus tiers for PayRam.
|
|
55
|
-
const chargeAmountUsdCents = 2500; // $25.00
|
|
56
|
-
const creditCents = chargeAmountUsdCents; // 1:1 for PayRam
|
|
57
|
-
expect(creditCents).toBe(2500);
|
|
58
|
-
// creditedCents must be 2500 (cents), not 25_000_000_000 (nanodollars)
|
|
59
|
-
expect(creditCents).toBeLessThan(1_000_000);
|
|
60
|
-
});
|
|
61
|
-
it("cents-to-nanodollar scale difference is preserved as a sanity constant", () => {
|
|
62
|
-
// Credit.SCALE = 1_000_000_000 nanodollars per dollar
|
|
63
|
-
// 1 USD cent = 10_000_000 nanodollars (SCALE / 100)
|
|
64
|
-
// This test documents the relationship so future developers understand the gap.
|
|
65
|
-
const CREDIT_SCALE = 1_000_000_000;
|
|
66
|
-
const CENTS_PER_DOLLAR = 100;
|
|
67
|
-
const NANODOLLARS_PER_CENT = CREDIT_SCALE / CENTS_PER_DOLLAR;
|
|
68
|
-
expect(NANODOLLARS_PER_CENT).toBe(10_000_000);
|
|
69
|
-
// $25 in cents = 2500. $25 in nanodollars = 25_000_000_000.
|
|
70
|
-
// These are 10_000_000x apart — confirming that mixing the two is catastrophic.
|
|
71
|
-
const twentyFiveDollarsInCents = 2500;
|
|
72
|
-
const twentyFiveDollarsInNanodollars = 25 * CREDIT_SCALE;
|
|
73
|
-
expect(twentyFiveDollarsInNanodollars / twentyFiveDollarsInCents).toBe(NANODOLLARS_PER_CENT);
|
|
74
|
-
});
|
|
75
|
-
});
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import { afterAll, beforeAll, beforeEach, describe, expect, it } from "vitest";
|
|
2
|
-
import { beginTestTransaction, createTestDb, endTestTransaction, rollbackTestTransaction } from "../../test/db.js";
|
|
3
|
-
import { PayRamChargeRepository } from "./charge-store.js";
|
|
4
|
-
describe("PayRamChargeRepository", () => {
|
|
5
|
-
let pool;
|
|
6
|
-
let db;
|
|
7
|
-
let store;
|
|
8
|
-
beforeAll(async () => {
|
|
9
|
-
({ db, pool } = await createTestDb());
|
|
10
|
-
await beginTestTransaction(pool);
|
|
11
|
-
});
|
|
12
|
-
afterAll(async () => {
|
|
13
|
-
await endTestTransaction(pool);
|
|
14
|
-
await pool.close();
|
|
15
|
-
});
|
|
16
|
-
beforeEach(async () => {
|
|
17
|
-
await rollbackTestTransaction(pool);
|
|
18
|
-
store = new PayRamChargeRepository(db);
|
|
19
|
-
});
|
|
20
|
-
it("create() stores a charge", async () => {
|
|
21
|
-
await store.create("ref-001", "tenant-1", 2500);
|
|
22
|
-
const charge = await store.getByReferenceId("ref-001");
|
|
23
|
-
expect(charge).not.toBeNull();
|
|
24
|
-
expect(charge?.referenceId).toBe("ref-001");
|
|
25
|
-
expect(charge?.tenantId).toBe("tenant-1");
|
|
26
|
-
expect(charge?.amountUsdCents).toBe(2500);
|
|
27
|
-
expect(charge?.status).toBe("OPEN");
|
|
28
|
-
expect(charge?.creditedAt).toBeNull();
|
|
29
|
-
});
|
|
30
|
-
it("getByReferenceId() returns null when not found", async () => {
|
|
31
|
-
const charge = await store.getByReferenceId("ref-nonexistent");
|
|
32
|
-
expect(charge).toBeNull();
|
|
33
|
-
});
|
|
34
|
-
it("updateStatus() updates status, currency and filled_amount", async () => {
|
|
35
|
-
await store.create("ref-002", "tenant-2", 5000);
|
|
36
|
-
await store.updateStatus("ref-002", "FILLED", "USDC", "50.00");
|
|
37
|
-
const charge = await store.getByReferenceId("ref-002");
|
|
38
|
-
expect(charge?.status).toBe("FILLED");
|
|
39
|
-
expect(charge?.currency).toBe("USDC");
|
|
40
|
-
expect(charge?.filledAmount).toBe("50.00");
|
|
41
|
-
});
|
|
42
|
-
it("updateStatus() handles partial updates (no currency)", async () => {
|
|
43
|
-
await store.create("ref-003", "tenant-3", 1000);
|
|
44
|
-
await store.updateStatus("ref-003", "VERIFYING");
|
|
45
|
-
const charge = await store.getByReferenceId("ref-003");
|
|
46
|
-
expect(charge?.status).toBe("VERIFYING");
|
|
47
|
-
expect(charge?.currency).toBeNull();
|
|
48
|
-
});
|
|
49
|
-
it("isCredited() returns false before markCredited", async () => {
|
|
50
|
-
await store.create("ref-004", "tenant-4", 1500);
|
|
51
|
-
expect(await store.isCredited("ref-004")).toBe(false);
|
|
52
|
-
});
|
|
53
|
-
it("markCredited() sets creditedAt", async () => {
|
|
54
|
-
await store.create("ref-005", "tenant-5", 3000);
|
|
55
|
-
await store.markCredited("ref-005");
|
|
56
|
-
const charge = await store.getByReferenceId("ref-005");
|
|
57
|
-
expect(charge?.creditedAt).not.toBeNull();
|
|
58
|
-
});
|
|
59
|
-
it("isCredited() returns true after markCredited", async () => {
|
|
60
|
-
await store.create("ref-006", "tenant-6", 2000);
|
|
61
|
-
await store.markCredited("ref-006");
|
|
62
|
-
expect(await store.isCredited("ref-006")).toBe(true);
|
|
63
|
-
});
|
|
64
|
-
});
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type { Payram } from "payram";
|
|
2
|
-
import type { PayRamChargeRepository } from "./charge-store.js";
|
|
3
|
-
import type { PayRamCheckoutOpts } from "./types.js";
|
|
4
|
-
/** Minimum payment amount in USD. */
|
|
5
|
-
export declare const MIN_PAYMENT_USD = 10;
|
|
6
|
-
/**
|
|
7
|
-
* Create a PayRam payment session and store the charge record.
|
|
8
|
-
*
|
|
9
|
-
* Returns the PayRam-hosted payment page URL and reference ID.
|
|
10
|
-
* The user is redirected to this URL to complete the crypto payment.
|
|
11
|
-
*/
|
|
12
|
-
export declare function createPayRamCheckout(payram: Payram, chargeStore: PayRamChargeRepository, opts: PayRamCheckoutOpts): Promise<{
|
|
13
|
-
referenceId: string;
|
|
14
|
-
url: string;
|
|
15
|
-
}>;
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
/** Minimum payment amount in USD. */
|
|
2
|
-
export const MIN_PAYMENT_USD = 10;
|
|
3
|
-
/**
|
|
4
|
-
* Create a PayRam payment session and store the charge record.
|
|
5
|
-
*
|
|
6
|
-
* Returns the PayRam-hosted payment page URL and reference ID.
|
|
7
|
-
* The user is redirected to this URL to complete the crypto payment.
|
|
8
|
-
*/
|
|
9
|
-
export async function createPayRamCheckout(payram, chargeStore, opts) {
|
|
10
|
-
if (opts.amountUsd < MIN_PAYMENT_USD) {
|
|
11
|
-
throw new Error(`Minimum payment amount is $${MIN_PAYMENT_USD}`);
|
|
12
|
-
}
|
|
13
|
-
const result = await payram.payments.initiatePayment({
|
|
14
|
-
customerEmail: `${opts.tenant}@${process.env.PLATFORM_DOMAIN ?? "wopr.bot"}`,
|
|
15
|
-
customerId: opts.tenant,
|
|
16
|
-
amountInUSD: opts.amountUsd,
|
|
17
|
-
});
|
|
18
|
-
// Store the charge record for webhook correlation.
|
|
19
|
-
await chargeStore.create(result.reference_id, opts.tenant, Math.round(opts.amountUsd * 100));
|
|
20
|
-
return {
|
|
21
|
-
referenceId: result.reference_id,
|
|
22
|
-
url: result.url,
|
|
23
|
-
};
|
|
24
|
-
}
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
-
import { beginTestTransaction, createTestDb, endTestTransaction, rollbackTestTransaction } from "../../test/db.js";
|
|
3
|
-
import { PayRamChargeRepository } from "./charge-store.js";
|
|
4
|
-
import { createPayRamCheckout, MIN_PAYMENT_USD } from "./checkout.js";
|
|
5
|
-
function createMockPayram(overrides = {}) {
|
|
6
|
-
return {
|
|
7
|
-
payments: {
|
|
8
|
-
initiatePayment: overrides.initiatePayment ??
|
|
9
|
-
vi.fn().mockResolvedValue({
|
|
10
|
-
reference_id: "ref-mock-001",
|
|
11
|
-
url: "https://payram.example.com/pay/ref-mock-001",
|
|
12
|
-
}),
|
|
13
|
-
},
|
|
14
|
-
};
|
|
15
|
-
}
|
|
16
|
-
describe("createPayRamCheckout", () => {
|
|
17
|
-
let pool;
|
|
18
|
-
let db;
|
|
19
|
-
let chargeStore;
|
|
20
|
-
let payram;
|
|
21
|
-
beforeAll(async () => {
|
|
22
|
-
({ db, pool } = await createTestDb());
|
|
23
|
-
await beginTestTransaction(pool);
|
|
24
|
-
});
|
|
25
|
-
afterAll(async () => {
|
|
26
|
-
await endTestTransaction(pool);
|
|
27
|
-
await pool.close();
|
|
28
|
-
});
|
|
29
|
-
beforeEach(async () => {
|
|
30
|
-
await rollbackTestTransaction(pool);
|
|
31
|
-
chargeStore = new PayRamChargeRepository(db);
|
|
32
|
-
payram = createMockPayram();
|
|
33
|
-
});
|
|
34
|
-
it("rejects amounts below $10 minimum", async () => {
|
|
35
|
-
await expect(createPayRamCheckout(payram, chargeStore, { tenant: "t-1", amountUsd: 5 })).rejects.toThrow(`Minimum payment amount is $${MIN_PAYMENT_USD}`);
|
|
36
|
-
});
|
|
37
|
-
it("rejects amounts of exactly $0", async () => {
|
|
38
|
-
await expect(createPayRamCheckout(payram, chargeStore, { tenant: "t-1", amountUsd: 0 })).rejects.toThrow();
|
|
39
|
-
});
|
|
40
|
-
it("calls payram.payments.initiatePayment with correct params", async () => {
|
|
41
|
-
const initiatePayment = vi.fn().mockResolvedValue({
|
|
42
|
-
reference_id: "ref-abc",
|
|
43
|
-
url: "https://payram.example.com/pay/ref-abc",
|
|
44
|
-
});
|
|
45
|
-
const mockPayram = createMockPayram({ initiatePayment });
|
|
46
|
-
await createPayRamCheckout(mockPayram, chargeStore, { tenant: "t-test", amountUsd: 25 });
|
|
47
|
-
expect(initiatePayment).toHaveBeenCalledWith({
|
|
48
|
-
customerEmail: "t-test@wopr.bot",
|
|
49
|
-
customerId: "t-test",
|
|
50
|
-
amountInUSD: 25,
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
|
-
it("stores the charge with correct amountUsdCents (converts from USD)", async () => {
|
|
54
|
-
const initiatePayment = vi.fn().mockResolvedValue({
|
|
55
|
-
reference_id: "ref-store-test",
|
|
56
|
-
url: "https://payram.example.com/pay/ref-store-test",
|
|
57
|
-
});
|
|
58
|
-
const mockPayram = createMockPayram({ initiatePayment });
|
|
59
|
-
await createPayRamCheckout(mockPayram, chargeStore, { tenant: "t-2", amountUsd: 25 });
|
|
60
|
-
const charge = await chargeStore.getByReferenceId("ref-store-test");
|
|
61
|
-
expect(charge).not.toBeNull();
|
|
62
|
-
expect(charge?.tenantId).toBe("t-2");
|
|
63
|
-
expect(charge?.amountUsdCents).toBe(2500); // $25.00 = 2500 cents
|
|
64
|
-
expect(charge?.status).toBe("OPEN");
|
|
65
|
-
});
|
|
66
|
-
it("returns referenceId and url from PayRam response", async () => {
|
|
67
|
-
const result = await createPayRamCheckout(payram, chargeStore, { tenant: "t-3", amountUsd: 10 });
|
|
68
|
-
expect(result.referenceId).toBe("ref-mock-001");
|
|
69
|
-
expect(result.url).toBe("https://payram.example.com/pay/ref-mock-001");
|
|
70
|
-
});
|
|
71
|
-
it("accepts exactly $10 (minimum boundary)", async () => {
|
|
72
|
-
await expect(createPayRamCheckout(payram, chargeStore, { tenant: "t-4", amountUsd: 10 })).resolves.not.toBeNull();
|
|
73
|
-
});
|
|
74
|
-
});
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { Payram } from "payram";
|
|
2
|
-
export function createPayRamClient(config) {
|
|
3
|
-
return new Payram({
|
|
4
|
-
apiKey: config.apiKey,
|
|
5
|
-
baseUrl: config.baseUrl,
|
|
6
|
-
config: { timeoutMs: 30_000, maxRetries: 2, retryPolicy: "safe" },
|
|
7
|
-
});
|
|
8
|
-
}
|
|
9
|
-
export function loadPayRamConfig() {
|
|
10
|
-
const apiKey = process.env.PAYRAM_API_KEY;
|
|
11
|
-
const baseUrl = process.env.PAYRAM_BASE_URL;
|
|
12
|
-
if (!apiKey || !baseUrl)
|
|
13
|
-
return null;
|
|
14
|
-
return { apiKey, baseUrl };
|
|
15
|
-
}
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import { Payram } from "payram";
|
|
2
|
-
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
3
|
-
import { createPayRamClient, loadPayRamConfig } from "./client.js";
|
|
4
|
-
describe("createPayRamClient", () => {
|
|
5
|
-
it("returns a Payram instance", () => {
|
|
6
|
-
const client = createPayRamClient({ apiKey: "test-key", baseUrl: "https://api.payram.test" });
|
|
7
|
-
expect(client).toBeInstanceOf(Payram);
|
|
8
|
-
});
|
|
9
|
-
it("passes config through to the Payram constructor", () => {
|
|
10
|
-
const client = createPayRamClient({ apiKey: "pk_live_123", baseUrl: "https://api.payram.io" });
|
|
11
|
-
expect(client).toBeInstanceOf(Payram);
|
|
12
|
-
});
|
|
13
|
-
});
|
|
14
|
-
describe("loadPayRamConfig", () => {
|
|
15
|
-
beforeEach(() => {
|
|
16
|
-
delete process.env.PAYRAM_API_KEY;
|
|
17
|
-
delete process.env.PAYRAM_BASE_URL;
|
|
18
|
-
});
|
|
19
|
-
afterEach(() => {
|
|
20
|
-
vi.unstubAllEnvs();
|
|
21
|
-
});
|
|
22
|
-
it("returns null when PAYRAM_API_KEY is missing", () => {
|
|
23
|
-
vi.stubEnv("PAYRAM_BASE_URL", "https://api.payram.test");
|
|
24
|
-
expect(loadPayRamConfig()).toBeNull();
|
|
25
|
-
});
|
|
26
|
-
it("returns null when PAYRAM_BASE_URL is missing", () => {
|
|
27
|
-
vi.stubEnv("PAYRAM_API_KEY", "pk_test_123");
|
|
28
|
-
expect(loadPayRamConfig()).toBeNull();
|
|
29
|
-
});
|
|
30
|
-
it("returns null when PAYRAM_API_KEY is an empty string", () => {
|
|
31
|
-
vi.stubEnv("PAYRAM_API_KEY", "");
|
|
32
|
-
vi.stubEnv("PAYRAM_BASE_URL", "https://api.payram.test");
|
|
33
|
-
expect(loadPayRamConfig()).toBeNull();
|
|
34
|
-
});
|
|
35
|
-
it("returns null when PAYRAM_BASE_URL is an empty string", () => {
|
|
36
|
-
vi.stubEnv("PAYRAM_API_KEY", "pk_test_123");
|
|
37
|
-
vi.stubEnv("PAYRAM_BASE_URL", "");
|
|
38
|
-
expect(loadPayRamConfig()).toBeNull();
|
|
39
|
-
});
|
|
40
|
-
it("returns config when both env vars are set", () => {
|
|
41
|
-
vi.stubEnv("PAYRAM_API_KEY", "pk_test_123");
|
|
42
|
-
vi.stubEnv("PAYRAM_BASE_URL", "https://api.payram.test");
|
|
43
|
-
const config = loadPayRamConfig();
|
|
44
|
-
expect(config).toEqual({
|
|
45
|
-
apiKey: "pk_test_123",
|
|
46
|
-
baseUrl: "https://api.payram.test",
|
|
47
|
-
});
|
|
48
|
-
});
|
|
49
|
-
it("returns null when both env vars are missing", () => {
|
|
50
|
-
expect(loadPayRamConfig()).toBeNull();
|
|
51
|
-
});
|
|
52
|
-
});
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
export type { IPayRamChargeRepository, PayRamChargeRecord } from "./charge-store.js";
|
|
2
|
-
export { DrizzlePayRamChargeRepository, PayRamChargeRepository } from "./charge-store.js";
|
|
3
|
-
export { createPayRamCheckout, MIN_PAYMENT_USD } from "./checkout.js";
|
|
4
|
-
export type { PayRamConfig } from "./client.js";
|
|
5
|
-
export { createPayRamClient, loadPayRamConfig } from "./client.js";
|
|
6
|
-
export type { PayRamBillingConfig, PayRamCheckoutOpts, PayRamPaymentState, PayRamWebhookPayload, PayRamWebhookResult, } from "./types.js";
|
|
7
|
-
export type { PayRamWebhookDeps } from "./webhook.js";
|
|
8
|
-
export { handlePayRamWebhook } from "./webhook.js";
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
export { DrizzlePayRamChargeRepository, PayRamChargeRepository } from "./charge-store.js";
|
|
2
|
-
export { createPayRamCheckout, MIN_PAYMENT_USD } from "./checkout.js";
|
|
3
|
-
export { createPayRamClient, loadPayRamConfig } from "./client.js";
|
|
4
|
-
export { handlePayRamWebhook } from "./webhook.js";
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
/** PayRam payment states (from PayRam API docs). */
|
|
2
|
-
export type PayRamPaymentState = "OPEN" | "VERIFYING" | "FILLED" | "OVER_FILLED" | "PARTIALLY_FILLED" | "CANCELLED";
|
|
3
|
-
/** Options for creating a PayRam payment session. */
|
|
4
|
-
export interface PayRamCheckoutOpts {
|
|
5
|
-
/** Internal tenant ID. */
|
|
6
|
-
tenant: string;
|
|
7
|
-
/** Amount in USD (minimum $10). */
|
|
8
|
-
amountUsd: number;
|
|
9
|
-
}
|
|
10
|
-
/** Webhook payload received from PayRam. */
|
|
11
|
-
export interface PayRamWebhookPayload {
|
|
12
|
-
/** Unique payment reference from session creation. */
|
|
13
|
-
reference_id: string;
|
|
14
|
-
/** Merchant invoice ID (echoed back if sent). */
|
|
15
|
-
invoice_id?: string;
|
|
16
|
-
/** Payment status. */
|
|
17
|
-
status: PayRamPaymentState;
|
|
18
|
-
/** Amount filled in this update. */
|
|
19
|
-
amount: string;
|
|
20
|
-
/** Currency symbol (ETH, USDC, USDT, etc.). */
|
|
21
|
-
currency: string;
|
|
22
|
-
/** Cumulative total filled so far. */
|
|
23
|
-
filled_amount: string;
|
|
24
|
-
}
|
|
25
|
-
/** Configuration for PayRam billing integration. */
|
|
26
|
-
export interface PayRamBillingConfig {
|
|
27
|
-
/** PayRam API key (from dashboard). */
|
|
28
|
-
apiKey: string;
|
|
29
|
-
/** PayRam self-hosted server base URL. */
|
|
30
|
-
baseUrl: string;
|
|
31
|
-
}
|
|
32
|
-
/** Result of processing a PayRam webhook event. */
|
|
33
|
-
export interface PayRamWebhookResult {
|
|
34
|
-
handled: boolean;
|
|
35
|
-
status: string;
|
|
36
|
-
tenant?: string;
|
|
37
|
-
creditedCents?: number;
|
|
38
|
-
reactivatedBots?: string[];
|
|
39
|
-
duplicate?: boolean;
|
|
40
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import type { ILedger } from "../../credits/ledger.js";
|
|
2
|
-
import type { IWebhookSeenRepository } from "../webhook-seen-repository.js";
|
|
3
|
-
import type { PayRamChargeRepository } from "./charge-store.js";
|
|
4
|
-
import type { PayRamWebhookPayload, PayRamWebhookResult } from "./types.js";
|
|
5
|
-
export interface PayRamWebhookDeps {
|
|
6
|
-
chargeStore: PayRamChargeRepository;
|
|
7
|
-
creditLedger: ILedger;
|
|
8
|
-
replayGuard: IWebhookSeenRepository;
|
|
9
|
-
/** Called after credits are purchased — consumer can reactivate suspended resources. Returns reactivated resource IDs. */
|
|
10
|
-
onCreditsPurchased?: (tenantId: string, ledger: ILedger) => Promise<string[]>;
|
|
11
|
-
}
|
|
12
|
-
/**
|
|
13
|
-
* Process a PayRam webhook event.
|
|
14
|
-
*
|
|
15
|
-
* Only credits the ledger on FILLED or OVER_FILLED status.
|
|
16
|
-
* Uses the PayRam reference_id mapped to the stored charge record
|
|
17
|
-
* for tenant resolution and idempotency.
|
|
18
|
-
*/
|
|
19
|
-
export declare function handlePayRamWebhook(deps: PayRamWebhookDeps, payload: PayRamWebhookPayload): Promise<PayRamWebhookResult>;
|
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import { Credit } from "../../credits/credit.js";
|
|
2
|
-
/**
|
|
3
|
-
* Process a PayRam webhook event.
|
|
4
|
-
*
|
|
5
|
-
* Only credits the ledger on FILLED or OVER_FILLED status.
|
|
6
|
-
* Uses the PayRam reference_id mapped to the stored charge record
|
|
7
|
-
* for tenant resolution and idempotency.
|
|
8
|
-
*/
|
|
9
|
-
export async function handlePayRamWebhook(deps, payload) {
|
|
10
|
-
const { chargeStore, creditLedger } = deps;
|
|
11
|
-
// Replay guard: deduplicate by reference_id + status combination.
|
|
12
|
-
const dedupeKey = `${payload.reference_id}:${payload.status}`;
|
|
13
|
-
if (await deps.replayGuard.isDuplicate(dedupeKey, "payram")) {
|
|
14
|
-
return { handled: true, status: payload.status, duplicate: true };
|
|
15
|
-
}
|
|
16
|
-
// Look up the charge record to find the tenant.
|
|
17
|
-
const charge = await chargeStore.getByReferenceId(payload.reference_id);
|
|
18
|
-
if (!charge) {
|
|
19
|
-
return { handled: false, status: payload.status };
|
|
20
|
-
}
|
|
21
|
-
// Update charge status regardless of payment state.
|
|
22
|
-
await chargeStore.updateStatus(payload.reference_id, payload.status, payload.currency, payload.filled_amount);
|
|
23
|
-
let result;
|
|
24
|
-
if (payload.status === "FILLED" || payload.status === "OVER_FILLED") {
|
|
25
|
-
// Idempotency: skip if already credited.
|
|
26
|
-
if (await chargeStore.isCredited(payload.reference_id)) {
|
|
27
|
-
result = {
|
|
28
|
-
handled: true,
|
|
29
|
-
status: payload.status,
|
|
30
|
-
tenant: charge.tenantId,
|
|
31
|
-
creditedCents: 0,
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
else {
|
|
35
|
-
// Credit the original USD amount requested (not the crypto amount).
|
|
36
|
-
// For OVER_FILLED, we still credit the requested amount — the
|
|
37
|
-
// overpayment stays in the PayRam wallet as a buffer.
|
|
38
|
-
const creditCents = charge.amountUsdCents;
|
|
39
|
-
await creditLedger.credit(charge.tenantId, Credit.fromCents(creditCents), "purchase", {
|
|
40
|
-
description: `Crypto credit purchase via PayRam (ref: ${payload.reference_id}, ${payload.currency ?? "crypto"})`,
|
|
41
|
-
referenceId: `payram:${payload.reference_id}`,
|
|
42
|
-
fundingSource: "payram",
|
|
43
|
-
});
|
|
44
|
-
await chargeStore.markCredited(payload.reference_id);
|
|
45
|
-
// Reactivate suspended resources after credit purchase.
|
|
46
|
-
let reactivatedBots;
|
|
47
|
-
if (deps.onCreditsPurchased) {
|
|
48
|
-
reactivatedBots = await deps.onCreditsPurchased(charge.tenantId, creditLedger);
|
|
49
|
-
if (reactivatedBots.length === 0)
|
|
50
|
-
reactivatedBots = undefined;
|
|
51
|
-
}
|
|
52
|
-
result = {
|
|
53
|
-
handled: true,
|
|
54
|
-
status: payload.status,
|
|
55
|
-
tenant: charge.tenantId,
|
|
56
|
-
creditedCents: creditCents,
|
|
57
|
-
reactivatedBots,
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
else {
|
|
62
|
-
// OPEN, VERIFYING, PARTIALLY_FILLED, CANCELLED — just track status.
|
|
63
|
-
result = {
|
|
64
|
-
handled: true,
|
|
65
|
-
status: payload.status,
|
|
66
|
-
tenant: charge.tenantId,
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
await deps.replayGuard.markSeen(dedupeKey, "payram");
|
|
70
|
-
return result;
|
|
71
|
-
}
|