Flutterwave Payment Integration in Laravel

Today, We are sharing with you how to integrate the Flutterwave payment integration using CURL in Laravel.

#1: Install Laravel Project

composer create-project --prefer-dist laravel/laravel laravelIntegration

#2: Setup Database

After successfully install laravel Application, Go to your project .env file and set up database credential and move next step :

DB_CONNECTION=mysql 
DB_HOST=127.0.0.1 
DB_PORT=3306 
DB_DATABASE=here your database name 
DB_USERNAME=here database username
DB_PASSWORD=here database password 

#3: Define Route

We register all route in a web.php file.

Route::get('flutterwave', 'PaymentController@index')->name('flutterwave');
Route::post('flutterwave/payment','PaymentController@store')->name('flutterwave.payment');
Route::get('flutterwave/callback','PaymentController@callback')->name('flutterwave-callback');

#4: Create a Controller 

php artisan make:controller PaymentController

It will build a controller file called PaymentController.php.

app/Http/Controllers/PaymentController.php

<?php

namespace App\Http\Controllers;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Http\Request;

class PaymentController extends Controller
{
    public function index()
    {
        return view('payment');
    }

    public function store(Request $request){
    	$data = [
            "tx_ref" => rand(),
            "amount" => $request->amount,
            "currency" => $request->currency,
            "redirect_url" => route('flutterwave-callback'),
            'customer' => [
                'email' => $request->email,
                'phonenumber' => $request->phone_no,
                'name' => $request->first_name . $request->last_name
            ],
        ];
        $url = "https://api.flutterwave.com/v3/payments";
        $headers = [
            'Content-Type: application/json',
            'Authorization: Bearer Secret_key' //Secret key of your account 
        ];
        
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 200);
        curl_setopt($curl, CURLOPT_TIMEOUT, 200);
        $response_body = curl_exec($curl);
        $err = curl_error($curl);
        curl_close($curl);
        $result = json_decode($response_body, true);
        if ($err) {
            throw new \Exception($err);
        }
            
        if (isset($result['status']) && $result['status'] == 'success') {
            if ( isset($result['data']['link']) && $result['data']['link'] != ' ') {
                return Redirect::to($result['data']['link']);
            }
        }

        throw new \Exception('Your transaction could not processed.');
    }

    public function callback(Request $request){
    	$response = $request->all();
    	if ($response['status'] == 'successful') {
    		$status = "SUCCESS";
    	}else{
    		$status = "FAIL";
    	}
    	//Store the transaction as per your requirement
    }
}

 

#5: Create View File

In this step you need to create blade view file.

resources/views/payment.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Flutterwave Payment in Laravel</title>
    <meta name="csrf-token" content="{{ csrf_token() }}">
</head>
<body>
    <div class="row mt-5">
        <div class="col-md-10 offset-md-1">
          <h3 class="text-center mb-4">Flutterwave Payment in laravel - Demo </h3>
          <form method="post" action="{{route('flutterwave.payment')}}">
            <div class="form-group row">
              <div class="col-md-6">
                  <label class="form-control-label">First name<span class="text-danger">*</span></label>
                  <input type="text" name="first_name" class="form-control" required="">
                  @if ($errors->has('first_name'))
                      <span class="text-danger help-block form-error">
                          <strong>{{ $errors->first('first_name') }}</strong>
                      </span>
                  @endif
              </div>
              <div class="col-md-6">
                  <label class="form-control-label">Last name<span class="text-danger">*</span></label>
                  <input type="text" name="last_name" class="form-control" required="">
                  @if ($errors->has('last_name'))
                      <span class="text-danger help-block form-error">
                          <strong>{{ $errors->first('last_name') }}</strong>
                      </span>
                  @endif
              </div>
              <div class="col-md-6">
                  <label class="form-control-label">Email<span class="text-danger">*</span></label>
                  <input type="text" name="email" class="form-control" required="">
                  @if ($errors->has('email'))
                      <span class="text-danger help-block form-error">
                          <strong>{{ $errors->first('email') }}</strong>
                      </span>
                  @endif
              </div>
              <div class="col-md-6">
                  <label class="form-control-label">Phone No<span class="text-danger">*</span></label>
                  <input type="number" name="phone_no" class="form-control" required="">
                  @if ($errors->has('phone_no'))
                      <span class="text-danger help-block form-error">
                          <strong>{{ $errors->first('phone_no') }}</strong>
                      </span>
                  @endif
              </div>
              <div class="col-md-6">
                  <label class="form-control-label">Amount<span class="text-danger">*</span></label>
                  <input type="number" name="amount" class="form-control" required="">
                  @if ($errors->has('amount'))
                      <span class="text-danger help-block form-error">
                          <strong>{{ $errors->first('amount') }}</strong>
                      </span>
                  @endif
              </div>
              <div class="col-md-6">
                  <label class="form-control-label">Currency<span class="text-danger">*</span></label>
                  <input type="text" name="currency" class="form-control" required="">
                  @if ($errors->has('currency'))
                      <span class="text-danger help-block form-error">
                          <strong>{{ $errors->first('currency') }}</strong>
                      </span>
                  @endif
              </div>
            </div>
          </form>
        </div>
    </div>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</body>
</html>

#6: Run Development Server

you can run to laravel project in your teminal.bellow command:

php artisan serve

I hope it will help you.