PHP code example of rutatiina / flutterwave-v3

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\Account;
//The data variable holds the payload
$data = array(
    "amount" => "3000",
    "type" => "debit_ng_account",//debit_ng_account or debit_uk_account
    "account_bank" => "044",
    "account_number" => "0690000037",
    "currency" => "NGN",//NGN or GBP
    "email" => "[email protected]",
    "phone_number" => "07067965809",
    "fullname" => "Olaobaju Abraham",
    "client_ip" => "154.123.220.1",
    "device_fingerprint" => "62wd23423rq324323qew1",
    "meta" => [
        "flightID" => "213213AS"
        ]       
    );

$payment = new Account();
$result = $payment->accountCharge($data);

if(isset($result['data'])){
  $id = $result['data']['id'];
  $verify = $payment->verifyTransaction($id);
}
print_r($result);


use Flutterwave\Ach;
//The data variable holds the payload



$data = array(
    "tx_ref" =>  "MC-1585230ew9v5050e8",
    "amount" => "100",
    "type" => "ach_payment",
    "currency" => "USD",
    "country" => "US",
    "email" => "[email protected]",
    "phone_number" => "0902620185",
    "fullname" => "Ekene Eze",
    "redirect_url" => "http://ekene.com/u/payment-completed",
    );

$payment = new Ach();

$result = $payment->achCharge($data);
if(isset($result['data'])){
  $id = $result['data']['id'];
  $verify = $payment->verifyTransaction($id);
}
print_r($result);



use Flutterwave\Card;
//The data variable holds the payload
$data = array(
    "card_number"=> "5531886652142950",
    "cvv"=> "564",
    "expiry_month"=> "09",
    "expiry_year"=> "22",
    "currency"=> "NGN",
    "amount" => "1000",
    "fullname"=> "Ekene Eze",
    "email"=> "[email protected]",
    "phone_number"=> "0902620185",
    "fullname" => "temi desola",
    //"tx_ref"=> "MC-3243e",// should be unique for every transaction
    "redirect_url"=> "https://webhook.site/3ed41e38-2c79-4c79-b455-97398730866c",
           
    );

$payment = new Card();
$res = $payment->cardCharge($data);//This call is to figure out the authmodel
$data['authorization']['mode'] = $res['meta']['authorization']['mode'];

if($res['meta']['authorization']['mode'] == 'pin'){

    //Supply authorization pin here
    $data['authorization']['pin'] = '3310';
}

if($res['meta']['authorization']['mode'] == 'avs_noauth'){
    //supply avs details here
    $data["authorization"] = array(
            "mode" => "avs_noauth",
            "city"=> "Sampleville",
            "address"=> "3310 sample street ",
            "state"=> "Simplicity",
            "country"=> "Simple",
            "zipcode"=> "000000",
        );
}

$result = $payment->cardCharge($data);//charge with new fields
// print_r($result);//this returns the an array

if($result['data']['auth_mode'] == 'otp'){
    $id = $result['data']['id'];
    $flw_ref = $result['data']['flw_ref'];
    $otp = '12345';
    $validate = $payment->validateTransaction($otp,$flw_ref);// you can print_r($validate) to see the response
    $verify = $payment->verifyTransaction($id);
}


use Flutterwave\MobileMoney;
//The data variable holds the payload
$data = array(
    "order_id" => "USS_URG_89245453s2323",
    "amount" => "1500",
    "type" => "mobile_money_rwanda",// could be mobile_money_rwanda,mobile_money_uganda, mobile_money_zambia, mobile_money_ghana
    "currency" => "RWF",
    "email" => "[email protected]",
    "phone_number" => "054709929220",
    "fullname" => "John Madakin",
    "client_ip" => "154.123.220.1",
    "device_fingerprint" => "62wd23423rq324323qew1",
    "meta" => [
        "flightID" => "213213AS"
        ]       
    );


$payment = new MobileMoney();
$result = $payment->mobilemoney($data);
$id = $result['data']['id'];
$verify = $payment->verifyTransaction($id);
$print_r($result);


