How to integrate Flutterwave Payment Gateway in Laravel

Moseskyey

Member
Sep 12, 2021
30
22
How to integrate Flutterwave Payment Gateway in Laravel

Flutterwave is a payment technology company that provides payment infrastructure and technology to enable businesses to accept payments from customers all over the world. The company was founded in 2016 by Iyinoluwa Aboyeji and Olugbenga Agboola and is headquartered in San Francisco, California, USA, with offices in Nigeria, Ghana, Kenya, and South Africa.

Flutterwave's payment platform allows businesses to accept payments through a variety of channels, including card payments, bank transfers, mobile money, and cryptocurrency. The company also provides payment APIs and other payment-related services to developers and businesses.

Flutterwave has raised over $225 million in funding from investors, including Visa, Mastercard, Fidelity, and Salesforce Ventures. The company has also partnered with several major banks, including Access Bank, Zenith Bank, and First Bank of Nigeria, to provide payment solutions to their customers.

Flutterwave has received several awards and recognitions for its innovative payment solutions, including being named the "Best Payments Company" at the 2020 AppsAfrica Innovation Awards.

Laravel Rave is a package to implement the Flutterwave Rave payment gateway easily with Laravel. You should go to Flutterwave to get your public and private keys.

Prerequisite
PHP (opens new window)7.2+, Laravel (opens new window), and Composer (opens new window)are required.

Installation
To get the latest version of Flutterwave, you have to install this package by simply using composer.

composer require kingflamez/laravelrave

Configuration
Next, you've to publish the configuration file using this command:

php artisan vendor:publish --provider="KingFlamez\Rave\RaveServiceProvider"

A configuration file named flutterwave.php will be placed in your config directory.

Usage
Open your .env file and add your public key, secret key, environment variable, and logo URL. You can get your keys from here.

FLW_PUBLIC_KEY=FLWPUBK-xxxxxxxxxxxxxxxxxxxxx-X
FLW_SECRET_KEY=FLWSECK-xxxxxxxxxxxxxxxxxxxxx-X
FLW_SECRET_HASH='My_lovelysite123'

Payment Modal Implementation​

Initiating Flutterwave Payment Modal is simple using this package:

1. Setup Routes

// The page that displays the payment form
Route::get('/', function () {
return view('welcome');
});
// The route that the button calls to initialize payment
Route::post('/pay', [FlutterwaveController::class, 'initialize'])->name('pay');
// The callback url after a payment
Route::get('/rave/callback', [FlutterwaveController::class, 'callback'])->name('callback');
2. Setup the Payment Page

A sample payment button will look like so:

Open welcome.blade.php:

<h3>Buy Movie Tickets N500.00</h3>
<form method="POST" action="{{ route('pay') }}" id="paymentForm">
{{ csrf_field() }}

<input name="name" placeholder="Name" />
<input name="email" type="email" placeholder="Your Email" />
<input name="phone" type="tel" placeholder="Phone number" />

<input type="submit" value="Buy" />
</form>

Setup your Controller​

Set up your controller to handle the routes. I created the FlutterwaveController. Use the Rave as a Flutterwave facade.

Example

<?php

namespace App\Http\Controllers;

use KingFlamez\Rave\Facades\Rave as Flutterwave;

class FlutterwaveController extends Controller
{
/**
* Initialize Rave payment process
* Return void
*/
public function initialize()
{
//This generates a payment reference
$reference = Flutterwave::generateReference();

// Enter the details of the payment
$data = [
'payment_options' => 'card,banktransfer',
'amount' => 500,
'email' => request()->email,
'tx_ref' => $reference,
'currency' => "NGN",
'redirect_url' => route('callback'),
'customer' => [
'email' => request()->email,
"phone_number" => request()->phone,
"name" => request()->name
],

"customizations" => [
"title" => 'Movie Ticket',
"description" => "20th October"
]
];

$payment = Flutterwave::initializePayment($data);


if ($payment['status'] !== 'success') {
// notify something went wrong
return;
}

return redirect($payment['data']['link']);
}

/**
* Obtain Rave callback information
* Return void
*/
public function callback()
{

$status = request()->status;

//if payment is successful
if ($status == 'successful') {

$transactionID = Flutterwave::getTransactionIDFromCallback();
$data = Flutterwave::verifyTransaction($transactionID);

dd($data);
}
elseif ($status == 'cancelled'){
//Put desired action/code after transaction has been cancelled here
}
else{
//Put desired action/code after transaction has failed here
}
// Get the transaction from your DB using the transaction reference (txref)
// Check if you have previously given value for the transaction. If you have, redirect to your success page else, continue
// Confirm that the currency on your db transaction is equal to the returned currency
// Confirm that the db transaction amount is equal to the returned amount
// Update the db transaction record (including parameters that didn't exist before the transaction is completed. for audit purposes)
// Give value for the transaction
// Update the transaction to note that you have given value for the transaction
// You can also redirect to your success page from here

}
}
Once the initialize is called, you get redirected to a flutterwave payment page. After a successful payment, the user is redirected to the callback page.

NOTE; IF YOU TRY ON SSH HOSTING AND YOU GET MANY ERRORS DOWNLOAD YOUR CODE ON YOUR COMPUTER THEN INSTALL FLUTTERWAVE THEN UPLOAD IT TO YOUR HOSTING WORK
 
Back
Top Bottom