PHP SDK

The official NahuPay PHP library requires PHP 8.1+ and uses Composer for dependency management.

Installation

composer require nahupay/nahupay-php

Initialisation

<?php
require_once 'vendor/autoload.php';

$client = new \NahuPay\Client([
    'api_key' => $_ENV['NAHUPAY_SECRET_KEY'],
]);

Payments

Create

$payment = $client->payments->create([
    'amount'      => 50000,
    'currency'    => 'ETB',
    'methods'     => ['telebirr', 'bank_transfer'],
    'success_url' => 'https://myshop.com/success',
    'cancel_url'  => 'https://myshop.com/cart',
    'metadata'    => ['order_id' => 'ORD-1234'],
]);

header('Location: ' . $payment->checkout_url);
exit;

Retrieve

$payment = $client->payments->retrieve('pay_01hx9m2k');
echo $payment->status; // "completed"

Webhooks

<?php
$payload   = file_get_contents('php://input');
$signature = $_SERVER['HTTP_NAHUPAY_SIGNATURE'] ?? '';

try {
    $event = $client->webhooks->constructEvent(
        $payload,
        $signature,
        $_ENV['NAHUPAY_WEBHOOK_SECRET']
    );
} catch (\NahuPay\Exception\SignatureVerificationException $e) {
    http_response_code(400);
    exit;
}

if ($event->type === 'payment.completed') {
    fulfillOrder($event->data->object);
}

http_response_code(200);
echo json_encode(['received' => true]);