Connect with us

How to Integrate Flutterwave Rave Payment System in your Laravel Applications

kizinho

Published

on

Follow
DEVELOPER PROGRAMMING: How to Integrate Flutterwave Rave Payment System in your Laravel Applications [New  Developer Programming] » Naijacrawl
Photo: Naijacrawl

Hi Folks, I know many laravel developers that have been using Paystack as their payment system have been finding it difficult to integrate Paystack Laravel Package from Laravel 6.0 - 7.0 versions. Today I have a beautiful solution by using Flutterwave Rave Payment System, this process will work on any laravel versions.

Requirements

  1. Running Exiting Project on Laravel
  2. Create Account on Flutterwave
  3. Then you are good to go

Lets Begin

composer require kingflamez/laravelrave

Once Flutterwave Rave for Laravel is installed, you need to register the service provider. Open up config/app.php and add the following to the providers key.

'providers' => [
 /* * Package Service Providers... */
 ... 
KingFlamez\Rave\RaveServiceProvider::class, 
...
]

Also add this to the aliases

'aliases' => [
 ... 
'Rave' => KingFlamez\Rave\Facades\Rave::class, 
...
]

Configuration

Publish the configuration file using this command:

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

Usage

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

RAVE_PUBLIC_KEY=FLWPUBK_TEST
-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-X
RAVE_SECRET_KEY=FLWSECK_TEST-xxxxxxxxxxxxxxx
xxxxxxxxxxxxxxx-XRAVE_TITLE="Naijacrawl"
RAVE_ENVIRONMENT="staging"
RAVE_LOGO="https://rave.flutterwave.com/
static/img/avatar@3x.png"
RAVE_PREFIX="rave"
  • RAVE_PUBLIC_KEY - This is the api public key gotten from your dashboard (compulsory)
  • RAVE_SECRET_KEY - This is the api secret key gotten from your dashboard (compulsory)
  • RAVE_TITLE - This is the title of the modal (optional)
  • RAVE_ENVIRONMENT - This can be staging or live. Staging API keys can be gotten here while live API keys can be gotten here (compulsory)
  • RAVE_LOGO - This is a custom logo that will be displayed on the modal (optional)
  • RAVE_PREFIX - This is a the prefix added to your transaction reference generated for you (optional)
  • SECRET_HASH - This is the secret hash for your webhook, this is necessary if you are setting up a recurrent payment


1. Setup Routes


Route::post('/pay', 'RaveController@initialize')
->name('pay');
Route::post('/rave/callback',
 'RaveController@callback')->name('callback');


2. Grant CSRF Access to Rave Callback


Go to app/Http/Middleware/VerifyCsrfToken.php 

and add your callback url to the $except array

protected $except = [ 'rave/callback'];

A sample form from your frontend will look like so:

@php
$array = array(array(
'metaname' => 'color',
 'metavalue' => 'blue'),
 array('metaname' => 'size',
 'metavalue' => 'big'));
@endphp
"POST" action="{{ route('pay') }}" id="paymentForm"> {{ csrf_field() }} type="hidden" name="amount" value="500" />type="hidden" name="payment_method" value="both" />type="hidden" name="description" value="Beats by Dre. 2017" />type="hidden" name="country" value="NG" />type="hidden" name="currency" value="NGN" />type="hidden" name="email" value="test@test.com" />type="hidden" name="firstname" value="Oluwole" />type="hidden" name="lastname" value="Adebiyi" />type="hidden" name="metadata" value="{{ json_encode($array) }}" >type="submit" value="Buy" />

In this implementation, we are expecting a form encoded POST request to this script. The request will contain the following parameters.

  • payment_method Can be card, account, both
  • description Your transaction description
  • logo Your logo url
  • title Your transaction title
  • country Your transaction country
  • currency Your transaction currency
  • email Your customer's email
  • firstname Your customer's firstname
  • lastname Your customer's lastname
  • phonenumber Your customer's phonenumber
  • ref Your transaction reference.
  • It must be unique per transaction.
  • By default, the Rave class generates
  • a unique transaction reference for each
  • transaction. Pass this parameter only if
  • you uncommented the related section in
  • the script below.

3. Setup your Controller

Setup your controller to handle the routes. I created the RaveController.

Use the Rave facade.


Example


namespaceApp\Http\Controllers;
use Illuminate\Http\Request;
//use the Rave Facadeuse Rave;class RaveController extends Controller{ 
/** * Initialize Rave payment process * @return void */ 
public function initialize() {
 //This initializes payment and redirects to the payment gateway 
//The initialize method takes the parameter of the redirect URL
 Rave::initialize(route('callback')); 
} 
/** * Obtain Rave callback information 
* @return void */public function callback() {
 $data =Rave::verifyTransaction(request()->txref);
dd($data); 
// view the data response 
 if ($data->status == 'success') {
 //do something to your database
}else
 {
//return invalid payment
}
 }
}



site


kizinho

