Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
//connect to the db in order to store the user responses
require_once "vendor/autoload.php";
include 'Driver.php';
$driver = new Driver();
use AfricasTalking\SDK\AfricasTalking;
$username = "sandbox";
$apiKey = "your africa's Talking API Key";
$AT = new AfricasTalking($username, $apiKey);
$payments = $AT->payments();
$airtime = $AT->airtime();
// Reads the variables sent via POST from Africas Talking
$sessionId = $_POST["sessionId"];
$serviceCode = $_POST["serviceCode"];
$phoneNumber = $_POST["phoneNumber"];
$text = $_POST["text"];
//get the last part of the text
$textArray=explode('*', $text);
$userResponse=trim(end($textArray));
//set the default level. This is to help us serve the user different menus as we progress
$level=0;
//check the user's level
$level = $driver->checkLevel($sessionId);
if ( $level == 0) {
$driver->insertLevel($sessionId,$phoneNumber,$level);
// This is the first request. Note how we start the response with CON
$response = "CON How much airtime would you like to buy?\n";
$response .= "Minimum: 100 \n";
$response .="Maximum: 10,000";
$driver->updateLevel($sessionId,1);
}
else if($level==1){
$driver->updateLevel($sessionId,2);
$response = "CON What is your account name?\n";
}
else if($level == 2){
$driver->updateLevel($sessionId,3);
$response = "CON What is your account number \n";
}
else if($level == 3){
$driver->updateLevel($sessionId,4);
$response = "CON Name of your bank, select from below \n";
$response .= "0. FCMB \n";
$response .= "1. Zenith \n";
$response .= "2. Access \n";
$response .= "3. Providus \n";
$response .= "4. Sterling\n";
}
else if($level == 4){
$driver -> updateLevel($sessionId,5);
$response = "CON When were you born ? \n";
$response .= "DD/MM/YYYY \n";
$bankcode = [234001, 234002, 234003, 234007, 234010 ];
$GLOBALS['bankCode'] = $bankcode[$userResponse];
}
else if($level == 5){
$driver -> updateLevel($sessionId,6);
$response = "CON You're about to send NGN ".$textArray[0]."to Elizabeth for an airtime purchase.Proceed ? \n";
$response .= "1. Okay \n";
$response .= "2. Cancel ";
}
else if($level == 6 && $userResponse == 2){
$response = "END Thank you for your time. Goodbye";
}
else if($level == 6 && $userResponse == 1){
$driver -> updateLevel($sessionId,7);
$response = "CON We are processing your request.Please enter your OTP \n";
$bankAccount = ['accountName'=>$textArray[1],'accountNumber'=>$textArray[2],'bankCode'=>$final,'dateOfBirth'=>"1989"];
$options = [ 'productName' => "Kibanda",
'bankAccount'=> $bankAccount,
'currencyCode'=>"NGN",
'amount'=>$textArray[0],
'narration'=>"Airtime payment"];
$responses = $payments->bankCheckout($options);
$tid = (array)$responses['data'];
$GLOBALS['tid'] = $tid['transactionId'];
}
else if ($level == 7){
$response = "END Thanks, you're airtime will arrive shortly";
$otp = $userResponse;
$payments-> validateBankCheckout(['transactionId'=>$tid,'otp'=>$otp]);
$airtime->send([
'recipients' => [ ["phoneNumber"=>$phoneNumber,"amount"=>"NGN ".$textArray[0] ],]]);
}
header('Content-type: text/plain');
echo $response;
// DONE!!!
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment