How to implement the Flutterwave payment gateway using PHP

What is Flutterwave Payment Gateway?

Flutterwave is a payment gateway that enables businesses and individuals to receive and make payments using different payment methods, such as credit cards, debit cards, bank transfers, and mobile money, in various countries across Africa and around the world.

Merchants can incorporate Flutterwave’s unified payment infrastructure into their websites, mobile apps, or point-of-sale systems to take payments from customers in their native currency. With Flutterwave, businesses can securely accept payments online and control all of their transactions, refunds, and settlements from a single platform.

Other payment-related services provided by Flutterwave include fraud detection and prevention, recurring payments, invoicing, and analytics. These services enable businesses to optimize their payment processes and increase sales. In order to give its users access to a wider variety of payment methods, Flutterwave also has connections with significant payment processors including Visa, Mastercard, and PayPal.

Sample code snippets for  Flutterwave payment gateway integration on your website.

<?php

// Step 1: Set up your Flutterwave API keys and base URL
$public_key = 'your_public_key';
$secret_key = 'your_secret_key';
$base_url = 'https://api.flutterwave.com/v3';

// Step 2: Create the payment payload
$payload = array(
    'tx_ref' => uniqid('tx_ref_', true),
    'amount' => 100,
    'currency' => 'NGN',
    'redirect_url' => 'https://your-redirect-url.com/complete-payment',
    'payment_options' => 'card',
    'meta' => array(
        'user_id' => 23,
        'product_id' => 46,
    ),
    'customer' => array(
        'email' => '[email protected]',
        'phone_number' => '08160413494',
        'name' => 'Sopdap Tech',
    ),
    'customizations' => array(
        'title' => 'My Company Name',
        'description' => 'Payment for item purchased on my company website',
        'logo' => 'https://your-company-logo-url.com/logo.png',
    ),
);

// Step 3: Create the payment URL
$payment_url = $base_url.'/payments';

// Step 4: Set the headers
$headers = array(
    'Authorization: Bearer '.$secret_key,
    'Content-Type: application/json',
);

// Step 5: Send the request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $payment_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);

// Step 6: Handle the response
$response_data = json_decode($response, true);
if ($response_data['status'] == 'success') {
    // Payment was successful, redirect the user to the payment page
    header('Location: '.$response_data['data']['link']);
} else {
    // Payment failed, display an error message
    echo 'Payment failed: '.$response_data['message'];
}
//if you need assistance reach me via the above sample phone number
?>

Clone it from here: https://github.com/SopeDapo/flutterwavePaymentGateway/blob/main/payment.php

In this example code, you need to replace YOUR_PUBLIC_KEY and YOUR_SECRET_KEY with your own public and secret keys provided by Flutterwave. You also need to set the amount, currency, and txref values according to your requirements.

The code sends a request to the Flutterwave API with the payment details, generates an authorization header using your secret key, and redirects the user to the payment page URL returned by the API. If an error occurs, it prints the error message.

Get the complete source code here: https://github.com/SopeDapo/flutterwavePaymentGateway

Reference: https://developer.flutterwave.com/docs/collecting-payments/overview

Thank you for ready drop your questions via comment.

 

 

 

 

Read More
SopdapTechMarket February 22, 2023 0 Comments