myfatoorah/library
PHP library for integrating MyFatoorah payment gateway. Create invoices, execute and verify payments, handle callbacks, and manage payment status in your Laravel or PHP app with a simple API wrapper and configuration options.
Installation Add the package via Composer:
composer require myfatoorah/library
Publish the config file (if needed):
php artisan vendor:publish --provider="MyFatoorah\Library\MyFatoorahServiceProvider" --tag="config"
Configuration
Set your API credentials in .env:
MYFATOORAH_MERCHANT_ID=your_merchant_id
MYFATOORAH_API_USERNAME=your_username
MYFATOORAH_API_PASSWORD=your_password
MYFATOORAH_API_KEY=your_api_key
MYFATOORAH_API_SECRET=your_api_secret
MYFATOORAH_API_URL=https://api.myfatoorah.com
First Use Case: Create an Invoice
use MyFatoorah\Library\MyFatoorah;
$myFatoorah = new MyFatoorah([
'merchant_id' => env('MYFATOORAH_MERCHANT_ID'),
'api_username' => env('MYFATOORAH_API_USERNAME'),
'api_password' => env('MYFATOORAH_API_PASSWORD'),
'api_key' => env('MYFATOORAH_API_KEY'),
'api_secret' => env('MYFATOORAH_API_SECRET'),
'api_url' => env('MYFATOORAH_API_URL'),
]);
$invoice = $myFatoorah->createInvoice([
'invoice_number' => 'INV-123',
'amount' => 100.00,
'currency_code' => 'SAR',
'customer' => [
'name' => 'John Doe',
'email' => 'john@example.com',
'phone_number' => '123456789',
],
'items' => [
[
'name' => 'Product 1',
'quantity' => 1,
'price' => 100.00,
],
],
]);
dd($invoice);
Invoice Management
createInvoice() for new invoices.getInvoice() to fetch an invoice by ID.cancelInvoice() to void a payment.Payment Links Generate a payment link for customer checkout:
$paymentLink = $myFatoorah->createPaymentLink([
'invoice_number' => 'INV-123',
'amount' => 100.00,
'currency_code' => 'SAR',
'customer' => [...],
'success_url' => route('payment.success'),
'failure_url' => route('payment.failure'),
]);
Webhooks Configure webhook endpoints in the MyFatoorah dashboard and validate payloads in Laravel:
public function handleWebhook(Request $request)
{
$myFatoorah = new MyFatoorah([...]);
$isValid = $myFatoorah->validateWebhook($request->all());
if ($isValid) {
// Process payment status update
}
}
Refunds Refund a paid invoice:
$refund = $myFatoorah->createRefund([
'invoice_id' => 123,
'amount' => 50.00,
'reason' => 'Partial refund',
]);
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->singleton(MyFatoorah::class, function ($app) {
return new MyFatoorah([
'merchant_id' => env('MYFATOORAH_MERCHANT_ID'),
// ...
]);
});
}
FormRequest:
use Illuminate\Foundation\Http\FormRequest;
class CreateInvoiceRequest extends FormRequest
{
public function rules()
{
return [
'invoice_number' => 'required|string',
'amount' => 'required|numeric|min:0.01',
'currency_code' => 'required|string|size:3',
];
}
}
$myFatoorah = new MyFatoorah([...], [
'debug' => true,
'log_file' => storage_path('logs/myfatoorah.log'),
]);
API Rate Limits
429 Too Many Requests gracefully:
try {
$invoice = $myFatoorah->getInvoice($id);
} catch (\MyFatoorah\Library\Exceptions\RateLimitException $e) {
// Retry with exponential backoff
}
Webhook Validation
validateWebhook() to prevent spoofing:
$isValid = $myFatoorah->validateWebhook($request->all(), $request->get('signature'));
if (!$isValid) {
abort(403, 'Invalid webhook signature');
}
Currency and Amount Precision
100.00 instead of 100) to avoid rounding errors.Invoice Number Uniqueness
$invoiceNumber = 'ORD-' . Str::upper(Strings::uuid());
'debug' => true in the client config to log API requests/responses to storage/logs/myfatoorah.log.MYFATOORAH_API_URL=https://sandbox.myfatoorah.com) for development:
$myFatoorah = new MyFatoorah([...], [
'api_url' => 'https://sandbox.myfatoorah.com',
]);
InvalidSignatureException: Verify api_key and api_secret in .env.InvalidRequestException: Check payload structure against MyFatoorah’s API docs.Custom Responses Extend the library to transform responses:
$myFatoorah->setResponseTransformer(function ($response) {
return [
'data' => $response->data,
'meta' => [
'status' => $response->status,
'message' => $response->message,
],
];
});
Middleware for API Calls Add middleware to log or modify requests:
$myFatoorah->setRequestMiddleware(function ($request) {
$request->headers->set('X-Custom-Header', 'value');
});
Event Dispatching Trigger Laravel events for payment status changes:
$myFatoorah->on('payment.succeeded', function ($payload) {
event(new PaymentSucceeded($payload));
});
How can I help you explore Laravel packages today?