Blog Detail

01

Feb
Implement Flutterwave Rave payment gateway with Laravel 8 cover image

arrow_back Implement Flutterwave Rave payment gateway with Laravel 8

Flutterwave is a technical software partner for banks and financial services providers that utilize Payment Account Attributes Inquiry, Foreign Exchange Rates, and Visa Direct to bring the convenience of new features like digital payments to consumers across Africa and beyond.

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, the 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 call back page.

For more details and features, you can visit its documentation and source code on Github.

Published at : 01-02-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project