PHP code example of david-maximous / fawaterak

1. Go to this page and download the library: Download david-maximous/fawaterak 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/ */

    

david-maximous / fawaterak example snippets


use DavidMaximous\Fawaterak\Classes\FawaterakPayment;

$response = (new FawaterakPayment())
    ->setFirstName('Ahmed')
    ->setLastName('Ali')
    ->setUserEmail('[email protected]')
    ->setUserPhone('01000000000')
    ->setAmount(250)
    ->pay();

return redirect($response['url']);

// response
[
    'status'       => 'success',
    'message'      => 'Transaction link created',
    'intent_key'   => '550e8400-e29b-41d4-a716-446655440000',
    'url'          => 'https://app.fawaterk.com/ts/a1b2c',
    'expires_in'   => 2592000,
    'payment_data' => null,
]

$response = (new FawaterakPayment())
    ->setFirstName('Ahmed')
    ->setLastName('Ali')
    ->setUserEmail('[email protected]')
    ->setUserPhone('01000000000')
    ->setAmount(250)
    ->setMethod('fawry')
    ->pay();

// response - Fawry / Aman / Masary
[
    'status'           => 'success',
    'intent_key'       => '550e8400-e29b-41d4-a716-446655440000',
    'url'              => null,
    'reference_number' => '981335305',        // show this to the customer
    'expire_date'      => '2021-07-06 15:53:41',
    'payment_data'     => ['referenceNumber' => '981335305', /* ... */],
]

// response - card: 'url' holds the 3-D Secure page, redirect to it
// response - wallet: 'system_reference' and 'iso_qr' are filled instead

$response = (new FawaterakPayment())
    ->setFirstName('Ahmed')
    ->setLastName('Ali')
    ->setUserEmail('[email protected]')
    ->setUserPhone('01000000000')
    ->setAmount(250)
    ->setMethod('fawry')                              // optional - omit for hosted checkout
    ->setItemName('Order #1001')                      // optional - defaults to "default"
    ->setQuantity(2)                                  // optional - defaults to 1
    ->setCurrency('EGP')                              // optional - defaults to EGP
    ->setLanguage('ar')                               // optional - checkout page language
    ->setPayload(['order_id' => 1001])                // optional - returned in the webhook
    ->setAddress('Cairo')                             // optional
    ->setCustomerNumber('CUST-1001')                  // optional
    ->setCustomerUniqueId('user_12345')               // optional -  SMSes the link
    ->setListStyle('v')                               // optional - 'h' (default) or 'v'
    ->setRedirectOption(true)                         // optional - force hosted checkout
    ->setAuthAndCapture(1)                            // optional - 0 or 1, card flows
    ->setMobileWalletNumber('01000000000')            // optional - 

$response = (new FawaterakPayment())->pay(
    250,                        // amount
    'Ahmed',                    // first name
    'Ali',                      // last name
    '[email protected]',        // email
    '01000000000',              // phone
    'fawry',                    // optional - method
    'Order #1001',              // optional - item name
    1,                          // optional - quantity
    'EGP',                      // optional - currency
    'ar',                       // optional - language
    ['order_id' => 1001]        // optional - payload
);

$body = (new FawaterakPayment())
    ->setFirstName('Ahmed')
    ->setLastName('Ali')
    ->setUserEmail('[email protected]')
    ->setUserPhone('01000000000')
    ->setAmount(250)
    ->debug();

dd($body);

// response
[
    'cartTotal'       => 250.0,
    'currency'        => 'EGP',
    'customer'        => ['first_name' => 'Ahmed', 'last_name' => 'Ali', 'email' => '...', 'phone' => '...'],
    'cartItems'       => [['name' => 'default', 'price' => 250, 'quantity' => 1]],
    'redirectionUrls' => ['successUrl' => '...', 'failUrl' => '...', 'pendingUrl' => '...'],
    'lang'            => 'en',
]

