abraham-flutterwave/laravel-payment
Install the Package
composer require abraham-flutterwave/laravel-payment
Publish the config file:
php artisan vendor:publish --provider="Abraham\Flutterwave\FlutterwaveServiceProvider"
Configure .env
Add your Flutterwave credentials:
FLUTTERWAVE_SECRET_KEY=your_secret_key
FLUTTERWAVE_PUBLIC_KEY=your_public_key
FLUTTERWAVE_TEST_MODE=true # Set to false for live transactions
First Use Case: Charge a Card
use Abraham\Flutterwave\Flutterwave;
$flutterwave = new Flutterwave();
$response = $flutterwave->charge([
'tx_ref' => 'unique-reference-' . time(),
'amount' => 1000, // Amount in kobo (e.g., 1000 = ₦10.00)
'currency' => 'NGN',
'payment_options' => 'card',
'customer' => [
'email' => 'user@example.com',
'phonenumber' => '2348012345678',
],
'customizations' => [
'title' => 'Payment for Product',
'description' => 'Test transaction',
],
]);
Transaction Types
charge() for one-time payments.subscribe() for recurring billing.transfer() for bank transfers or mobile money.collect() for multi-channel payments (e.g., card + mobile money).Webhook Handling
Route::post('/flutterwave-webhook', [PaymentController::class, 'handleWebhook']);
public function handleWebhook(Request $request) {
$flutterwave = new Flutterwave();
$response = $flutterwave->verifyWebhook($request->all());
if ($response['status'] === 'success') {
// Process successful transaction
}
}
Integration with Laravel Ecosystem
ChargePaymentJob::dispatch($paymentData);
PaymentProcessed, PaymentFailed).$user->notify(new PaymentSuccessNotification($response));
Testing
FLUTTERWAVE_TEST_MODE=true) with Flutterwave's test cards.$this->mock(Flutterwave::class, function ($mock) {
$mock->shouldReceive('charge')->andReturn(['status' => 'success']);
});
Amount Formatting
1000 = ₦10.00). Forgetting this causes validation errors.$amountInKobo = $amountInNaira * 100;
Webhook Verification
$flutterwave->verifyWebhook($request->all(), $request->header('X-Flutterwave-Signature'));
.env and validate it server-side.Test Mode Quirks
FLUTTERWAVE_TEST_MODE flag and test card list.Rate Limiting
Queue::push(new ProcessPaymentJob($data));
Currency Support
Enable Debug Mode
Add to .env:
FLUTTERWAVE_DEBUG=true
Logs errors to storage/logs/laravel.log.
API Response Inspection Always log raw responses for debugging:
$response = $flutterwave->charge($data);
\Log::debug('Flutterwave Response:', $response);
Common Errors
Invalid signature: Verify webhook signatures or check FLUTTERWAVE_SECRET_KEY.Insufficient funds: Ensure the test card has sufficient balance in test mode.Unsupported currency: Use a supported currency (e.g., NGN, USD, GHS).Custom Payment Methods Extend the package by creating a custom service:
class CustomFlutterwave extends Flutterwave {
public function customCharge(array $data) {
$data['custom_fields'] = ['custom_key' => 'custom_value'];
return parent::charge($data);
}
}
Middleware for Auth Protect payment routes with middleware:
Route::middleware(['auth:sanctum'])->post('/pay', [PaymentController::class, 'processPayment']);
Database Integration
Store transaction references (tx_ref) in your database to avoid duplicates:
$txRef = 'order-' . Str::uuid()->toString();
$flutterwave->charge(['tx_ref' => $txRef, ...]);
How can I help you explore Laravel packages today?