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

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


      
    tenv\Dotenv;
    use FirstIraqiBank\FIBPaymentSDK\Services\FIBAuthIntegrationService;
    use FirstIraqiBank\FIBPaymentSDK\Services\FIBPaymentIntegrationService;
  
  // Load environment variables from the .env file
  $dotenv = Dotenv::createImmutable(__DIR__.'/..');
  $dotenv->load();
  
  // Function to create a payment and return its ID
  function createPayment(): string
  {
  // Initialize the authentication service
  $authService = new FIBAuthIntegrationService();
  
      // Initialize the payment integration service
      $paymentService = new FIBPaymentIntegrationService($authService);
      
      try {
        // Create a new payment
        $paymentResponse = $paymentService->createPayment(1000, 'http://localhost/callback', 'Test payment description', 'https://your-redirect-url.com');
        $paymentData = json_decode($paymentResponse->getBody(), true);
        
         // This should typically be saved in a database or cache for real implementations
        // Payment details (example using an associative array)
        //    $paymentData = [
        //      'fib_payment_id' => $paymentData['paymentId'],
        //      'readable_code' => $paymentData['readableCode'],
        //      'personal_app_link' => $paymentData['personalAppLink'],
        //      'valid_until' => $paymentData['validUntil'],
        //    ];
        // Example: $databaseService->storePaymentDetails($paymentData);
        
        // Return the payment ID
        return $paymentData['paymentId'];
      } catch (Exception $e) {
        // Handle any errors
        throw new Exception("Error creating payment: " . $e->getMessage());
      }
  }




  e_once __DIR__ . '/create_payment.php'; // Include the file with createPayment() function
  
  use Dotenv\Dotenv;
  use FirstIraqiBank\FIBPaymentSDK\Services\FIBAuthIntegrationService;
  use FirstIraqiBank\FIBPaymentSDK\Services\FIBPaymentIntegrationService;

// Load environment variables from the .env file
  $dotenv = Dotenv::createImmutable(__DIR__.'/..');
  $dotenv->load();
  
  try {
    // Get the payment ID
      $paymentId = retrievePaymentId();    
    // This $paymentId should be retrieved from your database or cache in real implementations
    // Example: $paymentId = $databaseService->retrievePaymentId();
    
    // Initialize the authentication service
    $authService = new FIBAuthIntegrationService();
    
    // Initialize the payment integration service
    $paymentService = new FIBPaymentIntegrationService($authService);
    
    // Check Payment Status
    $response = $paymentService->checkPaymentStatus($paymentId);
    echo "Payment Status: " . $response['status'] ?? null . PHP_EOL;
    
    // Example: Store payment details in a database or cache for real implementations
    // $databaseService->storePaymentDetails($paymentDetails);
    
  } catch (Exception $e) {
    echo "Error checking payment status: " . $e->getMessage() . PHP_EOL;
  }





  e_once __DIR__ . '/create_payment.php'; // Include the file with createPayment() function
  
  use Dotenv\Dotenv;
  use FirstIraqiBank\FIBPaymentSDK\Services\FIBAuthIntegrationService;
  use FirstIraqiBank\FIBPaymentSDK\Services\FIBPaymentIntegrationService;

// Load environment variables from the .env file
  $dotenv = Dotenv::createImmutable(__DIR__.'/..');
  $dotenv->load();
  
  try {
    // Get the payment ID
    $paymentId = retrievePaymentId();
    
    // This $paymentId should be retrieved from your database or cache in real implementations
    // Example: $paymentId = $databaseService->retrievePaymentId();
    
    // Initialize the authentication service
    $authService = new FIBAuthIntegrationService();
    
    // Initialize the payment integration service
    $paymentService = new FIBPaymentIntegrationService($authService);
    
    // Refund Payment
    $response = $paymentService->refund($paymentId);
    echo "Refund Payment Status: " . $response['status_code'] . PHP_EOL;

    
    // Example: update payment details in a database or cache for real implementations
    // $databaseService->updatePaymentDetails($paymentDetails);
    
  } catch (Exception $e) {
    echo "Error Refunding payment: " . $e->getMessage() . PHP_EOL;
  }





  e_once __DIR__ . '/create_payment.php'; // Include the file with createPayment() function
  
  use Dotenv\Dotenv;
  use FirstIraqiBank\FIBPaymentSDK\Services\FIBAuthIntegrationService;
  use FirstIraqiBank\FIBPaymentSDK\Services\FIBPaymentIntegrationService;

// Load environment variables from the .env file
  $dotenv = Dotenv::createImmutable(__DIR__.'/..');
  $dotenv->load();
  
  try {
    // Get the payment ID
    $paymentId = retrievePaymentId();
    
    // This $paymentId should be retrieved from your database or cache in real implementations
    // Example: $paymentId = $databaseService->retrievePaymentId();
    
    // Initialize the authentication service
    $authService = new FIBAuthIntegrationService();
    
    // Initialize the payment integration service
    $paymentService = new FIBPaymentIntegrationService($authService);
    
    // cancel Payment
    $response = $paymentService->cancel($paymentId);
    // Check if the cancellation was successful
    if (in_array($response->getStatusCode(), [200, 201, 202, 204])) {
      echo "Cancel Payment Status: Successful\n";
    } else {
      echo "Cancel Payment Status: Failed with status code " . $response->getStatusCode() . "\n";
    }
    
    
    // Example: update payment details in a database or cache for real implementations
    // $databaseService->updatePaymentDetails($paymentDetails);
    
  } catch (Exception $e) {
    echo "Error Refunding payment: " . $e->getMessage() . PHP_EOL;
  }




use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;

// Define your callback endpoint
$app->post('/callback', function (Request $request, Response $response) {
    $payload = $request->getParsedBody();

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

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

    // Process the callback
    try {
        $paymentService->handleCallback($paymentId, $status);
        //TODO: Implement your callback handling logic here

        return $response->withJson([
            'message' => 'Callback processed successfully',
        ]);
    } catch (Exception $e) {
        return $response->withStatus(500)->withJson([
            'error' => 'Failed to process callback: ' . $e->getMessage(),
        ]);
    }
});

bash
 git clone https://github.com/First-Iraqi-Bank/fib-php-payment-sdk.git