TypeScript / JavaScript SDK

@hbank/sdk · v1.2.0 · Node 18+, Bun, Deno, edge runtimes

Download .zip
README.md

@hbank/sdk (TypeScript / JavaScript)

Client for the HBank merchant API. No dependencies; uses fetch, so it runs on Node 18+, Bun, Deno, Cloudflare Workers and Vercel/Next.js edge. Server side only: it needs your secret API key. (The browser part is the pay button script, /assets/hbank.js.)

npm install ./sdks/typescript   # builds dist/ on install
import { HBank, HBankError } from "@hbank/sdk";

const bank = new HBank({ apiKey: process.env.HBANK_API_KEY! }); // baseUrl: "http://localhost:8000" for local

const tid = await bank.createPayment({ amount: "24.90", currency: "EUR", description: "Order 1042",
                                       callback_url: "https://shop.example.com/hbank/callback" });
const checkout = bank.checkoutUrl(tid);
const { url, qr_svg } = await bank.createPaymentCode(tid);

const payment = await bank.getPayment(tid);
if (payment.status === "completed") ship(order);

const { refund, transaction } = await bank.refund(tid, { amount: "10.00", reason: "Returned one item" });

const sub = await bank.createSubscription({ amount: "9.99", currency: "EUR", interval: "month", description: "Pro plan" });
await bank.cancelSubscription(sub.id);

// Several subscriptions in one checkout: offers you made and new ones, one payment starts them all
const both = await bank.createSubscriptionsCheckout({ subscriptions: [sub.id, { amount: "2.50", currency: "EUR", interval: "week" }] });

await bank.convert(100, "EUR", "USD"); // 108.37

Answers use the API's field names (payment_code, active_period, …) and are fully typed. Amounts come back as numbers; pass strings in for exact input.

Errors

try {
  await bank.refund(tid);
} catch (e) {
  if (e instanceof HBankError) console.log(e.code, e.status, e.detail); // "not_refundable", 409, "…"
}

AuthenticationError (401/403) and HBankConnectionError (connection_failed, timeout) extend HBankError.

Pay button endpoint + callback

Express

import express from "express";
import { HBank, expressCallback } from "@hbank/sdk";

app.get("/checkout/hbank", async (req, res) => {
  const cart = await carts.get(req.query.cart);
  const tid = await bank.createPayment({ amount: cart.total, currency: "EUR", description: `Order ${cart.id}`,
                                         callback_url: "https://shop.example.com/hbank/callback" });
  res.redirect(bank.checkoutUrl(tid));
});

app.post("/hbank/callback", express.json(), expressCallback(async (cb) => {
  const order = await orders.byTransaction(cb.id);
  return order?.total === cb.amount ? true : "This order is no longer available";
}));

Next.js (app router), Hono, Remix, SvelteKit, Bun, Deno, Workers: anything with Web Request/Response:

// app/hbank/callback/route.ts
import { callbackHandler } from "@hbank/sdk";

export const POST = callbackHandler(async (cb) => (await orders.matches(cb.id, cb.amount)) || "Out of stock");
// Hono
app.post("/hbank/callback", (c) => callbackHandler(decide)(c.req.raw));

The decider returns true (charge), false, or a string shown to the customer (refuse). If it throws, the payment is refused. Callbacks are signed: pass { secret } with your signing secret (business settings → Callback URLs) to callbackHandler / expressCallback (Express: keep the raw body, express.json({ verify: (req, _res, buf) => { req.rawBody = buf; } })), or call await verifySignature(secret, header, rawBody). Check the callback against your own order too.

Tests

npm install && npm test     # builds, then runs test/ against ../testing/mock_server.py (needs Python 3)