PHP code example of thejano / fib-payment-laravel

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

    

thejano / fib-payment-laravel example snippets


use TheJano\FibPayment\Facades\FibPayment;

use TheJano\FibPayment\Facades\FibPayment;

$payment = FibPayment::create(1000, 'Order #5', [
    'expiresIn'    => 'PT12H',
    'refundableFor' => 'P7D',
    'category'     => 'ECOMMERCE',
    'redirectUri'  => 'https://your-app.com/order/5',
]);

use TheJano\FibPayment\Facades\FibPayment;

$status = FibPayment::status('payment-uuid');

use TheJano\FibPayment\Facades\FibPayment;

$result = FibPayment::cancel('payment-uuid');
// Returns [] on success

use TheJano\FibPayment\Facades\FibPayment;

$result = FibPayment::refund('payment-uuid');
// Returns [] on success

use TheJano\FibPayment\Facades\FibPayment;

$token = FibPayment::getToken();
// Returns the raw OAuth2 access token string

// routes/web.php (exclude from CSRF protection — it is a server-to-server POST)
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\FibCallbackController;

Route::post('/fib/callback', FibCallbackController::class);

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use TheJano\FibPayment\Facades\FibPayment;
use TheJano\FibPayment\Exceptions\FibPaymentException;

class FibCallbackController extends Controller
{
    public function __invoke(Request $request)
    {
        $paymentId = $request->input('id');

        try {
            // Re-verify against FIB instead of trusting the callback body.
            $payment = FibPayment::status($paymentId);
        } catch (FibPaymentException $e) {
            report($e);

            return response()->noContent(500);
        }

        if (($payment['status'] ?? null) === 'PAID') {
            // Fulfil the order, mark it paid, etc.
        }

        return response()->noContent();
    }
}

use TheJano\FibPayment\Services\PaymentService;

class CheckoutService
{
    public function __construct(private PaymentService $payments) {}

    public function startPayment(int $amount, string $description): array
    {
        return $this->payments->create($amount, $description);
    }
}

use TheJano\FibPayment\Facades\FibPayment;
use TheJano\FibPayment\Exceptions\FibPaymentException;

try {
    $payment = FibPayment::create(1000, 'Order #5');
} catch (FibPaymentException $e) {
    $httpStatus  = $e->getStatusCode();   // int, e.g. 400 or 422
    $body        = $e->getResponseBody(); // mixed — the decoded JSON response body
    $message     = $e->getMessage();      // string — 'FIB payment request failed.'

    // Log or handle accordingly
    \Log::error('FIB payment failed', [
        'status' => $httpStatus,
        'body'   => $body,
    ]);
}

use Illuminate\Support\Facades\Http;
use TheJano\FibPayment\Facades\FibPayment;

Http::fake([
    '*/auth/realms/fib-online-shop/protocol/openid-connect/token' => Http::response([
        'access_token' => 'fake-token',
    ]),
    '*/protected/v1/payments' => Http::response([
        'paymentId' => 'pay_123',
        'readableCode' => 'ABC123',
    ]),
]);

$payment = FibPayment::create(1000, 'Test order');

expect($payment['paymentId'])->toBe('pay_123');
bash
php artisan vendor:publish --tag="fib-payment"