$methods = (new FawaterakPayment())->listPaymentMethods(
    fresh: false   // optional - true bypasses the 10 minute cache
);

// response
[
    'status' => 'success',
    'data' => [
        [
            'payment_method_id'      => 2,
            'name_en'                => 'Visa-Mastercard',
            'name_ar'                => 'فيزا -ماستر كارد',
            'redirect'               => 'true',      // "false" = direct dispatch
            'logo'                   => 'https://app.fawaterk.com/clients/payment_options/mastercard-visa.png',
            'commission_on_customer' => 2,
            'integration_status'     => 1,
        ],
    ],
    'vendorSettingsData' => ['custome_iframe_title' => null],
]

$list = (new FawaterakPayment())->paymentMethodsList(
    lang: 'ar'   // optional - 'en' or 'ar', defaults to the app locale
);

// response
[2 => 'فيزا -ماستر كارد', 3 => 'فوري', 4 => 'ميزا']

use DavidMaximous\Fawaterak\Classes\FawaterakVerify;

$transaction = (new FawaterakVerify())->getTransactionData(
    intentKey: '550e8400-e29b-41d4-a716-446655440000'
);

// response
[
    'status'                 => 'success',   // the read worked; check 'paid' for the money
    'intent_key'             => '550e8400-e29b-41d4-a716-446655440000',
    'transaction_id'         => 12345,
    'paid'                   => 1,           // 1 or 0
    'status_text'            => 'paid',
    'paid_at'                => '2026-06-06 12:05:00',
    'total'                  => 100,
    'currency'               => 'EGP',
    'commission'             => 2.5,
    'customer_email'         => '[email protected]',
    'payment_method'         => 'Fawry',
    'due_date'               => '2026-06-08 12:00:00',
    'transaction_created_at' => '2026-06-06 12:00:00',
    'transaction_link'       => 'https://app.fawaterk.com/transactions/550e8400-...',
    'transaction_history'    => [['method' => ['name' => 'Fawry', 'logo' => '...'], 'amount' => '100.00 EGP', 'status' => 'success', 'reference' => '981335305', 'date' => '...']],
    'payload'                => ['order_id' => 'ORD-1001'],
    'process_data'           => [/* raw API response */],
]

$result = (new FawaterakVerify())->listTransactions(
    startDate: '2026-01-01',
    endDate:   '2026-01-31',
    perPage:   50,        // optional - defaults to 15
    page:      1,         // optional - defaults to 1
    payLoad:   'ORD-'     // optional - substring filter on your payload
);

// response
[
    'status' => 'success',
    'data' => [[
        'transaction_id'         => 12345,
        'intent_key'             => '550e8400-e29b-41d4-a716-446655440000',
        'status_text'            => 'paid',
        'total'                  => 100,
        'currency'               => 'EGP',
        'payment_method'         => 'Fawry',
        'customer_email'         => '[email protected]',
        'transaction_created_at' => '2026-06-06 12:00:00',
    ]],
    'pagination' => ['total' => 1, 'per_page' => 50, 'current_page' => 1, 'last_page' => 1, 'from' => 1, 'to' => 1],
]

$name = (new FawaterakVerify())->matchPaymentMethod(
    method: 3,
    lang: 'en'   // optional - 'en' or 'ar', defaults to the app locale
);
// "Fawry"

$hash = (new FawaterakVerify())->signature(
    stringToSign: 'TransactionId=12345&TransactionKey=550e8400-...&PaymentMethod=Fawry'
);

Route::post('/webhooks/fawaterak_json', function (Request $request) {
    $result = (new FawaterakVerify())->verifyPaidCallback($request);   // Request or plain array

    if ($result['success'] ?? false) {
        Order::where('intent_key', $result['transaction_key'])->first()->markAsPaid();
    } elseif ($result['pending'] ?? false) {
        // reference issued, nothing paid yet
    }

    return response()->json(['status' => 'ok']);   // answer 200 quickly
});

