PHP code example of mahmoud-hamed / laravel-nafith

1. Go to this page and download the library: Download mahmoud-hamed/laravel-nafith 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/ */

    

mahmoud-hamed / laravel-nafith example snippets


'nafith' => [
    'driver' => 'daily',
    'path'   => storage_path('logs/nafith.log'),
    'level'  => env('LOG_NAFITH_LEVEL', 'info'),
    'days'   => 14,
],
'nafith_webhook' => [
    'driver' => 'daily',
    'path'   => storage_path('logs/nafith-webhook.log'),
    'level'  => env('LOG_NAFITH_WEBHOOK_LEVEL', 'info'),
    'days'   => 14,
],

use MahmoudHamed\Nafith\Services\NafithService;

$nafithService = app(NafithService::class);

$sanad = $nafithService->createSanad([
    'debtor_national_id' => '1000000001',
    'debtor_phone'       => '+966500000000',
    'debtor_name'        => 'Ahmad Ali',
    'amount'             => 30000.00,
    'currency'           => 'SAR',
    'reference_id'       => 'ORDER-12345',
    'nafithable_type'    => App\Models\Order::class,
    'nafithable_id'      => 12345,
]);

// $sanad is a MahmoudHamed\Nafith\Models\NafithSanad instance
echo $sanad->sanad_number;  // e.g. "1010062627658168"
echo $sanad->status;         // "pending"

$nafithService->closeSanad($sanad);
// Automatically sends CLOSED if approved, CANCELLED_BY_CREDITOR otherwise

$freshSanad = $nafithService->syncStatus($sanad);
// Returns updated model if status changed, null if unchanged

use Illuminate\Support\Facades\Schedule;

Schedule::command('nafith:sync-statuses')->everyFiveMinutes();

'webhook_uri'       => 'nafith/callback',  // The path
'webhook_prefix'    => '',                  // Optional prefix (e.g. 'api')
'webhook_middleware' => ['throttle:60,1'],  // Public endpoint protection

use MahmoudHamed\Nafith\Events\NafithWebhookReceived;

// In your EventServiceProvider or via Event::listen()
Event::listen(NafithWebhookReceived::class, function (NafithWebhookReceived $event) {
    $data = $event->data;

    // $data['status']        -- normalized status (e.g. 'approved')
    // $data['sanad_number']  -- the Sanad number
    // $data['group_id']      -- the Sanad group ID
    // $data['reference_id']  -- your reference ID
    // $data['payload']       -- the full raw webhook payload

    if ($data['status'] === 'approved') {
        // Send notification, update order status, etc.
    }
});

use MahmoudHamed\Nafith\Models\NafithSanad;

class Order extends Model
{
    public function nafithSanads()
    {
        return $this->morphMany(NafithSanad::class, 'nafithable');
    }
}

use MahmoudHamed\Nafith\Models\NafithSanad;
use MahmoudHamed\Nafith\NafithStatus;

// All pending sanads
$sanads = NafithSanad::pending()->get();

// All expired pending sanads (past approval window)
$sanads = NafithSanad::expiredPending()->get();

// Sanads for a specific model
$sanads = NafithSanad::forModel(App\Models\Order::class, $orderId)->get();

// Check status
$sanad->isNafithApproved();  // true/false
$sanad->isNafithPending();   // true/false
$sanad->isExpired();         // true/false
$sanad->canClose();          // true/false

$sanad = NafithSanad::find(1);
$parentModel = $sanad->nafithable; // Returns the related Order, Bond, etc.

use MahmoudHamed\Nafith\Nafith;

$nafith = app(Nafith::class);

// Create a Sanad
$response = $nafith->createSingleSanad([
    'debtor' => ['national_id' => '1000000001'],
    'city_of_issuance' => '1',
    'debtor_phone_number' => '+966500000000',
    'total_value' => 30000,
    'currency' => 'SAR',
    'max_approve_duration' => 360,
    'reference_id' => 'MY-REF-123',
    'sanad' => [
        ['due_type' => 'upon request', 'total_value' => 30000, 'reference_id' => 'SANAD-1'],
    ],
]);

// Close a Sanad
$response = $nafith->closeSanad('group-id-123', 'closed');

// Get status
$response = $nafith->getSanadStatus('group-id-123');

// Verify a webhook signature
$valid = $nafith->verifyWebhook($signature, $timestamp, $rawBody, $path);

// Normalize any status string
$normalized = $nafith->normalizeStatus('accept'); // returns 'approved'

use MahmoudHamed\Nafith\NafithStatus;

NafithStatus::PENDING;                // 'pending'
NafithStatus::APPROVED;               // 'approved'
NafithStatus::REJECTED_BY_DEBTOR;     // 'rejected_by_debtor'
NafithStatus::NO_RESPONSE;            // 'no_response'
NafithStatus::CANCELLED_BY_CREDITOR;  // 'cancelled_by_creditor'
NafithStatus::CLOSED;                 // 'closed'

// Helpers
NafithStatus::PENDING->label();      // English label
NafithStatus::PENDING->labelAr();    // Arabic label
NafithStatus::PENDING->color();      // Bootstrap color
NafithStatus::PENDING->isActive();   // true (pending/approved are active)
NafithStatus::PENDING->isFinal();    // false
NafithStatus::PENDING->canApprove(); // true
NafithStatus::PENDING->canReject();  // true
NafithStatus::PENDING->canCancel();  // true

// From string
NafithStatus::fromString('approved'); // NafithStatus::APPROVED
NafithStatus::fromString(null);       // null
bash
php artisan vendor:publish --tag=nafith-config
php artisan vendor:publish --tag=nafith-migrations
bash
php artisan migrate

POST {your-domain}/nafith/callback