use Flutterwave\Ussd;
//The data variable holds the payload
$data = array(
        "tx_ref" => "MC-15852309v5050e8",
        "account_bank" => "058",
        "amount" => "1500",
        "currency" =>"NGN",
        "email" =>"[email protected]",
        "phone_number" =>"054709929220",
        "fullname" => "John Madakin",
        
      
    );

$payment = new Ussd();
$result = $payment->ussd($data);//initiates the charge

echo 'Dial '.$result['meta']['authorization']['note'].' to authorize your transaction';

//A webhook notification would be sent to you....

if(isset($result['data'])){
  $id = $result['data']['id'];
  $verify = $payment->verifyTransaction($id);
}



use Flutterwave\Mpesa;

$data = array(
    "amount" => "1500",
    "type" => "mpesa",
    "currency" => "KES",
    "email" => "[email protected]",
    "phone_number" => "054709929220",
    "fullname" => "Ekene Eze",
    "client_ip" => "154.123.220.1",
    "device_fingerprint" => "62wd23423rq324323qew1",
    "meta" => [
        "flightID" => "213213AS"
        ]       
    );

$payment = new Mpesa();

$result = $payment->mpesa($data);
print_r($result);
if(isset($result['data'])){
  $id = $result['data']['id'];
  $verify = $payment->verifyTransaction($id);
}


use Flutterwave\Transfer;
//sample payload for payBill()
$data = array(
    "account_bank"=> "044",
    "account_number"=> "0690000040",
    "amount"=> 5500,
    "narration"=> "Akhlm Pstmn Trnsfr xx007",
    "currency"=> "NGN",
    "reference"=> "akhlm-pstmnpyt-rfxx007_PMCKDU_1",// read the docs about testing successful and failed transaction.
    "callback_url"=> "https://webhook.site/b3e505b0-fe02-430e-a538-22bbbce8ce0d",
    "debit_currency"=> "NGN"
);

//sample payload for bulkBill()
$bulkdata = array(
  "title"=> "Staff salary",
  "bulk_data"=> array(
      array(
          "bank_code"=> "044",
          "account_number"=> "0690000032",
          "amount"=> 45000,
          "currency"=> "NGN",
          "narration"=> "akhlm blktrnsfr",
          "reference"=> "akhlm-blktrnsfr-xx03"
      ),
      array(
          "bank_code"=> "044",
          "account_number"=> "0690000034",
          "amount"=> 5000,
          "currency"=> "NGN",
          "narration"=> "akhlm blktrnsfr",
          "reference"=> "akhlm-blktrnsfr-xy03"
      ))
);

$getdata = array(
    //"reference"=>"edf-12de5223d2f32434753432"
     "id"=>"BIL136",
     "product_id"=>"OT150"
);

$listdata = array(
  'status'=>'failed'
);

$feedata = array(
'currency'=> 'NGN', //if currency is omitted. the default currency of NGN would be used.
'amount'=> 1000
);

$payment = new Transfer();
$result = $payment->singleTransfer($data);//initiate single transfer payment
$createBulkTransfer = $payment->bulkTransfer($bulkdata);// get bulk result....
$transfers = $payment->listTransfers($listdata);//you can add a payload for the page. you can remove the array if want to get it all.
$getTransferFee = $payment->getTransferFee($feedata);
if(isset($result['data'])){
  $id = $result['data']['id'];
  $verify = $payment->verifyTransaction($id);
}



use Flutterwave\VirtualCard;

