PHP code example of arafadev / payment-gateways

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

    

arafadev / payment-gateways example snippets


// config/payments.php
return [
    'callback_url' => 'https://your-domain.com/api/payment/callback',
    'success_url'  => 'https://your-domain.com/payment-success',
    'failed_url'   => 'https://your-domain.com/payment-failed',
    
    'moyasar' => [
        'mode'             => 'test', // or 'live'
        'test_base_url'    => 'https://api.moyasar.com',
        'test_api_key'     => 'your_test_api_key',
        'live_base_url'    => 'https://api.moyasar.com',
        'live_api_key'     => 'your_live_api_key',
        'callback_url'     => 'https://your-domain.com/callback',
    ],
    
    // Other gateway configurations...
];



namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Arafa\Payments\PaymentManager;
use Payment;

class PaymentController extends Controller
{
    public function paymentProcess(Request $request)
    {
        $gateway = Payment::gateway('moyasar');

        $response = $gateway->sendPayment($request);

        return $response;
    }

    public function callBack(Request $request): \Illuminate\Http\RedirectResponse
    {

        $gateway = Payment::gateway('moyasar');

        $response = $gateway->callBack($request);

        if ($response->success) {
            return redirect()->route('your_success_route');
        }
        return redirect()->route('your_failed_route');
    }
}

$response->success      // bool - Payment success indicator
$response->status       // string - Detailed status (success, failed, pending, etc.)
$response->unique_id    // string|null - Transaction ID
$response->amount       // float|null - Payment amount
$response->currency     // string|null - Currency code
$response->gateway_name // string - Payment gateway name
$response->raw          // array - Raw response data from the gateway



namespace App\Services\Payment;

use Illuminate\Http\Request;
use Arafa\Payments\Gateways\BasePaymentService;
use Arafa\Payments\Contracts\PaymentGatewayInterface;
use Arafa\Payments\Gateways\PaymentResponse;

class CustomGatewayService extends BasePaymentService implements PaymentGatewayInterface
{
    protected $name;
    protected $api_key;
    protected $base_url;
    protected array $header;
    
    public function __construct()
    {
        $this->name = 'custom_gateway';
        $this->mode = config("payments.{$this->name}.mode");
        $this->base_url = config("payments.{$this->name}.{$this->mode}_base_url");
        $this->api_key = config("payments.{$this->name}.{$this->mode}_api_key");
        $this->header = $this->buildHeader();
    }
    
    public function buildHeader(): array
    {
        return [
            'Authorization' => "Bearer {$this->api_key}",
            'Content-Type' => 'application/json',
            'Accept' => 'application/json'
        ];
    }
    
    public function sendPayment(Request $request)
    {
        // Implement your payment logic here
        $data = $request->all();
        $data['success_url'] = config("payments.callback_url");
        
        // Make API request to your payment gateway
        $response = $this->buildRequest('POST', '/api/payments', $data);
        
        // Return response with payment URL
        if ($response->getData(true)['success']) {
            return ['success' => true, 'url' => $response->getData(true)['payment_url']];
        }
        
        return ['success' => false, 'url' => null];
    }
    
    public function callBack(Request $request): PaymentResponse
    {
        try {
            // Extract data from request
            $raw = $request->all();
            
            // Determine success status
            $success = ($raw['status'] ?? '') === 'completed';
            
            // Extract payment status
            $status = $raw['status'] ?? 'unknown';
            
            // Extract transaction ID
            $transactionId = $raw['transaction_id'] ?? null;
            
            // Extract amount and currency
            $amount = $raw['amount'] ?? null;
            $currency = $raw['currency'] ?? null;
            
            // Return standardized PaymentResponse
            return new PaymentResponse(
                success: $success,
                status: $status,
                unique_id: $transactionId,
                amount: $amount,
                currency: $currency,
                gateway_name: $this->name,
                raw: $raw
            );
        } catch (\Exception $e) {
            // Handle exceptions
            \Log::error("Custom gateway callback error: " . $e->getMessage());
            
            return new PaymentResponse(
                success: false,
                status: 'error',
                unique_id: null,
                amount: null,
                currency: null,
                gateway_name: $this->name,
                raw: ['error' => $e->getMessage(), 'request' => $request->all()]
            );
        }
    }
}
bash
php artisan vendor:publish --provider="Arafa\Payments\PaymentServiceProvider" --tag="config" --force