Node.js
A dependency-free library written in TypeScript, with samples for Express, Next.js and plain http.
- Node.js 18+
- TypeScript
- ESM · CommonJS
Two clients with the same names, sync and async. In Django, Flask or a FastAPI service, type definitions help your editor and errors help your logs.
Install it into your virtual environment and read the key from an environment variable.
pip install https://hisarpay.com/downloads/hisarpay-1.0.0-py3-none-any.whlGive the amount as a Decimal or a string. Send the customer to payment_url.
import os
from hisarpay import HisarPay
client = HisarPay(os.environ["HISARPAY_API_KEY"])
def start_payment(order) -> str:
invoice = client.invoices.create({
"price_usd": order.total, # Decimal("49.90") or "49.90", never a float
"order_id": order.number, # the same order never gets a second invoice
"description": order.title,
"success_url": f"https://shop.example/orders/{order.number}",
})
return invoice["payment_url"] # redirect the customer hereHand over the raw bytes; with request.json the signature will not match.
import os
from flask import Flask, abort, request
from hisarpay import SignatureVerificationError, webhooks
app = Flask(__name__)
@app.post("/webhooks/hisarpay")
def hisarpay_webhook():
try:
event = webhooks.construct_event(
request.get_data(), # raw bytes, not request.json
request.headers.get(webhooks.SIGNATURE_HEADER),
os.environ["HISARPAY_WEBHOOK_SECRET"],
)
except SignatureVerificationError:
abort(400)
if event["livemode"] and event["type"] in ("invoice.paid", "invoice.overpaid"):
mark_paid(event["id"], event["data"]["invoice"]["order_id"]) # once per event id
return "", 204import os
from fastapi import FastAPI, HTTPException, Request, Response
from hisarpay import SignatureVerificationError, webhooks
app = FastAPI()
@app.post("/webhooks/hisarpay")
async def hisarpay_webhook(request: Request) -> Response:
try:
event = webhooks.construct_event(
await request.body(),
request.headers.get("x-hisarpay-signature"),
os.environ["HISARPAY_WEBHOOK_SECRET"],
)
except SignatureVerificationError as exc:
raise HTTPException(status_code=400, detail=str(exc))
...
return Response(status_code=204)# views.py
import os
from django.http import HttpResponse, HttpResponseBadRequest
from django.views.decorators.csrf import csrf_exempt
from hisarpay import SignatureVerificationError, webhooks
@csrf_exempt
def hisarpay_webhook(request):
try:
event = webhooks.construct_event(
request.body,
request.headers.get("X-HisarPay-Signature"),
os.environ["HISARPAY_WEBHOOK_SECRET"],
)
except SignatureVerificationError:
return HttpResponseBadRequest()
...
return HttpResponse(status=204)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.
All optional; the defaults suit most shops.
All of them extend hisarpay.HisarPayError.
| Error | When |
|---|---|
| ValidationError | One of the fields is wrong (400, 422) |
| AuthenticationError | Key missing, unknown or revoked (401) |
| PermissionDeniedError | Scope missing, IP not allowed, a live-only call with a test key (403) |
| RateLimitError | Too many requests (429) |
| APIConnectionError · APITimeoutError | The network was not reached, or the answer took too long |
| SignatureVerificationError | The webhook signature does not match |
A dependency-free library written in TypeScript, with samples for Express, Next.js and plain http.
A Composer package that works with plain PHP, Laravel and Symfony. No framework needed.
A predictable REST API for everything from invoices to withdrawals, with signed webhooks and a test mode.
Sign-up is free; your test keys are ready the moment you confirm your e-mail.