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

Kayode Banjo

4 weeks ago Nigeria

Good write up. Keep it up.

Reply

Zubdev

4 weeks ago Nigeria

Thank you!

Author

Phantasm Zero

1 month ago Nigeria

it doesnt work when i change my api public key to the live one

Reply

Zubdev

1 month ago Nigeria

It requires your secret key not your public key since all validations are done at the server side we make use of secret key not public key.
Hope it look helpful!

Author

Jack

1 month ago Rwanda

My website is live.
I'm getting this when i try to retrieve things in JSON
{"status":"success",
"message":"Hosted Link",
"data":{
"link":"undefined5414fd5a5daf0ecec70e"
}
}

The problem is this link inside data which is being undefined.
Any help plz.

Reply

Zubdev

1 month ago Nigeria

That not a good response from the API need to track back and check what the codes please

Author

Brown Nwankwo

2 months ago Nigeria

You really are talented. Making something from that which you have. Congratulations on coming this far and God bless you to go further. Thanks for the easily understandable tutorial.

Reply

Zubdev

1 month ago Nigeria

You are highly welcome!

Author

Peter Clark

2 months ago

Can I use the process.php to send data to SQL database for recurring payment. And please how does the process.php works cos I can't see anywhere where pay.php was referring to process.php please I really need this since am working on a subscription base website where recurring payment is needed. Thanks and I really need this which is kind of taking too late long 😂😂😂😂.

Reply

Zubdev

2 months ago Nigeria

If you could have take some time and watch the video you will defiantly answer your quest YOURSELF😂

Author

Kaembe Chisenga

2 months ago Zambia

Can you please create a tutorial on mobile money payment and withdraw.

Reply

Zubdev

2 months ago Nigeria

The process are still the same all you have to do is to change th payment_tipe form card to momo

Author

Charles-Clement Avul

2 months ago Nigeria

Thanks @Zubdev this really good, please can you do a video about recurring payments please. Thanks again.

Reply

Zubdev

2 months ago Nigeria

Hello sir. I am glad you like the video.
Recurring payment is very easy and simple to implement i am going to create the video on Recurring payment soon.

Author

Seunex Da G

2 months ago Nigeria

The video 📷 for recurring payment has been created!

Write Your Comments