1. Go to this page and download the library: Download rutatiina/flutterwave-v3 library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
rutatiina / flutterwave-v3 example snippets
// Prevent direct access to this class
define("BASEPATH", 1);
rwave\Rave;
use Flutterwave\EventHandlerInterface;
$URL = (isset($_SERVER['HTTPS']) ? 'https' : 'http') . '://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$getData = $_GET;
$postData = $_POST;
$publicKey = $_SERVER['PUBLIC_KEY'];
$secretKey = $_SERVER['SECRET_KEY'];
$success_url = $postData['successurl'];
$failure_url = $postData['failureurl'];
$env = $_SERVER['ENV'];
if($postData['amount']){
$_SESSION['publicKey'] = $publicKey;
$_SESSION['secretKey'] = $secretKey;
$_SESSION['env'] = $env;
$_SESSION['successurl'] = $success_url;
$_SESSION['failureurl'] = $failure_url;
$_SESSION['currency'] = $postData['currency'];
$_SESSION['amount'] = $postData['amount'];
}
$prefix = 'RV'; // Change this to the name of your business or app
$overrideRef = false;
// Uncomment here to enforce the useage of your own ref else a ref will be generated for you automatically
if($postData['ref']){
$prefix = $postData['ref'];
$overrideRef = true;
}
$payment = new Rave($_SESSION['secretKey'], $prefix, $overrideRef);
function getURL($url,$data = array()){
$urlArr = explode('?',$url);
$params = array_merge($_GET, $data);
$new_query_string = http_build_query($params).'&'.$urlArr[1];
$newUrl = $urlArr[0].'?'.$new_query_string;
return $newUrl;
};
// This is where you set how you want to handle the transaction at different stages
class myEventHandler implements EventHandlerInterface{
/**
* This is called when the Rave class is initialized
* */
function onInit($initializationData){
// Save the transaction to your DB.
}
/**
* This is called only when a transaction is successful
* */
function onSuccessful($transactionData){
// Get the transaction from your DB using the transaction reference (txref)
// Check if you have previously given value for the transaction. If you have, redirect to your successpage else, continue
// Comfirm that the transaction is successful
// Confirm that the chargecode is 00 or 0
// Confirm that the currency on your db transaction is equal to the returned currency
// Confirm that the db transaction amount is equal to the returned amount
// Update the db transaction record (('Location: '.getURL($_SESSION['failureurl'], array('event' => 'canceled')));
$_SESSION = array();
session_destroy();
}
}
/**
* This is called when a transaction doesn't return with a success or a failure response. This can be a timedout transaction on the Rave server or an abandoned transaction by the customer.
* */
function onTimeout($transactionReference, $data){
// Get the transaction from your DB using the transaction reference (txref)
// Queue it for requery. Preferably using a queue system. The requery should be about 15 minutes after.
// Ask the customer to contact your support and you should escalate this issue to the flutterwave support team. Send this as an email and as a notification on the page. just incase the page timesout or disconnects
if($_SESSION['publicKey']){
header('Location: '.getURL($_SESSION['failureurl'], array('event' => 'timedout')));
$_SESSION = array();
session_destroy();
}
}
}
if($postData['amount']){
// Make payment
$payment
->eventHandler(new myEventHandler)
->setAmount($postData['amount'])
->setPaymentOptions($postData['payment_options']) // value can be card, account or both
->setDescription($postData['description'])
->setLogo($postData['logo'])
->setTitle($postData['title'])
->setCountry($postData['country'])
->setCurrency($postData['currency'])
->setEmail($postData['email'])
->setFirstname($postData['firstname'])
->setLastname($postData['lastname'])
->setPhoneNumber($postData['phonenumber'])
->setPayButtonText($postData['pay_button_text'])
->setRedirectUrl($URL)
// ->setMetaData(array('metaname' => 'SomeDataName', 'metavalue' => 'SomeValue')) // can be called multiple times. Uncomment this to add meta datas
// ->setMetaData(array('metaname' => 'SomeOtherDataName', 'metavalue' => 'SomeOtherValue')) // can be called multiple times. Uncomment this to add meta datas
->initialize();
}else{
if($getData['cancelled'] && $getData['tx_ref']){
// Handle canceled payments
$payment
->eventHandler(new myEventHandler)
->requeryTransaction($getData['tx_ref'])
->paymentCanceled($getData['tx_ref']);
}elseif($getData['tx_ref']){
// Handle completed payments
$payment->logger->notice('Payment completed. Now requerying payment.');
$payment
->eventHandler(new myEventHandler)
->requeryTransaction($getData['tx_ref']);
}else{
$payment->logger->warn('Stop!!! Please pass the txref parameter!');
echo 'Stop!!! Please pass the txref parameter!';
}
}
use Flutterwave\Bvn;
//The data variable holds the payload
$bvn_number = "123456789";
$bvn = new Bvn();
$result = $bvn->verifyBVN($bvn_number);
print_r($result);
use Flutterwave\PaymentPlan;
//sample payload for payBill()
$data = array(
"amount"=> 2000,
"name"=> "plan 2",
"interval"=> "monthly",
"duration"=> 48
);
$update = array( "id" => "5356","name" => "The Game","status" => "Active");
$getdata = array("id"=>"5116");
$payment = new PaymentPlan();
$result = $payment->createPlan($data);//create a Plan reciept
$updateResult = $payment->updatePlan($update);//update a plan....
$paymentPlans = $payment->getPlans();//list all payment plans....
$aPlan = $payment->get_a_plan($getdata);//get a payment plans....
print_r($result);
use Flutterwave\Recipient;
$data = array(
"account_bank"=> "044",
"account_number"=> "0690000036",
);
$fetchdata = array(
'id' => '6153'
);
$deldata = array(
'id'=>'7236'
);
$payment = new Recipient();
$recipient1 = $payment->createRecipient($data);//Create a recipient for transfer
$recipients = $payment->listRecipients();// get all existing recipients
$recipient = $payment->fetchBeneficiary($fetchdata);//fetch a specific recipient.
$deleteRecipient = $payment->deleteBeneficiary($deldata);//delete recipient
print_r($recipient1);
use Flutterwave\Subscription;
$id = 1112 //Id of subscription plan
//$cid = 2222
$subscription = new Subscription();
$resultGet = $subscription->getAllSubscription();//gets all existing subscription
$resultActivate = $subscription->activateSubscription($id);// activates a subscription plan
$resultCancel = $subscription->cancelSubscription($cid);// activates a subscription plan
//returns the result
print_r($result);