I am a software developer, like meeting people and love blogging, that's why I developed naijacrawl because that's what I love doing.

Continue Reading
Click To Comment

BE First To Comment


    Leave a Reply

    Your email address will not be published. Required fields are marked *

    Laravel 6.5.2 Released this week for bug fix

    kizinho

    Published

    on

    NEWS: Laravel 6.5.2 Released this week for bug fix [New  Developer] » Naijacrawl
    Photo: Naijacrawl

    The Laravel team released v6.5.2 this week—this version is a patch version release, containing mostly changes and fixes.

    Here’s a few things of note:

    • If you’re using PostGIS types in Postgres, Laravel now supports a separation between geometry and geography types.
    • The BelongsToMany::cursor() method, now hydrates pivot relations.
    • Model serialization on jobs allows typed properties.

    You can see the full list of new features and updates below and the whole diff between 6.5.1 and 6.5.2 on GitHub. The full release notes for Laravel 6.0 are available in the GitHub v6 changelog:

    v6.5.2

    Added

    • Allowed model serialization on jobs for typed properties (#30604, #30605, 920c364)
    • Allowed fallback when facade root accessor has previously been resolved (#30616)
    • Added support for separation between geometry and geography types for Postgres (#30545)
    • Added createWithContent() method to Illuminate\Http\Testing\File and Illuminate\Http\Testing\FileFactory (2cc6fa3, 181db51)

    Refactoring

    • Improved PostgresGrammar::formatPostGisType() method readability (#30593)

    Changed

    • Added symfony/debug dependency to illuminate/pipeline (#30611)
    • Override BelongsToMany::cursor() to hydrate pivot relations (#30580)
    • Ignore Redis prefix when verifying channel access in RedisBroadcaster (#30597, d77ce36)


    Continue Reading

    How to Automate App Setup with Laravel Initializer

    kizinho

    Published

    on

    NEWS: How to Automate App Setup with Laravel Initializer [New  Developer] » Naijacrawl
    Photo: Naijacrawl

    Have you ever found yourself writing multiple manual steps to set up a Laravel application in a new environment? Laravel Initializer is a convenient way to automate installing and updating a Laravel application:

    Laravel Initializer gives you the ability to declare multiple processes and run them with app:install and app:update artisan commands, which run predefined actions chain depending on the current environment.

    The app:install and app:update commands use two distinct classes that run commands based on a given environment. First, the install command uses the App\Install class:

    namespace App;
    
    use MadWeb\Initializer\Contracts\Runner;
    
    class Install
    {
        public function production(Runner $run)
        {
            return $run
                ->external('composer', 'install', '--no-dev', '--prefer-dist', '--optimize-autoloader')
                ->artisan('key:generate')
                ->artisan('migrate', ['--force' => true])
                ->artisan('storage:link')
                ->external('npm', 'install', '--production')
                ->external('npm', 'run', 'production')
                ->artisan('route:cache')
                ->artisan('config:cache')
                ->artisan('event:cache');
        }
    
        public function local(Runner $run)
        {
            return $run
                ->external('composer', 'install')
                ->artisan('key:generate')
                ->artisan('migrate')
                ->artisan('storage:link')
                ->external('npm', 'install')
                ->external('npm', 'run', 'development');
        }
    }
    
    

    The app:update command looks similar, using an App\Update class:

    namespace App;
    
    use MadWeb\Initializer\Contracts\Runner;
    
    class Update
    {
        public function production(Runner $run)
        {
            return $run
                ->external('composer', 'install', '--no-dev', '--prefer-dist', '--optimize-autoloader')
                ->external('npm', 'install', '--production')
                ->external('npm', 'run', 'production')
                ->artisan('route:cache')
                ->artisan('config:cache')
                ->artisan('event:cache')
                ->artisan('migrate', ['--force' => true])
                ->artisan('cache:clear')
                ->artisan('queue:restart'); ->artisan('horizon:terminate');
        }
    
        public function local(Runner $run)
        {
            return $run
                ->external('composer', 'install')
                ->external('npm', 'install')
                ->external('npm', 'run', 'development')
                ->artisan('migrate')
                ->artisan('cache:clear');
        }
    }
    
    

    You can also inject dependencies from the service container if you need to access services while running commands.

    This package contains a variety of runner actions you should check out in the readme. I found the MakeCronTask dispatch interesting:

    $run->dispatch(new \MadWeb\Initializer\Jobs\MakeCronTask)
    
    

    MakeCronTask adds the following to the server’s crontab list:

    * * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
    
    

    You can do other things like creating a supervisord config for a typical queue worker or horizon.

    You can learn more about this package, get full installation instructions, and view the source code on GitHub at mad-web/laravel-initializer.

    Continue Reading

    How to convert image from png, jpg to webp image in Laravel

    kizinho

    Published

    on

    DEVELOPER PROGRAMMING: How to convert image from png, jpg to webp image in Laravel [New  Developer Programming] » Naijacrawl
    Photo: Naijacrawl

    Hi, folks will show how you can convert any image to webp image.

    Why Webp

    WebP is making headway in the image format space this year. The image format — which is developed by Google — uses both lossy and lossless compression by utilizing technology that Google purchased from On2 Technologies.


    WebP has been around a while now, actually, and it first made headlines in a 2010 Google press conference. But, like any new technology, it had some initial rough patches. Today, WebP is on version 1.0.0, so we thought it would be a great time to talk about what makes WebP so powerful, and why it’s a fantastic option for web designers and developers.

    a

    Before I proceed to describe what WebP actually is, let us focus on why it matters—it’s not only about making images smaller, but it’s also about why and how the images need to be made smaller.


    You may have heard about design mistakes killing your SEO and conversion rates. While there are some aspects that CSS or JavaScript are responsible for, like blocking rendering, the images nowadays are a huge part of any website’s weight. Therefore, users on slower connections will have trouble interacting with your website (unless it’s an AMP).

    Just recently, It’s an interesting area of expertise, where you take care of lots of small things to deliver one big thing—a superfast, superlight website that reads on any device and looks amazing at the same time.


    Chances are, you’ve seen WebP images before. If you open Naijacrawl in Google Chrome, all of those thumbnails are going to be WebP thumbnails. If you were to open the Facebook app on your android phone, all of the images that you see would be WebP. It’s definitely being utilized by various companies around the world to increase performance.


    So let's see how you can do the same using laravel to convert your images to webp image.


    Note this works in all laravel versions


    I assumed you have a project already or you can create one by using this command


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


    You must have installed a composer before you can run the command.


    We need a controller so let's create one

    php artisan make:controller 
    ImageWebpController
    


    Inside the controller, we will write a code that will process our image and convert it to webp inside a folder


    What it Requires

    file extension

    file name

    folder

    
    namespace
     App\Http\Controllers;
    use Illuminate\Http\Request;
    use Illuminate\Support\Str;
    class ImageWebpController 
    extends Controller {
    public function webpImage
    (Request $request) 
    {
    if ($request->hasFile('file')) {
    $file = $request->file('file');
    $extension = $file->getClientOriginalExtension(); 
    $rand = '-' . strtolower(Str::random(10));  
    $name = $rand . '.' . $extension;   
    return $this->processImage($file, $name);  
     }  
      }    
    private function processImage
    ($file, $name) 
    {  
    $file->move(public_path('/test/image'), 
    $name);  
          //convert to webp         
    $webp = public_path() . '/test/image/' .
     $name;      
      $im = imagecreatefromstring
    (file_get_contents($webp)); 
     $new_webp =
     preg_replace('"\.(jpg|jpeg|png|webp)$"',
     '.webp', $webp);  
    imagewebp($im, $new_webp, 50);  
     return'message'
     => 'image successfully 
    converted to webp.
     check your 
    folder to see it'
    ];   
     }
    }
    ?>

    from what i did, i check if the request is a file before proceeding , have to get extension of the file and use it make a slug name for the image, because i don't want it to carry original name.

    $extension = $file->
    getClientOriginalExtension();
    

    I have to use str function to form a characters for the name of the image

     $rand = '-' . 
    strtolower(Str::random(10));
    
    //final name
    $name = $rand . '.' . $extension;
    

    I needed another private function because i want to separate my code for easy to read using processImage function.

    
     private function processImage
    ($file, $name) {
    $file->move(public_path('/test/image'),
     $name); 
           //convert to webp         
    $webp = public_path() . '/test/image/' .
     $name;
     $im = imagecreatefromstring
    (file_get_contents($webp));  
    $new_webp =
     preg_replace('"\.(jpg|jpeg|png|webp)$"', '.webp',
     $webp);
    imagewebp($im, $new_webp, 50);        
    return'message' 
    => 'image successfully 
    converted to webp. 
    check your folder 
    to see it' ];  
      }
    ?>

    After i called it to the main function which

    is webpImage function .

    Inside processImage function ,

    the file is moved to my public folder called test/image before converting it to webp image

    //get image that
     we want to convert 
    $webp = public_path() .
     '/test/image/' . $name;
    //create the image        
    $im = imagecreatefromstring
    (file_get_contents($webp));
    //accept image extensions of jpg,png,webp 
    $im = imagecreatefromstring
    (file_get_contents($webp));
     $new_webp = 
    preg_replace('"\.(jpg|jpeg|png|webp)$"', 
    '.webp', $webp);
    //make the webp image and set the quality to 50 
    //the more quality the more it becomes broght and increase mb
    // you can set to 100 which means no quality will be reduced 
     imagewebp($im, $new_webp,50);
    ?>

    From my code i didn't add validation rule which you can do it if you needed it , just to make it allow only files and extension type


    Find this article helpful kindly share it.

    Continue Reading

    Latest



    Face of Naijacrawl


    ChukwubuikemSunday ChukwubuikemSunday

    Sunday Chukwubuikem


    Higher - 09061773292

    Trending