PHP code example of kommandhub / flutterwave-v3

1. Go to this page and download the library: Download kommandhub/flutterwave-v3 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/ */

    

kommandhub / flutterwave-v3 example snippets


use Kommandhub\Flutterwave\Flutterwave;

$flutterwave = new Flutterwave('YOUR_SECRET_KEY');

use Kommandhub\Flutterwave\Payloads\CustomerPayload;
use Kommandhub\Flutterwave\Payloads\CustomizationsPayload;
use Kommandhub\Flutterwave\Payloads\PaymentPayload;

$payload = new PaymentPayload(
    amount: 1000,
    currency: 'NGN',
    tx_ref: 'unique-transaction-ref-' . time(),
    redirect_url: 'https://your-app.com/callback',
    customer: new CustomerPayload(
        email: '[email protected]',
        phonenumber: '08012345678',
        name: 'John Doe'
    ),
    customizations: new CustomizationsPayload(
        title: 'My Store',
        logo: 'https://your-app.com/logo.png',
        description: 'Payment for order #42'
    )
);

$payment = $flutterwave->transactions()->initialize($payload);

// Redirect the user to complete payment
header('Location: ' . $payment['data']['link']);

$transactionId = $_GET['transaction_id'];
$result = $flutterwave->transactions()->verify($transactionId);

if ($result['data']['status'] === 'successful') {
    // Grant the customer access / fulfill the order
}

$tx = $flutterwave->transactions();

// Initialize a hosted-checkout payment
$tx->initialize(PayloadInterface $payload): array

// List all transactions (supports optional filters)
$tx->all(['from' => '2024-01-01', 'to' => '2024-12-31']): array

// Verify a transaction by ID
$tx->verify(string $id): array

// Get a transaction's event timeline
$tx->timeline(string $id): array

$tr = $flutterwave->transfers();

// Initiate a bank transfer
$tr->initiate(PayloadInterface $payload): array

// List all transfers (supports optional filters)
$tr->all(['status' => 'SUCCESSFUL']): array

// Get a single transfer by ID
$tr->get(string $id): array

// Calculate the fee for a transfer
$tr->fee(['amount' => 5000, 'currency' => 'NGN', 'type' => 'account']): array

// Get exchange rates between currencies
$tr->rates(['amount' => 100, 'source_currency' => 'USD', 'destination_currency' => 'NGN']): array

use Kommandhub\Flutterwave\Payloads\TransferPayload;

$payload = new TransferPayload(
    account_bank: '044',          // GTBank sort code
    account_number: '0690000031',
    amount: 5000,
    currency: 'NGN',
    narration: 'Payment for services',
    reference: 'ref-' . time(),
    callback_url: 'https://your-app.com/transfer-callback' // optional
);

$transfer = $flutterwave->transfers()->initiate($payload);

$rf = $flutterwave->refunds();

// Initiate a refund for a transaction (partial or full)
$rf->initiate(string $transactionId, PayloadInterface $payload): array

// List all refunds (supports optional filters)
$rf->all(): array

// Get a single refund by ID
$rf->get(string $id): array

use Kommandhub\Flutterwave\Payloads\RefundPayload;

$payload = new RefundPayload(
    amount: 500,                        // omit for full refund
    comments: 'Customer request'        // optional
);

$refund = $flutterwave->refunds()->initiate('12345678', $payload);

use Kommandhub\Flutterwave\Exceptions\FlutterwaveException;

try {
    $result = $flutterwave->transactions()->verify('invalid-id');
} catch (FlutterwaveException $e) {
    echo $e->getMessage();          // Human-readable error from Flutterwave
    echo $e->getCode();             // HTTP status code (e.g. 404)
    print_r($e->getResponseBody()); // Full decoded JSON body
    $e->getResponse();              // Raw PSR-7 ResponseInterface
}

use Kommandhub\Flutterwave\Flutterwave;
use Nyholm\Psr7\Factory\Psr17Factory;
use Symfony\Component\HttpClient\Psr18Client;

$factory = new Psr17Factory();

$flutterwave = new Flutterwave(
    secretKey: 'YOUR_SECRET_KEY',
    httpClient: new Psr18Client(),
    requestFactory: $factory,
    streamFactory: $factory
);

use Kommandhub\Flutterwave\Contracts\HttpClientInterface;

$flutterwave = new Flutterwave('YOUR_SECRET_KEY', client: $myCustomClient);