How to integrate Flutterwave v3 Payment Gateway in PHP

In this simple article i will have to take you through the Fluttervave document to easily implement the system into your PHP project.

So Let Get Started!

Throughout this process we create just 3 files in our project named: index, pay and process

Index.php

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>Rave payment Gateway</title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="">
    </head>
    <body>
        <form action="pay.php" method="POST">

        <label>Email</label>
        <input type="email" name="email">
        <br>
        <label>Amount</label>
        <input type="number" name="amount">
        <br>
        <input type="submit" name="pay" vlaue="Send Payment">

        </form>
    </body>
</html>

 

pay.php

<?php 
if(isset($_POST['pay']))
{
    $email = $_POST['email'];
    $amount = $_POST['amount'];

    //* Prepare our rave request
    $request = [
        'tx_ref' => time(),
        'amount' => $amount,
        'currency' => 'NGN',
        'payment_options' => 'card',
        'redirect_url' => 'http://localhost/yt/rave/process.php',
        'customer' => [
            'email' => $email,
            'name' => 'Zubdev'
        ],
        'meta' => [
            'price' => $amount
        ],
        'customizations' => [
            'title' => 'Paying for a sample product',
            'description' => 'sample'
        ]
    ];

    //* Ca;; f;iterwave emdpoint
    $curl = curl_init();

    curl_setopt_array($curl, array(
    CURLOPT_URL => 'https://api.flutterwave.com/v3/payments',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => json_encode($request),
    CURLOPT_HTTPHEADER => array(
        'Authorization: Bearer FLWSECK_TEST-eee25be1b44ef9a132a872075b3a0910-X',
        'Content-Type: application/json'
    ),
    ));

    $response = curl_exec($curl);

    curl_close($curl);
    
    $res = json_decode($response);
    if($res->status == 'success')
    {
        $link = $res->data->link;
        header('Location: '.$link);
    }
    else
    {
        echo 'We can not process your payment';
    }
}

?>

 

process.php

<?php 
    if(isset($_GET['status']))
    {
        //* check payment status
        if($_GET['status'] == 'cancelled')
        {
            // echo 'YOu cancel the payment';
            header('Location: index.php');
        }
        elseif($_GET['status'] == 'successful')
        {
            $txid = $_GET['transaction_id'];

            $curl = curl_init();
            curl_setopt_array($curl, array(
                CURLOPT_URL => "https://api.flutterwave.com/v3/transactions/{$txid}/verify",
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => "",
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => "GET",
                CURLOPT_HTTPHEADER => array(
                  "Content-Type: application/json",
                  "Authorization: Bearer FLWSECK_TEST-eee25be1b44ef9a132a872075b3a0910-X"
                ),
              ));
              
              $response = curl_exec($curl);
              
              curl_close($curl);
              
              $res = json_decode($response);
              if($res->status)
              {
                $amountPaid = $res->data->charged_amount;
                $amountToPay = $res->data->meta->price;
                if($amountPaid >= $amountToPay)
                {
                    echo 'Payment successful';

                    //* Continue to give item to the user
                }
                else
                {
                    echo 'Fraud transactio detected';
                }
              }
              else
              {
                  echo 'Can not process payment';
              }
        }
    }
?>

 

If you have any issue or problem base on this please use the comment below to nofify me i will surly get back as soon as possible.

Thank You!

Comments

Rosleyrodrigues

2 weeks ago

I'M Rosley Rodrigues Are you aware that you could be scammed while trying to invest into crypto? If you know this then you will have to do a thorough search before investing your funds into any platform. Crypto Recovery Scam: Desperation has been the major key used by scammers to get into victims, CRYPTO RECOVERY is real but not legal. What is real and not legal has procedures and one can likely fall into imposter hands and end up being scammed, so many reviews about crypto recovery have been posted by scammers who derive joy in seeing their fellow humans in tears. I will solemnly advise everyone to stop searching for crypto recovery agent in a wrong way, they are now so many search words on the internet being used by both real and fake, so therefor a new methods was introduced to connect with a real recovery agent which i myself writing this article has experience it magnificent excellent recovery work well done by SMITH WHITE HACK SERVICE crypto recovery/other’s General hack service. SMITH WHITE HACK SERVICE and his team are a great set of hackers team with verified profiles and they have been helpful in resolving scam issues simply with their technology tools and p2p forum union with crypto block, makes it much easier for them to carry out crypto scam funds recovery. My total scammed funds was $4.875,000 canadian dollar’s and I received $3.255 USD, converting that to Canadian dollar’s was greatly profitable and I had no regret for clearing out all the scammer wallet funds with the help of.
( )
( WHATSAPP +1 559 508 2403 )

Tonytony

3 years ago Nigeria

This is what i get

Parse error: syntax error, unexpected '[' in C:\xampplite\htdocs\VTU\pay2.php on line 8

and when i deploy on the web server online the page is blank

Please assist.

Christaiwo

4 years ago Nigeria

Where will i put the API ?

Christaiwo

4 years ago Nigeria

Hi thanks so much for this , but is there a way to get the transaction details ghrou GET and also, am using this gateway for an E-commerce website where order_id is necessary, so is there a way to send order_id as a GET request through the pay.php and also update database with it in the process.php ?

Pwcs

4 years ago

thank you very much for this code. Any php code for initiating a withdraw from flutter wave dashboard.

Selemanm

4 years ago

im finishing my payment but i cant get 'Payment successful'; what I should do?

http://localhost/yt/rave/process.php?status=successful&;tx_ref=1624381153&transaction_id=2302489

what I see right now is this
Object not found!
The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.

If you think this is a server error, please contact the webmaster.

Error 404 plz help

Kayode Banjo

4 years ago Nigeria

Good write up. Keep it up.