hbank-sdk (Java)
Client for the HBank merchant API. Java 17+, no dependencies (uses java.net.http and a small built-in JSON reader, so it won't clash with your Jackson or Gson version).
cd sdks/java && mvn install # then depend on cloud.thehsi.hbank:hbank-sdk:1.1.0
import cloud.thehsi.hbank.sdk.*;
import java.math.BigDecimal;
HBankClient bank = HBankClient.builder(System.getenv("HBANK_API_KEY"))
// .baseUrl("http://localhost:8000")
.build();
String tid = bank.createPayment(new BigDecimal("24.90"), "EUR", "Order 1042", "https://shop.example.com/hbank/callback");
String checkout = bank.checkoutUrl(tid);
Models.PaymentCode qr = bank.createPaymentCode(tid);
Models.Payment payment = bank.getPayment(tid);
if (payment.completed()) ship(order);
Models.RefundResult r = bank.refund(tid, new BigDecimal("10.00"), "Returned one item");
Models.Subscription sub = bank.createSubscription(new BigDecimal("9.99"), "EUR", "month", 1, "Pro plan");
bank.cancelSubscription(sub.id());
// Several subscriptions in one checkout: offers you made and new ones, one payment starts them all
var both = bank.createSubscriptionsCheckout(java.util.List.of(
Models.SubscriptionCheckoutItem.existing(sub.id()),
Models.SubscriptionCheckoutItem.offer(new BigDecimal("2.50"), "EUR", "week", 1, null)), null, null);
BigDecimal usd = bank.convert(new BigDecimal("100"), "EUR", "USD");
Answers are records; amounts BigDecimal, timestamps Instant. The client is thread-safe; make it a singleton.
Errors
HBankException is unchecked; code(), status(), detail(). Subclasses HBankException.Authentication (401/403) and HBankException.Connection (connection_failed / timeout; may or may not have reached HBank).
Spring Boot
@Configuration
class HBankConfig {
@Bean
HBankClient hbank(@Value("${hbank.api-key}") String key) {
return HBankClient.builder(key).build();
}
}
@RestController
class CheckoutController {
private final HBankClient bank;
private final Orders orders;
CheckoutController(HBankClient bank, Orders orders) { this.bank = bank; this.orders = orders; }
@GetMapping("/checkout/hbank")
ResponseEntity<Void> start(@RequestParam long cart) {
Cart c = orders.cart(cart);
String tid = bank.createPayment(c.total(), "EUR", "Order " + c.id(), "https://shop.example.com/hbank/callback");
return ResponseEntity.status(HttpStatus.FOUND).location(URI.create(bank.checkoutUrl(tid))).build();
}
@PostMapping(value = "/hbank/callback", produces = "application/json")
Map<String, Object> callback(@RequestBody String body) {
HBankCallback cb = HBankCallback.parse(body);
return orders.matches(cb.id(), cb.amount())
? HBankCallback.approve()
: HBankCallback.refuse("This order is no longer available");
}
}
Exclude the callback from CSRF protection if Spring Security is on. For servlets or Javalin, write HBankCallback.toJson(answer) with content type application/json. Callbacks are signed: check HBankCallback.verifySignature(secret, request.getHeader("HBank-Signature"), body) on the raw body with your signing secret (business settings → Callback URLs) before you approve, and check the callback against your own order.
Tests
mvn -q test-compile exec:java # runs HBankClientTest against ../testing/mock_server.py (needs Python 3)