PHP SDK
hbank/sdk · PHP 8.1+ with curl
hbank/sdk (PHP)
Client for the HBank merchant API. PHP 8.1+ with the curl extension; no Composer dependencies.
// composer.json of your shop
"repositories": [{ "type": "path", "url": "../HBank9/sdks/php" }],
"require": { "hbank/sdk": "*" }
use HBank\HBankClient;
$bank = new HBankClient(getenv('HBANK_API_KEY')); // second argument: 'http://localhost:8000' for local
$tid = $bank->createPayment('24.90', 'EUR', 'Order 1042', 'https://shop.example.com/hbank/callback');
header('Location: ' . $bank->checkoutUrl($tid));
$qr = $bank->createPaymentCode($tid); // ['code' => …, 'url' => …, 'qr_svg' => …]
$payment = $bank->getPayment($tid);
if ($payment['status'] === 'completed') { ship($order); }
$result = $bank->refund($tid, '10.00', 'Returned one item'); // amount null = everything left
$sub = $bank->createSubscription('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
$both = $bank->createSubscriptionsCheckout([$sub['id'], ['amount' => '2.50', 'currency' => 'EUR', 'interval' => 'week']]);
Answers are arrays with the API's field names. Pass amounts as strings ('24.90') for exact values; amounts in answers are floats.
Errors
try {
$bank->refund($tid);
} catch (\HBank\HBankException $e) {
$e->errorCode; // 'not_refundable', 'invalid_api_key', 'connection_failed', …
$e->status; // 409; 0 if HBank couldn't be reached
$e->detail; // readable message, if any
$e->isAuthentication(); $e->isConnection();
}
Callbacks
The callback's amount is a string with the exact digits ('24.90'); compare with bccomp() or your money type.
Plain PHP
$cb = \HBank\Callback::fromRequest();
\HBank\Callback::respond(orderMatches($cb->id, $cb->amount)
? \HBank\Callback::approve()
: \HBank\Callback::refuse('This order is no longer available'));
Laravel: add the route to the CSRF exceptions (VerifyCsrfToken::$except, or validateCsrfTokens(except: [...]) in Laravel 11).
Route::get('/checkout/hbank', function (Request $request, HBankClient $bank) {
$cart = Cart::findOrFail($request->query('cart'));
$tid = $bank->createPayment($cart->total, 'EUR', "Order {$cart->id}", route('hbank.callback'));
return redirect()->away($bank->checkoutUrl($tid));
});
Route::post('/hbank/callback', function (Request $request) {
$cb = \HBank\Callback::parse($request->getContent());
$ok = Order::where('hbank_transaction_id', $cb->id)->where('total', $cb->amount)->exists();
return response()->json($ok ? \HBank\Callback::approve() : \HBank\Callback::refuse('Out of stock'));
})->name('hbank.callback');
// AppServiceProvider::register()
$this->app->singleton(HBankClient::class, fn () => new HBankClient(config('services.hbank.key')));
Symfony
#[Route('/hbank/callback', methods: ['POST'])]
public function callback(Request $request): JsonResponse
{
$cb = \HBank\Callback::parse($request->getContent());
return new JsonResponse($this->orders->matches($cb->id, $cb->amount)
? \HBank\Callback::approve() : \HBank\Callback::refuse('Out of stock'));
}
Callbacks are signed: Callback::fromRequest($secret) refuses one without a valid HBank-Signature (your signing secret (business settings → Callback URLs)), or call Callback::verifySignature($secret, $header, $rawBody). Check the callback against your own order too.
Tests
php tests/run.php # starts ../testing/mock_server.py (needs Python 3)