PHP code example of darshphpdev / laravel-edfapay
1. Go to this page and download the library: Download darshphpdev/laravel-edfapay 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/ */
darshphpdev / laravel-edfapay example snippets
return [
// Choose operating environment mode: 'demo' or 'live'
'mode' => env('EDFAPAY_MODE', 'demo'),
// EdfaPay API Key
'api_key' => env('EDFAPAY_API_KEY'),
// API Environment Base URLs
'urls' => [
'demo' => '[https://demo-api.edfapay.com](https://demo-api.edfapay.com)',
'live' => '[https://app-api.edfapay.com](https://app-api.edfapay.com)',
],
// Default Fallback Currency
'currency' => env('EDFAPAY_CURRENCY', 'SAR'),
// Webhook Route Settings
'webhook' => [
'enable_default_route' => true,
'path' => 'api/v1/payments/edfapay/webhook',
'middleware' => ['api'],
],
];
use DarshPhpDev\EdfaPay\Facades\EdfaPay;
$response = EdfaPay::initSale()
->setOrderId('INV-2026-00941')
->setAmount(250.50) // Float or numeric strings are safely converted to precise decimals
->setCurrency('SAR')
->setUrls('https://yourdomain.com/payment/success', 'https://yourdomain.com/payment/failure')
->setNotificationUrl('https://yourdomain.com/api/v1/payments/edfapay/webhook') // Dynamic dynamic webhook link option
->setCustomerDetails([
'name' => 'Mustafa Ahmed',
'email' => '[email protected] ',
'phone' => '+966500000001',
])
->setAddress([
'country' => 'SA',
'city' => 'Riyadh',
'address' => 'Olaya District',
])
->initiate();
// Extract the gateway checkout URL from the response array
if (isset($response['redirectUrl'])) {
return redirect()->away($response['redirectUrl']);
}
use DarshPhpDev\EdfaPay\Facades\EdfaPay;
use DarshPhpDev\EdfaPay\Exceptions\EdfaPayValidationException;
use DarshPhpDev\EdfaPay\Exceptions\EdfaPayApiException;
try {
$response = EdfaPay::initSale()
->setOrderId('INV-2026-00941')
->setAmount(250.50)
->setUrls('https://yourdomain.com/payment/success', 'https://yourdomain.com/payment/failure')
->setCustomerDetails(['name' => 'Mustafa Ahmed', 'email' => '[email protected] ', 'phone' => '+966500000001'])
->initiate();
} catch (EdfaPayValidationException $e) {
// Missing or invalid
use DarshPhpDev\EdfaPay\Facades\EdfaPay;
use DarshPhpDev\EdfaPay\Exceptions\EdfaPayApiException;
try {
$response = EdfaPay::queryTransaction('fa522cc3-7b92-467b-b132-40794bf4734f');
} catch (EdfaPayApiException $e) {
dd($e->getCode(), $e->getResponseBody());
}
use DarshPhpDev\EdfaPay\Facades\EdfaPay;
use DarshPhpDev\EdfaPay\Exceptions\EdfaPayValidationException;
use DarshPhpDev\EdfaPay\Exceptions\EdfaPayApiException;
try {
$response = EdfaPay::initRefund()
->setTransactionId('fa522cc3-7b92-467b-b132-40794bf4734f')
->setAmount(250.50)
->setReason('Customer requested cancellation') // optional
->refund();
} catch (EdfaPayValidationException $e) {
dd($e->getErrors());
} catch (EdfaPayApiException $e) {
dd($e->getCode(), $e->getResponseBody());
}
protected $listen = [
\DarshPhpDev\EdfaPay\Events\EdfaPayWebhookReceived::class => [
\App\Listeners\ProcessEdfaPayCallback::class,
],
];
namespace App\Listeners;
use DarshPhpDev\EdfaPay\Events\EdfaPayWebhookReceived;
use App\Models\Order;
class ProcessEdfaPayCallback
{
public function handle(EdfaPayWebhookReceived $event)
{
$payload = $event->payload;
$orderId = $payload['orderId'] ?? null;
$status = $payload['status'] ?? null; // e.g. 'Approved' or 'Declined'
$transactionId = $payload['transactionId'] ?? null;
$order = Order::where('id', $orderId)->firstOrFail();
$order->update([
'status' => strtolower($status) === 'approved' ? 'paid' : 'failed',
'transaction_id' => $transactionId,
]);
}
}
bash
php artisan vendor:publish --provider="DarshPhpDev\EdfaPay\EdfaPayServiceProvider" --tag="edfapay-config"
bash
php artisan edfapay:check