$data = array(
    "currency"=>"NGN",
    "amount"=>20000,
    "billing_name"=>"Jermaine Graham",
    "billing_address"=>"2014 Forest Hills Drive",
    "billing_city"=>"Node",
    "billing_state"=>"Javascript",
    "billing_postal_code"=>"000009",
    "billing_country"=>"NG",
    "callback_url"=>"https://webhook.site/96374895-154d-4aa0-99b5-709a0a128674"
    );

    $trns_data = array('id'=> 'a41de883-c8da-45a0-9b23-37780c88285f');
    $getCardData = array('id'=>'7a81d279-a07a-4775-a55a-5fa2c98e20ae');
    $terminate_data = array('id'=>'1cb36826-8e05-40d6-8b9e-7f7439a141cb');
    $fund_data = array('id'=>'1cb36826-8e05-40d6-8b9e-7f7439a141cb', 'amount'=>'2000', 'debit_currency'=>'NGN');
    $withdraw_data = array('id'=>'1cb36826-8e05-40d6-8b9e-7f7439a141cb', 'amount'=>'500');
    $blockCard_data = array('id' => '1cb36826-8e05-40d6-8b9e-7f7439a141cb', 'status_action'=>'block');

    $card = new VirtualCard();
    $createCard = $card->createCard($data);//initiates the charge
    $getCard = $card->getCard($getCardData);
    $getCards = $card->listCards();
    $terminate = $card->terminateCard($terminate_data);
    $fund = $card->fundCard($fund_data);
    $transactions = $card->cardTransactions($trns_data);
    $withdraw = $card->cardWithdrawal($withdraw_data);
    $block_unblock = $card->block_unblock_card($blockCard_data);
    print_r($createCard);


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\Subaccount;

$data = array(
    "account_bank"=> "044",
    "account_number"=> "0690000037",
    "business_name"=> "Eternal Blue",
    "business_email"=> "[email protected]",
    "business_contact"=> "Anonymous",
    "business_contact_mobile"=> "090890382",
    "business_mobile"=> "09087930450",
    "country"=> "NG",
    "meta"=> array(
        array(
            "meta_name"=> "mem_adr",
            "meta_value"=> "0x16241F327213"
        )
    ),
    "split_type"=> "percentage",
    "split_value"=> 0.5
);

$fetch_data = array("id" => "RS_9247C52A37C5EB15C7E8E974CD1B35D7");
$update_data = array("id" => "2755","business_name"=>"Mad O!","business_email"=> "[email protected]",
"account_bank"=> "044","account_number"=> "0690000040","split_type"=> "flat","split_value"=> "200");

$subaccount = new Subaccount();
$createSubaccount = $subaccount->createSubaccount($data);
$getSubaccounts = $subaccount->getSubaccounts();
$fetchSubaccount = $subaccount->fetchSubaccount($fetch_data);
$updateSubaccount = $subaccount->updateSubaccount($update_data);
print_r($createSubaccount);


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);


use Flutterwave\Bill;

$data = array(
    "country"=> "NG",
	"customer"=> "+23490803840303",
	"amount"=> 500,
	"recurrence"=> "ONCE",
	"type"=> "AIRTIME",
	"reference"=> "9300049645534545454332433"
);

//sample payload for bulkBill()
$bulkdata = array(
    "bulk_reference"=>"edf-12de5223d2f3243474543",
    "callback_url"=>"https://webhook.site/96374895-154d-4aa0-99b5-709a0a128674",
    "bulk_data"=> array(
        array(
        "country"=> "NG",
        "customer"=> "+23490803840303",
        "amount"=> 500,
        "recurrence"=> "WEEKLY",
        "type"=> "AIRTIME",
        "reference"=>"930049200929"
        ),
        array(
        "country"=>"NG",
        "customer"=> "+23490803840304",
        "amount"=> 500,
        "recurrence"=> "WEEKLY",
        "type"=>"AIRTIME",
        "reference"=>"930004912332434232"
        )
    ),
);

$getdata = array(
    //"reference"=>"edf-12de5223d2f32434753432"
     "id"=>"BIL136",
     "product_id"=>"OT150"
);

$payment = new Bill();
$result = $payment->payBill($data);//create a bill paymenr
$bulkresult = $payment->bulkBill($bulkdata);//create bulk bill payment....
$getresult = $payment->getBill($getdata);// get bulk result....
$getAgencies = $payment->getAgencies();
$getBillCategories = $payment->getBillCategories();
print_r($result);