// response - paid
[
    'success'         => true,
    'transaction_key' => '550e8400-e29b-41d4-a716-446655440000',
    'transaction_id'  => 12345,
    'amount_paid'     => 100,                  // from the API - trust this one
    'paid_amount'     => '100.00',             // from the webhook body
    'currency'        => 'EGP',
    'payment_method'  => 'Fawry',
    'paid_at'         => '2026-06-06 12:05:00',
    'payload'         => ['order_id' => 'ORD-1001'],
    'customer'        => ['customer_email' => '[email protected]', /* ... */],
    'message'         => 'operation completed successfully',
    'process_data'    => [/* raw */],
]

// response - pending
['success' => false, 'pending' => true, 'transaction_key' => '...', 'reference_number' => '981335305', 'payload' => [...], 'message' => 'Payment is pending, ...']

// response - rejected
['success' => false, 'payment_id' => 12345, 'message' => 'Security checks are not passed by the system', 'process_data' => [...]]

$result = (new FawaterakVerify())->verifyFailedCallback($request);

// response
[
    'success'          => false,
    'verified'         => true,          // absent when the signature failed
    'transaction_key'  => '550e8400-...',
    'transaction_id'   => 12345,
    'payment_method'   => 'Visa-Mastercard',
    'amount'           => '150.00',
    'currency'         => 'EGP',
    'error_message'    => 'Payment declined by issuer',
    'gateway_response' => '{"gatewayCode":"DECLINED"}',
    'payload'          => ['order_id' => 'ORD-1001'],
    'process_data'     => [/* raw */],
]

$result = (new FawaterakVerify())->verifyCancelCallback($request);

// response
[
    'success'         => false,
    'verified'        => true,
    'canceled'        => true,
    'reference_id'    => 998877,
    'status'          => 'EXPIRED',      // or "CANCELED"
    'payment_method'  => 'Aman',
    'transaction_id'  => 12345,
    'transaction_key' => '550e8400-...',
    'payload'         => ['order_id' => 'ORD-1001'],
    'message'         => 'The payment reference has expired',
]

$result = (new FawaterakVerify())->verifyRefundCallback($request);

// response
[
    'success'        => true,
    'verified'       => true,
    'refunded'       => true,
    'transaction_id' => 12345,
    'amount'         => '50.00',
    'currency'       => 'EGP',
    'status'         => 1,
    'reason'         => 'Customer requested partial refund',
    'approved_at'    => '2026-06-02 10:15:00',
    'message'        => 'Refund request approved',
]

$result = (new FawaterakVerify())->verifyTokenCallback($request);

// response
[
    'success'              => true,
    'verified'             => true,
    'customer_unique_id'   => '222111333',
    'customer_token'       => '9731673377207107',   // charge this later
    'customer_card'        => '512345xxxxxx0008',
    'card_brand'           => 'MASTERCARD',
    'card_token_unique_id' => '2345',               // delete with this
]

use DavidMaximous\Fawaterak\Classes\FawaterakTokenization;

$screen = (new FawaterakTokenization())
    ->setCustomerUniqueId('user_12345')
    ->setCustomerFirstName('Ahmed')
    ->setCustomerLastName('Ali')
    ->setCustomerEmail('[email protected]')
    ->setCustomerPhone('01000000000')
    ->setCurrency('EGP')
    ->setAmount(100)                                   // optional - ttps://shop.test/cards/failed')
    ->setTokenWebhookUrl('https://shop.test/webhooks/fawaterak/token')   // optional
    ->createCardTokenScreen();

return redirect($screen['redirect_url']);

// response
[
    'status'       => 'success',
    'redirect_url' => 'https://staging.fawaterk.com/nbe/storeToken/19941166e947...',
    'message'      => 'Card token created successfully',
]

