bhekor/laravel-flutterwave

Laravel package for flutterwave

v0.0.1 2022-03-08 12:18 UTC

This package is auto-updated.

Last update: 2024-10-30 01:26:47 UTC


README

Latest Version on Packagist Software License Total Downloads

Implement Flutterwave Rave payment gateway easily with Laravel

To get the latest version of this Flutterwave package, simply use composer

composer require bhekor/laravel-flutterwave

Then publish the configuration file using this command:

php artisan vendor:publish --provider="Bhekor\LaravelFlutterwave\FlutterwaveServiceProvider"

Open your .env file and add your public key, secret key, environment variable and logo url like so:

FLW_PUBLIC_KEY=FLWPUBK-xxxxxxxxxxxxxxxxxxxxx-X
FLW_SECRET_KEY=FLWSECK-xxxxxxxxxxxxxxxxxxxxx-X
FLW_SECRET_HASH='awesome-project'
  • FLW_PUBLIC_KEY - This is the api public key gotten from your dashboard (compulsory)

  • FLW_SECRET_KEY - This is the api secret key gotten from your dashboard (compulsory)

  • FLW_SECRET_HASH - This is the secret hash for your webhook

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 to handle the routes. FlutterwaveController. Then import the Flutterwave facade

<?php

namespace App\Http\Controllers;

use Bhekor\LaravelFlutterwave\Facades\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 successpage 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 purpose)
        // 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

    }
}

Setup the 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');

Documentation

A friendly documentation from a package that this was created from can be found here

Contributing

Please feel free to fork this package and contribute by submitting a pull request to enhance the functionalities. I will appreciate that a lot. Also please add your name to the credits.

Kindly follow me on twitter!

Features

The current features have been implemented

  • Payment
  • Verification
  • Transfers
  • Banks
  • Beneficiaries

I will be working on this next

  • Tokenized Charge
  • Pre Auth Charge

If there are features you need urgently, I will be willing to prioritize them, please reach out to my twitter account

License

The MIT License (MIT). Please see License File for more information.