Skip to content
HisarPay

Crypto payments in PHP

A plain Composer package that asks for no framework. On shared hosting or inside Laravel or Symfony, a few lines take the payment.

  • PHP 8.1+
  • Composer
  • Laravel · Symfony

What it does

  • Needs only the curl and json extensions; no other package comes along.
  • Responses are plain arrays and amounts are strings, so no cent is lost.
  • Errors are separate exceptions by kind and carry the request ID.
  • Verifying with an empty secret never passes silently; it throws.
Creating an invoice from a terminal

Requirements

PHP
8.1 or later
Extensions
curl, json
HisarPay
A test key (hp_test_…)

Setup

  1. Step 1

    Install

    Without Composer, copy the source folder and write a PSR-4 loader; the sample project does exactly that.

    Terminal
    composer config repositories.hisarpay composer https://hisarpay.com/downloads
    composer require hisarpay/hisarpay-php
  2. Step 2

    Open the invoice

    Read the amount from the order on your server, never from the form. Send the customer to payment_url.

    PHP
    <?php
    require 'vendor/autoload.php';
    
    use HisarPay\Exception\HisarPayException;
    use HisarPay\HisarPay;
    
    $hisarpay = new HisarPay(getenv('HISARPAY_API_KEY'));
    
    try {
        $invoice = $hisarpay->invoices->create([
            'price_usd'   => $order['total'],     // "49.90", a decimal string
            'order_id'    => $order['number'],    // the same order never gets a second invoice
            'description' => $order['title'],
            'success_url' => 'https://shop.example/orders/' . $order['number'],
        ]);
    } catch (HisarPayException $e) {
        error_log($e->getMessage() . ' ' . $e->getRequestId());
        http_response_code(502);
        exit;
    }
    
    header('Location: ' . $invoice['payment_url'], true, 303);
  3. Step 3

    Verify the webhook

    Read the raw body. In Laravel, keep the route out of the CSRF check and hand the work to a queue.

    <?php
    use HisarPay\Exception\SignatureVerificationException;
    use HisarPay\Webhook;
    
    $raw = file_get_contents('php://input');          // raw bytes, not $_POST
    
    try {
        $event = Webhook::constructEvent(
            $raw,
            $_SERVER['HTTP_X_HISARPAY_SIGNATURE'] ?? null,
            getenv('HISARPAY_WEBHOOK_SECRET'),
        );
    } catch (SignatureVerificationException) {
        http_response_code(400);
        exit;
    }
    
    if ($event['livemode'] && in_array($event['type'], ['invoice.paid', 'invoice.overpaid'], true)) {
        markPaid($event['id'], $event['data']['invoice']['order_id']);   // once per event id
    }
    http_response_code(200);
  4. Step 4

    Run the sample shop

    Download the example project from the top of this page: a small shop with three products that verifies the webhook and shows the state of an order. The library is inside; put your test key in .env and start it.

Client options

All optional; the defaults suit most shops.

baseUrl
Defaults to https://hisarpay.com. Point it at a local API during development.
timeout
Timeout of one attempt in seconds. Defaults to 30.
maxRetries
Retries after the first attempt (network errors, timeouts, 429, 5xx). Defaults to 2.
transport
Your own HTTP layer (TransportInterface).
idempotencyKey
Your own key for one POST request. Left out, the library makes one and keeps it across retries.

Exceptions

All of them extend HisarPay\Exception\HisarPayException.

ExceptionWhen
ValidationExceptionOne of the fields is wrong (400, 422)
AuthenticationExceptionKey missing, unknown or revoked (401)
PermissionDeniedExceptionScope missing, IP not allowed, a live-only call with a test key (403)
NotFoundExceptionNo such record (404)
SignatureVerificationExceptionThe webhook signature does not match

Frequently asked questions

Does it run on shared hosting?
Yes, with PHP 8.1 and curl. Your webhook address must be reachable from outside over https.
Should I use this for WordPress or OpenCart?
No. There are ready-made plugins for those; the library is for sites you write yourself.
How should I calculate with amounts?
With a decimal library such as bcmath. With float, amounts of six decimals may come out different.
Which licence does the library have?
MIT. Read the source and ship it with your project.

Node.js

A dependency-free library written in TypeScript, with samples for Express, Next.js and plain http.

  • Node.js 18+
  • TypeScript
  • ESM · CommonJS

Python

Sync and async clients, samples for Flask, FastAPI and Django, type definitions included.

  • Python 3.9+
  • sync · async
  • Flask · FastAPI · Django

OpenCart

Separate builds for OpenCart 4 and 3, with a connection test button and configurable order status mapping.

  • OpenCart 4 · PHP 8+
  • OpenCart 3 · PHP 7.1+

Your first collection is an e-mail away

Sign-up is free; your test keys are ready the moment you confirm your e-mail.