| 1 | // npm test: builds, starts sdks/testing/mock_server.py (needs Python 3) and runs the SDK against it. |
| 2 | import { spawn } from "node:child_process"; |
| 3 | import { after, before, test } from "node:test"; |
| 4 | import assert from "node:assert/strict"; |
| 5 | import { fileURLToPath } from "node:url"; |
| 6 | import { HBank, HBankError, AuthenticationError, callbackHandler, parseCallback, verifySignature } from "../dist/index.js"; |
| 7 | |
| 8 | const port = 8700 + Math.floor(Math.random() * 200); |
| 9 | const base = `http://127.0.0.1:${port}`; |
| 10 | const bank = new HBank({ apiKey: "test_key", baseUrl: base }); |
| 11 | let server; |
| 12 | |
| 13 | before(async () => { |
| 14 | const script = fileURLToPath(new URL("../../testing/mock_server.py", import.meta.url)); |
| 15 | server = spawn(process.platform === "win32" ? "python" : "python3", [script, "--port", String(port)], { stdio: ["ignore", "pipe", "inherit"] }); |
| 16 | await new Promise((resolve) => server.stdout.once("data", resolve)); |
| 17 | }); |
| 18 | after(() => server?.kill()); |
| 19 | |
| 20 | const pay = (tid) => fetch(`${base}/_mock/pay/${tid}`, { method: "POST" }); |
| 21 | |
| 22 | test("payment, QR code, refund", async () => { |
| 23 | const tid = await bank.createPayment({ amount: "24.90", currency: "EUR", description: "Order 1042", callback_url: "https://shop.example/cb" }); |
| 24 | assert.equal(bank.checkoutUrl(tid), `${base}/api/payment?tid=${tid}`); |
| 25 | const code = await bank.createPaymentCode(tid); |
| 26 | assert.match(code.qr_svg, /\/qr\.svg$/); |
| 27 | assert.equal((await bank.getPayment(tid)).status, "pending"); |
| 28 | await pay(tid); |
| 29 | const r = await bank.refund(tid, { amount: 10, reason: "Returned one item" }); |
| 30 | assert.equal(r.refund.amount, 10); |
| 31 | assert.equal(r.transaction.refundable, 14.9); |
| 32 | await assert.rejects(bank.refund(tid, { amount: 100 }), (e) => e instanceof HBankError && e.code === "amount_too_high" && e.status === 400); |
| 33 | }); |
| 34 | |
| 35 | test("errors", async () => { |
| 36 | await assert.rejects(bank.createPayment({ amount: 0, currency: "EUR" }), (e) => e.code === "invalid_amount"); |
| 37 | await assert.rejects(bank.getPayment(999999), (e) => e.code === "transaction_not_found" && e.status === 404); |
| 38 | await assert.rejects(new HBank({ apiKey: "nope", baseUrl: base }).listSubscriptions(), (e) => e instanceof AuthenticationError && e.status === 403); |
| 39 | await assert.rejects(new HBank({ apiKey: "k", baseUrl: "http://127.0.0.1:1" }).rate("EUR", "USD"), (e) => e.code === "connection_failed"); |
| 40 | }); |
| 41 | |
| 42 | test("subscriptions", async () => { |
| 43 | const s = await bank.createSubscription({ amount: 9.99, currency: "EUR", interval: "month", description: "Pro plan" }); |
| 44 | assert.equal(s.status, "offered"); |
| 45 | const c = await bank.checkoutSubscription(s.id); |
| 46 | await pay(c.transaction_id); |
| 47 | assert.equal((await bank.getSubscription(s.id)).status, "active"); |
| 48 | assert.ok((await bank.listSubscriptions()).some((x) => x.id === s.id)); |
| 49 | assert.equal((await bank.cancelSubscription(s.id)).canceled_by, "merchant"); |
| 50 | await assert.rejects(bank.cancelSubscription(s.id), (e) => e.code === "already_canceled" && e.status === 409); |
| 51 | const one = await bank.createSubscriptionCheckout({ amount: 5, currency: "EUR", interval: "week", interval_count: 2 }); |
| 52 | assert.equal(one.subscription.interval_count, 2); |
| 53 | }); |
| 54 | |
| 55 | test("several subscriptions in one checkout", async () => { |
| 56 | const offered = await bank.createSubscription({ amount: 5, currency: "EUR", interval: "month", description: "Storage" }); |
| 57 | const c = await bank.createSubscriptionsCheckout({ |
| 58 | subscriptions: [offered.id, { amount: "2.50", currency: "EUR", interval: "week", description: "Support" }], |
| 59 | callback_url: "https://shop.example/cb", |
| 60 | }); |
| 61 | assert.deepEqual(c.subscriptions.map((x) => x.description), ["Storage", "Support"]); |
| 62 | assert.equal(c.subscriptions[0].id, offered.id); |
| 63 | await pay(c.transaction_id); |
| 64 | const payment = await bank.getPayment(c.transaction_id); |
| 65 | assert.equal(payment.amount, 7.5); |
| 66 | assert.deepEqual(payment.subscription_ids, c.subscriptions.map((x) => x.id)); |
| 67 | for (const x of c.subscriptions) assert.equal((await bank.getSubscription(x.id)).status, "active"); |
| 68 | await assert.rejects(bank.createSubscriptionsCheckout({ subscriptions: [offered.id] }), (e) => e.code === "not_open" && e.status === 409); |
| 69 | await assert.rejects(bank.createSubscriptionsCheckout({ |
| 70 | subscriptions: [{ amount: 1, currency: "EUR", interval: "month" }, { amount: 1, currency: "USD", interval: "month" }], |
| 71 | }), (e) => e.code === "mixed_currencies"); |
| 72 | }); |
| 73 | |
| 74 | test("currencies", async () => { |
| 75 | assert.equal(await bank.convert(100, "EUR", "USD"), 108.37); |
| 76 | assert.equal((await bank.rate("EUR", "USD")).to_rate, 1.0837); |
| 77 | }); |
| 78 | |
| 79 | test("callback handler", async () => { |
| 80 | const handler = callbackHandler((cb) => (cb.amount === 24.9 ? true : "Wrong amount")); |
| 81 | const ask = async (body) => (await handler(new Request("http://shop/cb", { method: "POST", body: JSON.stringify(body) }))).json(); |
| 82 | assert.deepEqual(await ask({ id: 1, amount: 24.9, currency: "EUR" }), { resume: true }); |
| 83 | assert.deepEqual(await ask({ id: 1, amount: 5, currency: "EUR" }), { resume: false, error: "Wrong amount" }); |
| 84 | assert.equal((await ask({})).resume, false); |
| 85 | assert.throws(() => parseCallback("nope"), (e) => e.code === "invalid_callback"); |
| 86 | }); |
| 87 | |
| 88 | test("callback signature", async () => { |
| 89 | const body = '{"id":7310,"amount":24.9,"currency":"EUR","status":"awaiting_approval","description":"Order 1042"}'; |
| 90 | const header = "t=1700000000,v1=fcfc30b9667c3a8e8aea68178c36716153e075f79c61521bf3586088d9877281"; |
| 91 | assert.equal(await verifySignature("hbcb_test", header, body, { now: 1700000100 }), true); |
| 92 | assert.equal(await verifySignature("hbcb_test", header, new TextEncoder().encode(body), { now: 1700000100 }), true); |
| 93 | assert.equal(await verifySignature("hbcb_other", header, body, { now: 1700000100 }), false); |
| 94 | assert.equal(await verifySignature("hbcb_test", header, body.replace("24.9", "0.1"), { now: 1700000100 }), false); |
| 95 | assert.equal(await verifySignature("hbcb_test", header, body, { now: 1700000400 }), false); |
| 96 | assert.equal(await verifySignature("hbcb_test", "garbage", body), false); |
| 97 | |
| 98 | // The handler refuses an unsigned callback when given the secret. |
| 99 | const handler = callbackHandler(() => true, { secret: "hbcb_test" }); |
| 100 | const res = await handler(new Request("http://x/cb", { method: "POST", body })); |
| 101 | assert.deepEqual(await res.json(), { resume: false, error: "Invalid callback" }); |
| 102 | }); |