$result = (new FawaterakTokenization())
    ->setOrder(250, 'EGP')                             // amount, currency
    ->setCustomerToken('8lvfHBZC3n7lmTRO')
    ->setCardCvv('100')                                // optional - only if CVV check is on
    ->setPayload(['order_id' => 1002])                 // optional
    ->setSuccessUrl('https://shop.test/success')       // optional
    ->setFailUrl('https://shop.test/failed')           // optional
    ->payWithToken();

return redirect($result['redirect_to']);

// response
['status' => 'success', 'redirect_to' => 'https://staging.fawaterk.com/mpgs/eyJpdiI6...auth']

$result = (new FawaterakTokenization())
    ->setOrder(99, 'EGP')
    ->setCustomerToken('UFZiaC98572rKZGZ')
    ->setCardCvv('100')                    // optional
    ->setPayload(['subscription_id' => 55])  // optional
    ->payRecurring();

// response
['status' => 'success', 'transaction_id' => 1011767, 'message' => 'Recurring payment completed successfully']

$result = (new FawaterakTokenization())
    ->setCustomerUniqueId('user_12345')
    ->setCardTokenUniqueId('2345')
    ->setDeleteTokenFromBank('9731673377207107')   // optional - also removes it at the bank
    ->deleteCustomerToken();

// response
['status' => 'success', 'message' => 'token deleted']

use DavidMaximous\Fawaterak\Classes\FawaterakRefund;

$refund = new FawaterakRefund();

$refund->types(lang: 'en');     // optional - 'en' or 'ar', defaults to the app locale
$refund->reasons(lang: 'en');   // optional

// types response
['status' => 'success', 'data' => ['0' => 'invoice', '1' => 'Payment link transaction', '2' => 'Collection link transaction', '3' => 'Integration transaction']]

// reasons response
['status' => 'success', 'data' => ['Product not available', 'Customer changed their mind', 'Incorrect product delivered', 'Other']]

$result = (new FawaterakRefund())
    ->setRefundType(FawaterakRefund::INTEGRATION_TRANSACTION)
    ->setRefundId(12345)
    ->setReason('Customer changed their mind')     // must be one of reasons()
    ->setRefundableAmount(100)                     // partial or full
    ->setComment('Returned one item')              // optional - internal note
    ->create();

// response
['status' => 'success', 'message' => 'Refunded successfully']

$refund->all();               // 10 per page
$refund->details(id: 42);
$refund->delete(id: 42);      // optional arg - or use setRefundId(42) first

// all() response
[
    'status' => 'success',
    'data' => [[
        'id' => 42, 'refundable_type' => 'Invoice', 'refundable_amount' => 50,
        'status' => 'pending', 'refundable_id' => 12345, 'reason' => 'Other',
        'created_at' => '2026-06-01 10:00:00', 'can_delete' => true,
    ]],
    'pagination' => ['total' => 1, 'per_page' => 10, 'current_page' => 1, 'last_page' => 1, /* ... */],
]

// details() response
['status' => 'success', 'data' => ['id' => 42, 'refundable_amount' => 50, 'status' => 'pending', 'reason' => 'Other', 'created_at' => '...']]

// delete() response
['status' => 'success', 'message' => 'Refund request canceled successfully']

use DavidMaximous\Fawaterak\Classes\FawaterakAuth;

$auth = new FawaterakAuth();

$auth->token(force: false);   // optional arg - true skips the cache
$auth->issue();               // new client_credentials exchange
$auth->refresh(refreshToken: null);  // optional arg - defaults to the stored one
$auth->forget();              // drop the cached token, e.g. after switching environments
$auth->isConfigured();        // bool

// issue() / refresh() response
['access_token' => 'eyJ0eXAiOi...', 'refresh_token' => 'def50200...', 'token_type' => 'Bearer', 'expires_in' => 31536000, 'expires_at' => 1780000000]

app()->setLocale('ar');
$result['message'];   // "تمت العملية بنجاح"