PHP code example of mostafax / payment-engine

1. Go to this page and download the library: Download mostafax/payment-engine 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/ */

    

mostafax / payment-engine example snippets


// app/Http/Middleware/VerifyCsrfToken.php
protected $except = [
    'payment/*/success',
    'payment/*/error',
    'api/payment/webhook/*',
];

use Mostafax\PaymentEngine\Facades\PaymentEngine;
use Mostafax\PaymentEngine\DTOs\InitiatePaymentDTO;
use Illuminate\Support\Str;

$dto = InitiatePaymentDTO::fromArray([
    'gateway'     => 'knet',
    'track_id'    => (string) Str::ulid(),
    'amount'      => 25.500,
    'currency'    => 'KWD',
    'success_url' => route('payment.return'),
    'error_url'   => route('payment.cancel'),
    'metadata'    => ['order_id' => $order->id],
]);

$result = PaymentEngine::initiate($dto);

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

// In EventServiceProvider::boot()
use Mostafax\PaymentEngine\Events\PaymentCaptured;

Event::listen(PaymentCaptured::class, function (PaymentCaptured $event) {
    Order::find($event->transaction->metadata['order_id'])?->markAsPaid();
});

$response = PaymentEngine::inquire(
    trackId: $trackId,
    gateway: 'knet',
    amount:  25.500,
);
// $response → GatewayResponseDTO

use Mostafax\PaymentEngine\Contracts\PaymentDriverInterface;
use Mostafax\PaymentEngine\DTOs\InitiatePaymentDTO;
use Mostafax\PaymentEngine\DTOs\GatewayResponseDTO;

final class MyGatewayDriver implements PaymentDriverInterface
{
    public function initiate(InitiatePaymentDTO $dto): array { ... }
    public function handleCallback(array $payload): GatewayResponseDTO { ... }
    public function inquire(string $trackId, ?float $amount = null): GatewayResponseDTO { ... }
    public function fetchTransactions(string $from, string $to, int $page = 1, int $perPage = 500): array { ... }
    public function getGatewayName(): string { return 'my-gateway'; }
    public function getSupportedCurrencies(): array { return ['KWD', 'USD']; }
}

config(['payment-engine.gateways.my-gateway' => [
    'driver' => MyGatewayDriver::class,
    'api_key' => env('MY_GATEWAY_KEY'),
    'sandbox' => env('MY_GATEWAY_SANDBOX', true),
]]);
bash
composer vendor:publish --tag=payment-engine-config
php artisan migrate
bash
php artisan queue:work --queue=payment-webhooks
php artisan queue:work --queue=payment-reconcile
php artisan queue:work --queue=payment-recovery
bash
# Sync date range
php artisan payment:reconcile knet --from=2026-06-01 --to=2026-06-10

# Async (background job)
php artisan payment:reconcile knet --async

# Recover stale pending transactions (>30 min)
php artisan payment:recover --minutes=30 --max=1000