1. Go to this page and download the library: Download mlquarizm/payment-gateway 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/ */
mlquarizm / payment-gateway example snippets
namespace App\Http\Middleware;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;
class VerifyCsrfToken extends Middleware
{
/**
* The URIs that should be excluded from CSRF verification.
*
* @var array<int, string>
*/
protected $except = [
'payment/callback/*', // Payment callbacks
'webhooks/payment/*', // Payment webhooks
];
}
return [
// Default payment gateway to use when not specified
'default_gateway' => env('PAYMENT_DEFAULT_GATEWAY', 'tabby'),
// Fallback URL when gateway-specific redirect_*_url are not set (callback appends ?status=...&gateway=...)
'redirect_fallback_url' => env('PAYMENT_REDIRECT_FALLBACK_URL', ''),
// URL the user is sent to after the package status Blade (success/error/cancel). Use {order_id} placeholder; replaced by payable_id (e.g. order id).
'redirect_after_status_url' => env('PAYMENT_REDIRECT_AFTER_STATUS_URL', ''),
// When redirect_after_status_url is empty, Blade redirects here after 5s. Use a public URL (e.g. dashboard); avoid auth/login.
'redirect_after_status_fallback_url' => env('PAYMENT_REDIRECT_AFTER_STATUS_FALLBACK_URL', ''),
// Callbacks configuration for payment events
'callbacks' => [
'tabby' => [
'on_success' => null, // Callable: fn($transaction) => void
'on_failure' => null, // Callable: fn($transaction, $reason) => void
],
'tamara' => [
'on_success' => null, // Callable: fn($transaction) => void
'on_failure' => null, // Callable: fn($transaction, $reason) => void
],
],
// Payment transaction configuration
'transaction' => [
'table' => 'payment_transactions', // Database table name
'polymorphic' => true, // Enable polymorphic relationships
],
];
use MLQuarizm\PaymentGateway\Factory\PaymentGatewayFactory;
use MLQuarizm\PaymentGateway\DTOs\TabbyPaymentDTO;
use MLQuarizm\PaymentGateway\DTOs\PaymentOrderDTO;
use MLQuarizm\PaymentGateway\DTOs\BuyerDTO;
use MLQuarizm\PaymentGateway\DTOs\BuyerHistoryDTO;
use MLQuarizm\PaymentGateway\DTOs\AddressDTO;
use MLQuarizm\PaymentGateway\DTOs\OrderItemDTO;
// Build DTOs
$orderDTO = new PaymentOrderDTO(
id: $order->id,
referenceId: (string) $order->id,
amount: 500.00,
currency: 'SAR',
description: "Order #{$order->id}"
);
$buyerDTO = new BuyerDTO(
name: $client->name,
email: $client->email, // optional (nullable)
phone: $client->full_phone // optional (nullable)
);
$addressDTO = new AddressDTO(
city: $order->city->name,
address: $order->address,
zip: $order->postal_code,
countryCode: 'SA'
);
$itemDTO = new OrderItemDTO(
referenceId: "service-{$order->service->id}",
title: $order->service->name,
description: $order->service->description,
quantity: 1,
unitPrice: 500.00
);
$buyerHistoryDTO = new BuyerHistoryDTO(
registeredSince: $client->created_at->toISOString(),
loyaltyLevel: 12,
isPhoneVerified: true,
isEmailVerified: (bool) $client->email_verified_at
);
$tabbyDTO = new TabbyPaymentDTO(
order: $orderDTO,
buyer: $buyerDTO,
shippingAddress: $addressDTO, // optional (nullable)
items: [$itemDTO],
buyerHistory: $buyerHistoryDTO, //
$paymentInfo = $gateway->initiatePayment($tabbyDTO);
if (!$paymentInfo[‘success’]) {
// Session was rejected by Tabby’s pre-scoring
return back()->withErrors([‘payment’ => $paymentInfo[‘message’]]);
}
// Only record transaction and redirect if session was created successfully
PaymentGateway::recordTransaction(...);
return redirect($paymentInfo[‘url’]);
[
‘url’ => null,
‘session_id’ => null,
‘payment_id’ => null,
‘success’ => false,
‘message’ => ‘Sorry, Tabby is unable to approve this purchase. Please use an alternative payment method for your order.’,
‘rejection_reason’ => ‘not_available’, // or ‘order_amount_too_high’, ‘order_amount_too_low’
‘rejection_reason_code’ => ‘...’, // Tabby internal code (nullable)
‘status’ => ‘rejected’,
]
use MLQuarizm\PaymentGateway\Events\PaymentSuccess;
use MLQuarizm\PaymentGateway\Events\PaymentFailed;
use MLQuarizm\PaymentGateway\Events\PaymentCancelled;
use App\Listeners\HandlePaymentSuccess;
use App\Listeners\HandlePaymentFailure;
use App\Listeners\HandlePaymentCancellation;
protected $listen = [
PaymentSuccess::class => [
HandlePaymentSuccess::class,
],
PaymentFailed::class => [
HandlePaymentFailure::class,
],
PaymentCancelled::class => [
HandlePaymentCancellation::class,
],
];
namespace App\Listeners;
use MLQuarizm\PaymentGateway\Events\PaymentSuccess;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
class HandlePaymentSuccess implements ShouldQueue
{
use InteractsWithQueue;
/**
* Handle the event.
*/
public function handle(PaymentSuccess $event): void
{
$transaction = $event->transaction;
$order = $transaction->payable; // Your Order model or any payable model
// Update order status
$order->update(['payment_status' => 'paid']);
// Send notifications
$order->client->notify(new PaymentSuccessfulNotification($order));
// Dispatch jobs, etc.
dispatch(new ProcessSuccessfulPaymentJob($order));
}
}
use Illuminate\Support\Facades\Event;
use MLQuarizm\PaymentGateway\Events\PaymentSuccess;
use MLQuarizm\PaymentGateway\Events\PaymentFailed;
public function boot(): void
{
Event::listen(PaymentSuccess::class, function (PaymentSuccess $event) {
$transaction = $event->transaction;
$order = $transaction->payable;
$order->update(['payment_status' => 'paid']);
// Handle success...
});
Event::listen(PaymentFailed::class, function (PaymentFailed $event) {
$transaction = $event->transaction;
$reason = $event->reason;
// Log failure, notify admin, etc.
Log::error('Payment failed', [
'transaction_id' => $transaction->id,
'reason' => $reason
]);
});
}
use Illuminate\Support\Facades\Event;
use MLQuarizm\PaymentGateway\Events\PaymentSuccess;
use MLQuarizm\PaymentGateway\Events\PaymentFailed;
use MLQuarizm\PaymentGateway\Events\PaymentCancelled;
use MLQuarizm\PaymentGateway\Events\PaymentPending;
// In a service provider or dedicated listener class
Event::listen(PaymentSuccess::class, function (PaymentSuccess $event) {
$transaction = $event->transaction;
// Update order, send notification, etc.
});
Event::listen(PaymentFailed::class, function (PaymentFailed $event) {
$transaction = $event->transaction;
$reason = $event->reason;
});
Event::listen(PaymentCancelled::class, function (PaymentCancelled $event) {
$transaction = $event->transaction;
});
PaymentTransaction::create([
'payable_type' => Order::class, // or any model
'payable_id' => $order->id,
'track_id' => (string) $order->id,
'payment_id' => $paymentInfo['payment_id'],
'payment_gateway' => 'tabby',
'amount' => 500.00,
'status' => 'pending',
]);