use Flutterwave\Ebill;

$data = array(
    "narration"=> "mndkn blls",
    "number_of_units"=> 2,//should be a string
    "currency"=> "NGN",
    "amount"=> 200,//should be a string
    "phone_number"=> "09384747474",
    "email"=>"[email protected]",
    "tx_ref"=> "akhlm-pstmn-1094434370393",
    "ip"=> "127.9.0.7",
    "custom_business_name"=> "John Madakin",
    "country"=> "NG"
);

$update = array(
    "reference"=>"RVEBLS-2B93A7039017-90937",//on creation of order, this is the flw_ref
    "currency"=> "NGN",
    "amount"=> "4000"
);

$payment = new Ebill();
$result = $payment->order($data);//create an order reciept
$updateResult = $payment->updateOrder($update);//create bulk bill payment....
print_r($result);


use Flutterwave\VirtualAccount;

//sample payload for payBill()
$data = array(
  "email"=> "[email protected]",
  "duration"=> 5,
  "frequency"=> 5,
  "amount"=>"22000",
  "is_permanent"=> true,
  "tx_ref"=> "jhn-mdkn-101923123463"
);

$bulkdata = array(
  "accounts"=> 5,
  "email"=> "[email protected]",
  "is_permanent"=> true,
  "tx_ref"=> "jhn-mndkn-012439283422"
);

$batch = array('batch_id' => 'RND_2641579516055928');

$getdata = array(
    "order_ref"=>"URF_1590362018488_8875935"
);

$account = new VirtualAccount();
$result = $account->createVirtualAccount($data);//create a virtak account
$bulkAccounts = $account->createBulkAccounts($bulkdata);//create bulk v accounts
$virtualAccounts = $account->getBulkAccounts($batch);//list all bulk accounts
$virtualAccount = $account->getAccountNumber($getdata);//get an account.
print_r($result);


use Flutterwave\TokenizedCharge;

$data = array(
     "token"=> "flw-t1nf-1ff187b04cecb4acff4ac62c2b6f7784-m03k",
     "currency"=> "NGN",
     "country"=> "NG",
     "amount"=> 30300,
     "email"=> "[email protected]",
     "first_name"=> "Anonymous",
     "last_name"=> "customer",
     "client_ip" =>"154.123.220.1",
     "device_fingerprint" =>"62wd23423rq324323qew1" 
    );

$payment = new TokinizedCharge();
$result = $payment->tokenCharge($data);//initiates the charge
$verify = $payment->verifyTransaction();
print_r($result);


use Flutterwave\Transactions;

$data = array(
'amount'=> 1000
);
$fetch_data = array(
'id'=>'345522'
);
$time_data = array(
  'id'=>'3434'
);

$history = new Transactions();
$transactions = $history->viewTransactions();
$transactionfee = $history->getTransactionFee($data);
$verifyTransaction = $history->verifyTransaction($fetch_data);
$timeline = $history->viewTimeline($time_data);
print_r($transactions);



use Flutterwave\VoucherPayment;
//The data variable holds the payload
$data = array(
        //"public_key": "FLWPUBK-6c4e3dcb21282d44f907c9c9ca7609cb-X"//you can ommit the public key as the key is take from your .env file
        //"tx_ref": "MC-15852309v5050e8",
        "amount"=> "100",
        "type"=> "voucher_payment",
        "currency"=> "ZAR",
        "pin"=> "19203804939000",
        "email"=>"[email protected]",
        "phone_number" =>"0902620185",
        "account_bank" => "058",
        "fullname" => "Ekene Eze",
        "client_ip" =>"154.123.220.1",
        "device_fingerprint" =>"62wd23423rq324323qew1",
        "meta" => array(
            "flightID"=> "123949494DC"
        )     
    );

$payment = new VoucherPayment();
$result = $payment->voucher($data);
if(isset($result['data'])){
  $id = $result['data']['id'];
  $verify = $payment->verifyTransaction($id);
}
print_r($result);
$array