PHP code example of first-iraqi-bank / fib-laravel-payment-sdk

1. Go to this page and download the library: Download first-iraqi-bank/fib-laravel-payment-sdk 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/ */

    

first-iraqi-bank / fib-laravel-payment-sdk example snippets


'providers' => [
    // Other Service Providers...

    FirstIraqiBank\FIBPaymentSDK\FIBPaymentServiceProvider::class,
],



use FirstIraqiBank\FIBPaymentSDK\Services\FIBPaymentIntegrationService;

protected $paymentService;

// Inject the FIBPaymentIntegrationService in your controller's construct.
public function __construct(FIBPaymentIntegrationService $paymentService)
{
    $this->paymentService = $paymentService;
}

try {
    // Call the createPayment method of the FIBPaymentIntegrationService
    $response = $this->paymentService->createPayment(1000, 'http://localhost/callback', 'Your payment description');

    $paymentData = json_decode($response->getBody(), true);

    // Return a response with the payment details and structure it as per your need.
    if($response->successful()) {
        return response()->json([
            'message' => 'Payment created successfully!',
            'payment' => $paymentData,
        ]);
    }
} catch (Exception $e) {
    // Handle any errors that might occur.
    return response()->json([
        'message' => 'Error creating payment: ' . $e->getMessage()
    ], 500);
}



use FirstIraqiBank\FIBPaymentSDK\Services\FIBPaymentIntegrationService;

protected $paymentService;

// Inject the FIBPaymentIntegrationService in your controller's construct.
public function __construct(FIBPaymentIntegrationService $paymentService)
{
    $this->paymentService = $paymentService;
}

try {
    $paymentId = 'your_payment_id'; // Retrieve from your storage

    // Call the checkPaymentStatus method of the FIBPaymentIntegrationService
    $response = $this->paymentService->checkPaymentStatus($paymentId);
    $paymentData = json_decode($response->getBody(), true);

    //return the status and structure it as per your need.
    if($response->successful()) {
        return response()->json([
            'status' => $paymentData['status'],
        ]);
    } else{
        return response()->json([
            'data' => $paymentData,
        ]);
    }

} catch (Exception $e) {
    // Handle any errors that might occur
    return response()->json([
        'message' => 'Error creating payment: ' . $e->getMessage()
    ], 500);
}



use FirstIraqiBank\FIBPaymentSDK\Services\FIBPaymentIntegrationService;

protected $paymentService;

// Inject the FIBPaymentIntegrationService in your controller's construct.
public function __construct(FIBPaymentIntegrationService $paymentService)
{
    $this->paymentService = $paymentService;
}

try {
    $paymentId = 'your_payment_id'; // Retrieve from your storage
    $response = $this->paymentService->refund($paymentId);
    echo "Refund Payment Status: " . $response;
} catch (Exception $e) {
    echo "Error Refunding payment: " . $e->getMessage();
}



use FirstIraqiBank\FIBPaymentSDK\Services\FIBPaymentIntegrationService;

protected $paymentService;

// Inject the FIBPaymentIntegrationService in your controller's construct.
public function __construct(FIBPaymentIntegrationService $paymentService)
{
    $this->paymentService = $paymentService;
}

try {
    $paymentId = 'your_payment_id'; // Retrieve from your storage

    // Call the cancelPayment method of the FIBPaymentIntegrationService
    $response = $this->paymentService->cancelPayment($paymentId);

    //return the response and structure it as per your need.
    if (in_array($response->getStatusCode(), [200, 201, 202, 204])) {
        return response()->json([
            'message' => "payment canceled Successfully",
        ]);
    } else {
        return response()->json([
            'message' => "payment cancelation faild ",
            'data' => $response->json()
        ]);
    }
} catch (Exception $e) {
    // Handle any errors that might occur
    return response()->json([
        'message' => 'Error creating payment: ' . $e->getMessage()
    ], 500);
}

// web.php or api.php
Route::post('/callback', [PaymentController::class, 'handleCallback']);

// PaymentController.php
public function handleCallback(Request $request)
{
    $payload = $request->all();

    $paymentId = $payload['id'] ?? null;
    $status = $payload['status'] ?? null;

    if (!$paymentId || !$status) {
        return response()->json(['error' => 'Invalid callback payload'], 400);
    }

    try {
        // Implement your callback handling logic
        return response()->json(['message' => 'Callback processed successfully']);
    } catch (Exception $e) {
        return response()->json(['error' => 'Failed to process callback: ' . $e->getMessage()], 500);
    }
}


return [
    // Other service providers...

    FirstIraqiBank\FIBPaymentSDK\FIBPaymentServiceProvider::class,
];
shell
php artisan vendor:publish --tag=fib-payment-sdk-config
shell
php artisan migrate