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/ */
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'