PHP code example of keenops / php-nhiftz

1. Go to this page and download the library: Download keenops/php-nhiftz library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

keenops / php-nhiftz example snippets


use Keenops\PhpNhiftz\NhifClient;
use Keenops\PhpNhiftz\Config\Configuration;
use Keenops\PhpNhiftz\Config\Environment;

$config = new Configuration(
    clientId: 'your_client_id',
    clientSecret: 'your_client_secret',
    facilityCode: 'FAC001',
    environment: Environment::SANDBOX, // or Environment::PRODUCTION
);

$nhif = new NhifClient($config);
$nhif->setUsername('your_username');

// Verify a card
$response = $nhif->serviceHub()->verification()->getCardDetails('12345678');

if ($response->isSuccessful()) {
    $cardDetails = $response->json();
    // Process card details...
}

use Keenops\PhpNhiftz\DTOs\ServiceHub\VerifyCardModel;

$verification = new VerifyCardModel(
    cardNo: '12345678',
    verifierId: 'VER001',
    cardTypeId: '1',
    visitTypeId: 1,
    biometricMethod: 'fingerprint',
    fpCode: 'R1',
);

$response = $nhif->serviceHub()->verification()->verifyCard($verification);

use Keenops\PhpNhiftz\DTOs\ServiceHub\PatientAdmissionModel;

$admission = new PatientAdmissionModel(
    authorizationNo: 'AUTH123',
    fullName: 'John Doe',
    gender: 'M',
    dateOfBirth: new DateTime('1990-01-15'),
    admissionTypeId: 1,
    wardTypeId: 2,
    roomTypeId: 1,
    chargesPerDay: 50000.00,
    practitionerNo: 'PRAC001',
    dateAdmitted: new DateTime(),
);

$response = $nhif->serviceHub()->admissions()->admitPatient($admission);

use Keenops\PhpNhiftz\DTOs\OCS\Folio;
use Keenops\PhpNhiftz\DTOs\OCS\FolioItem;
use Keenops\PhpNhiftz\DTOs\OCS\FolioDisease;

$folio = new Folio(
    facilityCode: 'FAC001',
    claimYear: 2026,
    claimMonth: 3,
    folioNo: 1,
    cardNo: '12345678',
    firstName: 'John',
    lastName: 'Doe',
    gender: 'M',
    dateOfBirth: new DateTime('1990-01-15'),
    attendanceDate: new DateTime('2026-03-15'),
    authorizationNo: 'AUTH123',
    amountClaimed: 150000.00,
    folioItems: [
        new FolioItem(
            itemCode: 'ITEM001',
            unitPrice: 50000.00,
            itemQuantity: 3,
            amountClaimed: 150000.00,
        ),
    ],
    folioDiseases: [
        new FolioDisease(diseaseCode: 'A00'),
    ],
);

$response = $nhif->ocs()->claims()->submitFolio($folio);

'providers' => [
    // ...
    Keenops\PhpNhiftz\Integrations\Laravel\NhifServiceProvider::class,
],

'aliases' => [
    // ...
    'Nhif' => Keenops\PhpNhiftz\Integrations\Laravel\NhifFacade::class,
],

// app/Nhif/StaffUsernameResolver.php
namespace App\Nhif;

use Keenops\PhpNhiftz\Contracts\UsernameResolverInterface;

class StaffUsernameResolver implements UsernameResolverInterface
{
    public function __invoke(): ?string
    {
        // Resolve username from your application's context
        return auth()->user()?->nhif_username;
    }
}

'username_resolver' => \App\Nhif\StaffUsernameResolver::class,

// app/Providers/AppServiceProvider.php
public function boot(): void
{
    app('nhif')->setUsernameResolver(function () {
        return auth()->user()?->nhif_username;
    });
}

Nhif::setUsername('username');

use Keenops\PhpNhiftz\Integrations\Laravel\NhifFacade as Nhif;

$response = Nhif::serviceHub()->verification()->getCardDetails('12345678');

use Keenops\PhpNhiftz\NhifClient;

class PatientController extends Controller
{
    public function verify(NhifClient $nhif, string $cardNo)
    {
        return $nhif->serviceHub()->verification()->getCardDetails($cardNo);
    }
}

// config/bundles.php
return [
    // ...
    Keenops\PhpNhiftz\Integrations\Symfony\NhifBundle::class => ['all' => true],
];

// src/Nhif/StaffUsernameResolver.php
namespace App\Nhif;

use Keenops\PhpNhiftz\Contracts\UsernameResolverInterface;

class StaffUsernameResolver implements UsernameResolverInterface
{
    public function __invoke(): ?string
    {
        // Resolve username from your application's context
        return $this->security->getUser()?->getNhifUsername();
    }
}

use Keenops\PhpNhiftz\NhifClient;

class PatientController extends AbstractController
{
    public function __construct(private NhifClient $nhif) {}

    public function verify(string $cardNo): Response
    {
        $response = $this->nhif->serviceHub()->verification()->getCardDetails($cardNo);
        // ...
    }
}

use Keenops\PhpNhiftz\Exceptions\AuthenticationException;
use Keenops\PhpNhiftz\Exceptions\ApiException;
use Keenops\PhpNhiftz\Exceptions\NetworkException;
use Keenops\PhpNhiftz\Exceptions\ValidationException;

try {
    $response = $nhif->serviceHub()->verification()->getCardDetails('12345678');
} catch (AuthenticationException $e) {
    // Handle authentication errors (invalid credentials, expired token)
} catch (ApiException $e) {
    // Handle API errors (4xx, 5xx responses)
    $statusCode = $e->getCode();
    $context = $e->getContext();
} catch (NetworkException $e) {
    // Handle network errors (connection failures, timeouts)
} catch (ValidationException $e) {
    // Handle validation errors
}

use GuzzleHttp\Client;

$httpClient = new Client([
    'timeout' => 60,
    'verify' => true,
]);

$nhif = new NhifClient($config, $httpClient);

use Symfony\Component\Cache\Psr16Cache;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;

$cache = new Psr16Cache(new FilesystemAdapter());
$nhif = new NhifClient($config, null, $cache);
bash
composer 
bash
php artisan vendor:publish --tag=nhif-config