<?php
include "../functions/func_misc.php";
include "../functions/func_cus_account.php";
include "../functions/func_customer.php";
include "../functions/func_payglobal_voucher.php";
include "../functions/func_pricing_policy.php";
include "../functions/func_send_receive_money.php";
include "../functions/func_transactions.php";
include "../functions/func_commission.php";
include "../functions/func_token.php";
include "../functions/func_global_account.php";
include "../functions/func_notification.php";
include "../functions/func_viewquery.php";
include "../functions/func_system.php";
include "../functions/func_report.php";
include "../functions/func_response.php";
include "../../system/airtime/airtime.php";
include "../../system/airtime/airtime_lr.php";
include "../../system/bank/bank-index.php";
include "../../system/bank_test/lr_func_bank_api.php";
include "../../../config/connection.php";
include "../../system/zenith/func_card.php";
include "../../system/hubtel/hubtel_func.php";
include "../../system/one4all/one4all_func.php";
include "../../system/vend/classes/Vend.php";


////////////////////Get Preferred Currency
function getLiberianCode($dbc,$phoneNum){
    $query = "SELECT * FROM customer_tbl WHERE phoneNum = '$phoneNum'";
    $q = mysqli_query($dbc,$query);
    $data = mysqli_fetch_array($q);
    //$cCode = $data['countryCode'];
    return $data;
}

function exchange_currency($dbc,$phoneNum,$countryCode){
    if(update_country_code($dbc,$phoneNum,$countryCode,'MULTIPLE')){
        return true;
    } else{
        return false;
    }
}


function getLiberianCurrency($dbc,$cCode){
    $query1 = "SELECT * FROM country_tbl WHERE countryCode='$cCode'";
    $q1 = mysqli_query($dbc,$query1);
    $data1 = mysqli_fetch_array($q1);
    //$currency = $data1['currency'];
    return $data1;
}
////////////////////////////////////////////



            if (isset($_REQUEST['action'])) {
                if ($_SERVER['REQUEST_METHOD'] == 'POST'){
                    $action = $_REQUEST['action'];
                    switch ($action) {
                        CASE 500:
                            //Firebase id creator
                            $fireToken = html_filter($dbc, $_REQUEST['fireToken']);
                            $phoneNum = html_filter($dbc, $_REQUEST['phoneNum']);
                            if($source == '1'){
                                $phoneNum = "+".html_filter($dbc, $_REQUEST['phoneNum']);
                            }
                            if (check_if_number_exist($dbc, $phoneNum)) {

                                $q = mysqli_query($dbc, "UPDATE customer_tbl SET firebaseKey = '$fireToken' WHERE phoneNum = '$phoneNum'");
                                if ($q) {
                                    echo "SUCCESSFUL";

                                } else {
                                    echo "UNSUCCESSFUL";
                                }
                            }
                            break;
                        case 700:

                            $phoneNum = html_filter($dbc, $_REQUEST['recipient']);
                            if($source == '1'){
                                $phoneNum = "+".html_filter($dbc, $_REQUEST['recipient']);
                            }
                            if (check_if_number_exist($dbc, $phoneNum)) {
                                $cus = get_customer_by_phoneNum($dbc, $phoneNum);
                                $fullName = $cus['fullName'];
                                echo $fullName;
                            } else {
                                //Call token generator
                                $token = tokenGenerator();
                                echo $token;
                            }

                            break;
                        case 701:

                            $phoneNum    = html_filter($dbc, $_REQUEST['phoneNum']);
                            $countryCode = html_filter($dbc, $_REQUEST['countryCode']);
                            $countryData = get_country_code_with_currency($dbc, $countryCode);

                            $newPhoneNum = $countryData['callCode'] . $phoneNum;
                            $code = genSMSCode();
                            $message = "Use this code: {$code} to activate your account";

                            if($source == '1'){
                                $message = "Please tap the following link to activate PayGlobal Account:payglobal://?contact={$code}";
                            }
                            sendSMS($newPhoneNum, $message);
                            echo trim($code);
                            break;

                        case 501:
                            $platform = html_filter($dbc, $_REQUEST['platform']);
                            if($platform == 'ios'){
                                $res = array();
                                //update_system_status($dbc,$serviceType,$status)
                                //$system_status = get_system_status_by_serviceType($dbc, "HUBTEL")
                                //$res['review'] = 'review4';
                                //$res['review'] = 'active';
                                $res['review'] = get_system_status_by_serviceType($dbc, "IOS_APP_STATUS")['status'];
                                $res['statusCode'] = 200;
                                echo json_encode($res);
                            }
                            break;

                        case 502:
                            $status = html_filter($dbc, $_REQUEST['review_status']);
                            //if($platform == 'ios'){
                            $res = array();
                            $serviceType = "IOS_APP_STATUS";
                            update_system_status($dbc,$serviceType,$status);
                            //$system_status = get_system_status_by_serviceType($dbc, "IOS_APP_STATUS")
                            //$res['review'] = 'review4';
                            $res['review'] = $status;
                            $res['statusCode'] = 200;
                            echo json_encode($res);
                            //}
                            break;

                        case 702:
                            $from = html_filter($dbc, $_REQUEST['from']);
                            $to = html_filter($dbc, $_REQUEST['to']);
                            $phoneNum = html_filter($dbc, $_REQUEST['phoneNum']);
                            $email = html_filter($dbc, $_REQUEST['email']);
                            if($source == '1'){
                                $phoneNum = "+".html_filter($dbc, $_REQUEST['phoneNum']);
                            }

                            if (check_if_number_exist($dbc, $phoneNum)) {
                                if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
                                    $customerData = get_customer_by_phoneNum($dbc, $phoneNum);
                                    // $data = get_all_transaction_by_dateRate($dbc,$customerData['id'],$from,$to);

                                    echo "SUCCESSFUL";
                                } else {
                                    echo 'WRONG_EMAIL';
                                }

                            } else {
                                echo 'UNSUCCESSFUL';
                            }
                            break;
                        case 900:
                            $phoneNum = html_filter($dbc, $_REQUEST['rcontact1']);
                            $password = passwordHash($_REQUEST['pword'], $mySalt);
                            $simSID = html_filter($dbc, $_REQUEST['simsid']);
                            $simSerial = html_filter($dbc, $_REQUEST['simserial']);
                            $keyCode = html_filter($dbc, $_REQUEST['keyCode']);
                            $phoneIMEI = html_filter($dbc, $_REQUEST['pid']);
                            $countryCode = html_filter($dbc, $_REQUEST['countryCode']);

                            $countryData = get_country_code_with_currency($dbc, $countryCode);
                            //$phoneNum = $countryData['callCode'].$phoneNum;
                            //$phoneNum = phone_format($phoneNum);

                            if($source == '1'){
                                $phoneNum = "+".html_filter($dbc, $_REQUEST['rcontact1']);
                            }

                            if (check_if_number_exist($dbc, $phoneNum)) {
                                if (verify_password($dbc, $password, $phoneNum)) {
                                    if (update_keyCode_variables($dbc, $keyCode, $phoneIMEI, $simSID, $simSerial, $phoneNum)) {
                                        $vvv1 = "PHONE_EXISTS";
                                        //echo "(".$countryData['callCode']."/".$vvv1."*".$countryData['currency'].")";
                                        echo "({$countryData['callCode']}/{$vvv1}*{$countryData['currency']})";
                                    }
                                } else {
                                    echo "INCORRECT_PASSWORD";
                                }

                            }
                            break;
                        case 901:
                            //Registration for the first time on smart phone
                            $data = [];
                            $firstname = html_filter($dbc, $_REQUEST['firstname']);
                            $lastname  = html_filter($dbc, $_REQUEST['lastname']);
                            $middlename  = html_filter($dbc, $_REQUEST['middlename']);

                            $data["physical_address"]   = $_REQUEST['address1']."~~".$_REQUEST['address2']."~~".$_REQUEST['address3'];

                            $data["fullName"]  = $lastname." ".$middlename." ".$firstname;
                            $data["dob"]       = html_filter($dbc, $_REQUEST['dob']);
                            $data["email"]     = html_filter($dbc, $_REQUEST['email']);
                            $data["password"]  = encrypting_pass($_REQUEST['password']);
                            $phoneNum           = html_filter($dbc, $_REQUEST['phoneNum']);
                            $data["countryCode"] = html_filter($dbc, $_REQUEST['countryCode']);

                            $countryData = get_country_code_with_currency($dbc,$data["countryCode"]);
                            $data["phoneNum"]    = $countryData['callCode'].$phoneNum;

                            if (check_if_email_exist($dbc, $data["email"])){

                                $response['statusCode'] = 500;
                                $response['message']    = 'Sorry, Email already exist in our system';

                            }elseif(check_if_number_exist($dbc,$data["phoneNum"])){

                                $response['statusCode'] = 500;
                                $response['message']    = 'Sorry, Phone Number already exist in our system';

                            }else {
                                if (add_customer_by_web($dbc, $data)) {

                                    $otp_code = genSMSCode();
                                    $custdata = get_customer_by_phoneNum($dbc,$data["phoneNum"]);

                                    $response['statusCode']   = 200;
                                    $response['callCode']     = $countryData['callCode'];
                                    $response['currency']     = $countryData['currency'];
                                    $response['customer_id']  = $custdata["id"];
                                    $response['phoneNum']     = $custdata["phoneNum"];
                                    $response['message']      = 'Registration Successful';
                                    $response['OTP']          = $otp_code;

                                    $message = "Use this code OTP: {$otp_code} to activate your account";
                                    sendSMS($data["phoneNum"], $message);

                                } else {
                                    $response['statusCode'] = 500;
                                    $response['message'] = 'Registration could not be completed. Try again later';
                                }
                            }

                            echo json_encode($response);

                            break;

                        case  "CHECK-CUSTOMER-EXIST":

                            $email         = html_filter($dbc, $_REQUEST['email']);
                            $phoneNum      = html_filter($dbc, $_REQUEST['phoneNum']);
                            $countryCode   = html_filter($dbc, $_REQUEST['countryCode']);
                            $checkaction   =  $_POST["checkaction"];
                            $countryData   = get_country_code_with_currency($dbc,$countryCode);
                            $data["phoneNum"]    = $countryData['callCode'].$phoneNum;

                            switch($checkaction):
                                case "EMAIL":

                                    if (check_if_email_exist($dbc, $email)){
                                        $response['statusCode'] = 500;
                                        $response['message']    = 'Sorry, Email already exist in our system';
                                    }else{
                                        $response['statusCode'] = 200;
                                        $response['message']    = 'Success';
                                    }
                                 break;

                                case "PHONENUM":

                                    $custdata = get_customer_by_phoneNum($dbc,$phoneNum);

                                    if(check_if_number_exist($dbc,$phoneNum)) {
                                        $response['statusCode'] = 500;
                                        $response['message'] = 'Sorry, Phone Number already exist in our system';
                                    }else{
                                        $response['statusCode'] = 200;
                                        $response['message']    = "Success";

                                    }
                                 break;

                                 default;

                            endswitch;

                            echo json_encode($response);

                            break;
                        case 902:
                            //Restore Registered Account

                            $phoneNum = html_filter($dbc, $_REQUEST['phoneNum']);
                            $keyCode = html_filter($dbc, $_REQUEST['keyCode']);
                            $countryCode = html_filter($dbc, $_REQUEST['countryCode']);
                            $password = passwordHash($_REQUEST['password'], $mySalt);

                            $countryData = get_country_code_with_currency($dbc, $countryCode);
                            $phoneNum = $countryData['callCode'] . $phoneNum;

                            if (check_phoneNum_and_password($dbc, $phoneNum, $password)) {
                                if (update_key_code($dbc, $keyCode, $phoneNum)) {
                                    echo "(SUCCESSFUL/{$countryData['callCode']}*{$countryData['currency']})";
                                } else {
                                    echo "UNSUCCESSFUL1";
                                }
                            } else {
                                echo "UNSUCCESSFUL2";//." ".$phoneNum." ".$password

                            }
                            break;
                        case 911:
                            //Migration Restore USSD Registered Account

                            $phoneNum = html_filter($dbc, $_REQUEST['phoneNum']);
                            $keyCode = html_filter($dbc, $_REQUEST['keyCode']);
                            $countryCode = html_filter($dbc, $_REQUEST['countryCode']);
                            $password = passwordHash($_REQUEST['password'], $mySalt);
                            $pin = html_filter($dbc, $_REQUEST['pin']);
                            $phoneIMEI = html_filter($dbc, $_REQUEST['pid']);
                            $simSID = html_filter($dbc, $_REQUEST['simsid']);
                            $simSerial = html_filter($dbc, $_REQUEST['simSerial']);

                            $countryData = get_country_code_with_currency($dbc, $countryCode);
                            $phoneNum = $countryData['callCode'] . $phoneNum;

                            if (check_phoneNum_and_pin($dbc, $phoneNum, trim($pin))) {
                                if (restore_ussd_account_on_smart($dbc, $phoneNum, $keyCode, $password, $simSID, $simSerial, $phoneIMEI)) {
                                    echo "(SUCCESSFUL/{$countryData['callCode']}*{$countryData['currency']})";
                                } else {
                                    echo "UNSUCCESSFUL";
                                }
                            } else {
                                echo "UNSUCCESSFUL";
                            }
                            break;
                        case 903:
                            // Load money using Pay Global Voucher
                            $phoneNum = html_filter($dbc, $_REQUEST['rcontact']);
                            if($source == '1'){
                                $phoneNum = "+".html_filter($dbc, $_REQUEST['rcontact']);
                            }
                            $amount = html_filter($dbc, $_REQUEST['ramount']);
                            $voucherCode = html_filter($dbc, $_REQUEST['voucherCode']);
                            $keyCode = html_filter($dbc, $_REQUEST['keyCode']);
                            $countryCode = html_filter($dbc, $_REQUEST['countryCode']);
                            if (check_voucher_exist($dbc, $voucherCode, $amount, $countryCode)) {
                                if (check_phoneNum_and_keyCode($dbc, $phoneNum, $keyCode)) {
                                    if (recharge_with_voucher($dbc, $phoneNum, $voucherCode,$countryCode)) {
                                        echo("VOUCHER_ADD");
                                    } else {
                                        echo("VOUCHER_ADDITION_FAILED");
                                    }
                                } else {
                                    echo("NOT_VERIFIED_USER");
                                }
                            } else {
                                echo("WRONG_VOUCHER");
                            }
                            break;

                        CASE 908:
                            // Token, Receive money from token
                            $phoneNum = html_filter($dbc, $_REQUEST['recipientNumber']);
                            $keyCode = html_filter($dbc, $_REQUEST['keyCode']);
                            $token = trim(html_filter($dbc, $_REQUEST['token']));
                            $tokenPin = trim(html_filter($dbc, $_REQUEST['pin']));
                            if($source == '1'){
                                $phoneNum = "+".html_filter($dbc, $_REQUEST['recipientNumber']);
                            }

                            $recipientData = get_customer_by_phoneNum($dbc, $phoneNum);
                            if (check_phoneNum_and_keyCode($dbc, $phoneNum, $keyCode)) {
                                if (check_if_number_exist_as_agent($dbc, $phoneNum)) {
                                    if (check_token_exist($dbc, $token, $tokenPin, $recipientData['countryCode'])) {

                                        if (check_if_token_used($dbc, $token)) {
                                            $tokenData = get_token_mgt_by_token_pin($dbc, $token, $tokenPin);
                                            $senderData = get_customer_by_id($dbc, $tokenData['senderID']);

                                            $transactionData = get_main_transaction_by_transaction_uid($dbc, $tokenData['transaction_uid']);
                                            if(add_funds($dbc,$recipientData['id'],$transactionData['recipientAmount'],$recipientData['countryCode'])){
                                                $pricing = get_pricing_by_id($dbc, $transactionData['pricing_id']);
                                                $recipientAcc = get_cus_account_info_by_customerID($dbc, $recipientData['id'],$recipientData['countryCode']);
                                                if (update_transaction_ussd_by_transaction_id($dbc, $recipientData, $transactionData['transaction_uid'], $recipientAcc['balance'])) {
                                                    if (update_token_mgt_after_redraws($dbc, $recipientData, $transactionData['transaction_uid'])) {
                                                        $agentFee = $transactionData['fee'] * $pricing['recipientCommission'];
                                                        $fee = $transactionData['exRate'] * $agentFee;
                                                        $newTransactionData = get_main_transaction_by_transaction_uid($dbc, $tokenData['transaction_uid']);
                                                        if (add_agent_commission($dbc, $newTransactionData, $fee, $recipientData['countryCode'])) {
                                                            echo "SUCCESSFUL";
                                                            $currencyData = get_currency_by_countryCode($dbc,$recipientData['countryCode']);

                                                            $message = "You have received an amount of {$currencyData['currency']} {$newTransactionData['recipientAmount']} from token redemption. Transaction ID is {$newTransactionData['transaction_uid']}";
                                                            sendSMS($phoneNum, $message);
                                                        }
                                                    }
                                                }

                                            } else{
                                                echo "UNSUCCESSFUL";
                                            }
                                        } else {
                                            echo "ALREADY_CLAIMED";
                                        }
                                    } else {
                                        echo "WRONG_TOKEN";
                                    }
                                } else {
                                    echo "UNAUTHORISED_USER";
                                }
                            } else {
                                echo("Sorry, Error authenticating user!");
                            }
                            // End of token Retrieval
                            break;

                        CASE 910:
                            // Airtime TopUp
                            $sendPhoneNum = html_filter($dbc, $_REQUEST['senderPhone']);
                            if($source == '1'){
                                $sendPhoneNum = "+".html_filter($dbc, $_REQUEST['senderPhone']);
                            }
                            $airtimeNumber = html_filter($dbc, $_REQUEST['recipientPhone']);
                            $country = html_filter($dbc, $_REQUEST['country']);
                            $network = html_filter($dbc, $_REQUEST['network']);
                            $amount = html_filter($dbc, $_REQUEST['amount']);
                            $keyCode = html_filter($dbc, $_REQUEST['keyCode']);
                            $transactionType = 'AIRTIME';
                            $airtimeStatus = 1;
                            $senderType = get_customer_by_phoneNum($dbc,$sendPhoneNum);

                            if($countryCode == 'lr' OR $countryCode == 'lr1'){
                                if($airtimeStatus == 1) {
                                    if (check_phoneNum_and_keyCode($dbc, $sendPhoneNum, $keyCode)) {
                                        $senderData = get_customer_by_phoneNum($dbc, $sendPhoneNum);
                                        $networkData = strtoupper($network);
                                        $senderAcc = get_cus_account_info_by_customerID($dbc, $senderData['id'],'lr');
                                        $transaction_uid = genTransactionID();
                                        $airTimeAccData = get_customer_by_phoneNum($dbc,"+231772000000");
                                        if ($senderAcc['balance'] >= $amount) {

                                            if (redraw_funds($dbc, $senderData['id'], $amount,'lr')) {
                                                if(add_funds($dbc,$airTimeAccData['id'],$amount,'lr')) {
                                                    $senderAcc = get_cus_account_info_by_customerID($dbc, $senderData['id'],'lr');
                                                    $airtimeAcc = get_cus_account_info_by_customerID($dbc, $airTimeAccData['id'],'lr');

                                                    if (lr_add_airtime_transaction_by_smartPhone($dbc, $senderData['id'],$airTimeAccData['id'],$airtimeNumber, $amount,$amount, 'lr','lr', $network, $transaction_uid, 0.00, $senderAcc['balance'],$airtimeAcc['balance'])) {

                                                        /*
                                                        * Agents gets commission when sells airtime
                                                        */
                                                        if ($senderData['accountType'] == 2 OR $senderData['accountType'] == 4) {
                                                            $fee = $amount * 0.025;

                                                            $transactionData = array();
                                                            $transactionData['recipientID'] = $senderData['id'];
                                                            $transactionData['senderID'] = $senderData['id'];
                                                            $transactionData['transactionTypes'] = 'AIRTIME';
                                                            $transactionData['transaction_uid'] = $transaction_uid;

                                                            if (add_agent_commission($dbc, $transactionData, $fee, 'lr')){
                                                                if(add_airtimeExpense($dbc, $senderData['countryCode'], $fee)){
                                                                    $response = lr_buy_airtime($airtimeNumber, $amount, $networkData, $transaction_uid);
                                                                    if ($response['status']['typeName'] == "Success") {

                                                                        if(lr_update_airtime_transactions_balance($dbc,$transaction_uid,$senderAcc['balance'],$airtimeAcc['balance'])){
                                                                            /*
                                                            * End of agents get commission
                                                            */
                                                                            $airtimeAmount= moneyFormat2($amount);
                                                                            $senderBalance = moneyFormat2($senderAcc['balance']);

                                                                            $currencyDate = get_currency_by_countryCode($dbc,$senderData['countryCode']);


                                                                            //$message = "You have purchased {$amount} value of Airtime for {$airtimeNumber}";
                                                                            $senderMessage = "You have purchased {$currencyDate['currency']} {$airtimeAmount} airtime for {$airtimeNumber} Balance: {$fcurr} {$senderBalance} Transaction ID: {$transaction_uid}";
                                                                            //$recipientMessage = "You have received airtime of {$airtimeAmount}, from GN Mobile Money ({$senderData['fullName']} - {$senderData['phoneNum']}) ";
                                                                            $recipientMessage = "You have received {$currencyDate['currency']} {$airtimeAmount} airtime, from ({$senderData['fullName']} - {$senderData['phoneNum']}) Dial *712# to register for GN Mobile Money";
                                                                            sendSMS($airtimeNumber, $recipientMessage);
                                                                            sendSMS($senderData['phoneNum'],$senderMessage);

                                                                            if ($senderData['firebaseKey'] != '') {
                                                                                firebaseCall($senderData['firebaseKey'],  $senderMessage);
                                                                            }

                                                                            echo "SUCCESSFUL";
                                                                        }

                                                                    }
                                                                }
                                                            }

                                                        }


                                                    } else {
                                                        echo "UNSUCCESSFUL1";
                                                    }
                                                } else{
                                                    echo "UNSUCCESSFUL1";
                                                }
                                            } else {
                                                echo "UNSUCCESSFUL3";
                                            }

                                        } else {
                                            echo 'LOW_BALANCE';
                                        }


                                    } else {
                                        echo("Sorry, Error authenticating user!");
                                    }
                                }else{
                                    echo 'TRANSACTION_ERROR';
                                }




                            } else{
                                if($senderType['accountType']==1){

                                    if($airtimeStatus == 1) {
                                        if (check_phoneNum_and_keyCode($dbc, $sendPhoneNum, $keyCode)) {
                                            $senderData = get_customer_by_phoneNum($dbc, $sendPhoneNum);
                                            $networkData = get_airtime_network($network);
                                            $senderAcc = get_cus_account_info_by_customerID($dbc, $senderData['id'],$senderData['countryCode']);
                                            $transaction_uid = genTransactionID();
                                            $airTimeAccData = get_customer_by_phoneNum($dbc,"+233772000000");
                                            if ($senderData['countryCode'] == 'gh') {
                                                if ($senderAcc['balance'] >= $amount) {
                                                    $response = buy_airtime($airtimeNumber, $amount, $networkData, $transaction_uid);
                                                    if ($response['status'] == "OK") {
                                                        if (redraw_funds($dbc, $senderData['id'], $amount,$senderData['countryCode'])) {
                                                            if(add_funds($dbc,$airTimeAccData['id'],$amount,$airTimeAccData['countryCode'])) {
                                                                $senderAcc = get_cus_account_info_by_customerID($dbc, $senderData['id'],$senderData['countryCode']);
                                                                $airtimeAcc = get_cus_account_info_by_customerID($dbc, $airTimeAccData['id'],$airTimeAccData['countryCode']);
                                                                if (add_airtime_transaction_by_smartPhone($dbc, $senderData['id'],$airTimeAccData['id'],$airtimeNumber, $amount,$amount, $senderData['countryCode'],$airTimeAccData['countryCode'], $network, $transaction_uid, $response['balance'], $senderAcc['balance'],$airtimeAcc['balance'])) {
                                                                    /*
                                                                    * Agents gets commission when sells airtime
                                                                    */
                                                                    if ($senderData['countryCode'] == 'gh' AND $senderData['accountType'] == 2 OR $senderData['accountType'] == 4) {
                                                                        $fee = $amount * 0.04;

                                                                        $transactionData = array();
                                                                        $transactionData['recipientID'] = $senderData['id'];
                                                                        $transactionData['senderID'] = $senderData['id'];
                                                                        $transactionData['transactionTypes'] = 'AIRTIME';
                                                                        $transactionData['transaction_uid'] = $transaction_uid;
                                                                        if (add_agent_commission($dbc, $transactionData, $fee, $senderData['countryCode'])) {
                                                                            add_airtimeExpense($dbc, $senderData['countryCode'], $fee);

                                                                        }

                                                                    }

                                                                    /*
                                                                     * End of agents get commission
                                                                     */
                                                                    $airtimeAmount= moneyFormat2($amount);
                                                                    $senderBalance = moneyFormat2($senderAcc['balance']);

                                                                    $currencyDate = get_currency_by_countryCode($dbc,$senderData['countryCode']);


                                                                    //$message = "You have purchased {$amount} value of Airtime for {$airtimeNumber}";
                                                                    $senderMessage = "You have purchased {$currencyDate['currency']} {$airtimeAmount} airtime for {$airtimeNumber} Balance: {$fcurr} {$senderBalance} Transaction ID: {$transaction_uid}";
                                                                    //$recipientMessage = "You have received airtime of {$airtimeAmount}, from GN Mobile Money ({$senderData['fullName']} - {$senderData['phoneNum']}) ";
                                                                    $recipientMessage = "You have received {$currencyDate['currency']} {$airtimeAmount} airtime, from ({$senderData['fullName']} - {$senderData['phoneNum']}) Dial *712# to register for GN Mobile Money";
                                                                    sendSMS($airtimeNumber, $recipientMessage);
                                                                    sendSMS($senderData['phoneNum'],$senderMessage);

                                                                    if ($senderData['firebaseKey'] != '') {
                                                                        firebaseCall($senderData['firebaseKey'],  $senderMessage);
                                                                    }

                                                                    echo "SUCCESSFUL";
                                                                } else {
                                                                    echo "UNSUCCESSFUL1";
                                                                }
                                                            } else{
                                                                echo "UNSUCCESSFUL1";
                                                            }
                                                        } else {
                                                            echo "UNSUCCESSFUL3";
                                                        }
                                                    } else {
                                                        echo 'TRANSACTION_ERROR';
                                                    }

                                                } else {
                                                    echo 'LOW_BALANCE';
                                                }
                                            } else {
                                                // International to Ghana Airtime purchase
                                                $rate = get_conversion_rate($dbc,$senderData['countryCode'],'gh');
                                                $newAmount = $amount * $rate;
                                                if ($senderAcc['balance'] >= $newAmount) {
                                                    $response = buy_airtime($airtimeNumber, $newAmount, $networkData, $transaction_uid);
                                                    if ($response['status'] == "OK") {
                                                        if (redraw_funds($dbc, $senderData['id'], $amount,$senderData['countryCode'])) {
                                                            if(add_funds($dbc,$airTimeAccData['id'],$newAmount,$airTimeAccData['countryCode'])) {
                                                                if(transcription($dbc,$senderData,$airTimeAccData,$transactionType,$amount,'',$transaction_uid)) {
                                                                    $senderAcc = get_cus_account_info_by_customerID($dbc, $senderData['id'],$senderData['countryCode']);
                                                                    $airtimeAcc = get_cus_account_info_by_customerID($dbc, $airTimeAccData['id'],$airTimeAccData['countryCode']);
                                                                    if (add_airtime_transaction_by_smartPhone($dbc, $senderData['id'], $airTimeAccData['id'], $airtimeNumber,$amount, $newAmount, $senderData['countryCode'], $airTimeAccData['countryCode'], $network, $transaction_uid, $response['balance'], $senderAcc['balance'], $airtimeAcc['balance'])) {
                                                                        /*
                                                                        * Agents gets commission when sells airtime
                                                                        */
                                                                        /*if ($senderData['countryCode'] == 'gh' AND $senderData['accountType'] == 2 OR $senderData['accountType'] == 4) {
                                                                            $fee = $amount * 0.02;

                                                                            $transactionData = array();
                                                                            $transactionData['recipientID'] = $senderData['id'];
                                                                            $transactionData['senderID'] = $senderData['id'];
                                                                            $transactionData['transactionTypes'] = 'AIRTIME';
                                                                            $transactionData['transaction_uid'] = $transaction_uid;
                                                                            if (add_agent_commission($dbc, $transactionData, $fee, $senderData['countryCode'])) {
                                                                                add_airtimeExpense($dbc, $senderData['countryCode'], $fee);

                                                                            }

                                                                        }
                                                                        */

                                                                        /*
                                                                         * End of agents get commission
                                                                         */

                                                                        $currencyDate = get_currency_by_countryCode($dbc,$senderData['countryCode']);

                                                                        $message = "You have purchased {$currencyDate['currency']} {$amount} airtime for {$airtimeNumber}";
                                                                        sendSMS($senderData['phoneNum'],$message);

                                                                        if ($senderData['firebaseKey'] != '') {
                                                                            firebaseCall($senderData['firebaseKey'],  $message);
                                                                        }

                                                                        echo "SUCCESSFUL";
                                                                    } else {
                                                                        echo "UNSUCCESSFUL1";
                                                                    }
                                                                }
                                                            } else{
                                                                echo "UNSUCCESSFUL1";
                                                            }
                                                        } else {
                                                            echo "UNSUCCESSFUL3";
                                                        }
                                                    } else {
                                                        echo 'TRANSACTION_ERROR';
                                                    }

                                                } else {
                                                    echo 'LOW_BALANCE';
                                                }

                                            }

                                        } else {
                                            echo("Sorry, Error authenticating user!");
                                        }
                                    }else{
                                        echo 'TRANSACTION_ERROR';
                                    }

                                }else{
                                    echo 'Service currently unavailable';//'TRANSACTION_ERROR';
                                }
                            }






                            break;

                        CASE 914:

                            $phoneNum = html_filter($dbc, $_REQUEST['phoneNum']);
                            if($source == '1'){
                                $phoneNum = "+".html_filter($dbc, $_REQUEST['phoneNum']);
                            }
                            $profile = get_customer_by_phoneNum($dbc,$phoneNum);

                            if($profile['countryCode']=="gh"){

                                if (isset($_REQUEST['status'])) {
                                    /*
                                     * WALLET to GN Bank Account
                                     */
                                    $status = html_filter($dbc, $_REQUEST['status']);
                                    if ($status == true) {
                                        $fee = html_filter($dbc, $_REQUEST['fee']);
                                        $transaction_uid = html_filter($dbc, $_REQUEST['transaction_uid']);
                                        $transactionData = get_bank_transaction_by_transaction_uid($dbc, $transaction_uid);
                                        $senderData = get_customer_by_id($dbc, $transactionData['senderID']);
                                        $recipientData = get_customer_by_id($dbc, $transactionData['recipientID']);

                                        $validate = bank_get_account_info($transactionData['senderBankAccount']);
                                        $comment = "GNM WTB ({$senderData['fullName']} - {$senderData['phoneNum']}) TO ({$validate['accountName']})";
                                        if (redraw_funds($dbc, $senderData['id'], $transactionData['sendersAmount'],$senderData['countryCode'])) {
                                            if (add_funds($dbc, $recipientData['id'], $transactionData['recipientAmount'],$recipientData['countryCode'])) {
                                                if(redraw_funds($dbc,$senderData['id'],$fee,$senderData['countryCode'])) {
                                                    $transactionData['senderCountryCode'] = $senderData['countryCode'];
                                                    if (add_payGlobal_commission($dbc, $transactionData, $transactionData['fee'])) {
                                                        $senderAcc = get_cus_account_info_by_customerID($dbc, $senderData['id'],$senderData['countryCode']);
                                                        $recipientAcc = get_cus_account_info_by_customerID($dbc, $recipientData['id'],$recipientData['countryCode']);
                                                        if (update_bank_transaction_before_api_call($dbc, $transaction_uid, $senderAcc['balance'])) {
                                                            $transferData = bank_transfer_funds($transactionData['recipientBankAccount'], $transactionData['senderBankAccount'], $transactionData['recipientAmount'], "0", $comment, $transactionData['foreignId']);
                                                            if ($transferData['comment'] == "Successful") {
                                                                if (update_bank_transaction_after_success($dbc, $transactionData['transaction_uid'], $transferData['foreignId'])) {
                                                                    if(update_bank_transaction_balance($dbc,$transaction_uid,$recipientAcc['balance'],$senderAcc['balance'])) {
                                                                        $transactionData['senderCountryCode'] = "gh";
                                                                        $transactionData['transactionTypes'] = "WTB";

                                                                        $currencyDate = get_currency_by_countryCode($dbc,$senderData['countryCode']);

                                                                        $message = "{$currencyDate['currency']} {$transactionData['recipientAmount']} was transferred to {$transactionData['senderBankAccount']}, transaction ID: {$transactionData['transaction_uid']}";
                                                                        sendSMS($senderData['phoneNum'], $message);
                                                                        echo "SUCCESSFUL";
                                                                    }
                                                                }
                                                            } else {

                                                                echo "UNSUCCESSFUL";
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        } else {
                                            echo "UNSUCCESSFUL";
                                        }

                                    } else {
                                        echo "UNSUCCESSFUL";
                                    }

                                } else {
                                    $phoneNum = html_filter($dbc, $_REQUEST['phoneNum']);
                                    if($source == '1'){
                                        $phoneNum = "+".html_filter($dbc, $_REQUEST['phoneNum']);
                                    }
                                    $keyCode = html_filter($dbc, $_REQUEST['keyCode']);
                                    $amount = html_filter($dbc, $_REQUEST['Bamount']);
                                    $accountNum = html_filter($dbc, $_REQUEST['accNum']);
                                    if (check_phoneNum_and_keyCode($dbc, $phoneNum, $keyCode)) {
                                        $system_status = get_system_status_by_serviceType($dbc, "BANK");
                                        if ($system_status['status'] == 'ON') {
                                            $senderData = get_customer_by_phoneNum($dbc, $phoneNum);
                                            $systemNumber = get_system_number_by_name($dbc,'NEW_OVA','gh');
                                            $recipientData = get_customer_by_phoneNum($dbc, $systemNumber['phoneNum']);
                                            $validate = bank_get_account_info($accountNum);
                                            if ($validate['accountName'] != '') {
                                                /*$recipientData['uGroup'] = 2000;
                                                $senderData['uGroup'] = 2000;
                                                $recipientData['accountType'] = 2000;
                                                $senderData['accountType'] = 2000;*/
                                                $balance = check_for_enough_balance_on_WTB($dbc, $senderData, $recipientData, $amount,'WTB');
                                                //sendSMS("+233246022602",$systemNumber['phoneNum']);
                                                if ($balance['status'] == true) {

                                                    /*
                                                     * Check if customer is Agent or Super Agent. If any Charge no fee.
                                                     *
                                                    $cusData = get_customer_by_id($dbc, $senderData['id']);
                                                    if ($cusData['accountType'] == 2 OR $cusData['accountType'] == 4) {
                                                        $balance['fee'] = 0.00;
                                                        $balance['newAmount'] = $balance['exactAmount'];
                                                    }
                                                    *
                                                    * End of No fee for super agents and agents
                                                    */

                                                    if (dailyKyc($dbc, $senderData['id'], $senderData['countryCode'], $balance['exactAmount'], $balance['fee'])) {
                                                        if (monthlyKyc($dbc, $senderData['id'], $senderData['countryCode'], $balance['exactAmount'], $balance['fee'])) {
                                                            $rate = get_conversion_rate($dbc, $senderData['countryCode'], $recipientData['countryCode']);
                                                            $recipientAmount = $balance['exactAmount'] * $rate;

                                                            if (add_bank_transaction_WTB_smart($dbc, $accountNum, $senderData, $recipientData, 'SMART', 'WTB', $balance, $recipientAmount,$systemNumber)) {
                                                                $fee = moneyFormat($balance['fee']);
                                                                echo "({$validate['accountName']}/{$fee}*{$balance['transaction_uid']}!{$balance['status']})";

                                                            } else {
                                                                echo "ERROR";
                                                            }
                                                        } else {
                                                            echo "MONTHLY_LIMIT";
                                                        }
                                                    } else {
                                                        echo 'DAILY_LIMIT';
                                                    }
                                                } else {
                                                    echo "LOW_BALANCE";
                                                }
                                            } else {
                                                echo "WRONG_ACCOUNT_NUMBER";
                                            }
                                        }else{
                                            echo("Sorry, Error authenticating user!");
                                        }
                                    } else {
                                        echo("Sorry, Error authenticating user!");
                                    }
                                }


                            }else{

                                /*
                                 * Liberia GN Bank Account to payGlobal Wallet
                                 */
                                if (isset($_REQUEST['status'])) {
                                    $status = html_filter($dbc, $_REQUEST['status']);
                                    if ($status == true) {
                                        $fee = html_filter($dbc, $_REQUEST['fee']);
                                        $transaction_uid = html_filter($dbc, $_REQUEST['transaction_uid']);
                                        $transactionData = get_bank_transaction_by_transaction_uid($dbc, $transaction_uid);
                                        $recipientData = get_customer_by_id($dbc, $transactionData['recipientID']);
                                        $senderData = get_customer_by_id($dbc, $transactionData['senderID']);
                                        $totalAmount = $transactionData['fee'] + $transactionData['recipientAmount'];
                                        $validate = lr_bank_get_account_info($transactionData['recipientBankAccount']);
                                        $comment = "GNM BTW ({$validate['accountName']}) TO ({$recipientData['fullName']} - {$recipientData['phoneNum']})";
                                        if (update_bank_transaction_before_api_call($dbc, $transaction_uid, 0)) {
                                            $transferData = lr_bank_transfer_funds_new($transactionData['recipientBankAccount'], $transactionData['senderBankAccount'], $totalAmount, "0", $comment, $transactionData['foreignId']);
                                            if ($transferData['comment'] == "Successful") {
                                                if (redraw_funds($dbc, $senderData['id'], $transactionData['sendersAmount'],$senderData['countryCode'])) {
                                                    if (add_funds($dbc, $recipientData['id'], $transactionData['recipientAmount'],$recipientData['countryCode'])) {
                                                        $all_transactionData = get_bank_transaction_by_transaction_uid($dbc, $transactionData['transaction_uid']);
                                                        //$all_transactionData['senderID'] = $transactionData['recipientID'];
                                                        $all_transactionData['senderCountryCode'] = $senderData['countryCode'];
                                                        $all_transactionData['transactionTypes'] = $transactionData['serviceType'];
                                                        if (add_payGlobal_commission($dbc, $all_transactionData, $all_transactionData['fee'])) {
                                                            if (update_bank_transaction_after_success($dbc, $transactionData['transaction_uid'], $transferData['foreignId'])) {
                                                                $recipientAcc = get_cus_account_info_by_customerID($dbc, $recipientData['id'],$recipientData['countryCode']);
                                                                $senderAcc = get_cus_account_info_by_customerID($dbc, $senderData['id'],$senderData['countryCode']);
                                                                if (update_bank_transaction_balance($dbc, $transactionData['transaction_uid'], $recipientAcc['balance'],$senderAcc['balance'])) {
                                                                    //$message = "{$transactionData['recipientAmount']} was transferred to your wallet, transaction ID: {$transactionData['transaction_uid']}";

                                                                    $currency = get_currency_by_countryCode($dbc,$senderData['countryCode']);
                                                                    $message = "An amount of {$currency['currency']} {$transactionData['recipientAmount']} was transferred to your wallet. Balance {$recipientAcc['balance']} Transaction ID {$transactionData['transaction_uid']}";
                                                                    sendSMS($recipientData['phoneNum'], $message);
                                                                    echo "SUCCESSFUL";
                                                                }
                                                            }
                                                        }

                                                    }
                                                }

                                            } else {
                                                echo "UNSUCCESSFUL";
                                            }

                                        }
                                    } else {
                                        echo "UNSUCCESSFUL2";
                                    }
                                } else {
                                    $phoneNum = html_filter($dbc, $_REQUEST['phoneNum']);
                                    if($source == '1'){
                                        $phoneNum = "+".html_filter($dbc, $_REQUEST['phoneNum']);
                                    }
                                    $keyCode = html_filter($dbc, $_REQUEST['keyCode']);
                                    $amount = html_filter($dbc, $_REQUEST['amount']);
                                    $recipientAcc = html_filter($dbc, $_REQUEST['senderAccount']);
                                    $recipientData = get_customer_by_phoneNum($dbc, $phoneNum);

                                    $systemNumber = get_system_number_by_name($dbc,'NEW_OVA_LR',$recipientData['countryCode']);
                                    $senderData = get_customer_by_phoneNum($dbc, $systemNumber['phoneNum']);

                                    /*$recipientData['uGroup'] = 2000;
                                    $senderData['uGroup'] = 2000;
                                    $recipientData['accountType'] = 2000;
                                    $senderData['accountType'] = 2000;*/

                                    if (check_phoneNum_and_keyCode($dbc, $phoneNum, $keyCode)) {
                                        $system_status = get_system_status_by_serviceType($dbc, "BANK");
                                        if ($system_status['status'] == 'ON') {
                                            $linked = lr_bank_get_linked_accounts_new($phoneNum);
                                            if ($linked['status'] != 'NOT_LINKED') {
                                                $linkedArray = array($linked['account1'], $linked['account2'], $linked['account3']);
                                                if (in_array($recipientAcc, $linkedArray)) {
                                                    $balance = lr_bank_get_account_info($recipientAcc);
                                                    $data = check_for_enough_balance_on_BTW($dbc, $senderData, $recipientData, $amount, 'BTW');
                                                    //sendSMS("+233246022602",$amount);
                                                    if ($balance['availableBal'] >= $data['newAmount']) {
                                                        //$validate = bank_validate_accountNumber($recipientAcc);
                                                        if ($balance['accountName'] != '') {
                                                            $transaction_uid = genTransactionID();
                                                            /*
                                                             * Check if customer is Agent or Super Agent. If any Charge no fee.
                                                             *
                                                            $cusData = get_customer_by_id($dbc,$recipientData['id']);
                                                            if ($cusData['accountType'] == 2 OR $cusData['accountType'] == 4) {
                                                                $data['fee'] = 0.00;
                                                                $data['newAmount'] = $data['exactAmount'];
                                                            }
                                                            *
                                                             * End of No fee for super agents and agents
                                                             */

                                                            if (add_bank_transaction_BTW_smart($dbc, $senderData, $recipientData, $data, $recipientAcc, 'SMART', 'BTW', $transaction_uid, $data['fee'],$systemNumber)) {
                                                                $fee = moneyFormat($data['fee']);
                                                                $status = true;
                                                                echo "({$balance['accountName']}/{$fee}*{$transaction_uid}!{$status})";
                                                            } else {
                                                                echo 'ERROR';
                                                            };
                                                        } else {
                                                            echo "INVALID_SENDER_ACCOUNT";
                                                        }
                                                    } else {
                                                        echo "LOW_BALANCE";
                                                    }
                                                } else {
                                                    echo "INVALID_SENDER_ACCOUNT";
                                                }
                                            } else {
                                                echo "NOT_LINKED";
                                            }
                                        }else{
                                            echo("Sorry, Error authenticating user!");
                                        }
                                    } else {
                                        echo("Sorry, Error authenticating user!");
                                    }
                                }


                            }



                            break;


                        CASE 932:

                            $phoneNum = html_filter($dbc, $_REQUEST['phoneNum']);
                            if($source == '1'){
                                $phoneNum = "+".html_filter($dbc, $_REQUEST['phoneNum']);
                            }
                            $profile = get_customer_by_phoneNum($dbc,$phoneNum);

                            if($profile['countryCode']=="gh"){

                                /*
                                 * GN Bank Account to GN Bank Account
                                 */
                                if (isset($_REQUEST['status'])) {
                                    $status = html_filter($dbc, $_REQUEST['status']);
                                    if ($status == true) {
                                        $fee = html_filter($dbc, $_REQUEST['fee']);
                                        $transaction_uid = html_filter($dbc, $_REQUEST['transaction_uid']);
                                        $transactionData = get_bank_transaction_by_transaction_uid($dbc, $transaction_uid);
                                        $senderData = get_customer_by_id($dbc, $transactionData['senderID']);

                                        $senderValidate = bank_get_account_info($transactionData['senderBankAccount']);
                                        $recipientValidate = bank_get_account_info($transactionData['recipientBankAccount']);
                                        $comment = "GNM BTB ({$senderValidate['accountName']}) TO ({$recipientValidate['accountName']})";
                                        $transferData = bank_transfer_funds($transactionData['senderBankAccount'], $transactionData['recipientBankAccount'], $transactionData['sendersAmount'], "0", $comment, $transactionData['foreignId']);
                                        if ($transferData['comment'] == "Successful") {

                                            $bankCommissionAccount = '132000000043';
                                            $feeForeignId = genBankTransactionID_main();
                                            if($transactionData['fee'] > 0){ // check fee
                                                $feeTransfer = bank_transfer_funds($transactionData['senderBankAccount'], $bankCommissionAccount, $transactionData['fee'], "0", "GN MONEY FEE - BANK TO BANK - {$senderData['phoneNum']}", $feeForeignId);
                                                if ($feeTransfer['comment'] == "Successful") {
                                                    if (add_bank_commission($dbc, $transactionData['senderID'], $transactionData['senderBankAccount'], $transactionData['recipientBankAccount'], $bankCommissionAccount, $transactionData['fee'], $transactionData['transaction_uid'], $feeTransfer['foreignId'], 'COMPLETED')) {
                                                        if (update_bank_transaction_after_success($dbc, $transactionData['transaction_uid'], $transferData['foreignId'])) {

                                                            $currencyData = get_currency_by_countryCode($dbc,$senderData['countryCode']);

                                                            $message = "{$currencyData['countryCode']} {$transactionData['recipientAmount']} was transferred to {$transactionData['recipientBankAccount']}, transaction ID: {$transactionData['transaction_uid']}";
                                                            sendSMS($senderData['phoneNum'], $message);
                                                            echo "SUCCESSFUL";
                                                        }else {
                                                            echo "UNSUCCESSFUL12";
                                                        }
                                                    }else {
                                                        echo "UNSUCCESSFUL13";
                                                    }

                                                }else {
                                                    echo "UNSUCCESSFUL14";
                                                }
                                            } else{
                                                if (update_bank_transaction_after_success($dbc, $transactionData['transaction_uid'], $transferData['foreignId'])) {
                                                    $code = getLiberianCode($dbc,$senderData['phoneNum']);
                                                    $cCode = $code['countryCode'];
                                                    $curr1 = getLiberianCurrency($dbc,$cCode);
                                                    $fcurr = $curr1['currency'];
                                                    $message = "{$fcurr} {$transactionData['recipientAmount']} was transferred to {$transactionData['recipientBankAccount']}, transaction ID: {$transactionData['transaction_uid']}";
                                                    sendSMS($senderData['phoneNum'], $message);
                                                    echo "SUCCESSFUL";
                                                }else {
                                                    echo "UNSUCCESSFUL12";
                                                }
                                            }

                                        } else {
                                            print_r($transferData);
                                        }
                                    } else {
                                        echo "UNSUCCESSFUL2";
                                    }
                                } else {
                                    $phoneNum = html_filter($dbc, $_REQUEST['phoneNum']);
                                    if($source == '1'){
                                        $phoneNum = "+".html_filter($dbc, $_REQUEST['phoneNum']);
                                    }
                                    $keyCode = html_filter($dbc, $_REQUEST['keyCode']);
                                    $amount = html_filter($dbc, $_REQUEST['amount']);
                                    $senderAcc = html_filter($dbc, $_REQUEST['senderAccount']);
                                    $recipientAcc = html_filter($dbc, $_REQUEST['recipientAccount']);
                                    $senderData = get_customer_by_phoneNum($dbc, $phoneNum);
                                    if (check_phoneNum_and_keyCode($dbc, $phoneNum, $keyCode)) {

                                        $system_status = get_system_status_by_serviceType($dbc, "BANK");
                                        if ($system_status['status'] == 'ON') {
                                            $linked = bank_get_linked_accounts_new($phoneNum);
                                            if ($linked['status'] != 'NOT_LINKED') {
                                                $linkedArray = array($linked['account1'], $linked['account2'], $linked['account3']);
                                                if (in_array($senderAcc, $linkedArray)) {

                                                    $recipientData = array();
                                                    $recipientData['uGroup'] = 2000;
                                                    $senderData['uGroup'] = 2000;
                                                    $recipientData['accountType'] = 2000;
                                                    $recipientData['countryCode'] = $senderData['countryCode'];
                                                    $senderData['accountType'] = 2000;
                                                    $feeData = check_for_enough_balance_on_BTB($dbc, $senderData, $recipientData, $amount, 'BTB');
                                                    $balance = bank_get_account_info($senderAcc);
                                                    if ($balance['availableBal'] >= $feeData['newAmount']) {
                                                        $validate = bank_get_account_info($recipientAcc);
                                                        if ($balance['accountName'] != '') {
                                                            $transaction_uid = genTransactionID();
                                                            if (add_bank_transaction_BTB_smart($dbc, $senderData, $amount, $senderAcc, $recipientAcc, 'SMART', 'BTB', $transaction_uid, $feeData['fee'])) {
                                                                $fee = moneyFormat2($feeData['fee']);
                                                                $status = true;
                                                                echo "({$validate['accountName']}/{$fee}*{$transaction_uid}!{$status})";
                                                            }
                                                        } else {
                                                            echo "INVALID_RECIPIENT_ACCOUNT";
                                                        }

                                                    } else {
                                                        echo "LOW_BALANCE";
                                                    }
                                                } else {
                                                    echo "INVALID_SENDER_ACCOUNT";
                                                }
                                            } else {
                                                echo "NOT_LINKED";
                                            }
                                        }else{
                                            echo "Sorry, Error authenticating user!";
                                        }
                                    } else {
                                        echo("Sorry, Error authenticating user!");
                                    }
                                }


                            }else{

                                /*
                                * Liberia GN Bank Account to GN Bank Account
                                */
                                if (isset($_REQUEST['status'])) {
                                    $status = html_filter($dbc, $_REQUEST['status']);
                                    if ($status == true) {
                                        $fee = html_filter($dbc, $_REQUEST['fee']);
                                        $transaction_uid = html_filter($dbc, $_REQUEST['transaction_uid']);
                                        $transactionData = get_bank_transaction_by_transaction_uid($dbc, $transaction_uid);
                                        $senderData = get_customer_by_id($dbc, $transactionData['senderID']);

                                        $senderValidate = lr_bank_get_account_info($transactionData['senderBankAccount']);
                                        $recipientValidate = lr_bank_get_account_info($transactionData['recipientBankAccount']);
                                        if ($senderValidate['currency'] == $recipientValidate['currency']) {

                                            $comment = "GNM BTB ({$senderValidate['accountName']}) TO ({$recipientValidate['accountName']})";
                                            $transferData = lr_bank_transfer_funds_new($transactionData['senderBankAccount'], $transactionData['recipientBankAccount'], $transactionData['sendersAmount'], "0", $comment, $transactionData['foreignId']);
                                            if ($transferData['comment'] == "Successful") {

                                                if($senderValidate['currency']=="USD"){

                                                    $bankCommissionAccount = '432120000055';

                                                }elseif($senderValidate['currency']=="LRD"){

                                                    $bankCommissionAccount = '132120000054';

                                                }

                                                $feeForeignId = genBankTransactionID_main();
                                                if ($transactionData['fee'] > 0) { // check fee
                                                    $feeTransfer = lr_bank_transfer_funds_new($transactionData['senderBankAccount'], $bankCommissionAccount, $transactionData['fee'], "0", "GN MONEY FEE - BANK TO BANK - {$senderData['phoneNum']}", $feeForeignId);
                                                    if ($feeTransfer['comment'] == "Successful") {
                                                        if (add_bank_commission($dbc, $transactionData['senderID'], $transactionData['senderBankAccount'], $transactionData['recipientBankAccount'], $bankCommissionAccount, $transactionData['fee'], $transactionData['transaction_uid'], $feeTransfer['foreignId'], 'COMPLETED')) {
                                                            if (update_bank_transaction_after_success($dbc, $transactionData['transaction_uid'], $transferData['foreignId'])) {

                                                                $currencyData = get_currency_by_countryCode($dbc, $senderData['countryCode']);

                                                                $message = "{$currencyData['countryCode']} {$transactionData['recipientAmount']} was transferred to {$transactionData['recipientBankAccount']}, transaction ID: {$transactionData['transaction_uid']}";
                                                                sendSMS($senderData['phoneNum'], $message);
                                                                echo "SUCCESSFUL";
                                                            } else {
                                                                echo "UNSUCCESSFUL12";
                                                            }
                                                        } else {
                                                            echo "UNSUCCESSFUL13";
                                                        }

                                                    } else {
                                                        echo "UNSUCCESSFUL14";
                                                    }
                                                } else {
                                                    if (update_bank_transaction_after_success($dbc, $transactionData['transaction_uid'], $transferData['foreignId'])) {
                                                        $currencyData['countryCode'] = get_currency_by_countryCode($dbc, $senderData['countryCode']);
                                                        $message = "{$currencyData} {$transactionData['recipientAmount']} was transferred to {$transactionData['recipientBankAccount']}, transaction ID: {$transactionData['transaction_uid']}";
                                                        sendSMS($senderData['phoneNum'], $message);
                                                        echo "SUCCESSFUL";
                                                    } else {
                                                        echo "UNSUCCESSFUL12";
                                                    }
                                                }

                                            } else {
                                                echo "UNSUCCESSFUL";
                                            }
                                        }else{
                                            echo "UNSUCCESSFUL";
                                        }

                                    } else {
                                        echo "UNSUCCESSFUL2";
                                    }
                                } else {
                                    $phoneNum = html_filter($dbc, $_REQUEST['phoneNum']);
                                    if($source == '1'){
                                        $phoneNum = "+".html_filter($dbc, $_REQUEST['phoneNum']);
                                    }
                                    $keyCode = html_filter($dbc, $_REQUEST['keyCode']);
                                    $amount = html_filter($dbc, $_REQUEST['amount']);
                                    $senderAcc = html_filter($dbc, $_REQUEST['senderAccount']);
                                    $recipientAcc = html_filter($dbc, $_REQUEST['recipientAccount']);
                                    $senderData = get_customer_by_phoneNum($dbc, $phoneNum);
                                    if (check_phoneNum_and_keyCode($dbc, $phoneNum, $keyCode)) {

                                        $system_status = get_system_status_by_serviceType($dbc, "BANK");
                                        if ($system_status['status'] == 'ON') {
                                            $linked = lr_bank_get_linked_accounts_new($phoneNum);
                                            if ($linked['status'] != 'NOT_LINKED') {
                                                $linkedArray = array($linked['account1'], $linked['account2'], $linked['account3']);
                                                if (in_array($senderAcc, $linkedArray)) {

                                                    $recipientData = array();
                                                    $recipientData['uGroup'] = 2000;
                                                    $senderData['uGroup'] = 2000;
                                                    $recipientData['accountType'] = 2000;
                                                    $recipientData['countryCode'] = $senderData['countryCode'];
                                                    $senderData['accountType'] = 2000;
                                                    $feeData = check_for_enough_balance_on_BTB($dbc, $senderData, $recipientData, $amount, 'BTB');
                                                    $balance = lr_bank_get_account_info($senderAcc);
                                                    if ($balance['availableBal'] >= $feeData['newAmount']) {
                                                        $validate = lr_bank_get_account_info($recipientAcc);
                                                        if ($balance['accountName'] != '') {
                                                            $transaction_uid = genTransactionID();
                                                            if (add_bank_transaction_BTB_smart($dbc, $senderData, $amount, $senderAcc, $recipientAcc, 'SMART', 'BTB', $transaction_uid, $feeData['fee'])) {
                                                                $fee = moneyFormat2($feeData['fee']);
                                                                $status = true;
                                                                echo "({$validate['accountName']}/{$fee}*{$transaction_uid}!{$status})";
                                                            }
                                                        } else {
                                                            echo "INVALID_RECIPIENT_ACCOUNT";
                                                        }

                                                    } else {
                                                        echo "LOW_BALANCE";
                                                    }
                                                } else {
                                                    echo "INVALID_SENDER_ACCOUNT";
                                                }
                                            } else {
                                                echo "NOT_LINKED";
                                            }
                                        }else{
                                            echo "Sorry, Error authenticating user!";
                                        }
                                    } else {
                                        echo("Sorry, Error authenticating user!");
                                    }
                                }


                            }


                            break;

                        CASE 904:
                            /*
                             * GN Bank Account to payGlobal Wallet
                             */
                            $phoneNum = html_filter($dbc, $_REQUEST['phoneNum']);
                            if($source == '1'){
                                $phoneNum = "+".html_filter($dbc, $_REQUEST['phoneNum']);
                            }
                            $profile = get_customer_by_phoneNum($dbc,$phoneNum);

                            if($profile['countryCode']=="gh"){
                                //GN SAVING GHANA

                                if (isset($_REQUEST['status'])) {
                                    $status = html_filter($dbc, $_REQUEST['status']);
                                    if ($status == true) {
                                        $fee = html_filter($dbc, $_REQUEST['fee']);
                                        $transaction_uid = html_filter($dbc, $_REQUEST['transaction_uid']);
                                        $transactionData = get_bank_transaction_by_transaction_uid($dbc, $transaction_uid);
                                        $recipientData = get_customer_by_id($dbc, $transactionData['recipientID']);
                                        $senderData = get_customer_by_id($dbc, $transactionData['senderID']);
                                        $totalAmount = $transactionData['fee'] + $transactionData['recipientAmount'];
                                        $validate = bank_get_account_info($transactionData['recipientBankAccount']);
                                        $comment = "GNM BTW ({$validate['accountName']}) TO ({$recipientData['fullName']} - {$recipientData['phoneNum']})";
                                        if (update_bank_transaction_before_api_call($dbc, $transaction_uid, 0)) {
                                            $transferData = bank_transfer_funds($transactionData['recipientBankAccount'], $transactionData['senderBankAccount'], $totalAmount, "0", $comment, $transactionData['foreignId']);
                                            if ($transferData['comment'] == "Successful") {
                                                if (redraw_funds($dbc, $senderData['id'], $transactionData['sendersAmount'],$senderData['countryCode'])) {
                                                    if (add_funds($dbc, $recipientData['id'], $transactionData['recipientAmount'],$recipientData['countryCode'])) {
                                                        $all_transactionData = get_bank_transaction_by_transaction_uid($dbc, $transactionData['transaction_uid']);
                                                        //$all_transactionData['senderID'] = $transactionData['recipientID'];
                                                        $all_transactionData['senderCountryCode'] = $senderData['countryCode'];
                                                        $all_transactionData['transactionTypes'] = $transactionData['serviceType'];
                                                        if (add_payGlobal_commission($dbc, $all_transactionData, $all_transactionData['fee'])) {
                                                            if (update_bank_transaction_after_success($dbc, $transactionData['transaction_uid'], $transferData['foreignId'])) {
                                                                $recipientAcc = get_cus_account_info_by_customerID($dbc, $recipientData['id'],$recipientData['countryCode']);
                                                                $senderAcc = get_cus_account_info_by_customerID($dbc, $senderData['id'],$senderData['countryCode']);
                                                                if (update_bank_transaction_balance($dbc, $transactionData['transaction_uid'], $recipientAcc['balance'],$senderAcc['balance'])) {
                                                                    //$message = "{$transactionData['recipientAmount']} was transferred to your wallet, transaction ID: {$transactionData['transaction_uid']}";

                                                                    $code = getLiberianCode($dbc,$senderData['phoneNum']);
                                                                    $cCode = $code['countryCode'];
                                                                    $curr1 = getLiberianCurrency($dbc,$cCode);
                                                                    $fcurr = $curr1['currency'];
                                                                    $message = "An amount of {$fcurr} {$transactionData['recipientAmount']} was transferred to your wallet. Balance {$recipientAcc['balance']} Transaction ID {$transactionData['transaction_uid']}";
                                                                    sendSMS($recipientData['phoneNum'], $message);
                                                                    echo "SUCCESSFUL";
                                                                }
                                                            }
                                                        }

                                                    }
                                                }

                                            } else {
                                                echo "UNSUCCESSFUL";
                                            }

                                        }
                                    } else {
                                        echo "UNSUCCESSFUL2";
                                    }
                                } else {
                                    $phoneNum = html_filter($dbc, $_REQUEST['phoneNum']);
                                    if($source == '1'){
                                        $phoneNum = "+".html_filter($dbc, $_REQUEST['phoneNum']);
                                    }
                                    $keyCode = html_filter($dbc, $_REQUEST['keyCode']);
                                    $amount = html_filter($dbc, $_REQUEST['amount']);
                                    $recipientAcc = html_filter($dbc, $_REQUEST['senderAccount']);
                                    $recipientData = get_customer_by_phoneNum($dbc, $phoneNum);

                                    $systemNumber = get_system_number_by_name($dbc,'NEW_OVA','gh');
                                    $senderData = get_customer_by_phoneNum($dbc, $systemNumber['phoneNum']);

                                    /*$recipientData['uGroup'] = 2000;
                                    $senderData['uGroup'] = 2000;
                                    $recipientData['accountType'] = 2000;
                                    $senderData['accountType'] = 2000;*/

                                    if (check_phoneNum_and_keyCode($dbc, $phoneNum, $keyCode)) {
                                        $system_status = get_system_status_by_serviceType($dbc, "BANK");
                                        if ($system_status['status'] == 'ON') {
                                            $linked = bank_get_linked_accounts_new($phoneNum);
                                            if ($linked['status'] != 'NOT_LINKED') {
                                                $linkedArray = array($linked['account1'], $linked['account2'], $linked['account3']);
                                                if (in_array($recipientAcc, $linkedArray)) {
                                                    $balance = bank_get_account_info($recipientAcc);
                                                    $data = check_for_enough_balance_on_BTW($dbc, $senderData, $recipientData, $amount, 'BTW');
                                                    //sendSMS("+233246022602",$amount);
                                                    if ($balance['availableBal'] >= $data['newAmount']) {
                                                        //$validate = bank_validate_accountNumber($recipientAcc);
                                                        if ($balance['accountName'] != '') {
                                                            $transaction_uid = genTransactionID();
                                                            /*
                                                             * Check if customer is Agent or Super Agent. If any Charge no fee.
                                                             *
                                                            $cusData = get_customer_by_id($dbc,$recipientData['id']);
                                                            if ($cusData['accountType'] == 2 OR $cusData['accountType'] == 4) {
                                                                $data['fee'] = 0.00;
                                                                $data['newAmount'] = $data['exactAmount'];
                                                            }
                                                            *
                                                             * End of No fee for super agents and agents
                                                             */

                                                            if (add_bank_transaction_BTW_smart($dbc, $senderData, $recipientData, $data, $recipientAcc, 'SMART', 'BTW', $transaction_uid, $data['fee'],$systemNumber)) {
                                                                $fee = moneyFormat($data['fee']);
                                                                $status = true;
                                                                echo "({$balance['accountName']}/{$fee}*{$transaction_uid}!{$status})";
                                                            } else {
                                                                echo 'ERROR';
                                                            };
                                                        } else {
                                                            echo "INVALID_SENDER_ACCOUNT";
                                                        }
                                                    } else {
                                                        echo "LOW_BALANCE";
                                                    }
                                                } else {
                                                    echo "INVALID_SENDER_ACCOUNT";
                                                }
                                            } else {
                                                echo "NOT_LINKED";
                                            }
                                        }else{
                                            echo("Sorry, Error authenticating user!");
                                        }
                                    } else {
                                        echo("Sorry, Error authenticating user!");
                                    }
                                }

                            }else{
                                //GN BANK LIBERIA
                                /*
                                 * Liberia GN Bank Account to payGlobal Wallet
                                 */
                                if (isset($_REQUEST['status'])) {
                                    $status = html_filter($dbc, $_REQUEST['status']);
                                    if ($status == true) {
                                        $fee = html_filter($dbc, $_REQUEST['fee']);
                                        $transaction_uid = html_filter($dbc, $_REQUEST['transaction_uid']);
                                        $transactionData = get_bank_transaction_by_transaction_uid($dbc, $transaction_uid);
                                        $recipientData = get_customer_by_id($dbc, $transactionData['recipientID']);
                                        $senderData = get_customer_by_id($dbc, $transactionData['senderID']);
                                        $totalAmount = $transactionData['fee'] + $transactionData['recipientAmount'];
                                        $validate = lr_bank_get_account_info($transactionData['recipientBankAccount']);
                                        $comment = "GNM BTW ({$validate['accountName']}) TO ({$recipientData['fullName']} - {$recipientData['phoneNum']})";
                                        if (update_bank_transaction_before_api_call($dbc, $transaction_uid, 0)) {
                                            $transferData = lr_bank_transfer_funds_new($transactionData['recipientBankAccount'], $transactionData['senderBankAccount'], $totalAmount, "0", $comment, $transactionData['foreignId']);
                                            if ($transferData['comment'] == "Successful") {
                                                if (redraw_funds($dbc, $senderData['id'], $transactionData['sendersAmount'],$senderData['countryCode'])) {
                                                    if (add_funds($dbc, $recipientData['id'], $transactionData['recipientAmount'],$recipientData['countryCode'])) {
                                                        $all_transactionData = get_bank_transaction_by_transaction_uid($dbc, $transactionData['transaction_uid']);
                                                        //$all_transactionData['senderID'] = $transactionData['recipientID'];
                                                        $all_transactionData['senderCountryCode'] = $senderData['countryCode'];
                                                        $all_transactionData['transactionTypes'] = $transactionData['serviceType'];
                                                        if (add_payGlobal_commission($dbc, $all_transactionData, $all_transactionData['fee'])) {
                                                            if (update_bank_transaction_after_success($dbc, $transactionData['transaction_uid'], $transferData['foreignId'])) {
                                                                $recipientAcc = get_cus_account_info_by_customerID($dbc, $recipientData['id'],$recipientData['countryCode']);
                                                                $senderAcc = get_cus_account_info_by_customerID($dbc, $senderData['id'],$senderData['countryCode']);
                                                                if (update_bank_transaction_balance($dbc, $transactionData['transaction_uid'], $recipientAcc['balance'],$senderAcc['balance'])) {
                                                                    //$message = "{$transactionData['recipientAmount']} was transferred to your wallet, transaction ID: {$transactionData['transaction_uid']}";

                                                                    $currency = get_currency_by_countryCode($dbc,$senderData['countryCode']);
                                                                    $message = "An amount of {$currency['currency']} {$transactionData['recipientAmount']} was transferred to your wallet. Balance {$recipientAcc['balance']} Transaction ID {$transactionData['transaction_uid']}";
                                                                    sendSMS($recipientData['phoneNum'], $message);
                                                                    echo "SUCCESSFUL";
                                                                }
                                                            }
                                                        }

                                                    }
                                                }

                                            } else {
                                                echo "UNSUCCESSFUL";
                                            }

                                        }
                                    } else {
                                        echo "UNSUCCESSFUL2";
                                    }
                                } else {
                                    $phoneNum = html_filter($dbc, $_REQUEST['phoneNum']);
                                    if($source == '1'){
                                        $phoneNum = "+".html_filter($dbc, $_REQUEST['phoneNum']);
                                    }
                                    $keyCode = html_filter($dbc, $_REQUEST['keyCode']);
                                    $amount = html_filter($dbc, $_REQUEST['amount']);
                                    $recipientAcc = html_filter($dbc, $_REQUEST['senderAccount']);
                                    $recipientData = get_customer_by_phoneNum($dbc, $phoneNum);

                                    $systemNumber = get_system_number_by_name($dbc,'NEW_OVA_LR',$recipientData['countryCode']);
                                    $senderData = get_customer_by_phoneNum($dbc, $systemNumber['phoneNum']);

                                    /*$recipientData['uGroup'] = 2000;
                                    $senderData['uGroup'] = 2000;
                                    $recipientData['accountType'] = 2000;
                                    $senderData['accountType'] = 2000;*/

                                    if (check_phoneNum_and_keyCode($dbc, $phoneNum, $keyCode)) {
                                        $system_status = get_system_status_by_serviceType($dbc, "BANK");
                                        if ($system_status['status'] == 'ON') {
                                            $linked = lr_bank_get_linked_accounts_new($phoneNum);
                                            if ($linked['status'] != 'NOT_LINKED') {
                                                $linkedArray = array($linked['account1'], $linked['account2'], $linked['account3']);
                                                if (in_array($recipientAcc, $linkedArray)) {
                                                    $balance = lr_bank_get_account_info($recipientAcc);
                                                    $data = check_for_enough_balance_on_BTW($dbc, $senderData, $recipientData, $amount, 'BTW');
                                                    //sendSMS("+233246022602",$amount);
                                                    if ($balance['availableBal'] >= $data['newAmount']) {
                                                        //$validate = bank_validate_accountNumber($recipientAcc);
                                                        if ($balance['accountName'] != '') {
                                                            $transaction_uid = genTransactionID();
                                                            /*
                                                             * Check if customer is Agent or Super Agent. If any Charge no fee.
                                                             *
                                                            $cusData = get_customer_by_id($dbc,$recipientData['id']);
                                                            if ($cusData['accountType'] == 2 OR $cusData['accountType'] == 4) {
                                                                $data['fee'] = 0.00;
                                                                $data['newAmount'] = $data['exactAmount'];
                                                            }
                                                            *
                                                             * End of No fee for super agents and agents
                                                             */

                                                            if (add_bank_transaction_BTW_smart($dbc, $senderData, $recipientData, $data, $recipientAcc, 'SMART', 'BTW', $transaction_uid, $data['fee'],$systemNumber)) {
                                                                $fee = moneyFormat($data['fee']);
                                                                $status = true;
                                                                echo "({$balance['accountName']}/{$fee}*{$transaction_uid}!{$status})";
                                                            } else {
                                                                echo 'ERROR';
                                                            };
                                                        } else {
                                                            echo "INVALID_SENDER_ACCOUNT";
                                                        }
                                                    } else {
                                                        echo "LOW_BALANCE";
                                                    }
                                                } else {
                                                    echo "INVALID_SENDER_ACCOUNT";
                                                }
                                            } else {
                                                echo "NOT_LINKED";
                                            }
                                        }else{
                                            echo("Sorry, Error authenticating user!");
                                        }
                                    } else {
                                        echo("Sorry, Error authenticating user!");
                                    }
                                }


                            }



                            break;
                        CASE 925:
                            // Activate USSD Code
                            $phoneNum = html_filter($dbc, $_REQUEST['rcontact1']);
                            if($source == '1'){
                                $phoneNum = "+".html_filter($dbc, $_REQUEST['rcontact1']);
                            }
                            $password = passwordHash($_REQUEST['password'], $mySalt);
                            $ussdCode = passwordHash($_REQUEST['ussdCode'], $mySalt);
                            if (check_phoneNum_and_password($dbc, $phoneNum, $password)) {
                                if (update_ussdCode($dbc, $phoneNum, $ussdCode)) {
                                    echo "USSD_ACTIVATED";
                                } else {
                                    echo "TRY_AGAIN";
                                }
                            } else {
                                echo "WRONG_PASSWORD";
                            }
                            break;
                        CASE 926:
                            // Check Balance
                            $phoneNum = html_filter($dbc, $_REQUEST['phoneNum']);
                            if($source == '1'){
                                $phoneNum = "+".html_filter($dbc, $_REQUEST['phoneNum']);
                            }
                            $customerData = get_customer_by_phoneNum($dbc, $phoneNum);
                            if (check_if_number_exist($dbc, $phoneNum)) {

                                $acc_info = get_cus_account_info_by_customerID($dbc, $customerData['id'],$customerData['countryCode']);
                                echo $acc_info['balance'];
                            } else {
                                echo "PHONE_DONT_EXIST";
                            }
                            break;
                        CASE 927:
                            // Generate Unregistered user token
                            $sendPhoneNum = html_filter($dbc, $_REQUEST['senderPhone']);
                            if($source == '1'){
                                $sendPhoneNum = "+".html_filter($dbc, $_REQUEST['senderPhone']);
                            }
                            $amount = html_filter($dbc, $_REQUEST['amount']);
                            $keyCode = html_filter($dbc, $_REQUEST['keyCode']);
                            $tokenPin = html_filter($dbc, $_REQUEST['pin']);
                            $recipientCountry = html_filter($dbc, $_REQUEST['recipientCountry']);
                            $intended_Recipient_name = html_filter($dbc, $_REQUEST['recipient_name']);
                            $intended_Recipient_address = html_filter($dbc, $_REQUEST['recipient_address']);
                            $intended_Recipient_phonenumber = html_filter($dbc, $_REQUEST['recipient_phonenumber']);

                            $response = array();

                            if (check_phoneNum_and_keyCode($dbc, $sendPhoneNum, $keyCode)) {
                                $senderData = get_customer_by_phoneNum($dbc, $sendPhoneNum);
                                $recipientData = array();
                                $general_service = "TOKEN";
                                $recipientData['uGroup'] = 1; // public group
                                $recipientData['accountType'] = 2; // Agent or Super Agent
                                $recipientData['countryCode'] = $recipientCountry;


                                if (check_if_number_exist_as_superAgent_or_agent_or_individual($dbc, $sendPhoneNum)) {
                                    $check_balance = check_for_enough_balance_token($dbc, $senderData, $recipientData, $amount, $general_service);
                                    if ($check_balance['status'] == true) {
                                        $recipientData['recipientCountryCode'] = $recipientCountry;
                                        $recipientData['recipientAmount'] = "";
                                        $token = generate_token();
                                        $hashToken = passwordHash($token,$mySalt);
                                        $hashpin = passwordHash($tokenPin,$mySalt);
                                        $funds = $check_balance['newAmount'];
                                        if (redraw_funds($dbc, $senderData['id'], $funds,$senderData['countryCode'])) {
                                            if (add_to_token_mgt_after_processing($dbc, $senderData['id'], $senderData['countryCode'], $token, $tokenPin, $check_balance['transaction_uid'], $recipient_name, $recipient_phonenumber, $recipient_address, $recipientCountry)) {

                                                $rate = get_conversion_rate($dbc,$senderData['countryCode'],$recipientCountry);
                                                $convertedAmount = $amount * $rate;
                                                $senderAcc = get_cus_account_info_by_customerID($dbc,$senderData['id'],$senderData['countryCode']);

                                                if (add_main_transaction($dbc, $recipientData, $senderData, $general_service, "", $check_balance['transaction_uid'], "WAITING", $check_balance['fee'], $amount, $convertedAmount, "SMART", $check_balance['pricing_id'])) {
                                                    $senderCountryData = get_currency_by_countryCode($dbc,$senderData['countryCode']);
                                                    $recipientCountryData = get_currency_by_countryCode($dbc,$recipientCountry);

                                                    if(updateTokenVeriableTable($dbc,$check_balance['transaction_uid'],$hashToken,$hashpin,$senderData['phoneNum'],$senderData['fullName'],$amount,$senderCountryData['currency'],$convertedAmount,$recipientCountryData['currency'],$intended_Recipient_name,$intended_Recipient_phonenumber,$intended_Recipient_address)) {

                                                        if (transcription($dbc, $senderData, $recipientData, $general_service, $amount, '', $check_balance['transaction_uid'])) {
                                                            if (update_main_transaction_after_processing($dbc, $check_balance['transaction_uid'], 'WAITING', $senderAcc['balance'], 0)) {
                                                                $agentFee = $check_balance['fee'] * $check_balance['senderCommission'];
                                                                $payGlobalFee = $check_balance['fee'] * $check_balance['sysCommission'];
                                                                $data = array();
                                                                $data['recipientID'] = $senderData['id'];
                                                                $data['senderID'] = $senderData['id'];
                                                                $data['senderCountryCode'] = $senderData['countryCode'];
                                                                $data['transactionTypes'] = $general_service;
                                                                $data['transaction_uid'] = $check_balance['transaction_uid'];

                                                                $amount = moneyFormat2($amount);
                                                                if ($senderData['accountType'] == 1) {
                                                                    if (add_payGlobal_commission($dbc, $data, $payGlobalFee)) {

                                                                        // $message = "Transaction Completed, Token is {$token} and Code is {$tokenPin} for an amount of {$amount}.";


                                                                        //echo "(SUCCESSFUL/{$token})";
                                                                        $code2 = $recipientData['recipientCountryCode'];
                                                                        $cCode2 = $code2;
                                                                        $curr2 = getLiberianCurrency($dbc, $cCode2);
                                                                        $fcurr2 = $curr2['currency'];


                                                                        $response['statusCode'] = 200;
                                                                        $response['fee'] = moneyFormat2($check_balance['fee']);
                                                                        $response['token'] = $token;
                                                                        $response['senderAmount'] = moneyFormat2($amount);
                                                                        $response['recipientAmount'] = moneyFormat2($convertedAmount);
                                                                        $response['recipientCurrency'] = $fcurr2;

                                                                        $message = "Token: {$token} with an amount of {$fcurr2} {$amount} was generated, secret code is: {$tokenPin}  Balance: {$fcurr} {$senderAcc['balance']} Transaction ID: {$check_balance['transaction_uid']}";
                                                                        sendSMS($sendPhoneNum, $message);

                                                                    }
                                                                } else {
                                                                    if (add_agent_commission($dbc, $data, $agentFee, $senderData['countryCode'])) {
                                                                        if (add_payGlobal_commission($dbc, $data, $payGlobalFee)) {
                                                                            //$message = "Transaction Completed, Token is {$token} and Code is {$tokenPin} for an amount of {$amount}.";
                                                                            //echo "(SUCCESSFUL/{$token})";
                                                                            $code2 = $recipientData['recipientCountryCode'];
                                                                            $cCode2 = $code2;
                                                                            $curr2 = getLiberianCurrency($dbc, $cCode2);
                                                                            $fcurr2 = $curr2['currency'];

                                                                            $response = array();
                                                                            $response['statusCode'] = 200;
                                                                            $response['fee'] = moneyFormat2($check_balance['fee']);
                                                                            $response['token'] = $token;
                                                                            $response['senderAmount'] = moneyFormat2($amount);
                                                                            $response['recipientAmount'] = moneyFormat2($convertedAmount);
                                                                            $response['recipientCurrency'] = $fcurr2;


                                                                            $message = "Token: {$token} with an amount of {$fcurr2} {$amount} was generated, secret code is: {$tokenPin}  Balance: {$senderAcc['balance']} Transaction ID: {$check_balance['transaction_uid']}";
                                                                            sendSMS($sendPhoneNum, $message);
                                                                            //echo "(SUCCESSFUL/{$token})";

                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }

                                                    }
                                                } else {
                                                    //echo "UNSUCCESSFUL";
                                                    $response['statusCode'] = 500;
                                                    $response['message'] = 'Sorry, Error performing transaction!';
                                                }
                                            } else {
                                                //echo "UNSUCCESSFUL1";
                                                $response['statusCode'] = 500;
                                                $response['message'] = 'Sorry, Error performing transaction!';
                                            }
                                        } else {

                                            //echo "UNSUCCESSFUL2";
                                            $response['statusCode'] = 500;
                                            $response['message'] = 'Sorry, Error performing transaction!';
                                        }
                                    } else {
                                        //echo "0"; //Insufficient Account Balance Returns 0 string
                                        $response['statusCode'] = 500;
                                        $response['message'] = 'Insufficient Account Balance';
                                    }
                                } else {
                                    //echo("UNAUTHORISED_USER");
                                    $response['statusCode'] = 500;
                                    $response['message'] = 'Sorry, Unauthorized user!';
                                }
                            } else {
                                //echo("Sorry, Error authenticating user!");
                                $response['statusCode'] = 500;
                                $response['message'] = 'Sorry, Error authenticating user!';
                            }
                            echo json_encode($response);

                            break;
                        CASE 928:

                            $from = html_filter($dbc, $_REQUEST['from']);
                            $to = html_filter($dbc, $_REQUEST['to']);
                            $phoneNum = html_filter($dbc, $_REQUEST['phoneNum']);
                            if($source == '1'){
                                $phoneNum = "+".html_filter($dbc, $_REQUEST['phoneNum']);
                            }
                            if (check_if_number_exist($dbc, $phoneNum)) {
                                $customerData = get_customer_by_phoneNum($dbc, $phoneNum);
                                $data = get_mainTransaction_by_dateRange($dbc, $customerData['id'], $from, $to);
                                $res = array();
                                array_push($res['result'], $data);
                                echo json_encode($res);

                            } else {
                                echo "PHONE_DONT_EXIST";
                            }
                            break;

                        CASE 929:
                            $from = html_filter($dbc, $_REQUEST['from']);
                            $to = html_filter($dbc, $_REQUEST['to']);
                            $phoneNum = html_filter($dbc, $_REQUEST['phoneNum']);
                            if($source == '1'){
                                $phoneNum = "+".html_filter($dbc, $_REQUEST['phoneNum']);
                            }
                            if (check_if_number_exist($dbc, $phoneNum)) {
                                //$from = '2017-08-02';
                                // $to = '2017-10-02';
                                $customerData = get_customer_by_phoneNum($dbc, $phoneNum);
                                $datas = get_mainTransaction_by_dateRange_for_recipient($dbc, $customerData['id'], $from, $to);

                                $output = array();
                                foreach ($datas as $data) {
                                    $recipient = get_customer_by_id($dbc, $data['senderID']);
                                    $output[] = array('rcontact' => $recipient['phoneNum'], 'amount' => $data['recipientAmount'], 'date' => $data['dateTime']);
                                }

                                echo json_encode($output);

                            } else {
                                echo "PHONE_DONT_EXIST";
                            }
                            break;

                        CASE 930 :
                            // Change password
                            $phoneNum = html_filter($dbc, $_REQUEST['phoneNum']);
                            if($source == '1'){
                                $phoneNum = "+".html_filter($dbc, $_REQUEST['phoneNum']);
                            }
                            $password = passwordHash($_REQUEST['password'], $mySalt);
                            $keyCode = html_filter($dbc, $_REQUEST['keyCode']);

                            if (check_phoneNum_and_keyCode($dbc, $phoneNum, $keyCode)) {
                                $customerData = get_customer_by_phoneNum($dbc, $phoneNum);
                                if (change_customer_password($dbc, $customerData['id'], $password)) {
                                    echo "SUCCESSFUL";
                                }
                            } else {
                                echo("UNSUCCESSFUL");
                            }
                            break;

                        CASE 931 :
                            // Change USSD PIN

                            $phoneNum = html_filter($dbc, $_REQUEST['phoneNum']);
                            if($source == '1'){
                                $phoneNum = "+".html_filter($dbc, $_REQUEST['phoneNum']);
                            }
                            $pin = html_filter($dbc, $_REQUEST['pin']);
                            $keyCode = html_filter($dbc, $_REQUEST['keyCode']);

                            if (check_phoneNum_and_keyCode($dbc, $phoneNum, $keyCode)) {
                                $customerData = get_customer_by_phoneNum($dbc, $phoneNum);
                                if (change_customer_ussd_pin($dbc, $customerData['id'], $pin)) {
                                    echo "SUCCESSFUL";
                                }
                            } else {
                                echo("UNSUCCESSFUL");
                            }
                            break;
                        CASE 934:
                            // Update Profile
                            $phoneNum = html_filter($dbc, $_REQUEST['phoneNum']);
                            if($source == '1'){
                                $phoneNum = "+".html_filter($dbc, $_REQUEST['phoneNum']);
                            }
                            $fullName = html_filter($dbc, $_REQUEST['fullName']);
                            $idNumber = html_filter($dbc, $_REQUEST['idNumber']);
                            $idType = html_filter($dbc, $_REQUEST['idType']);
                            $idExpiringDate = html_filter($dbc, $_REQUEST['idExpiringDate']);
                            $email = html_filter($dbc, $_REQUEST['email']);
                            $address = html_filter($dbc, $_REQUEST['address']);
                            $city = html_filter($dbc, $_REQUEST['city']);
                            $region = html_filter($dbc, $_REQUEST['region']);
                            $postCode = html_filter($dbc, $_REQUEST['postcode']);
                            $nextOfKin = html_filter($dbc, $_REQUEST['nextOfKin']);
                            $nextOfKin_phone = html_filter($dbc, $_REQUEST['nextOfKin_phone']);
                            $socialSec = html_filter($dbc, $_REQUEST['socialSecurity']);
                            $keyCode = html_filter($dbc, $_REQUEST['keyCode']);
                            if (check_phoneNum_and_keyCode($dbc, $phoneNum, $keyCode)) {
                                if (update_customer_smart($dbc, $phoneNum, $fullName, $idType, $idNumber, $address, $email,$idExpiringDate,$city,$region,$postCode,$nextOfKin,$nextOfKin_phone,$socialSec)) {
                                    echo "SUCCESSFUL";
                                } else {
                                    echo "UNSUCCESSFUL";
                                }
                            } else {
                                echo("UNSUCCESSFUL");
                            }

                            break;
                        CASE 935:

                            // View Bank Account Balance
                            $phoneNum = html_filter($dbc, $_REQUEST['phoneNum']);
                            if($source == '1'){
                                $phoneNum = "+".html_filter($dbc, $_REQUEST['phoneNum']);
                            }
                            $keyCode = html_filter($dbc, $_REQUEST['keyCode']);
                            $accountNum = html_filter($dbc, $_REQUEST['accountNumber']);
                            $customerData = get_customer_by_phoneNum($dbc, $phoneNum);
                            if($customerData['countryCode'] == 'gh') {
                                if (check_phoneNum_and_keyCode($dbc, $phoneNum, $keyCode)) {
                                    $linked = bank_get_linked_accounts_new($phoneNum);
                                    if ($linked['status'] != 'NOT_LINKED') {
                                        $linkedArray = array($linked['account1'], $linked['account2'], $linked['account3']);
                                        if (in_array($accountNum, $linkedArray)) {
                                            $balance = bank_get_account_info($accountNum);

                                            if ($balance['statusCode'] == 'N') {
                                                echo "GHC".' '.moneyFormat2($balance['availableBal']);
                                            } else {
                                                echo "ERROR_OCCUR";
                                            }
                                        } else {
                                            echo "INVALID_ACCOUNT_NUMBER";
                                        }
                                    } else {
                                        echo "NOT_LINKED";
                                    }
                                } else {
                                    echo("Sorry, Error authenticating user!");
                                }
                            }else{
                                if (check_phoneNum_and_keyCode($dbc, $phoneNum, $keyCode)) {
                                    $linked = lr_bank_get_linked_accounts_new($phoneNum);
                                    if ($linked['status'] != 'NOT_LINKED') {
                                        $linkedArray = array($linked['account1'], $linked['account2'], $linked['account3']);
                                        if (in_array($accountNum, $linkedArray)) {
                                            $balance = lr_bank_get_account_info($accountNum);
                                            if ($balance['statusCode'] == 'N') {
                                                echo $balance['currency'].' '.moneyFormat2($balance['availableBal']);
                                            } else {
                                                echo "ERROR_OCCUR";
                                            }
                                        } else {
                                            echo "INVALID_ACCOUNT_NUMBER";
                                        }
                                    } else {
                                        echo "NOT_LINKED";
                                    }
                                } else {
                                    echo("Sorry, Error authenticating user!");
                                }
                            }

                            break;
                        /*
                         * New set of transactions starting from code 400
                         */
                        CASE 401: // Money  transfer
                            //Send money to another pay global Account both local and International
                            if (isset($_REQUEST['status'])) {
                                $response = array();
                                $status = html_filter($dbc, $_REQUEST['status']);
                                if ($status == true) {
                                    $fee = html_filter($dbc, $_REQUEST['fee']);
                                    $transaction_uid = html_filter($dbc, $_REQUEST['transaction_uid']);

                                    $transactionData = get_main_transaction_by_transaction_uid($dbc, $transaction_uid);
                                    $recipientData = get_customer_by_id($dbc, $transactionData['recipientID']);
                                    $senderData = get_customer_by_id($dbc, $transactionData['senderID']);
                                    $totalAmount = $transactionData['sendersAmount'] + $transactionData['fee'];
                                    if (check_existing_balance($dbc, $transactionData['senderID'], $totalAmount,$senderData['countryCode'])) {


                                        if ($transactionData['senderCountryCode'] == $transactionData['recipientCountryCode']) {
                                            //Same country Transactions

                                            if (translation($dbc, $transactionData)) {
                                                $senderAcc = get_cus_account_info_by_customerID($dbc, $senderData['id'],$senderData['countryCode']);
                                                $recipientAcc = get_cus_account_info_by_customerID($dbc, $recipientData['id'],$recipientData['countryCode']);
                                                if (update_main_transaction_after_processing($dbc, $transactionData['transaction_uid'], "COMPLETED", $senderAcc['balance'], $recipientAcc['balance'])) {
                                                    $pricing = get_pricing_by_id($dbc, $transactionData['pricing_id']);
                                                    /*
                                                     * This set of code apply when an agent of super agent is loading an individual Wallet.
                                                     * Thus the agent or the super agent received 0.004% of the amount
                                                     */
                                                    if (($senderData['accountType'] == 2 OR $senderData['accountType'] == 4) AND $recipientData['accountType'] == 1) {

                                                        $senderCom = $fee * $pricing['senderCommission'];
                                                        if (add_agent_commission_for_addMoney($dbc, $transactionData, $senderCom, $senderData['countryCode'], $senderData['id'])) {
                                                            // Redraw funds
                                                            add_commissionExpense($dbc, $senderData['countryCode'], $senderCom);

                                                        }
                                                    }

                                                    if ($senderData['accountType'] == 4 AND $recipientData['accountType'] == 2) {
                                                        $senderCom = $fee * $pricing['senderCommission'];
                                                        if (add_agent_commission_for_addMoney($dbc, $transactionData, $senderCom, $senderData['countryCode'], $senderData['id'])) {
                                                            // Redraw funds
                                                            add_commissionExpense($dbc, $senderData['countryCode'], $senderCom);
                                                        }
                                                    }
                                                    /*
                                                     *end of loading money commission
                                                     */

                                                    if ($recipientData['accountType'] == 2 OR $recipientData['accountType'] == 4) {// check if recipient is  Agent, then share commission

                                                        $agentFee = $transactionData['fee'] * $pricing['recipientCommission'];
                                                        $payGlobalFee = $transactionData['fee'] * $pricing['sysCommission'];


                                                        if (add_agent_commission($dbc, $transactionData, $agentFee, $recipientData['countryCode'])) {
                                                            if (add_payGlobal_commission($dbc, $transactionData, $payGlobalFee)) {

                                                                $reciAcc = get_cus_account_info_by_customerID($dbc,$recipientData['id'],$recipientData['countryCode']);

                                                                $currencyData = get_country_code_with_currency($dbc,$recipientData['countryCode']);

                                                                $recipientMessage = "You have received an amount of {$currencyData['currency']} {$transactionData['recipientAmount']} from {$senderData['fullName']} {$senderData['companyName']} - {$senderData['phoneNum']}, Balance: {$fcurr} {$reciAcc['balance']}  Transaction ID {$transaction_uid}";
                                                                $senderMessage = "You have sent an amount of {$currencyData['currency']} {$transactionData['sendersAmount']} to {$recipientData['fullName']}{$senderData['companyName']}, Transaction ID {$transaction_uid}";

                                                                sendSMS($recipientData['phoneNum'], $recipientMessage);
                                                                if ($senderData['firebaseKey'] != '') {
                                                                    firebaseCall($senderData['firebaseKey'], $senderMessage);
                                                                }
                                                                if ($recipientData['firebaseKey'] != '') {
                                                                    firebaseCall($recipientData['firebaseKey'], $recipientMessage);
                                                                }

                                                                $response['statusCode'] = 200;
                                                                $response['message'] = "Transaction Successful";
                                                            }
                                                        }
                                                    } else { // PayGlobal Takes all fees
                                                        $agentFee = $fee * $pricing['sysCommission'];
                                                        if (add_payGlobal_commission($dbc, $transactionData, $agentFee)) {

                                                            $reciAcc = get_cus_account_info_by_customerID($dbc,$recipientData['id'],$recipientData['countryCode']);

                                                            $code = getLiberianCode($dbc,$recipientData['phoneNum']);
                                                            $cCode = $code['countryCode'];
                                                            $curr1 = getLiberianCurrency($dbc,$cCode);
                                                            $fcurr = $curr1['currency'];

                                                            $recipientMessage = "You have received an amount of {$fcurr} {$transactionData['recipientAmount']} from {$senderData['fullName']} {$senderData['companyName']} - {$senderData['phoneNum']}, Balance: {$fcurr} {$reciAcc['balance']}  Transaction ID {$transaction_uid}";
                                                            $senderMessage = "You have sent an amount of {$fcurr} {$transactionData['sendersAmount']} to {$recipientData['fullName']}{$senderData['companyName']}, Transaction ID {$transaction_uid}";

                                                            sendSMS($recipientData['phoneNum'], $recipientMessage);
                                                            if ($senderData['firebaseKey'] != '') {
                                                                firebaseCall($senderData['firebaseKey'], $senderMessage);
                                                            }
                                                            if ($recipientData['firebaseKey'] != '') {
                                                                firebaseCall($recipientData['firebaseKey'], $recipientMessage);
                                                            }
                                                            $response['statusCode'] = 200;
                                                            $response['message'] = "Transaction Successful";
                                                        }
                                                    }
                                                }
                                            } else {
                                                $response['statusCode'] = 500;
                                                $response['message'] = "Error Occur!";
                                            }
                                        } else {
                                            /*
                                             * INTERNATIONAL MONEY TRANSFER
                                             */
                                            if(redraw_funds($dbc,$senderData['id'],$transactionData['sendersAmount'],$senderData['countryCode'])) {
                                                if (add_funds($dbc, $recipientData['id'], $transactionData['recipientAmount'],$recipientData['countryCode'])){
                                                    if (transcription($dbc, $senderData, $recipientData, $transactionData['transactionTypes'], $transactionData['sendersAmount'], $transactionData['billCode'], $transaction_uid)) {
                                                        //redraw fee
                                                        if (redraw_funds($dbc, $senderData['id'], $transactionData['fee'],$senderData['countryCode'])) {
                                                            $senderAcc = get_cus_account_info_by_customerID($dbc, $senderData['id'],$senderData['countryCode']);
                                                            $recipientAcc = get_cus_account_info_by_customerID($dbc, $recipientData['id'],$recipientData['countryCode']);
                                                            if (update_main_transaction_after_processing($dbc, $transactionData['transaction_uid'], "COMPLETED", $senderAcc['balance'], $recipientAcc['balance'])) {
                                                                $newTransactionData = get_main_transaction_by_transaction_uid($dbc, $transaction_uid);
                                                                $pricing = get_pricing_by_id($dbc, $transactionData['pricing_id']);
                                                                if ($recipientData['accountType'] == 2 OR $recipientData['accountType'] == 4) {// check if recipient is  Agent, then share commission


                                                                    $rate = get_conversion_rate($dbc,$senderData['countryCode'],$recipientData['countryCode']);
                                                                    $agentFee = $transactionData['fee'] * $pricing['recipientCommission'] * $rate;
                                                                    $payGlobalFee = $transactionData['fee'] * $pricing['sysCommission'];


                                                                    if (add_agent_commission($dbc, $newTransactionData, $agentFee, $recipientData['countryCode'])) {
                                                                        if (add_payGlobal_commission($dbc, $newTransactionData, $payGlobalFee)) {

                                                                            $reciAcc = get_cus_account_info_by_customerID($dbc,$recipientData['id'],$recipientData['countryCode']);

                                                                            if ($senderData['companyName'] == '') {
                                                                                $senderData['companyName'] = $senderData['fullName'];
                                                                            }

                                                                            if ($recipientData['companyName'] == '') {
                                                                                $recipientData['companyName'] = $recipientData['fullName'];
                                                                            }

                                                                            $code = getLiberianCode($dbc,$senderData['phoneNum'] );
                                                                            $cCode = $code['countryCode'];
                                                                            $curr1 = getLiberianCurrency($dbc,$cCode);
                                                                            $fcurr = $curr1['currency'];

                                                                            $senderMessage = "You have Sent an amount of {$fcurr} {$newTransactionData['sendersAmount']} to {$recipientData['fullName']}{$senderData['companyName']}, Transaction ID {$transaction_uid}";

                                                                            sendSMS($senderData['phoneNum'], $senderMessage);
                                                                            if ($senderData['firebaseKey'] != '') {
                                                                                firebaseCall($senderData['firebaseKey'], $senderMessage);
                                                                            }
                                                                            if ($recipientData['firebaseKey'] != '') {
                                                                                firebaseCall($recipientData['firebaseKey'], $recipientMessage);
                                                                            }

                                                                            $code = getLiberianCode($dbc,$recipientData['phoneNum']);
                                                                            $cCode = $code['countryCode'];
                                                                            $curr1 = getLiberianCurrency($dbc,$cCode);
                                                                            $fcurr = $curr1['currency'];
                                                                            $recipientMessage = "You have received an amount of {$fcurr} {$newTransactionData['recipientAmount']} from {$senderData['fullName']} {$senderData['companyName']} - {$senderData['phoneNum']}, Balance: {$fcurr} {$reciAcc['balance']}  Transaction ID {$transaction_uid}";

                                                                            sendSMS($recipientData['phoneNum'], $recipientMessage);
                                                                            $response['statusCode'] = 200;
                                                                            $response['message'] = "Transaction Successful";
                                                                        }
                                                                    }
                                                                } else {
                                                                    // PayGlobal takes all fees
                                                                    $payGlobalFee = $transactionData['fee'] * $pricing['sysCommission'];
                                                                    if (add_payGlobal_commission($dbc, $transactionData, $payGlobalFee)) {
                                                                        //$recipientMessage = "You have received an amount of {$newTransactionData['recipientAmount']} from {$senderData['fullName']}, Transaction ID {$transaction_uid}";
                                                                        // $senderMessage = "You have Sent an amount of {$newTransactionData['senderAmount']} to {$recipientData['fullName']}, Transaction ID {$transaction_uid}";
                                                                        $reciAcc = get_cus_account_info_by_customerID($dbc,$recipientData['id'],$recipientData['countryCode']);

                                                                        $code = getLiberianCode($dbc,$senderData['phoneNum'] );
                                                                        $cCode = $code['countryCode'];
                                                                        $curr1 = getLiberianCurrency($dbc,$cCode);
                                                                        $fcurr = $curr1['currency'];

                                                                        $code1 = getLiberianCode($dbc,$recipientData['phoneNum']);
                                                                        $cCode1 = $code1['countryCode'];
                                                                        $curr11 = getLiberianCurrency($dbc,$cCode1);
                                                                        $fcurr1 = $curr11['currency'];

                                                                        $recipientMessage = "You have received an amount of {$fcurr1} {$newTransactionData['recipientAmount']} from {$senderData['fullName']} {$senderData['companyName']} - {$senderData['phoneNum']}, Balance: {$fcurr1} {$reciAcc['balance']}  Transaction ID {$transaction_uid}";
                                                                        $senderMessage = "You have Sent an amount of {$fcurr} {$newTransactionData['sendersAmount']} to {$recipientData['fullName']}{$senderData['companyName']}, Transaction ID {$transaction_uid}";


                                                                        //sendSMS($recipientData['phoneNum'], $recipientMessage);
                                                                        if ($senderData['firebaseKey'] != '') {
                                                                            firebaseCall($senderData['firebaseKey'], $senderMessage);
                                                                        }
                                                                        if ($recipientData['firebaseKey'] != '') {
                                                                            firebaseCall($recipientData['firebaseKey'], $recipientMessage);
                                                                        }

                                                                        sendSMS($recipientData['phoneNum'], $recipientMessage);
                                                                        sendSMS($senderData['phoneNum'], $senderMessage);
                                                                        $response['statusCode'] = 200;
                                                                        $response['message'] = "Transaction Successful";
                                                                    }

                                                                }
                                                            } else {
                                                                $response['statusCode'] = 500;
                                                                $response['message'] = "Error occur";
                                                            }

                                                        }
                                                    }
                                                }
                                            }
                                        }


                                    } else {
                                        echo "LOW_BALANCE";
                                    }

                                } else {
                                    $response['statusCode'] = 500;
                                    $response['message'] = 'Error status code';
                                }

                                echo json_encode($response);
                            } else {
                                // Main money transfer Initiation.
                                $response = array();
                                $sendPhoneNum = html_filter($dbc, $_REQUEST['senderPhone']);
                                if($source == '1'){
                                    $sendPhoneNum = "+".html_filter($dbc, $_REQUEST['senderPhone']);
                                }
                                $recipientPhoneNum = html_filter($dbc, $_REQUEST['recipientPhone']);
                                if($source == '1'){
                                    $recipientPhoneNum = "+".html_filter($dbc, $_REQUEST['recipientPhone']);
                                }
                                $transactionType = html_filter($dbc, $_REQUEST['transactionType']);
                                $amount = html_filter($dbc, $_REQUEST['amount']);
                                $keyCode = html_filter($dbc, $_REQUEST['keyCode']);

                                $sendersData11 = get_customer_by_phoneNum($dbc, $sendPhoneNum);
                                $recipientData11 = get_customer_by_phoneNum($dbc, $recipientPhoneNum);
                                $sa_country_code = $sendersData11['countryCode'];
                                $customer_country_code = $recipientData11['countryCode'];

                                if(($sa_country_code=="lr" or $sa_country_code=="lr1") AND ($customer_country_code=="lr" or $customer_country_code=="lr1")){
                                    exchange_currency($dbc, $customerData['phoneNum'],$sa_country_code);
                                }

                                if (check_phoneNum_and_keyCode($dbc, $sendPhoneNum, $keyCode)) {
                                    if (check_if_number_exist($dbc, $recipientPhoneNum)) {
                                        if (check_if_number_exist_as_superAgent_or_agent_or_individual($dbc, $sendPhoneNum)) {
                                            if (check_if_number_exist_as_superAgent_or_agent_or_individual_or_merchant($dbc, $recipientPhoneNum)) {
                                                $sendersData = get_customer_by_phoneNum($dbc, $sendPhoneNum);
                                                $recipientData = get_customer_by_phoneNum($dbc, $recipientPhoneNum);
                                                $check_balance = check_if_balance_is_greater_than_redraw($dbc, $sendersData, $amount, $recipientData, $transactionType);
                                                if ($check_balance['status'] == true) {
                                                    if (dailyKyc($dbc, $sendersData['id'], $sendersData['countryCode'], $check_balance['exactAmount'], $check_balance['fee'])) {
                                                        if (monthlyKyc($dbc, $sendersData['id'], $sendersData['countryCode'], $check_balance['exactAmount'], $check_balance['fee'])) {

                                                            //get converted rate
                                                            $rate = get_conversion_rate($dbc,$sendersData['countryCode'],$recipientData['countryCode']);
                                                            $recipientAmount = $check_balance['exactAmount'] * $rate;

                                                            if (add_main_transaction($dbc, $recipientData, $sendersData, $transactionType, "", $check_balance['transaction_uid'], "PROCESSING", $check_balance['fee'], $check_balance['exactAmount'], $recipientAmount, "SMART", $check_balance['pricing_id'])) {

                                                                $recipientCountryData = get_country_code_with_currency($dbc,$recipientData['countryCode']);



                                                                $response['statusCode'] = 200;
                                                                $response['recipientName'] = $check_balance['recipientName'];
                                                                $response['fee'] = moneyFormat2($check_balance['fee']);
                                                                $response['transaction_uid'] = $check_balance['transaction_uid'];
                                                                $response['senderAmount'] = moneyFormat2($check_balance['exactAmount']);
                                                                $response['recipientAmount'] = moneyFormat2($recipientAmount);
                                                                $response['totalAmount'] = moneyFormat2($check_balance['newAmount']);
                                                                $response['xRate'] = moneyFormat2($rate);
                                                                $response['status'] = $check_balance['status'];
                                                                $response['recipientCurrency'] = $recipientCountryData['currency'];

                                                                // echo "({$check_balance['recipientName']}/{$check_balance['fee']}*{$check_balance['transaction_uid']}!{$check_balance['status']})";
                                                            }
                                                        } else {
                                                            // Monthly Limit
                                                            $response['statusCode'] = 500;
                                                            $response['message'] = 'Monthly Limit Reached';
                                                        }
                                                    } else {
                                                        // Daily Limit
                                                        $response['statusCode'] = 500;
                                                        $response['message'] = 'Daily Limit Reached'. $check_balance['pricing_id'];
                                                    }
                                                }elseif ($check_balance['status'] == 'error'){
                                                    //Unauthorised Transaction
                                                    $response['statusCode'] = 500;
                                                    $response['message'] = 'You are not allowed to perform this transaction';
                                                }else {
                                                    //Insufficient Account Balance Returns
                                                    $response['statusCode'] = 500;
                                                    $response['message'] = 'You have Insufficient Account Balance';
                                                }
                                            } else {
                                                //Invalid Recipient Number
                                                $response['statusCode'] = 500;
                                                $response['message'] = 'Invalid Recipient Number';
                                            }
                                        } else {
                                            //Invalid Recipient Number
                                            $response['statusCode'] = 500;
                                            $response['message'] = 'Unauthorised to perform this transaction';
                                        }

                                    } else {
                                        //Recipient Number DO not exist
                                        $response['statusCode'] = 500;
                                        $response['message'] = 'Sorry, Recipient does not Exist, Please use Token!';
                                    }
                                } else {
                                    //Recipient Number DO not exist
                                    $response['statusCode'] = 500;
                                    $response['message'] = 'Sorry, Error authenticating user!';
                                }
                                echo json_encode($response);
                            }
                            break;

                        CASE 402: // Money  Payment to marchant
                            //Make payment to a merchant locally
                            if (isset($_REQUEST['status'])) {
                                $response = array();
                                $status = html_filter($dbc, $_REQUEST['status']);
                                if ($status == true) {
                                    $fee = html_filter($dbc, $_REQUEST['fee']);
                                    $transaction_uid = html_filter($dbc, $_REQUEST['transaction_uid']);

                                    $transactionData = get_main_transaction_by_transaction_uid($dbc, $transaction_uid);
                                    $recipientData = get_customer_by_id($dbc, $transactionData['recipientID']);
                                    $senderData = get_customer_by_id($dbc, $transactionData['senderID']);
                                    $totalAmount = $transactionData['sendersAmount'] + $transactionData['fee'];
                                    if (check_existing_balance($dbc, $transactionData['senderID'], $totalAmount,$senderData['countryCode'])) {
                                        if ($transactionData['senderCountryCode'] == $transactionData['recipientCountryCode']) {
                                            if (translation($dbc, $transactionData)) {
                                                $senderAcc = get_cus_account_info_by_customerID($dbc, $senderData['id'],$senderData['countryCode']);
                                                $recipientAcc = get_cus_account_info_by_customerID($dbc, $recipientData['id'],$recipientData['countryCode']);
                                                if (update_main_transaction_after_processing($dbc, $transactionData['transaction_uid'], "COMPLETED", $senderAcc['balance'], $recipientAcc['balance'])) {
                                                    $pricing = get_pricing_by_id($dbc, $transactionData['pricing_id']);

                                                    //PayGlobal Takes all fees
                                                    $agentFee = $fee * $pricing['sysCommission'];
                                                    if (add_payGlobal_commission($dbc, $transactionData, $agentFee)) {

                                                        $code = getLiberianCode($dbc,$senderData['phoneNum'] );
                                                        $cCode = $code['countryCode'];
                                                        $curr1 = getLiberianCurrency($dbc,$cCode);
                                                        $fcurr = $curr1['currency'];

                                                        $code1 = getLiberianCode($dbc,$recipientData['phoneNum']);
                                                        $cCode1 = $code1['countryCode'];
                                                        $curr11 = getLiberianCurrency($dbc,$cCode1);
                                                        $fcurr1 = $curr11['currency'];

                                                        $recipientMessage = "You have received an amount of {$fcurr1} {$transactionData['recipientAmount']} from {$senderData['fullName']}, Transaction ID {$transaction_uid}";



                                                        sendSMS($recipientData['phoneNum'], $recipientMessage);
                                                        if ($recipientData['firebaseKey'] != '') {
                                                            firebaseCall($recipientData['firebaseKey'], $recipientMessage);
                                                        }


                                                        $senderMessage = "You paid an amount of {$fcurr} {$transactionData['sendersAmount']} to {$recipientData['fullName']}, Transaction ID {$transaction_uid}";
                                                        sendSMS($senderData['phoneNum'], $senderMessage);
                                                        if ($senderData['firebaseKey'] != '') {
                                                            firebaseCall($senderData['firebaseKey'], $senderMessage);
                                                        }

                                                        $response['statusCode'] = 200;
                                                        $response['message'] = "Transaction Successful";
                                                    }
                                                }
                                            } else {
                                                $response['statusCode'] = 500;
                                                $response['message'] = "Error Occur!";
                                            }
                                        } else {
                                            $response['statusCode'] = 500;
                                            $response['message'] = "Make payment is restricted to same country";
                                        }

                                    } else {
                                        $response['statusCode'] = 500;
                                        $response['message'] = "You don't have enough balance, Please load your wallet";
                                    }

                                } else {
                                    $response['statusCode'] = 500;
                                    $response['message'] = 'Error status code';
                                }

                                echo json_encode($response);
                            } else {
                                // Main Make Payment Initiation.
                                $response = array();
                                $response['statusCode'] = 500;
                                $response['message'] = "Service Temporary unavailable ";
                                echo json_encode($response);
                            }
                            break;

                        CASE 403: //Bill Payment
                            if (isset($_REQUEST['status'])) {
                                $response = array();
                                $status = html_filter($dbc, $_REQUEST['status']);
                                if ($status == true) {
                                    $fee = html_filter($dbc, $_REQUEST['fee']);
                                    $transaction_uid = html_filter($dbc, $_REQUEST['transaction_uid']);

                                    $transactionData = get_main_transaction_by_transaction_uid($dbc, $transaction_uid);
                                    $recipientData = get_customer_by_id($dbc, $transactionData['recipientID']);
                                    $senderData = get_customer_by_id($dbc, $transactionData['senderID']);
                                    $totalAmount = $transactionData['sendersAmount'] + $transactionData['fee'];
                                    if (check_existing_balance($dbc, $transactionData['senderID'], $totalAmount,$senderData['countryCode'])) {
                                        if ($transactionData['senderCountryCode'] == $transactionData['recipientCountryCode']) {
                                            if (translation($dbc, $transactionData)) {
                                                $senderAcc = get_cus_account_info_by_customerID($dbc, $senderData['id'],$senderData['countryCode']);
                                                $recipientAcc = get_cus_account_info_by_customerID($dbc, $recipientData['id'],$recipientData['countryCode']);
                                                if (update_main_transaction_after_processing($dbc, $transactionData['transaction_uid'], "COMPLETED", $senderAcc['balance'], $recipientAcc['balance'])) {
                                                    $pricing = get_pricing_by_id($dbc, $transactionData['pricing_id']);

                                                    //PayGlobal Takes all fees
                                                    $agentFee = $fee * $pricing['sysCommission'];
                                                    if (add_payGlobal_commission($dbc, $transactionData, $agentFee)) {

                                                        $code = getLiberianCode($dbc,$senderData['phoneNum'] );
                                                        $cCode = $code['countryCode'];
                                                        $curr1 = getLiberianCurrency($dbc,$cCode);
                                                        $fcurr = $curr1['currency'];

                                                        $code1 = getLiberianCode($dbc,$recipientData['phoneNum']);
                                                        $cCode1 = $code1['countryCode'];
                                                        $curr11 = getLiberianCurrency($dbc,$cCode1);
                                                        $fcurr1 = $curr11['currency'];

                                                        $recipientMessage = "You have received an amount of {$fcurr1} {$transactionData['recipientAmount']} from {$senderData['fullName']}, Transaction ID {$transaction_uid}";
                                                        sendSMS($recipientData['phoneNum'], $recipientMessage);
                                                        if ($recipientData['firebaseKey'] != '') {
                                                            firebaseCall($recipientData['firebaseKey'], $recipientMessage);
                                                        }


                                                        $senderMessage = "You paid an amount of {$fcurr} {$transactionData['sendersAmount']} to {$recipientData['fullName']}, Transaction ID {$transaction_uid}";
                                                        sendSMS($senderData['phoneNum'], $senderMessage);
                                                        if ($senderData['firebaseKey'] != '') {
                                                            firebaseCall($senderData['firebaseKey'], $senderMessage);
                                                        }

                                                        $response['statusCode'] = 200;
                                                        $response['message'] = "Transaction Successful";
                                                    }
                                                }
                                            } else {
                                                $response['statusCode'] = 500;
                                                $response['message'] = "Error Occur!";
                                            }
                                        } else {

                                            // International Bill or Premium Payment
                                            if (redraw_funds($dbc,$senderData['id'],$totalAmount,$senderData['countryCode'])) {
                                                if(add_funds($dbc,$recipientData['id'],$transactionData['recipientAmount'],$recipientData['countryCode'])) {
                                                    if(transcription($dbc,$senderData,$recipientData,$transactionData['transactionTypes'],$transactionData['sendersAmount'],$transactionData['billCode'],$transactionData['transaction_uid'])) {
                                                        $senderAcc = get_cus_account_info_by_customerID($dbc, $senderData['id'],$senderData['countryCode']);
                                                        $recipientAcc = get_cus_account_info_by_customerID($dbc, $recipientData['id'],$recipientData['countryCode']);
                                                        if (update_main_transaction_after_processing($dbc, $transactionData['transaction_uid'], "COMPLETED", $senderAcc['balance'], $recipientAcc['balance'])) {
                                                            $pricing = get_pricing_by_id($dbc, $transactionData['pricing_id']);

                                                            //PayGlobal Takes all fees
                                                            $agentFee = $fee * $pricing['sysCommission'];
                                                            if (add_payGlobal_commission($dbc, $transactionData, $agentFee)) {

                                                                $code = getLiberianCode($dbc,$senderData['phoneNum'] );
                                                                $cCode = $code['countryCode'];
                                                                $curr1 = getLiberianCurrency($dbc,$cCode);
                                                                $fcurr = $curr1['currency'];

                                                                $code1 = getLiberianCode($dbc,$recipientData['phoneNum']);
                                                                $cCode1 = $code1['countryCode'];
                                                                $curr11 = getLiberianCurrency($dbc,$cCode1);
                                                                $fcurr1 = $curr11['currency'];


                                                                $recipientMessage = "You have received an amount of {$fcurr1} {$transactionData['recipientAmount']} from {$senderData['fullName']}, Transaction ID {$transaction_uid}";
                                                                sendSMS($recipientData['phoneNum'], $recipientMessage);
                                                                if ($recipientData['firebaseKey'] != '') {
                                                                    firebaseCall($recipientData['firebaseKey'], $recipientMessage);
                                                                }


                                                                $senderMessage = "You paid an amount of {$fcurr} {$transactionData['senderAmount']} to {$recipientData['fullName']}, Transaction ID {$transaction_uid}";
                                                                sendSMS($senderData['phoneNum'], $senderMessage);
                                                                if ($senderData['firebaseKey'] != '') {
                                                                    firebaseCall($senderData['firebaseKey'], $senderMessage);
                                                                }

                                                                $response['statusCode'] = 200;
                                                                $response['message'] = "Transaction Successful";
                                                            }
                                                        }
                                                    }
                                                }
                                            } else {
                                                $response['statusCode'] = 500;
                                                $response['message'] = "Error Occur!";
                                            }
                                        }

                                    } else {
                                        $response['statusCode'] = 500;
                                        $response['message'] = "You don't have enough balance, Please load your wallet";
                                    }

                                } else {
                                    $response['statusCode'] = 500;
                                    $response['message'] = 'Error status code';
                                }

                                echo json_encode($response);
                            } else {
                                // Bill Payment with bill code
                                $response = array();

                                $sendPhoneNum = html_filter($dbc, $_REQUEST['senderPhone']);
                                if($source == '1'){
                                    $sendPhoneNum = "+".html_filter($dbc, $_REQUEST['senderPhone']);
                                }
                                $keyCode = html_filter($dbc, $_REQUEST['keyCode']);
                                $recipientPhoneNum = html_filter($dbc, $_REQUEST['recipientPhone']);
                                if($source == '1'){
                                    $recipientPhoneNum = "+".html_filter($dbc, $_REQUEST['recipientPhone']);
                                }
                                $amount = html_filter($dbc, $_REQUEST['amount']);
                                $billCode = html_filter($dbc, $_REQUEST['billCode']);
                                $transactionType = html_filter($dbc, $_REQUEST['transactionType']);

                                if (check_phoneNum_and_keyCode($dbc, $sendPhoneNum, $keyCode)) {
                                    if (check_if_number_exist_as_superAgent_or_agent_or_individual($dbc, $sendPhoneNum)) {
                                        if (check_if_number_exist_as_merchant($dbc, $recipientPhoneNum)) {
                                            $sendersData = get_customer_by_phoneNum($dbc, $sendPhoneNum);
                                            $recipientData = get_customer_by_phoneNum($dbc, $recipientPhoneNum);
                                            $check_balance = check_if_balance_is_greater_than_redraw($dbc, $sendersData, $amount, $recipientData, $transactionType);

                                            $rate = get_conversion_rate($dbc,$sendersData['countryCode'],$recipientData['countryCode']);
                                            $convertedAmount = $amount * $rate;

                                            if ($check_balance['status'] == true) {
                                                if (dailyKyc($dbc, $sendersData['id'], $sendersData['countryCode'], $check_balance['exactAmount'], $check_balance['fee'])) {
                                                    if (monthlyKyc($dbc, $sendersData['id'], $sendersData['countryCode'], $check_balance['exactAmount'], $check_balance['fee'])) {
                                                        if (add_main_transaction($dbc, $recipientData, $sendersData, $transactionType, $billCode, $check_balance['transaction_uid'], "WAITING", $check_balance['fee'], $check_balance['exactAmount'], $convertedAmount, "SMART",$check_balance['pricing_id'])) {

                                                            //echo "({$check_balance['recipientName']}/{$check_balance['fee']}*{$check_balance['transaction_uid']}!{$check_balance['status']})";
                                                            $response['statusCode'] = 200;
                                                            $response['recipientName'] = $check_balance['recipientName'];
                                                            $response['fee'] = $check_balance['fee']."";
                                                            $response['transaction_uid'] = $check_balance['transaction_uid'];
                                                            $response['convertedAmount'] = $convertedAmount."";
                                                            $response['billCode'] = $billCode;
                                                            $response['status'] = $check_balance['status'];
                                                            $response['recipientCountry'] = $recipientData['countryCode'];
                                                        }
                                                    } else {
                                                        //echo "MONTHLY_LIMIT";
                                                        $response['statusCode'] = 500;
                                                        $response['message'] = 'You have reach your monthly Limit';
                                                    }
                                                } else {
                                                    //echo "DAILY_LIMIT";
                                                    $response['statusCode'] = 500;
                                                    $response['message'] = 'You have reach your Daily Limit';
                                                }
                                            }elseif ($check_balance['status'] == 'error'){
                                                //Unauthorised Transaction
                                                $response['statusCode'] = 500;
                                                $response['message'] = 'You are not allowed to perform this transaction';
                                            } else {
                                                //echo "0"; //Insufficient Account Balance Returns 0 string
                                                $response['statusCode'] = 500;
                                                $response['message'] = 'Insufficient Wallet Balance, Load Wallet';
                                            }
                                        } else {
                                            //echo("Sorry, transaction is only for merchants and Individuals");
                                            $response['statusCode'] = 500;
                                            $response['message'] = 'Sorry, transaction is only for merchants';
                                        }
                                    } else {
                                        //echo 'Sorry you not authorise for this transactions';
                                        $response['statusCode'] = 500;
                                        $response['message'] = 'Sorry you not authorise for this transactions';
                                    }
                                } else {
                                    // Sorry, Error authenticating user!
                                    $response['statusCode'] = 500;
                                    $response['message'] = 'Sorry, Error authenticating user!';
                                }
                                echo json_encode($response);
                            }
                            break;

                        CASE 404: //roaming Payment
                            $status = html_filter($dbc, $_REQUEST['status']);
                            if ($status == true) {
                                $response = array();
                                $transaction_uid = html_filter($dbc, $_REQUEST['transaction_uid']);
                                $convertedFee = html_filter($dbc, $_REQUEST['convertedFee']);

                                $transactionData = get_main_transaction_by_transaction_uid($dbc, $transaction_uid);
                                $senderData = get_customer_by_id($dbc, $transactionData['senderID']);
                                $recipientData = get_customer_by_id($dbc, $transactionData['recipientID']);
                                if (transcription($dbc, $senderData, $recipientData, $transactionData['transactionTypes'], $transactionData['sendersAmount'], $transactionData['billCode'], $transaction_uid)) {

                                    if (redraw_funds($dbc, $senderData['id'], $convertedFee,$senderData['countryCode'])) {
                                        $senderAcc = get_cus_account_info_by_customerID($dbc, $senderData['id'],$senderData['countryCode']);
                                        $recipientAcc = get_cus_account_info_by_customerID($dbc, $recipientData['id'],$recipientData['countryCode']);

                                        if (update_main_transaction_after_processing($dbc, $transactionData['transaction_uid'], "COMPLETED", $senderAcc['balance'], $recipientAcc['balance'])) {
                                            $newTransactionData = get_main_transaction_by_transaction_uid($dbc, $transaction_uid);

                                            if (add_payGlobal_commission($dbc, $newTransactionData, $convertedFee)) {


                                                $recipientMessage = "You have received an amount of {$newTransactionData['recipientAmount']} from {$senderData['fullName']}, Transaction ID {$transaction_uid}";
                                                $senderMessage = "Payment was made with an amount of {$newTransactionData['senderAmount']} to {$recipientData['CompanyName']}, Transaction ID {$transaction_uid}";
                                                sendSMS($recipientData['phoneNum'], $recipientMessage);
                                                if ($senderData['firebaseKey'] != '') {
                                                    firebaseCall($senderData['firebaseKey'], $senderMessage);
                                                }
                                                if ($recipientData['firebaseKey'] != '') {
                                                    firebaseCall($recipientData['firebaseKey'], $recipientMessage);
                                                }

                                                sendSMS($recipientData['phoneNum'], $recipientMessage);
                                                $response['statusCode'] = 200;
                                                $response['message'] = 'Roaming payment successful';
                                            }

                                        } else {
                                            $response['statusCode'] = 500;
                                            $response['message'] = 'Sorry, Occur';
                                        }
                                    }
                                }

                                echo json_encode($response);
                            } else {
                                $response = array();

                                $sendPhoneNum = html_filter($dbc, $_REQUEST['senderPhone']);
                                if($source == '1'){
                                    $sendPhoneNum = "+".html_filter($dbc, $_REQUEST['senderPhone']);
                                }
                                $recipientPhoneNum = html_filter($dbc, $_REQUEST['recipientPhone']);
                                if($source == '1'){
                                    $recipientPhoneNum = "+".html_filter($dbc, $_REQUEST['recipientPhone']);
                                }
                                $transactionType = html_filter($dbc, $_REQUEST['transactionType']);
                                $amount = html_filter($dbc, $_REQUEST['amount']);
                                $keyCode = html_filter($dbc, $_REQUEST['keyCode']);

                                if (check_phoneNum_and_keyCode($dbc, $sendPhoneNum, $keyCode)) {
                                    if (check_if_number_exist($dbc, $recipientPhoneNum)) {
                                        if (check_if_number_exist_as_individual($dbc, $sendPhoneNum)) {
                                            if (check_if_number_exist_as_merchant($dbc, $recipientPhoneNum)) {
                                                $sendersData = get_customer_by_phoneNum($dbc, $sendPhoneNum);
                                                $recipientData = get_customer_by_phoneNum($dbc, $recipientPhoneNum);
                                                $check_balance = check_if_balance_is_greater_than_redraw_roaming($dbc, $sendersData, $amount, $recipientData, $transactionType);
                                                if ($check_balance['status'] == true) {
                                                    if (dailyKyc($dbc, $sendersData['id'], $sendersData['countryCode'], $check_balance['convertedAmount'], $check_balance['fee'])) {
                                                        if (monthlyKyc($dbc, $sendersData['id'], $sendersData['countryCode'], $check_balance['convertedAmount'], $check_balance['fee'])) {
                                                            if (add_main_transaction($dbc, $recipientData, $sendersData, $transactionType, "", $check_balance['transaction_uid'], "PROCESSING", $check_balance['fee'], $check_balance['convertedAmount'], $check_balance['exactAmount'], "SMART", $check_balance['pricing_id'])) {
                                                                //echo "({$check_balance['recipientName']}/{$check_balance['fee']}*{$check_balance['transaction_uid']}!{$check_balance['status']}~{$check_balance['convertedAmount']}|{$check_balance['convertedFee']})";
                                                                $response['statusCode'] = 200;
                                                                $response['recipientName'] = $check_balance['recipientName'];
                                                                $response['fee'] = $check_balance['fee'];
                                                                $response['transaction_uid'] = $check_balance['transaction_uid'];
                                                                $response['status'] = $check_balance['status'];
                                                                $response['convertedAmount'] = $check_balance['convertedAmount'];
                                                                $response['convertedFee'] = $check_balance['convertedFee'];
                                                            }
                                                        } else {
                                                            //echo "MONTHLY_LIMIT";
                                                            $response['statusCode'] = 500;
                                                            $response['message'] = 'You have reach your Monthly Limit';
                                                        }
                                                    } else {
                                                        //echo "DAILY_LIMIT";
                                                        $response['statusCode'] = 500;
                                                        $response['message'] = 'You have reach your Daily Limit';
                                                    }
                                                } else {
                                                    //echo "0"; //Insufficient Account Balance Returns 0 string
                                                    $response['statusCode'] = 500;
                                                    $response['message'] = 'Insufficient Wallet Balance, Load Wallet';
                                                }
                                            } else {
                                                //Invalid Recipient Number
                                                $response['statusCode'] = 500;
                                                $response['message'] = 'Invalid Recipient Number, Please try Again';
                                            }
                                        } else {
                                            //Unauthorised user
                                            $response['statusCode'] = 500;
                                            $response['message'] = 'Unauthorised to perform this transaction';
                                        }

                                    } else {
                                        //Invalid Recipient user
                                        $response['statusCode'] = 500;
                                        $response['message'] = 'Sorry, Recipient does not Exist, Please use Token!';
                                    }
                                } else {
                                    //Unauthorised user
                                    $response['statusCode'] = 500;
                                    $response['message'] = 'Sorry, Error Authentication';
                                }
                                echo json_encode($response);
                            }

                            break;
                        CASE 405: //Activate Debit Card
                            $response = array();
                            $sendPhoneNum = html_filter($dbc, $_REQUEST['senderPhone']);
                            if($source == '1'){
                                $sendPhoneNum = "+".html_filter($dbc, $_REQUEST['senderPhone']);
                            }
                            $keyCode = html_filter($dbc, $_REQUEST['keyCode']);
                            if (check_phoneNum_and_keyCode($dbc, $sendPhoneNum, $keyCode)) {
                                $customerData = get_customer_by_phoneNum($dbc, $sendPhoneNum);
                                if (z_check_customer_already_signUp($dbc, $customerData['id'])) {
                                    $response['statusCode'] = 500;
                                    $response['message'] = 'You have already Activated, Please proceed to add a Card';
                                } else {
                                    //$test = z_signUp($dbc, $customerData, genActive_id());
                                    if (z_signUp($dbc, $customerData, genActive_id())) {
                                        $response['statusCode'] = 200;
                                        $response['message'] = 'Debit Card Option Activated';
                                    } else {
                                        $response['statusCode'] = 500;
                                        $response['message'] = 'Error Activating Debit Card Option';
                                    }

                                }
                            } else {
                                // Sorry, Error authenticating user!
                                $response['statusCode'] = 500;
                                $response['message'] = 'Sorry, Error authenticating user!';
                            }
                            echo json_encode($response);
                            break;
                        CASE 406: //Add New Debit Card

                            $response = array();
                            $sendPhoneNum = html_filter($dbc, $_REQUEST['senderPhone']);
                            if($source == '1'){
                                $sendPhoneNum = "+".html_filter($dbc, $_REQUEST['senderPhone']);
                            }
                            $keyCode = html_filter($dbc, $_REQUEST['keyCode']);

                            $cardType = html_filter($dbc, $_REQUEST['cardType']);
                            $cardNumber = html_filter($dbc, $_REQUEST['cardNumber']);
                            $nameOnCard = html_filter($dbc, $_REQUEST['nameOnCard']);
                            $cardCVV = html_filter($dbc, $_REQUEST['cardCVV']);
                            $cardExMonth = html_filter($dbc, $_REQUEST['cardExMonth']);
                            $cardExYear = html_filter($dbc, $_REQUEST['cardExYear']);
                            $cardStatus = 'good';
                            if($cardStatus == 'good') {
                                if(is_numeric($cardNumber) AND is_numeric($cardCVV)) {
                                    if(strlen($cardNumber) == 16) {
                                        if (check_phoneNum_and_keyCode($dbc, $sendPhoneNum, $keyCode)) {
                                            $customerData = get_customer_by_phoneNum($dbc, $sendPhoneNum);
                                            if (z_check_customer_already_signUp($dbc, $customerData['id'])) {
                                                $res = z_add_new_card($dbc, $customerData, $cardType, $cardNumber, $nameOnCard, $cardCVV, $cardExMonth, $cardExYear);
                                                //$response = $res;
                                                if ($res['status'] == 'true') {
                                                    $response['statusCode'] = 200;
                                                    $response['message'] = $res['message'];
                                                } else {
                                                    $response['statusCode'] = 500;
                                                    $response['message'] = $res['message'];
                                                }
                                            } else {
                                                $response['statusCode'] = 500;
                                                $response['message'] = 'Debit Card Option Not activated';
                                            }
                                        } else {
                                            // Sorry, Error authenticating user!
                                            $response['statusCode'] = 500;
                                            $response['message'] = 'Sorry, Error authenticating user!';
                                        }
                                    }else{
                                        $response['statusCode'] = 500;
                                        $response['message'] = 'Invalid Card Length!';
                                    }
                                }else{
                                    $response['statusCode'] = 500;
                                    $response['message'] = 'Invalid Card number!';
                                }
                            }else{
                                $response['statusCode'] = 500;
                                $response['message'] = 'Sorry, Service not available now.';
                            }
                            echo json_encode($response);
                            break;
                        CASE 407: //Confirm Debit Card
                            $response = array();
                            $sendPhoneNum = html_filter($dbc, $_REQUEST['senderPhone']);
                            if($source == '1'){
                                $sendPhoneNum = "+".html_filter($dbc, $_REQUEST['senderPhone']);
                            }
                            $keyCode = html_filter($dbc, $_REQUEST['keyCode']);

                            $cardID = html_filter($dbc, $_REQUEST['cardID']);
                            $amount = html_filter($dbc, $_REQUEST['amount']);

                            if (check_phoneNum_and_keyCode($dbc, $sendPhoneNum, $keyCode)) {
                                $customerData = get_customer_by_phoneNum($dbc,$sendPhoneNum);
                                $data = z_confirm_new_card($dbc, $customerData, $cardID, $amount);
                                if ($data['confirm-status'] == 'true'){

                                    z_card_verify_log($dbc,$customerData,$cardID,$amount,'SUCCESS');

                                    $response['statusCode'] = 200;
                                    $response['message'] = 'You have successfully verified your card. You can now add money to your PayGlobal wallet with your verified card';
                                }else {

                                    z_card_verify_log($dbc,$customerData,$cardID,$amount,'ERROR');

                                    $response['statusCode'] = 500;
                                    $response['message'] = 'Your card verification has failed. Please contact us on +233501464140 or email us at support@payappglobal.com for assistance.';
                                }
                            } else {
                                // Sorry, Error authenticating user!
                                $response['statusCode'] = 500;
                                $response['message'] = 'Sorry, Error authenticating user!';
                            }
                            echo json_encode($response);
                            break;
                        CASE 408: //List All cards
                            $response = array();
                            $sendPhoneNum = html_filter($dbc, $_REQUEST['senderPhone']);
                            if($source == '1'){
                                $sendPhoneNum = "+".html_filter($dbc, $_REQUEST['senderPhone']);
                            }
                            $keyCode = html_filter($dbc, $_REQUEST['keyCode']);
                            if (check_phoneNum_and_keyCode($dbc, $sendPhoneNum, $keyCode)) {
                                $customerData = get_customer_by_phoneNum($dbc, $sendPhoneNum);
                                if (z_check_customer_already_signUp($dbc, $customerData['id'])) {
                                    $cards = z_list_all_cards($dbc, $customerData['id']);
                                    $response = $cards;
                                    $response['statusCode'] = 200;
                                } else {
                                    $response['statusCode'] = 500;
                                    $response['message'] = 'No Payment card added yet';
                                }
                            } else {
                                // Sorry, Error authenticating user!
                                $response['statusCode'] = 500;
                                $response['message'] = 'Sorry, Error authenticating user!';
                            }
                            echo json_encode($response);

                            break;

                        CASE 409: //List  of all cards
                            $response = array();
                            $sendPhoneNum = html_filter($dbc, $_REQUEST['senderPhone']);
                            if($source == '1'){
                                $sendPhoneNum = "+".html_filter($dbc, $_REQUEST['senderPhone']);
                            }
                            $keyCode = html_filter($dbc, $_REQUEST['keyCode']);
                            $cardWaiting_ID = html_filter($dbc, $_REQUEST['waiting_id']);
                            if (check_phoneNum_and_keyCode($dbc, $sendPhoneNum, $keyCode)) {
                                $customerData = get_customer_by_phoneNum($dbc, $sendPhoneNum);
                                if (z_check_customer_already_signUp($dbc, $customerData['id'])) {
                                    $cards = z_list_all_cards($dbc, $customerData['id']);
                                    $response = $cards;
                                    $response['statusCode'] = 200;
                                } else {
                                    $response['statusCode'] = 500;
                                    $response['message'] = 'Debit Card Option Not activated';
                                }
                            } else {
                                // Sorry, Error authenticating user!
                                $response['statusCode'] = 500;
                                $response['message'] = 'Sorry, Error authenticating user!';
                            }
                            echo json_encode($response);
                            break;
                        CASE 410: //Perform Transactions by card
                            $response = array();
                            $sendPhoneNum = html_filter($dbc, $_REQUEST['senderPhone']);
                            if($source == '1'){
                                $sendPhoneNum = "+".html_filter($dbc, $_REQUEST['senderPhone']);
                            }
                            $keyCode = html_filter($dbc, $_REQUEST['keyCode']);
                            $cardID = html_filter($dbc, $_REQUEST['cardID']);
                            $amount = html_filter($dbc, $_REQUEST['amount']);
                            if (check_phoneNum_and_keyCode($dbc, $sendPhoneNum, $keyCode)) {
                                $customerData = get_customer_by_phoneNum($dbc, $sendPhoneNum);
                                if (z_check_customer_already_signUp($dbc, $customerData['id'])) {
                                    if (z_check_if_cardID_exist_on_transaction($dbc, $cardID, $customerData['id'])) {
                                        $global_zenith_acc = "+233771000000";

                                        $convertedAmount = z_convert_amount($dbc, $customerData['countryCode'], $amount);
                                        $transaction_uid = genTransactionID();
                                        if (z_add_transaction($dbc, $customerData, $cardID, $amount, $convertedAmount, $transaction_uid, $global_zenith_acc)) {
                                            // pass converted amount to zenith API
                                            $data = z_new_transaction($dbc, $customerData, $cardID, $convertedAmount, $transaction_uid);
                                            if ($data['PaymentResponse']['PaymentStatus'] == 'APPROVED') {
                                                $global_zenith_data = get_customer_by_phoneNum($dbc, $global_zenith_acc);
                                                if (redraw_funds($dbc, $global_zenith_data['id'], $convertedAmount,$global_zenith_data['countryCode'])) {
                                                    if (add_funds($dbc, $customerData['id'], $amount,$customerData['countryCode'])) {
                                                        $cus_Acc = get_cus_account_info_by_customerID($dbc, $customerData['id'],$customerData['countryCode']);
                                                        if (z_update_transaction($dbc, $transaction_uid, $data['PaymentResponse']['Reason'], "COMPLETED", $cus_Acc['balance'])) {
                                                            $response['statusCode'] = 200;
                                                            $response['message'] = $data['PaymentResponse']['Reason'];

                                                        }
                                                    }
                                                }
                                            } else {
                                                if (z_update_transaction($dbc, $transaction_uid, $data['PaymentResponse']['Reason'], "CANCELLED", 0)) {

                                                    $response['statusCode'] = 500;
                                                    //$response['message'] = 'Transaction Declined, Please check your balance or Contact your bank';
                                                    $response['message'] = $data['PaymentResponse']['Reason'];
                                                }
                                            }
                                        } else {
                                            $response['statusCode'] = 500;
                                            $response['message'] = 'Error initiating transaction';
                                        }
                                    } else {
                                        $response['statusCode'] = 500;
                                        $response['message'] = 'Debit Card Option Not activated';
                                    }
                                } else {
                                    $response['statusCode'] = 500;
                                    $response['message'] = 'Debit Card Option Not activated';
                                }
                            } else {
                                // Sorry, Error authenticating user!
                                $response['statusCode'] = 500;
                                $response['message'] = 'Sorry, Error authenticating user!';
                            }
                            echo json_encode($response);

                            break;
                        CASE 411: // Hubtel Pull
                            $response = array();
                            $sendPhoneNum = html_filter($dbc, $_REQUEST['senderPhone']);
                            if($source == '1'){
                                $sendPhoneNum = "+".html_filter($dbc, $_REQUEST['senderPhone']);
                            }
                            $keyCode = html_filter($dbc, $_REQUEST['keyCode']);

                            $pull_status = 'on';
                            if($pull_status == 'on') {

                                $mm_phoneNum = html_filter($dbc, $_REQUEST['mm_phone']);
                                $mm_channel = html_filter($dbc, $_REQUEST['mm_channel']);
                                $mm_Name = html_filter($dbc, $_REQUEST['mm_name']);
                                $mm_amount = html_filter($dbc, $_REQUEST['mm_amount']);
                                $mm_email = html_filter($dbc, $_REQUEST['mm_email']);
                                $mm_cash_token = html_filter($dbc,$_REQUEST['mm_cash_token']);

                                $mm_transactionType = 'MM-PULL';
                                $mm_desc = "Load GN Mobile Money";
                                if ($mm_email == "" OR $mm_email == null) {
                                    $mm_email = "alert@payappglobal.com";
                                }
                                $mm_Name = 'PAYGLOBAL';

                                if (check_phoneNum_and_keyCode($dbc, $sendPhoneNum, $keyCode)) {
                                    $system_status = get_system_status_by_serviceType($dbc, "HUBTEL");
                                    if ($system_status['status'] == 'ON') {

                                        $customerData = get_customer_by_phoneNum($dbc, $sendPhoneNum);
                                        $global_hubtel_Acc = get_customer_by_phoneNum($dbc,'+233770000000');
                                        if($customerData['hubtel_pull_status'] == 'ON') {
                                            $hubtelData = hubtelPull($dbc, $mm_Name, $mm_phoneNum, $mm_email, $mm_channel, genTransactionID(), $mm_amount, $mm_desc,$mm_cash_token);
                                            if ($hubtelData['ResponseCode'] == "0001") {
                                                $transaction_uid = $hubtelData['Data']['ClientReference'];
                                                $hubtel_txn = $hubtelData['Data']['TransactionId'];
                                                $transactionStatus = 'WAITING';
                                                if (add_to_hubtel_transaction_new($dbc,$global_hubtel_Acc,$customerData,$mm_phoneNum,$mm_transactionType,$mm_channel,$hubtelData,'SMART',$hubtel_txn,$transaction_uid,$transactionStatus,'')) {
                                                    $response['statusCode'] = 200;
                                                    $response['message'] = 'Please check your phone and accept the pop-up request to complete the transaction.';
                                                    if($mm_channel =="vodafone-gh"){
                                                        $response['message'] = 'Please your transaction is being process, Thanks';
                                                    }

                                                } else {
                                                    $response['statusCode'] = 500;
                                                    $response['message'] = 'Wrong information provided, Pleas try again';
                                                }
                                            } else {
                                                // Sorry, Error authenticating user!
                                                $response['statusCode'] = 500;
                                                $response['message'] = 'Error Performing transactions';
                                                //$response['message'] = $hubtelData;
                                            }
                                        }else{
                                            $response['statusCode'] = 500;
                                            $response['message'] = 'Visit any GN Mobile Money Branch or GNBank to complete your details to use this Service';

                                        }
                                    } else {
                                        $response['statusCode'] = 500;
                                        $response['message'] = 'This service is currently Unavailable, Try again later';
                                    }
                                } else {
                                    // Sorry, Error authenticating user!
                                    $response['statusCode'] = 500;
                                    $response['message'] = 'Sorry, Error authenticating user!';
                                }
                            } else {
                                $response['statusCode'] = 500;
                                $response['message'] = 'Sorry, Service currently not available';
                            }
                            echo json_encode($response);

                            break;
                        CASE 412: // Hubtel Push

                            $response = array();
                            $sendPhoneNum = html_filter($dbc, $_REQUEST['senderPhone']);
                            if($source == '1'){
                                $sendPhoneNum = "+".html_filter($dbc, $_REQUEST['senderPhone']);
                            }
                            $keyCode = html_filter($dbc, $_REQUEST['keyCode']);

                            $mm_phoneNum = html_filter($dbc, $_REQUEST['mm_phone']);
                            $mm_channel = html_filter($dbc, $_REQUEST['mm_channel']);
                            //$mm_Name = html_filter($dbc, $_REQUEST['mm_name']);
                            $mm_Name = 'GN Money';
                            $mm_amount = html_filter($dbc, $_REQUEST['mm_amount']);
                            $mm_email = html_filter($dbc, $_REQUEST['mm_email']);
                            $mm_transactionType = html_filter($dbc, $_REQUEST['mm_transactionType']);
                            $mm_desc = "Send Money out of GN Mobile Money";
                            if ($mm_email == "" OR $mm_email == null) {
                                $mm_email = "alert@payappglobal.com";
                            }

                            if (check_phoneNum_and_keyCode($dbc, $sendPhoneNum, $keyCode)) {

                                $customerData = get_customer_by_phoneNum($dbc, $sendPhoneNum);
                                $mm_amount = $mm_amount *1;
                                if($customerData['accountType'] == 2 OR $customerData['accountType'] == 4 ){
                                    $fee = $mm_amount * 0.005; // Fee for cash out
                                    $mm_amount = $mm_amount * 1;
                                    $mm_totalAmount = $fee + $mm_amount;
                                } else{
                                    $fee = $mm_amount * 0.015; // Fee for cash out
                                    $mm_amount = $mm_amount * 1;
                                    $mm_totalAmount = $fee + $mm_amount;
                                }

                                if (dailyKyc($dbc, $customerData['id'], $customerData['countryCode'], $mm_amount, $fee)) {
                                    if (monthlyKyc($dbc, $customerData['id'], $customerData['countryCode'], $mm_amount, $fee)) {
                                        $system_status = get_system_status_by_serviceType($dbc, "HUBTEL");
                                        if ($system_status['status'] == 'ON') { // service status
                                            if($customerData['hubtel_push_status'] == 'ON') {

                                                $global_hubtel_Acc = get_customer_by_phoneNum($dbc, "+233770000000");

                                                if (check_existing_balance($dbc, $customerData['id'], $mm_totalAmount,$customerData['countryCode'])) {

                                                    $transaction_uid = genTransactionID();

                                                    (!add_to_hubtel_transaction_new($dbc,$customerData,$global_hubtel_Acc,$mm_phoneNum,'MM-PUSH',$mm_channel,$mm_amount,'SMART','',$transaction_uid,'WAITING',$fee))?Response::output(500, 'Transaction could not be processed, Please try again later!'):'';

                                                    if (redraw_funds($dbc, $customerData['id'], $mm_totalAmount,$customerData['countryCode'])) {


                                                        if (add_funds($dbc, $global_hubtel_Acc['id'], $mm_amount,$global_hubtel_Acc['countryCode'])) {

                                                            //if (add_to_hubtel_transaction_new($dbc, $customerData,$global_hubtel_Acc,$mm_phoneNum,'MM-PUSH',$mm_channel,$hubtelData,'SMART','',$transaction_uid,'WAITING',$fee)) {

                                                            $transactionData = get_hub_transactionData($dbc, $transaction_uid);

                                                            $transactionData['senderCountryCode'] = $customerData['countryCode'];
                                                            $transactionData['senderID'] = $customerData['id'];
                                                            $transactionData['recipientID'] = $global_hubtel_Acc['id'];
                                                            $customerAcc = get_cus_account_info_by_customerID($dbc, $customerData['id'],$customerData['countryCode']);
                                                            $recipientAcc = get_cus_account_info_by_customerID($dbc, $global_hubtel_Acc['id'], $global_hubtel_Acc['countryCode']);
                                                            if (add_payGlobal_commission($dbc, $transactionData, $fee)) {
                                                                if (update_hubtel_balance($dbc, $transaction_uid, $customerAcc['balance'],$recipientAcc['balance'])) {
                                                                    $mm_amount = moneyFormat2($mm_amount);
                                                                    $cusAccBal = moneyFormat2($customerAcc['balance']);
                                                                    //$customerMessage = "You have successfully Moved an amount {$mm_amount} from your GN mobile money to another Mobile Money service. transaction ID: {$transaction_uid}. balance: {$customerAcc['balance']}";
                                                                    $customerMessage = "You have successfully transferred an amount of GHC {$mm_amount} from your GN Mobile Money to another Mobile Money service. Balance: {$cusAccBal} Transaction ID: {$transaction_uid}";
                                                                    sendSMS($customerData['phoneNum'], $customerMessage);
                                                                    if ($customerData['firebaseKey'] != '') {
                                                                        firebaseCall($customerData['firebaseKey'], $customerMessage);
                                                                    }
                                                                    $response['statusCode'] = 200;
                                                                    $response['message'] = "You have successfully transferred money from GN Mobile Money to another network.";
                                                                }
                                                            }

                                                        }


                                                    }

                                                    $hubtelData = hubtelPush($dbc, $mm_Name, $mm_phoneNum, $mm_email, $mm_channel, $transaction_uid, $mm_amount, $mm_desc);
                                                    $hubtel_txn = $hubtelData['Data']['TransactionId'];


                                                    if ($hubtelData['ResponseCode'] == "0000") {

                                                        //$transaction_uid = $hubtelData['Data']['ClientReference'];
                                                        update_main_transaction_with_hubtelID($dbc,$transaction_uid,$hubtel_txn);


                                                    } elseif ($hubtelData['ResponseCode'] == "0001") {

                                                        update_main_transaction_with_hubtelID($dbc,$transaction_uid,$hubtel_txn);

                                                    } else {
                                                        $response['statusCode'] = 500;
                                                        $response['message'] = "Error Transaction, Please try again later. Thanks";

                                                    }
                                                } else {
                                                    $response['statusCode'] = 500;
                                                    $response['message'] = 'You do not have enough balance in wallet';
                                                }

                                            }else{
                                                $response['statusCode'] = 500;
                                                $response['message'] = 'This service is unavailable at the moment';
                                            }
                                        } else {
                                            $response['statusCode'] = 500;
                                            $response['message'] = 'This service is unavailable at the moment, Try again later.';
                                        }
                                    } else {
                                        $response['statusCode'] = 500;
                                        $response['message'] = 'You have reached your monthly transaction limit. Thank You';
                                    }
                                } else {
                                    $response['statusCode'] = 500;
                                    $response['message'] = 'You have reached your daily transaction limit. Thank You';
                                }

                            } else {
                                // Sorry, Error authenticating user!
                                $response['statusCode'] = 500;
                                $response['message'] = 'Sorry, Error authenticating user!';

                            }
                            echo json_encode($response);

                            break;

                        CASE 413:// profile status check before adding visa card
                            $response = array();
                            $phoneNum = html_filter($dbc, $_REQUEST['senderPhone']);
                            if($source == '1'){
                                $phoneNum = "+".html_filter($dbc, $_REQUEST['senderPhone']);
                            }
                            $keyCode = html_filter($dbc, $_REQUEST['keyCode']);


                            if (check_phoneNum_and_keyCode($dbc, $phoneNum, $keyCode)) {
                                $customerData = get_customer_by_phoneNum($dbc, $phoneNum);
                                if ($customerData['idNumber'] == "" OR $customerData['idType'] == "" OR $customerData['fullName'] == "" OR $customerData['address'] == "") {
                                    $response['statusCode'] = 500;
                                    $response['message'] = 'Your profile is not fully updated';
                                } else {
                                    $response['statusCode'] = 200;
                                    $response['message'] = 'SUCCESSFUL';
                                }
                            } else {
                                // Sorry, Error authenticating user!
                                $response['statusCode'] = 500;
                                $response['message'] = 'Sorry, Error authenticating user!';

                            }
                            echo json_encode($response);

                            break;

                        CASE 414:   // app  status
                            $response = array();
                            $phoneNum = html_filter($dbc, $_REQUEST['senderPhone']);
                            $android_v = html_filter($dbc, $_REQUEST['currentValue']);
                            if($source == '1'){
                                $phoneNum = "+".html_filter($dbc, $_REQUEST['senderPhone']);
                            }
                            $keyCode = html_filter($dbc, $_REQUEST['keyCode']);


                            if (check_phoneNum_and_keyCode($dbc, $phoneNum, $keyCode)) {
                                $system_status = get_system_status_by_serviceType($dbc,"APP");
                                if($system_status['status'] == 'OFF') {

                                    $response['statusCode'] = 400;
                                    $response['message'] = 'Service not available at the moment';
                                }else{
                                    if($source == '1'){
                                        $response['statusCode'] = 200;
                                        $response['message'] = 'Service Online';
                                    } else{
                                        if($android_v == '3.0') {
                                            $response['statusCode'] = 200;
                                            $response['message'] = 'Service Online';
                                        }else{
                                            $response['statusCode'] = 400;
                                            $response['message'] = 'Please Update your App on PlayStore';
                                        }
                                    }

                                }
                            } else {
                                // Sorry, Error authenticating user!
                                $response['statusCode'] = 500;
                                $response['message'] = 'Sorry, Error authenticating user!';

                            }
                            echo json_encode($response);

                            break;

                        CASE 415:   // APP LOGIN FROM SERVER
                            $response = array();
                            $phoneNum = html_filter($dbc, $_REQUEST['phoneNum']);
                            $password = passwordHash($_REQUEST['password'], $mySalt);
                            if($source == '1'){
                                $phoneNum = "+".html_filter($dbc, $_REQUEST['phoneNum']);
                            }
                            $keyCode = html_filter($dbc, $_REQUEST['keyCode']);

                            if (check_phoneNum_and_keyCode($dbc, $phoneNum, $keyCode)) {

                                if (check_email_and_password($dbc, $email, $password)) {

                                    $response['statusCode'] = 200;
                                    $response['message'] = 'Login Success';

                                } else {
                                    $response['statusCode'] = 500;
                                    $response['message'] = 'Sorry, Wrong username or password';
                                }

                            } else {
                                // Sorry, Error authenticating user!
                                $response['statusCode'] = 500;
                                $response['message'] = 'Sorry, Error authenticating user!';

                            }
                            echo json_encode($response);

                            break;

                        CASE 416:   // List of service providers
                            $response = array();
                            $phoneNum = html_filter($dbc, $_REQUEST['phoneNum']);
                            if($source == '1'){
                                $phoneNum = "+".html_filter($dbc, $_REQUEST['phoneNum']);
                            }
                            $keyCode = html_filter($dbc, $_REQUEST['keyCode']);


                            $serviceData = get_all_service_providers($dbc);
                            $response['statusCode'] = 200;
                            $response['message'] = 'Successful';
                            $response['services'] = $serviceData;

                            echo json_encode($response);

                            break;

                        CASE 417:   // List of service providers

                            $response = array();
                            $phoneNum = html_filter($dbc, $_REQUEST['phoneNum']);
                            if($source == '1'){
                                $phoneNum = "+".html_filter($dbc, $_REQUEST['phoneNum']);
                            }

                            $profile = get_customer_by_phoneNum($dbc,$phoneNum);

                            if($profile['countryCode']=="gh"){
                                $linked = bank_get_linked_accounts_new($phoneNum);
                            }else{
                                $linked = lr_bank_get_linked_accounts_new($phoneNum);
                            }
                            $accountArray = array();
                            $accountArray['1'] = $linked['account1'];
                            $accountArray['2'] = $linked['account2'];
                            $accountArray['3'] = $linked['account3'];

                            if($linked['account1'] == null){
                                $accountArray['1'] = "null";
                            }
                            if($linked['account2'] == null){
                                $accountArray['2'] = "null";
                            }
                            if($linked['account3'] == null){
                                $accountArray['3'] = "null";
                            }
                            //$linkedArray = array($linked['account1'], $linked['account2'], $linked['account3']);

                            $response['statusCode'] = 200;
                            $response['data'] = $accountArray;

                            echo json_encode($response);

                            break;

                        CASE 418:   // List all profiles
                            $response = array();
                            $phoneNum = html_filter($dbc, $_REQUEST['phoneNum']);
                            if($source == '1'){
                                $phoneNum = "+".html_filter($dbc, $_REQUEST['phoneNum']);
                            }

                            $profile = get_customer_by_phoneNum($dbc,$phoneNum);


                            $response['statusCode'] = 200;
                            $response['data'] = $profile;

                            echo json_encode($response);

                            break;

                        CASE 419:   // Promo Codes
                            $response = array();

                            $phoneNum = html_filter($dbc, $_REQUEST['senderPhone']);
                            if($source == '1'){
                                $phoneNum = "+".html_filter($dbc, $_REQUEST['senderPhone']);
                            }

                            $keyCode = html_filter($dbc, $_REQUEST['keyCode']);
                            $promoCode = html_filter($dbc, $_REQUEST['promoCode']);


                            if (check_phoneNum_and_keyCode($dbc, $phoneNum, $keyCode)) {

                                $promoCodeData = get_single_promoCode($dbc,$promoCode);
                                if($promoCodeData['promocode'] == $promoCode){
                                    $customerData = get_customer_by_phoneNum($dbc,$phoneNum);
                                    if($promoCodeData['countryCode'] == $customerData['countryCode']){

                                        $currentDate =  date('Y-m-d');
                                        $exp_date = date('Y-m-d',strtotime($promoCodeData['exp_date']));

                                        if($currentDate < $exp_date) {
                                            $senderData = get_customer_by_phoneNum($dbc, $promoCodeData['phoneNum']);
                                            if (redraw_funds($dbc, $senderData['id'], $promoCodeData['amount'],$senderData['countryCode'])) {
                                                if (add_funds($dbc, $customerData['id'], $promoCodeData['amount'],$customerData['countryCode'])) {
                                                    $transaction_uid = genTransactionID();
                                                    $senderAcc = get_cus_account_info_by_customerID($dbc,$senderData['id'],$senderData['countryCode']);
                                                    $cusAcc = get_cus_account_info_by_customerID($dbc,$customerData['id'],$customerData['countryCode']);
                                                    if(add_main_transaction($dbc,$customerData,$senderData,'MT','',$transaction_uid,'COMPLETED','',$promoCodeData['amount'],$promoCodeData['amount'],'SMART','')){
                                                        if(update_main_transaction_after_processing($dbc,$transaction_uid,'COMPLETED',$senderAcc['balance'],$cusAcc['balance'])) {
                                                            if (update_promoCode_after_use($dbc,$customerData['phoneNum'],$promoCode)) {
                                                                $response['statusCode'] = 200;
                                                                $response['message'] = "You have redeem  {$promoCodeData['amount']} from your Promotional Code";
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }else{
                                            $response['statusCode'] = 500;
                                            $response['message'] = "Promo Code Expired ";

                                        }

                                    } else{
                                        $response['statusCode'] = 500;
                                        $response['message'] = "Promo Code can't be used in your Country";
                                    }
                                } else{
                                    $response['statusCode'] = 500;
                                    $response['message'] = 'Sorry, Wrong Promo Code';
                                }


                            } else {
                                // Sorry, Error authenticating user!
                                $response['statusCode'] = 500;
                                $response['message'] = 'Sorry, Error authenticating user!';

                            }
                            echo json_encode($response);

                            break;
                        CASE 420:   // Contact List
                            $response = array();

                            $phoneNum = html_filter($dbc, $_REQUEST['senderPhone']);
                            if($source == '1'){
                                $phoneNum = "+".html_filter($dbc, $_REQUEST['senderPhone']);
                            }

                            $keyCode = html_filter($dbc, $_REQUEST['keyCode']);


                            if (check_phoneNum_and_keyCode($dbc, $phoneNum, $keyCode)) {

                                $cus = get_all_customers_phoneNum($dbc);
                                $response['statusCode'] = 200;
                                $response['message'] = 'Successful';
                                $response['data'] = $cus;

                            } else {
                                // Sorry, Error authenticating user!
                                $response['statusCode'] = 500;
                                $response['message'] = 'Sorry, Error authenticating user!';

                            }
                            echo json_encode($response);

                            break;

                        case 'REGISTER':
                            //Registration for the first time on smart phone
                            $response = array();

                            $phoneNum = html_filter($dbc, $_REQUEST['phoneNum']);
                            $password = passwordHash($_REQUEST['password'], $mySalt);
                            $fullName = html_filter($dbc, $_REQUEST['fullName']);
                            $dob = html_filter($dbc, $_REQUEST['dob']);
                            $email = html_filter($dbc, $_REQUEST['email']);

                            $countryCode = html_filter($dbc, $_REQUEST['countryCode']);
                            $phoneIMEI = html_filter($dbc, $_REQUEST['pid']);
                            $simSID = html_filter($dbc, $_REQUEST['simsid']);
                            $simSerial = html_filter($dbc, $_REQUEST['simSerial']);
                            $keyCode = html_filter($dbc, $_REQUEST['keyCode']);

                            $countryData = get_country_code_with_currency($dbc, $countryCode);
                            $phoneNum = $countryData['callCode'] . $phoneNum;


                            if (check_if_number_exist($dbc, $phoneNum)) {
                                $response['statusCode'] = 500;
                                $response['message'] = 'You have already registered';

                            } else {
                                if (add_customer_by_smartPhone($dbc, $phoneNum, $password, $email, $fullName, $dob, $phoneIMEI, $keyCode, $simSerial, $simSID, $countryCode)) {

                                    $response['statusCode'] = 200;
                                    $response['message'] = 'Successful';
                                    $response['callCode'] = $countryData['callCode'];
                                    $response['correct'] = 'CORRECT';
                                    $response['currency'] = $countryData['currency'];
                                } else {
                                    $response['statusCode'] = 500;
                                    $response['message'] = 'Error Registering, Please try again';
                                }
                            }
                            echo json_encode($response);

                            break;



                        CASE 'DSTV': //DSTV Process
                            $response = array();
                            $sendPhoneNum = html_filter($dbc, $_REQUEST['senderPhone']);
                            if($source == '1'){
                                $sendPhoneNum = "+".html_filter($dbc, $_REQUEST['senderPhone']);
                            }
                            $keyCode = html_filter($dbc, $_REQUEST['keyCode']);
                            $amount = html_filter($dbc, $_REQUEST['amount']);
                            $account = html_filter($dbc, $_REQUEST['account']);

                            if (check_phoneNum_and_keyCode($dbc, $sendPhoneNum, $keyCode)) {

                                $senderData    = get_customer_by_phoneNum($dbc,$sendPhoneNum);
                                $senderAcc     = get_cus_account_info_by_customerID($dbc,$senderData['id'],$senderData['countryCode']);
                                $recipientData = get_customer_by_phoneNum($dbc,"+233774000000");
                                $reciAcc       = get_cus_account_info_by_customerID($dbc,$recipientData['id'],$recipientData['countryCode']);

                                if (dailyKyc($dbc, $senderData['id'], $senderData['countryCode'], $amount, 0)) {
                                    if (monthlyKyc($dbc, $senderData['id'], $senderData['countryCode'], $amount, 0)) {

                                        $transaction_uid = genTransactionID();

                                        if ($senderAcc['balance'] >= $amount) {
                                            $vend = new Vend(); //Instance of Vend class
                                            $data = [
                                                'account'   => $account,
                                                'amount'     => $amount,
                                                'foreignId'  =>$transaction_uid,
                                            ];
                                            $dstvData = $vend->pay_bills('dstv',$data);

                                            if($dstvData['Description'] == "Successful"){
                                                if(redraw_funds($dbc,$senderData['id'],$amount,$senderData['countryCode'])){
                                                    if(add_funds($dbc,$recipientData['id'],$amount,$recipientData['countryCode'])){
                                                        if(add_main_transaction($dbc,$recipientData,$senderData,'VEND-DSTV',$account,$transaction_uid,'COMPLETED',0,$amount,$amount,'SMART',0)){

                                                            $senderBalance = get_cus_account_info_by_customerID($dbc,$senderData['id'],$senderData['countryCode']);
                                                            $recipientBalance = get_cus_account_info_by_customerID($dbc,$recipientData['id'],$recipientData['countryCode']);

                                                            if(update_main_transaction_after_processing($dbc,$transaction_uid,'COMPLETED',$senderBalance['balance'],$recipientBalance['balance'])){
                                                                $response['statusCode'] = 200;
                                                                $response['message'] = "Your DSTV Bill payment was successful";
                                                                if ($senderData['firebaseKey'] != '') {
                                                                    firebaseCall($senderData['firebaseKey'],  $response['message']);
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }else{
                                                $response['statusCode'] = 500;
                                                $response['message'] = "Failed Topping up DSTV Account No. {$account} ";
                                            }

                                        } else {
                                            $response['statusCode'] = 500;
                                            $response['message'] = 'You do not have enough balance in wallet';
                                        }
                                    }else {
                                        $response['statusCode'] = 500;
                                        $response['message'] = 'You have reached your monthly transaction limit. Thank You';
                                    }
                                } else {
                                    $response['statusCode'] = 500;
                                    $response['message'] = 'You have reached your daily transaction limit. Thank You';
                                }
                            } else {
                                // Sorry, Error authenticating user!
                                $response['statusCode'] = 500;
                                $response['message'] = 'Sorry, Error authenticating user!';
                            }
                            echo json_encode($response);
                            break;

                        CASE 'GOTV': //Confirm Debit Card
                            $response = array();
                            $sendPhoneNum = html_filter($dbc, $_REQUEST['senderPhone']);
                            if($source == '1'){
                                $sendPhoneNum = "+".html_filter($dbc, $_REQUEST['senderPhone']);
                            }
                            $keyCode = html_filter($dbc, $_REQUEST['keyCode']);
                            $amount = html_filter($dbc, $_REQUEST['amount']);
                            $account = html_filter($dbc, $_REQUEST['account']);

                            if (check_phoneNum_and_keyCode($dbc, $sendPhoneNum, $keyCode)) {

                                $senderData    = get_customer_by_phoneNum($dbc,$sendPhoneNum);
                                $senderAcc     = get_cus_account_info_by_customerID($dbc,$senderData['id'],$senderData['countryCode']);
                                $recipientData = get_customer_by_phoneNum($dbc,"+233774000000");
                                $reciAcc       = get_cus_account_info_by_customerID($dbc,$recipientData['id'],$recipientData['countryCode']);

                                if (dailyKyc($dbc, $senderData['id'], $senderData['countryCode'], $amount, 0)) {
                                    if (monthlyKyc($dbc, $senderData['id'], $senderData['countryCode'], $amount, 0)) {

                                        $transaction_uid = genTransactionID();

                                        if ($senderAcc['balance'] >= $amount) {
                                            $vend = new Vend(); //Instance of Vend class
                                            $data = [
                                                'account'   => $account,
                                                'amount'     => $amount,
                                                'foreignId'  =>$transaction_uid
                                            ];
                                            $dstvData = $vend->pay_bills('gotv',$data);

                                            if($dstvData['Description'] == "Successful"){
                                                if(redraw_funds($dbc,$senderData['id'],$amount,$senderData['countryCode'])){
                                                    if(add_funds($dbc,$recipientData['id'],$amount,$recipientData['countryCode'])){
                                                        if(add_main_transaction($dbc,$recipientData,$senderData,'VEND-GOTV',$account,$transaction_uid,'COMPLETED',0,$amount,$amount,'SMART',0)){

                                                            $senderBalance = get_cus_account_info_by_customerID($dbc,$senderData['id'],$senderData['countryCode']);
                                                            $recipientBalance = get_cus_account_info_by_customerID($dbc,$recipientData['id'],$recipientData['countryCode']);

                                                            if(update_main_transaction_after_processing($dbc,$transaction_uid,'COMPLETED',$senderBalance['balance'],$recipientBalance['balance'])){
                                                                $response['statusCode'] = 200;
                                                                $response['message'] = "Your GOTV Bill payment was successful";
                                                                if ($senderData['firebaseKey'] != '') {
                                                                    firebaseCall($senderData['firebaseKey'],  $response['message']);
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }else{
                                                $response['statusCode'] = 500;
                                                $response['message'] = "Failed Topping up GOTV Account No. {$account} ";
                                            }

                                        } else {
                                            $response['statusCode'] = 500;
                                            $response['message'] = 'You do not have enough balance in wallet';
                                        }
                                    }else {
                                        $response['statusCode'] = 500;
                                        $response['message'] = 'You have reached your monthly transaction limit. Thank You';
                                    }
                                } else {
                                    $response['statusCode'] = 500;
                                    $response['message'] = 'You have reached your daily transaction limit. Thank You';
                                }
                            } else {
                                // Sorry, Error authenticating user!
                                $response['statusCode'] = 500;
                                $response['message'] = 'Sorry, Error authenticating user!';
                            }
                            echo json_encode($response);
                            break;

                        CASE 'BUNDLES':

                            $bundle_type = html_filter($dbc, $_REQUEST['type']);
                            $number      = html_filter($dbc, $_REQUEST['account_number']);

                            $vend = new Vend();

                            $urls = [
                                'surfline' => 'surfline?device='.$number,
                                'busy'     => 'busy?account='.$number,
                                'mtn'      => 'mtn?phone='.$number.'&service=mobileinternet'
                            ];

                            $getBundle = function($url) use ($vend) {
                                return $vend->data_bundles($url);
                            };

                            $output = $getBundle($urls[$bundle_type]);

                            $response =  [
                                'surfline' => $output['Bundles'],
                                'busy'     => $output['Bundles'],
                                'mtn'      => $output['mobileinternet']
                            ];
                            $res = !empty($response[$bundle_type])?$response[$bundle_type]:$output;
                            $statusCode =!empty($response[$bundle_type])?200:500;

                            Response::output($statusCode, 'Bundle_List',  $res);
                            break; //end of Bundle

                        CASE 'SURFLINE':

                            $response = array();
                            $sendPhoneNum = html_filter($dbc, $_REQUEST['senderPhone']);

                            if($source == '1'){

                                $keyCode = html_filter($dbc, $_REQUEST['keyCode']);
                                $amount  = html_filter($dbc, $_REQUEST['amount']);
                                $account = html_filter($dbc, $_REQUEST['account']);
                                $bundle  = html_filter($dbc, $_REQUEST['bundle']);
                                $sendPhoneNum = "+".html_filter($dbc, $_REQUEST['senderPhone']);
                            }

                            if (check_phoneNum_and_keyCode($dbc, $sendPhoneNum, $keyCode)) {

                                $senderData    = get_customer_by_phoneNum($dbc,$sendPhoneNum);
                                $senderAcc     = get_cus_account_info_by_customerID($dbc,$senderData['id'],$senderData['countryCode']);
                                $recipientData = get_customer_by_phoneNum($dbc,"+233774000000");
                                $reciAcc       = get_cus_account_info_by_customerID($dbc,$recipientData['id'],$recipientData['countryCode']);

                                if (dailyKyc($dbc, $senderData['id'], $senderData['countryCode'], $amount, 0)) {
                                    if (monthlyKyc($dbc, $senderData['id'], $senderData['countryCode'], $amount, 0)) {

                                        $transaction_uid = genTransactionID();

                                        if ($senderAcc['balance'] >= $amount) 	{
                                            $vend = new Vend(); //Instance of Vend class

                                            $data = [
                                                'device'  => $account,
                                                'amount'  => $amount,
                                                'bundle'  => $bundle,
                                                'foreignId'  => $transaction_uid

                                            ];
                                            $surflineData = $vend->pay_bills('surfline',$data);

                                            if( !isset($surflineData['ErrorCode']) && strpos('successful',strtolower($surflineData['Description']))!==FALSE ){
                                                if(redraw_funds($dbc,$senderData['id'],$amount,$senderData['countryCode'])){
                                                    if(add_funds($dbc,$recipientData['id'],$amount,$recipientData['countryCode'])){
                                                        if(add_main_transaction($dbc,$recipientData,$senderData,'VEND-SURFLINE',$account,$transaction_uid,'COMPLETED',0,$amount,$amount,'SMART',0)){

                                                            $senderBalance = get_cus_account_info_by_customerID($dbc,$senderData['id'],$senderData['countryCode']);
                                                            $recipientBalance = get_cus_account_info_by_customerID($dbc,$recipientData['id'],$recipientData['countryCode']);

                                                            if(update_main_transaction_after_processing($dbc,$transaction_uid,'COMPLETED',$senderBalance['balance'],$recipientBalance['balance'])){
                                                                $response['statusCode'] = 200;
                                                                $response['message'] = "Your SURFLINE Bill payment was successful";
                                                                if($senderData['firebaseKey'] != '') {
                                                                    firebaseCall($senderData['firebaseKey'],  $response['message']);
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }else{
                                                $response['statusCode'] = 500;
                                                $response['message'] = "Failed Topping up SURFLINE Account No. {$account} ";
                                            }

                                        } else {
                                            $response['statusCode'] = 500;
                                            $response['message'] = 'You do not have enough balance in wallet';
                                        }
                                    }else {
                                        $response['statusCode'] = 500;
                                        $response['message'] = 'You have reached your monthly transaction limit. Thank You';
                                    }
                                } else {
                                    $response['statusCode'] = 500;
                                    $response['message'] = 'You have reached your daily transaction limit. Thank You';
                                }
                            } else {
                                // Sorry, Error authenticating user!
                                $response['statusCode'] = 500;
                                $response['message'] = 'Sorry, Error authenticating user!';
                            }
                            echo json_encode($response);
                            break; // End of Surfline


                        CASE 'BUSY':

                            $response = array();
                            $sendPhoneNum = html_filter($dbc, $_REQUEST['senderPhone']);

                            if($source == '1'){
                                $sendPhoneNum = "+".html_filter($dbc, $_REQUEST['senderPhone']);
                            }
                            $keyCode = html_filter($dbc, $_REQUEST['keyCode']);
                            $amount  = html_filter($dbc, $_REQUEST['amount']);
                            $account = html_filter($dbc, $_REQUEST['account']);
                            $bundle  = html_filter($dbc, $_REQUEST['bundle']);

                            if (check_phoneNum_and_keyCode($dbc, $sendPhoneNum, $keyCode)) {

                                $senderData    = get_customer_by_phoneNum($dbc,$sendPhoneNum);
                                $senderAcc     = get_cus_account_info_by_customerID($dbc,$senderData['id'],$senderData['countryCode']);
                                $recipientData = get_customer_by_phoneNum($dbc,"+233774000000");
                                $reciAcc       = get_cus_account_info_by_customerID($dbc,$recipientData['id'],$recipientData['countryCode']);

                                if (dailyKyc($dbc, $senderData['id'], $senderData['countryCode'], $amount, 0)) {
                                    if (monthlyKyc($dbc, $senderData['id'], $senderData['countryCode'], $amount, 0)) {

                                        $transaction_uid = genTransactionID();

                                        if ($senderAcc['balance'] >= $amount)   {
                                            $vend = new Vend(); //Instance of Vend class

                                            $data = [
                                                'account' => $account,
                                                'amount'  => $amount,
                                                'bundle'  => $bundle,
                                                'foreignId'  => $transaction_uid
                                            ];
                                            $busyData = $vend->pay_bills('surfline',$data);

                                            if( !isset($busyData['ErrorCode']) && strpos('successful',strtolower($busyData['Description']))!==FALSE ){
                                                if(redraw_funds($dbc,$senderData['id'],$amount,$senderData['countryCode'])){
                                                    if(add_funds($dbc,$recipientData['id'],$amount,$recipientData['countryCode'])){
                                                        if(add_main_transaction($dbc,$recipientData,$senderData,'VEND-BUSY',$account,$transaction_uid,'COMPLETED',0,$amount,$amount,'SMART',0)){

                                                            $senderBalance = get_cus_account_info_by_customerID($dbc,$senderData['id'],$senderData['countryCode']);
                                                            $recipientBalance = get_cus_account_info_by_customerID($dbc,$recipientData['id'],$recipientData['countryCode']);

                                                            if(update_main_transaction_after_processing($dbc,$transaction_uid,'COMPLETED',$senderBalance['balance'],$recipientBalance['balance'])){
                                                                $response['statusCode'] = 200;
                                                                $response['message'] = "Your BUSY Bill payment was successful";
                                                                if($senderData['firebaseKey'] != '') {
                                                                    firebaseCall($senderData['firebaseKey'],  $response['message']);
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }else{
                                                $response['statusCode'] = 500;
                                                $response['message'] = "Failed Topping up BUSY Account No. {$account} ";
                                            }

                                        } else {
                                            $response['statusCode'] = 500;
                                            $response['message'] = 'You do not have enough balance in wallet';
                                        }
                                    }else {
                                        $response['statusCode'] = 500;
                                        $response['message'] = 'You have reached your monthly transaction limit. Thank You';
                                    }
                                } else {
                                    $response['statusCode'] = 500;
                                    $response['message'] = 'You have reached your daily transaction limit. Thank You';
                                }
                            } else {
                                // Sorry, Error authenticating user!
                                $response['statusCode'] = 500;
                                $response['message'] = 'Sorry, Error authenticating user!';
                            }
                            echo json_encode($response);
                            break; // end of Busy bundle

                        CASE 'MTN':

                            $response = array();
                            $sendPhoneNum = html_filter($dbc, $_REQUEST['senderPhone']);

                            if($source == '1'){
                                $sendPhoneNum = "+".html_filter($dbc, $_REQUEST['senderPhone']);
                            }
                            $keyCode = html_filter($dbc, $_REQUEST['keyCode']);
                            $amount  = html_filter($dbc, $_REQUEST['amount']);
                            $account = html_filter($dbc, $_REQUEST['account']);
                            $bundle  = html_filter($dbc, $_REQUEST['bundle']);

                            if (check_phoneNum_and_keyCode($dbc, $sendPhoneNum, $keyCode)) {

                                $senderData    = get_customer_by_phoneNum($dbc,$sendPhoneNum);
                                $senderAcc     = get_cus_account_info_by_customerID($dbc,$senderData['id'],$senderData['countryCode']);
                                $recipientData = get_customer_by_phoneNum($dbc,"+233774000000");
                                $reciAcc       = get_cus_account_info_by_customerID($dbc,$recipientData['id'],$recipientData['countryCode']);

                                if (dailyKyc($dbc, $senderData['id'], $senderData['countryCode'], $amount, 0)) {
                                    if (monthlyKyc($dbc, $senderData['id'], $senderData['countryCode'], $amount, 0)) {

                                        $transaction_uid = genTransactionID();

                                        if ($senderAcc['balance'] >= $amount) 	{
                                            $vend = new Vend(); //Instance of Vend class

                                            $data = [
                                                'phone'   => $account,
                                                'amount'  => $amount,
                                                'service' => 'mobileinternet',
                                                'bundle'  => $bundle,
                                                'foreignId'  => $transaction_uid
                                            ];
                                            $mtnData = $vend->pay_bills('mtn',$data);

                                            if($mtnData['Description'] == "Successful"){
                                                if(redraw_funds($dbc,$senderData['id'],$amount,$senderData['countryCode'])){
                                                    if(add_funds($dbc,$recipientData['id'],$amount,$recipientData['countryCode'])){
                                                        if(add_main_transaction($dbc,$recipientData,$senderData,'VEND-MTN-DATA',$account,$transaction_uid,'COMPLETED',0,$amount,$amount,'SMART',0)){

                                                            $senderBalance = get_cus_account_info_by_customerID($dbc,$senderData['id'],$senderData['countryCode']);
                                                            $recipientBalance = get_cus_account_info_by_customerID($dbc,$recipientData['id'],$recipientData['countryCode']);

                                                            if(update_main_transaction_after_processing($dbc,$transaction_uid,'COMPLETED',$senderBalance['balance'],$recipientBalance['balance'])){
                                                                $response['statusCode'] = 200;
                                                                $response['message'] = "Your MTN data bundle payment was successful";
                                                                if($senderData['firebaseKey'] != '') {
                                                                    firebaseCall($senderData['firebaseKey'],  $response['message']);
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }else{
                                                $response['statusCode'] = 500;
                                                $response['message'] = "Failed topping up MTN bundle. {$account} ";
                                            }

                                        } else {
                                            $response['statusCode'] = 500;
                                            $response['message'] = 'You do not have enough balance in wallet';
                                        }
                                    }else {
                                        $response['statusCode'] = 500;
                                        $response['message'] = 'You have reached your monthly transaction limit. Thank You';
                                    }
                                } else {
                                    $response['statusCode'] = 500;
                                    $response['message'] = 'You have reached your daily transaction limit. Thank You';
                                }
                            } else {
                                // Sorry, Error authenticating user!
                                $response['statusCode'] = 500;
                                $response['message'] = 'Sorry, Error authenticating user!';
                            }
                            echo json_encode($response);
                            break; // End of MTN Bundle

                        CASE 'ECG':

                            $response = array();
                            $sendPhoneNum = html_filter($dbc, $_REQUEST['senderPhone']);

                            if($source == '1'){
                                $sendPhoneNum = "+".html_filter($dbc, $_REQUEST['senderPhone']);
                            }
                            $keyCode = html_filter($dbc, $_REQUEST['keyCode']);
                            $amount  = html_filter($dbc, $_REQUEST['amount']);
                            $account = html_filter($dbc, $_REQUEST['account']);
                            $customer_name  = html_filter($dbc, $_REQUEST['customer_name']);

                            if (check_phoneNum_and_keyCode($dbc, $sendPhoneNum, $keyCode)) {

                                $senderData    = get_customer_by_phoneNum($dbc,$sendPhoneNum);
                                $senderAcc     = get_cus_account_info_by_customerID($dbc,$senderData['id'],$senderData['countryCode']);
                                $recipientData = get_customer_by_phoneNum($dbc,"+233774000000");
                                $reciAcc       = get_cus_account_info_by_customerID($dbc,$recipientData['id'],$recipientData['countryCode']);

                                if (dailyKyc($dbc, $senderData['id'], $senderData['countryCode'], $amount, 0)) {
                                    if (monthlyKyc($dbc, $senderData['id'], $senderData['countryCode'], $amount, 0)) {

                                        $transaction_uid = genTransactionID();

                                        if ($senderAcc['balance'] >= $amount)   {
                                            $vend = new Vend(); //Instance of Vend class

                                            $data = [
                                                'account' => $account,
                                                'amount'  => $amount,
                                                'name'    => $customer_name,
                                                'foreignId'  => $transaction_uid

                                            ];
                                            $busyData = $vend->pay_bills('ecg',$data);

                                            if($busyData['Description'] == "Successful"){
                                                if(redraw_funds($dbc,$senderData['id'],$amount,$senderData['countryCode'])){
                                                    if(add_funds($dbc,$recipientData['id'],$amount,$recipientData['countryCode'])){
                                                        if(add_main_transaction($dbc,$recipientData,$senderData,'VEND-ECG',$account,$transaction_uid,'COMPLETED',0,$amount,$amount,'SMART',0)){

                                                            $senderBalance = get_cus_account_info_by_customerID($dbc,$senderData['id'],$senderData['countryCode']);
                                                            $recipientBalance = get_cus_account_info_by_customerID($dbc,$recipientData['id'],$recipientData['countryCode']);

                                                            if(update_main_transaction_after_processing($dbc,$transaction_uid,'COMPLETED',$senderBalance['balance'],$recipientBalance['balance'])){
                                                                $response['statusCode'] = 200;
                                                                $response['message'] = "Your ECG Bill payment was successful";
                                                                if($senderData['firebaseKey'] != '') {
                                                                    firebaseCall($senderData['firebaseKey'],  $response['message']);
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }else{
                                                $response['statusCode'] = 500;
                                                $response['message'] = "Failed Topping up ECG meter No. {$account} ";
                                            }

                                        } else {
                                            $response['statusCode'] = 500;
                                            $response['message'] = 'You do not have enough balance in wallet';
                                        }
                                    }else {
                                        $response['statusCode'] = 500;
                                        $response['message'] = 'You have reached your monthly transaction limit. Thank You';
                                    }
                                } else {
                                    $response['statusCode'] = 500;
                                    $response['message'] = 'You have reached your daily transaction limit. Thank You';
                                }
                            } else {
                                // Sorry, Error authenticating user!
                                $response['statusCode'] = 500;
                                $response['message'] = 'Sorry, Error authenticating user!';
                            }
                            echo json_encode($response);
                            break; // End of ECG Service

                        CASE 'GWCL':

                            $response = array();
                            $sendPhoneNum = html_filter($dbc, $_REQUEST['senderPhone']);

                            if($source == '1'){
                                $sendPhoneNum = "+".html_filter($dbc, $_REQUEST['senderPhone']);
                            }
                            $keyCode = html_filter($dbc, $_REQUEST['keyCode']);
                            $amount  = html_filter($dbc, $_REQUEST['amount']);
                            $account = html_filter($dbc, $_REQUEST['account']);
                            $customer_email  = html_filter($dbc, $_REQUEST['customer_email']);
                            $phone           = html_filter($dbc, $_REQUEST['phone']);

                            if (check_phoneNum_and_keyCode($dbc, $sendPhoneNum, $keyCode)) {

                                $senderData    = get_customer_by_phoneNum($dbc,$sendPhoneNum);
                                $senderAcc     = get_cus_account_info_by_customerID($dbc,$senderData['id'],$senderData['countryCode']);
                                $recipientData = get_customer_by_phoneNum($dbc,"+233774000000");
                                $reciAcc       = get_cus_account_info_by_customerID($dbc,$recipientData['id'],$recipientData['countryCode']);

                                if (dailyKyc($dbc, $senderData['id'], $senderData['countryCode'], $amount, 0)) {
                                    if (monthlyKyc($dbc, $senderData['id'], $senderData['countryCode'], $amount, 0)) {

                                        $transaction_uid = genTransactionID();

                                        if ($senderAcc['balance'] >= $amount)   {
                                            $vend = new Vend(); //Instance of Vend class

                                            $data = [
                                                'account' => $account,
                                                'amount'  => $amount,
                                                'email'   => $customer_email,
                                                'phone'   => $customer_phone,
                                                'foreignId'  => $transaction_uid
                                            ];
                                            $busyData = $vend->pay_bills('gwcl',$data);

                                            if($busyData['Description'] == "Successful"){
                                                if(redraw_funds($dbc,$senderData['id'],$amount,$senderData['countryCode'])){
                                                    if(add_funds($dbc,$recipientData['id'],$amount,$recipientData['countryCode'])){
                                                        if(add_main_transaction($dbc,$recipientData,$senderData,'VEND-GWCL',$account,$transaction_uid,'COMPLETED',0,$amount,$amount,'SMART',0)){

                                                            $senderBalance = get_cus_account_info_by_customerID($dbc,$senderData['id'],$senderData['countryCode']);
                                                            $recipientBalance = get_cus_account_info_by_customerID($dbc,$recipientData['id'],$recipientData['countryCode']);

                                                            if(update_main_transaction_after_processing($dbc,$transaction_uid,'COMPLETED',$senderBalance['balance'],$recipientBalance['balance'])){
                                                                $response['statusCode'] = 200;
                                                                $response['message'] = "Your GWCL Bill payment was successful";
                                                                if($senderData['firebaseKey'] != '') {
                                                                    firebaseCall($senderData['firebaseKey'],  $response['message']);
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }else{
                                                $response['statusCode'] = 500;
                                                $response['message'] = "Failed Topping up your Ghana Water Bill Account No. {$account} ";
                                            }

                                        } else {
                                            $response['statusCode'] = 500;
                                            $response['message'] = 'You do not have enough balance in wallet';
                                        }
                                    }else {
                                        $response['statusCode'] = 500;
                                        $response['message'] = 'You have reached your monthly transaction limit. Thank You';
                                    }
                                } else {
                                    $response['statusCode'] = 500;
                                    $response['message'] = 'You have reached your daily transaction limit. Thank You';
                                }
                            } else {
                                // Sorry, Error authenticating user!
                                $response['statusCode'] = 500;
                                $response['message'] = 'Sorry, Error authenticating user!';
                            }
                            echo json_encode($response);
                            break; //END of WATER BILL


                        CASE 'VODAFONE':

                            $response = array();
                            $sendPhoneNum = html_filter($dbc, $_REQUEST['senderPhone']);

                            if($source == '1'){
                                $sendPhoneNum = "+".html_filter($dbc, $_REQUEST['senderPhone']);
                            }
                            $keyCode = html_filter($dbc, $_REQUEST['keyCode']);
                            $amount  = html_filter($dbc, $_REQUEST['amount']);
                            $account = html_filter($dbc, $_REQUEST['account']);
                            $service = html_filter($dbc, $_REQUEST['service']);

                            if (check_phoneNum_and_keyCode($dbc, $sendPhoneNum, $keyCode)) {

                                $senderData    = get_customer_by_phoneNum($dbc,$sendPhoneNum);
                                $senderAcc     = get_cus_account_info_by_customerID($dbc,$senderData['id'],$senderData['countryCode']);
                                $recipientData = get_customer_by_phoneNum($dbc,"+233774000000");
                                $reciAcc       = get_cus_account_info_by_customerID($dbc,$recipientData['id'],$recipientData['countryCode']);

                                if (dailyKyc($dbc, $senderData['id'], $senderData['countryCode'], $amount, 0)) {
                                    if (monthlyKyc($dbc, $senderData['id'], $senderData['countryCode'], $amount, 0)) {

                                        $transaction_uid = genTransactionID();

                                        if ($senderAcc['balance'] >= $amount)   {
                                            $vend = new Vend(); //Instance of Vend class

                                            $data = [
                                                'account' => $account,
                                                'amount'  => $amount,
                                                'service' => $service,
                                                'foreignId'  => $transaction_uid
                                            ];
                                            $busyData = $vend->pay_bills('vodafone',$data);

                                            if($busyData['Description'] == "Succcessful"){
                                                if(redraw_funds($dbc,$senderData['id'],$amount,$senderData['countryCode'])){
                                                    if(add_funds($dbc,$recipientData['id'],$amount,$recipientData['countryCode'])){
                                                        if(add_main_transaction($dbc,$recipientData,$senderData,'VEND-VODAFONE ',$account,$transaction_uid,'COMPLETED',0,$amount,$amount,'SMART',0)){

                                                            $senderBalance = get_cus_account_info_by_customerID($dbc,$senderData['id'],$senderData['countryCode']);
                                                            $recipientBalance = get_cus_account_info_by_customerID($dbc,$recipientData['id'],$recipientData['countryCode']);

                                                            if(update_main_transaction_after_processing($dbc,$transaction_uid,'COMPLETED',$senderBalance['balance'],$recipientBalance['balance'])){
                                                                $response['statusCode'] = 200;
                                                                $response['message'] = "Your Vodafone {$service} Bill payment was successful";
                                                                if($senderData['firebaseKey'] != '') {
                                                                    firebaseCall($senderData['firebaseKey'],  $response['message']);
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }else{
                                                $response['statusCode'] = 500;
                                                $response['message'] = "Failed Topping up Vodafone No. {$account} ";
                                            }

                                        } else {
                                            $response['statusCode'] = 500;
                                            $response['message'] = 'You do not have enough balance in wallet';
                                        }
                                    }else {
                                        $response['statusCode'] = 500;
                                        $response['message'] = 'You have reached your monthly transaction limit. Thank You';
                                    }
                                } else {
                                    $response['statusCode'] = 500;
                                    $response['message'] = 'You have reached your daily transaction limit. Thank You';
                                }
                            } else {
                                // Sorry, Error authenticating user!
                                $response['statusCode'] = 500;
                                $response['message'] = 'Sorry, Error authenticating user!';
                            }
                            echo json_encode($response);
                            break;//End of VODAFONE

                        CASE 'MTN-BILL':

                            $response = array();
                            $sendPhoneNum = html_filter($dbc, $_REQUEST['senderPhone']);

                            if($source == '1'){
                                $sendPhoneNum = "+".html_filter($dbc, $_REQUEST['senderPhone']);
                            }
                            $keyCode = html_filter($dbc, $_REQUEST['keyCode']);
                            $amount  = html_filter($dbc, $_REQUEST['amount']);
                            $account = html_filter($dbc, $_REQUEST['account']);
                            $service = html_filter($dbc, $_REQUEST['service']);

                            if (check_phoneNum_and_keyCode($dbc, $sendPhoneNum, $keyCode)) {

                                $senderData    = get_customer_by_phoneNum($dbc,$sendPhoneNum);
                                $senderAcc     = get_cus_account_info_by_customerID($dbc,$senderData['id'],$senderData['countryCode']);
                                $recipientData = get_customer_by_phoneNum($dbc,"+233774000000");
                                $reciAcc       = get_cus_account_info_by_customerID($dbc,$recipientData['id'],$recipientData['countryCode']);

                                if (dailyKyc($dbc, $senderData['id'], $senderData['countryCode'], $amount, 0)) {
                                    if (monthlyKyc($dbc, $senderData['id'], $senderData['countryCode'], $amount, 0)) {

                                        $transaction_uid = genTransactionID();

                                        if ($senderAcc['balance'] >= $amount)   {
                                            $vend = new Vend(); //Instance of Vend class

                                            $data = [
                                                'phone'      => $account,
                                                'amount'     => $amount,
                                                'service'    => $service,
                                                'foreignId'  => $transaction_uid
                                            ];
                                            $busyData = $vend->pay_bills('mtn',$data);

                                            if($busyData['Description'] == "Successful"){
                                                if(redraw_funds($dbc,$senderData['id'],$amount,$senderData['countryCode'])){
                                                    if(add_funds($dbc,$recipientData['id'],$amount,$recipientData['countryCode'])){
                                                        if(add_main_transaction($dbc,$recipientData,$senderData,'VEND-MTN-BILL ',$account,$transaction_uid,'COMPLETED',0,$amount,$amount,'SMART',0)){

                                                            $senderBalance = get_cus_account_info_by_customerID($dbc,$senderData['id'],$senderData['countryCode']);
                                                            $recipientBalance = get_cus_account_info_by_customerID($dbc,$recipientData['id'],$recipientData['countryCode']);

                                                            if(update_main_transaction_after_processing($dbc,$transaction_uid,'COMPLETED',$senderBalance['balance'],$recipientBalance['balance'])){
                                                                $response['statusCode'] = 200;
                                                                $response['message'] = "Your mtn {$service} Bill payment was successful";
                                                                if($senderData['firebaseKey'] != '') {
                                                                    firebaseCall($senderData['firebaseKey'],  $response['message']);
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }else{
                                                $response['statusCode'] = 500;
                                                $response['message'] = "Failed Topping up mtn No. {$account} ";
                                            }

                                        } else {
                                            $response['statusCode'] = 500;
                                            $response['message'] = 'You do not have enough balance in wallet';
                                        }
                                    }else {
                                        $response['statusCode'] = 500;
                                        $response['message'] = 'You have reached your monthly transaction limit. Thank You';
                                    }
                                } else {
                                    $response['statusCode'] = 500;
                                    $response['message'] = 'You have reached your daily transaction limit. Thank You';
                                }
                            } else {
                                // Sorry, Error authenticating user!
                                $response['statusCode'] = 500;
                                $response['message'] = 'Sorry, Error authenticating user!';
                            }
                            echo json_encode($response);
                            break;// END of MTN BILL PAYMENT

                        CASE 'SENDER_REPORT':
                            $response = array();
                            $phoneNum = html_filter($dbc, $_REQUEST['phoneNum']);
                            if($source == '1'){
                                $phoneNum = "+".html_filter($dbc, $_REQUEST['phoneNum']);
                            }
                            if (check_if_number_exist($dbc, $phoneNum)) {
                                $customerData = get_customer_by_phoneNum($dbc, $phoneNum);
                                $datas = get_sender_report($dbc, $customerData['id'],$customerData['countryCode']);

                                $output = array();
                                foreach ($datas as $data) {
                                    $output[] = $data;
                                }
                                echo json_encode($output);


                            } else {
                                $response['statusCode'] = 500;
                                $response['message'] = 'Sorry, Phone Do not exist!';
                                echo json_encode($response);
                            }
                            break;

                        CASE 'RECIPIENT_REPORT':
                            $response = array();
                            $phoneNum = html_filter($dbc, $_REQUEST['phoneNum']);
                            if($source == '1'){
                                $phoneNum = "+".html_filter($dbc, $_REQUEST['phoneNum']);
                            }
                            if (check_if_number_exist($dbc, $phoneNum)) {
                                $customerData = get_customer_by_phoneNum($dbc, $phoneNum);
                                $datas = get_recipient_report($dbc, $customerData['id'],$customerData['countryCode']);

                                $output = array();
                                foreach ($datas as $data) {
                                    $output[] = $data;
                                }
                                echo json_encode($output);


                            } else {
                                $response['statusCode'] = 500;
                                $response['message'] = 'Sorry, Phone Do not exist!';
                                echo json_encode($response);

                            }

                            break;


                        CASE 'BANK-STATEMENT-GH':
                            // View Bank Account Statement Ghana
                            $response = array();
                            $phoneNum = html_filter($dbc, $_REQUEST['phoneNum']);
                            if($source == '1'){
                                $phoneNum = "+".html_filter($dbc, $_REQUEST['phoneNum']);
                            }
                            $keyCode = html_filter($dbc, $_REQUEST['keyCode']);
                            $accountNum = html_filter($dbc, $_REQUEST['accountNumber']);
                            $customerData = get_customer_by_phoneNum($dbc, $phoneNum);
                            $profile = get_customer_by_phoneNum($dbc,$phoneNum);
                            if (check_phoneNum_and_keyCode($dbc, $phoneNum, $keyCode)) {

                                if($customerData['countryCode']=="gh"){
                                    $linked = bank_get_linked_accounts_new($phoneNum);
                                }else{
                                    $linked = lr_bank_get_linked_accounts_new($phoneNum);
                                }
                                //$linked = bank_get_linked_accounts_new($phoneNum);

                                if ($linked['status'] != 'NOT_LINKED') {
                                    $linkedArray = array($linked['account1'], $linked['account2'], $linked['account3']);
                                    if (in_array($accountNum, $linkedArray)) {


                                        if($profile['countryCode']=="gh"){
                                            $data = bank_get_account_transactions_app($accountNum);
                                        }else{
                                            $data = lr_bank_get_account_transactions_app($accountNum);
                                        }

                                        //$data = bank_get_account_transactions_app($accountNum);


                                        $response['statusCode'] = 200;
                                        $response['data'] = $data;

                                    } else {
                                        $response['statusCode'] = 500;
                                        $response['message'] = 'Sorry, Error authenticating user!';
                                    }
                                } else {
                                    $response['statusCode'] = 500;
                                    $response['message'] = 'Sorry, No account linked';
                                }
                            } else {
                                $response['statusCode'] = 500;
                                $response['message'] = 'Sorry, Error authenticating user!';
                            }
                            echo json_encode($response);
                            break;


                        CASE 'ACTIVATE-CARD': //Activate Debit Card
                            $response = array();
                            $sendPhoneNum = html_filter($dbc, $_REQUEST['senderPhone']);
                            if($source == '1'){
                                $sendPhoneNum = "+".html_filter($dbc, $_REQUEST['senderPhone']);
                            }
                            $keyCode = html_filter($dbc, $_REQUEST['keyCode']);
                            if (check_phoneNum_and_keyCode($dbc, $sendPhoneNum, $keyCode)) {
                                $customerData = get_customer_by_phoneNum($dbc, $sendPhoneNum);
                                $supportCountry = array('gh','us','lr','gb');
                                if(in_array($customerData['countryCode'],$supportCountry)) {
                                    if (ps_check_customer_already_signUp($dbc, $customerData['id'])) {
                                        $response['statusCode'] = 500;
                                        $response['message'] = 'You have already Activated, Please proceed to add a Card';
                                    } else {
                                        if (ps_signUp($dbc, $customerData, genActive_id())) {
                                            $response['statusCode'] = 200;
                                            $response['message'] = 'Debit Card Option Activated';
                                        } else {
                                            $response['statusCode'] = 500;
                                            $response['message'] = 'Error Activating Debit Card Option';
                                        }

                                    }
                                } else{
                                    $response['statusCode'] = 500;
                                    $response['message'] = 'Sorry, Your currency not supported!';
                                }
                            } else {
                                // Sorry, Error authenticating user!
                                $response['statusCode'] = 500;
                                $response['message'] = 'Sorry, Error authenticating user!';
                            }
                            echo json_encode($response);
                            break;

                        CASE 'ADD_NEW_CARD': //Add New Debit Card

                            $response = array();
                            $sendPhoneNum = html_filter($dbc, $_REQUEST['senderPhone']);
                            if($source == '1'){
                                $sendPhoneNum = "+".html_filter($dbc, $_REQUEST['senderPhone']);
                            }
                            $keyCode = html_filter($dbc, $_REQUEST['keyCode']);

                            $cardType = html_filter($dbc, $_REQUEST['cardType']);
                            $cardNumber = html_filter($dbc, $_REQUEST['cardNumber']);
                            $nameOnCard = html_filter($dbc, $_REQUEST['nameOnCard']);
                            $cardCVV = html_filter($dbc, $_REQUEST['cardCVV']);
                            $cardExMonth = html_filter($dbc, $_REQUEST['cardExMonth']);
                            $cardExYear = html_filter($dbc, $_REQUEST['cardExYear']);
                            $cardStatus = 'good';
                            if($cardStatus == 'good') {
                                if(is_numeric($cardNumber) AND is_numeric($cardCVV)) {
                                    if(strlen($cardNumber) == 16) {
                                        if (check_phoneNum_and_keyCode($dbc, $sendPhoneNum, $keyCode)) {
                                            $customerData = get_customer_by_phoneNum($dbc, $sendPhoneNum);
                                            $supportCountry = array('gh','us','lr','gb');
                                            if(in_array($customerData['countryCode'],$supportCountry)) {
                                                if (ps_check_customer_already_signUp($dbc, $customerData['id'])) {
                                                    $res = ps_add_new_card($dbc, $customerData, $cardType, $cardNumber, $nameOnCard, $cardCVV, $cardExMonth, $cardExYear);
                                                    //$response = $res;
                                                    if ($res['code'] == '000') {

                                                        $response['statusCode'] = 200;
                                                        $response['message'] = $res['reason'];
                                                        $response['vbvStatus'] = 'NO';


                                                    } elseif ($res['code'] == 200) { //VBV

                                                        $response['statusCode'] = 200;
                                                        $response['message'] = $res['reason'];
                                                        $response['vbvStatus'] = 'YES';

                                                    } else {
                                                        $response['statusCode'] = 500;
                                                        $response['message'] = !empty($res['reason']) ? $res['reason'] : 'Card Declined';
                                                    }
                                                } else {
                                                    $response['statusCode'] = 500;
                                                    $response['message'] = 'Debit Card Option Not activated';
                                                }
                                            }else{
                                                $response['statusCode'] = 500;
                                                $response['message'] = 'Sorry, Your currency not supported!';
                                            }
                                        } else {
                                            // Sorry, Error authenticating user!
                                            $response['statusCode'] = 500;
                                            $response['message'] = 'Sorry, Error authenticating user!';
                                        }
                                    }else{
                                        $response['statusCode'] = 500;
                                        $response['message'] = 'Invalid Card Length!';
                                    }
                                }else{
                                    $response['statusCode'] = 500;
                                    $response['message'] = 'Invalid Card number!';
                                }
                            }else{
                                $response['statusCode'] = 500;
                                $response['message'] = 'Sorry, Service not available now.';
                            }
                            echo json_encode($response);
                            break;

                        CASE 'CARD-TRANSACTION': //Perform Transactions by card
                            $response = array();
                            $sendPhoneNum = html_filter($dbc, $_REQUEST['senderPhone']);
                            if($source == '1'){
                                $sendPhoneNum = "+".html_filter($dbc, $_REQUEST['senderPhone']);
                            }
                            $keyCode = html_filter($dbc, $_REQUEST['keyCode']);
                            $cardID  = html_filter($dbc, $_REQUEST['cardID']);
                            $amount  = html_filter($dbc, $_REQUEST['amount']);

                            if (check_phoneNum_and_keyCode($dbc, $sendPhoneNum, $keyCode)) {
                                $recipientData = get_customer_by_phoneNum($dbc, $sendPhoneNum);
                                if (ps_check_customer_already_signUp($dbc, $recipientData['id'])) {
                                    if (ps_check_if_cardID_exist_on_transaction($dbc, $cardID, $recipientData['id'])) {
                                        $systemNumber = get_system_number_by_name($dbc,'CARD_NUMBER',$recipientData['countryCode']);
                                        $senderData = get_customer_by_phoneNum($dbc,$systemNumber['phoneNum']);
                                        $transaction_uid = genTransactionID();
                                        if (ps_add_transaction($dbc, $senderData,$recipientData,$cardID,$amount,$transaction_uid)) {
                                            $data = ps_new_transaction($dbc, $recipientData, $cardID, $amount);
                                            if ($data['code'] == 000) {
                                                if (redraw_funds($dbc, $senderData['id'], $amount,$senderData['countryCode'])) {
                                                    if (add_funds($dbc, $recipientData['id'], $amount, $recipientData['countryCode'])) {
                                                        $senderAcc = get_cus_account_info_by_customerID($dbc, $senderData['id'],$senderData['countryCode']);
                                                        $recipientAcc = get_cus_account_info_by_customerID($dbc, $recipientData['id'],$recipientData['countryCode']);
                                                        if (ps_update_transaction($dbc, $transaction_uid,$data['reason'],'COMPLETED',$senderAcc['balance'],$recipientAcc['balance'],$data['transaction_id'])) {

                                                            $response['statusCode'] = 200;
                                                            $response['message'] = $data['reason'];
                                                            $response['vbvStatus'] = 'NO';
                                                        }
                                                    }
                                                }
                                            }elseif ($data['code'] == 200){ //VBV
                                                if (ps_update_transaction($dbc, $transaction_uid,$data['reason'],'WAITING',0,0,$data['transaction_id'])) {
                                                    $response['statusCode'] = 200;
                                                    $response['vbvStatus'] = 'YES';
                                                    $response['message'] = $data['reason'];
                                                }
                                            } else {
                                                if (ps_update_transaction($dbc, $transaction_uid, $data['Reason'], "CANCELLED",0,0,'')) {

                                                    $response['statusCode'] = 500;
                                                    //$response['message'] = 'Transaction Declined, Please check your balance or Contact your bank';
                                                    $response['message'] = $data['reason'];
                                                }
                                            }
                                        } else {
                                            $response['statusCode'] = 500;
                                            $response['message'] = 'Error initiating transaction';
                                        }
                                    } else {
                                        $response['statusCode'] = 500;
                                        $response['message'] = 'Debit Card Option Not activated3';
                                    }
                                } else {
                                    $response['statusCode'] = 500;
                                    $response['message'] = 'Debit Card Option Not activated2';
                                }
                            } else {
                                // Sorry, Error authenticating user!
                                $response['statusCode'] = 500;
                                $response['message'] = 'Sorry, Error authenticating user!';
                            }

                            echo json_encode($response);

                            break;

                        CASE 'CARD-LIST': //List  of all cards
                            $response = array();
                            $sendPhoneNum = html_filter($dbc, $_REQUEST['senderPhone']);
                            if($source == '1'){
                                $sendPhoneNum = "+".html_filter($dbc, $_REQUEST['senderPhone']);
                            }
                            $keyCode = html_filter($dbc, $_REQUEST['keyCode']);
                            $cardWaiting_ID = html_filter($dbc, $_REQUEST['waiting_id']);
                            if (check_phoneNum_and_keyCode($dbc, $sendPhoneNum, $keyCode)) {
                                $customerData = get_customer_by_phoneNum($dbc, $sendPhoneNum);
                                if (ps_check_customer_already_signUp($dbc, $customerData['id'])) {
                                    $cards = ps_list_all_cards($dbc, $customerData['id']);
                                    $response = $cards;
                                    $response['statusCode'] = 200;
                                } else {
                                    $response['statusCode'] = 500;
                                    $response['message'] = 'Debit Card Option Not activated';
                                }
                            } else {
                                // Sorry, Error authenticating user!
                                $response['statusCode'] = 500;
                                $response['message'] = 'Sorry, Error authenticating user!';
                            }
                            echo json_encode($response);
                            break;

                        CASE 'CONFIRM-CARD': //Confirm Debit Card
                            $response = array();
                            $sendPhoneNum = html_filter($dbc, $_REQUEST['senderPhone']);
                            if($source == '1'){
                                $sendPhoneNum = "+".html_filter($dbc, $_REQUEST['senderPhone']);
                            }
                            $keyCode = html_filter($dbc, $_REQUEST['keyCode']);

                            $cardID = html_filter($dbc, $_REQUEST['cardID']);
                            $amount = html_filter($dbc, $_REQUEST['amount']);

                            if (check_phoneNum_and_keyCode($dbc, $sendPhoneNum, $keyCode)) {
                                $customerData = get_customer_by_phoneNum($dbc,$sendPhoneNum);
                                $data = ps_confirm_new_card($dbc, $customerData, $cardID, $amount);
                                if ($data['confirm-status'] == 'true'){

                                    z_card_verify_log($dbc,$customerData,$cardID,$amount,'SUCCESS');

                                    $response['statusCode'] = 200;
                                    //$response['message'] = 'You have successfully verified your card. You can now add money to your PayGlobal wallet with your verified card';
                                    $response['message'] = $data['reason'];

                                }else{
                                    $response['statusCode'] = 500;
                                    $response['message'] = $data['reason'];
                                }
                            } else {
                                // Sorry, Error authenticating user!
                                $response['statusCode'] = 500;
                                $response['message'] = 'Sorry, Error authenticating user!';
                            }
                            echo json_encode($response);
                            break;

                        CASE 'REMOVE-CARD':
                            //Remove card
                            $response = array();
                            $phoneNum = html_filter($dbc, $_REQUEST['phoneNum']);
                            if($source == '1'){
                                $phoneNum = "+".html_filter($dbc, $_REQUEST['phoneNum']);
                            }
                            $keyCode = html_filter($dbc, $_REQUEST['keyCode']);
                            $cardID = html_filter($dbc, $_REQUEST['cardID']);

                            $customerData = get_customer_by_phoneNum($dbc, $phoneNum);

                            $data = ps_remove_card($dbc,$customerData,$cardID);

                            if($data['code'] == '000'){

                                if(ps_remove_card_query($dbc,$cardID)){

                                    $response['statusCode'] = 200;
                                    $response['message'] = $data['reason'];

                                }else{

                                    $response['statusCode'] = 500;
                                    $response['message']    = "Try Again";
                                }

                            }else{

                                $response['statusCode'] = 500;
                                $response['message'] = $data['reason'];

                            }

                            echo json_encode($response);
                            break;
                        CASE 'COMMISSION_BALANCE':
                            // Check commission balance
                            $response = array();
                            $phoneNum = html_filter($dbc, $_REQUEST['phoneNum']);
                            if($source == '1'){
                                $phoneNum = "+".html_filter($dbc, $_REQUEST['phoneNum']);
                            }
                            $customerData = get_customer_by_phoneNum($dbc, $phoneNum);
                            if (check_if_number_exist($dbc, $phoneNum)) {

                                $acc_info = get_cus_account_info_by_customerID($dbc, $customerData['id'],$customerData['countryCode']);
                                $response['commission']= $acc_info['commissionAccount'];
                                $response['statusCode']= 200;

                            } else {
                                $response['message']= 'Wrong phone number';
                                $response['statusCode']= 500;
                            }

                            echo json_encode($response);
                            break;

                        CASE 'RED_COM':
                            $response = array();
                            $phoneNum = html_filter($dbc, $_REQUEST['phoneNum']);
                            if($source == '1'){
                                $phoneNum = "+".html_filter($dbc, $_REQUEST['phoneNum']);
                            }
                            $customerData = get_customer_by_phoneNum($dbc, $phoneNum);
                            $cusAcc = get_cus_account_info_by_customerID($dbc, $customerData['id'],$customerData['countryCode']);
                            if ($cusAcc['commissionAccount'] > 0.00) {
                                if (funds_from_commission_to_wallet($dbc, $customerData, $cusAcc['commissionAccount'])) {
                                    $transaction_uid = genTransactionID();

                                    if (add_commission_to_acc_transaction($dbc, $customerData['id'], $cusAcc['commissionAccount'], 'PC', $transaction_uid)) {
                                        $systemNumber = get_system_number_by_name($dbc, 'COMMISSION', $customerData['countryCode']);
                                        $systemNumberData = get_customer_by_phoneNum($dbc, $systemNumber['phoneNum']);
                                        if (redraw_funds($dbc, $systemNumberData['id'], $cusAcc['commissionAccount'],$systemNumberData['countryCode'])) {
                                            $systemAcc = get_cus_account_info_by_customerID($dbc, $systemNumberData['id'],$systemNumberData['countryCode']);
                                            $cusAccNew = get_cus_account_info_by_customerID($dbc, $customerData['id'],$customerData['countryCode']);
                                            if (add_main_transaction($dbc, $customerData, $systemNumberData, 'COMMISSION', '', $transaction_uid, 'COMPLETED', '', $cusAcc['commissionAccount'], $cusAcc['commissionAccount'], 'PC', '')) {
                                                if (update_bank_transaction_balance($dbc, $transaction_uid, $cusAccNew['balance'], $systemAcc['balance'])) {
                                                    $amount = moneyFormat($cusAcc['commissionAccount']);
                                                    sendSMS($customerData['phoneNum'], $message);
                                                    if ($customerData['firebaseKey'] != '') {
                                                        firebaseCall($customerData['firebaseKey'], $message);
                                                    }
                                                    $response['statusCode'] = 200;
                                                    $response['message'] = 'Commission Transferred';
                                                }
                                            }

                                        }
                                    }

                                }
                            }
                            echo json_encode($response);
                            break;
                        CASE 'LIST-CURRENCY':
                            $response = array();
                            $phoneNum = html_filter($dbc, $_REQUEST['phoneNum']);
                            if($source == '1'){
                                $phoneNum = "+".html_filter($dbc, $_REQUEST['phoneNum']);
                            }
                            if (check_if_number_exist($dbc, $phoneNum)) {
                                $datas = get_system_country($dbc);

                                $output = array();
                                foreach ($datas as $data) {
                                    $output[] = $data;
                                }
                                echo json_encode($output);


                            } else {
                                $response['statusCode'] = 500;
                                $response['message'] = 'Sorry, Phone Do not exist!';
                                echo json_encode($response);

                            }

                            break;
                        CASE 'CURRENCY-EXCHANGE':
                            $response = array();
                            $phoneNum = html_filter($dbc, $_REQUEST['phoneNum']);
                            $currency = html_filter($dbc, $_REQUEST['currency']);
                            $countryCode = html_filter($dbc, $_REQUEST['countryCode']);
                            if($source == '1'){
                                $phoneNum = "+".html_filter($dbc, $_REQUEST['phoneNum']);
                            }
                            if(check_if_number_exist($dbc, $phoneNum)) {

                                if(update_country_code($dbc,$phoneNum,$countryCode,'MULTIPLE')){

                                    $response['statusCode'] = 200;
                                    $response['message'] = 'Currency switch is successful';
                                }else{
                                    $response['statusCode'] = 500;
                                    $response['message'] = 'Switch Currency was not successful';
                                }

                            } else {
                                $response['statusCode'] = 500;
                                $response['message'] = 'Sorry, Phone Do not exist!';
                            }
                            echo json_encode($response);
                            break;

                        case 'CARD-TO-MOMO':

                            $response = array();
                            $sendPhoneNum = html_filter($dbc, $_REQUEST['senderPhone']);
                            if ($source == '1') {
                                $sendPhoneNum = "+" . html_filter($dbc, $_REQUEST['senderPhone']);
                            }
                            $keyCode = html_filter($dbc, $_REQUEST['keyCode']);
                            $cardID  = html_filter($dbc, $_REQUEST['cardID']);
                            $amount  = html_filter($dbc, $_REQUEST['amount']);

                            if (check_phoneNum_and_keyCode($dbc, $sendPhoneNum, $keyCode)) {
                                $recipientData = get_customer_by_phoneNum($dbc, $sendPhoneNum);
                                if (ps_check_customer_already_signUp($dbc, $recipientData['id'])) {
                                    if (ps_check_if_cardID_exist_on_transaction($dbc, $cardID, $recipientData['id'])) {
                                        $systemNumber = get_system_number_by_name($dbc, 'CARD_NUMBER', $recipientData['countryCode']);
                                        $senderData = get_customer_by_phoneNum($dbc, $systemNumber['phoneNum']);
                                        $transaction_uid = genTransactionID();
                                        if (ps_add_transaction($dbc, $senderData, $recipientData, $cardID, $amount, $transaction_uid)) {
                                            $data = ps_new_transaction($dbc, $recipientData, $cardID, $amount);
                                            if ($data['code'] == 000) {
                                                if (redraw_funds($dbc, $senderData['id'], $amount, $senderData['countryCode'])) {
                                                    if (add_funds($dbc, $recipientData['id'], $amount, $recipientData['countryCode'])) {
                                                        $senderAcc = get_cus_account_info_by_customerID($dbc, $senderData['id'], $senderData['countryCode']);
                                                        $recipientAcc = get_cus_account_info_by_customerID($dbc, $recipientData['id'], $recipientData['countryCode']);
                                                        if (ps_update_transaction($dbc, $transaction_uid, $data['reason'], 'COMPLETED', $senderAcc['balance'], $recipientAcc['balance'], $data['transaction_id'])) {

                                                            /*$response['statusCode'] = 200;
                                                            $response['message'] = $data['reason'];
                                                            $response['vbvStatus'] = 'NO'; */

                                                            //****************************************************************

                                                            $response = array();
                                                            $sendPhoneNum = html_filter($dbc, $_REQUEST['senderPhone']);
                                                            if ($source == '1') {
                                                                $sendPhoneNum = "+" . html_filter($dbc, $_REQUEST['senderPhone']);
                                                            }
                                                            $keyCode = html_filter($dbc, $_REQUEST['keyCode']);

                                                            $mm_phoneNum = html_filter($dbc, $_REQUEST['mm_phone']);
                                                            $mm_channel = html_filter($dbc, $_REQUEST['mm_channel']);
                                                            //$mm_Name = html_filter($dbc, $_REQUEST['mm_name']);
                                                            $mm_Name = 'GN Money';
                                                            $mm_amount = html_filter($dbc, $_REQUEST['mm_amount']);
                                                            $mm_email = html_filter($dbc, $_REQUEST['mm_email']);
                                                            $mm_transactionType = html_filter($dbc, $_REQUEST['mm_transactionType']);
                                                            $mm_desc = "Send Money out of GN Mobile Money";
                                                            if ($mm_email == "" or $mm_email == null) {
                                                                $mm_email = "alert@payappglobal.com";
                                                            }

                                                            if (check_phoneNum_and_keyCode($dbc, $sendPhoneNum, $keyCode)) {

                                                                $customerData = get_customer_by_phoneNum($dbc, $sendPhoneNum);
                                                                $mm_amount = $mm_amount * 1;
                                                                if ($customerData['accountType'] == 2 or $customerData['accountType'] == 4) {
                                                                    $fee = $mm_amount * 0.005; // Fee for cash out
                                                                    $mm_amount = $mm_amount * 1;
                                                                    $mm_totalAmount = $fee + $mm_amount;
                                                                } else {
                                                                    $fee = $mm_amount * 0.015; // Fee for cash out
                                                                    $mm_amount = $mm_amount * 1;
                                                                    $mm_totalAmount = $fee + $mm_amount;
                                                                }

                                                                if (dailyKyc($dbc, $customerData['id'], $customerData['countryCode'], $mm_amount, $fee)) {
                                                                    if (monthlyKyc($dbc, $customerData['id'], $customerData['countryCode'], $mm_amount, $fee)) {
                                                                        $system_status = get_system_status_by_serviceType($dbc, "HUBTEL");
                                                                        if ($system_status['status'] == 'ON') { // service status
                                                                            if ($customerData['hubtel_push_status'] == 'ON') {

                                                                                $global_hubtel_Acc = get_customer_by_phoneNum($dbc, "+233770000000");

                                                                                if (check_existing_balance($dbc, $customerData['id'], $mm_totalAmount, $customerData['countryCode'])) {

                                                                                    $transaction_uid = genTransactionID();

                                                                                    (!add_to_hubtel_transaction_new($dbc, $customerData, $global_hubtel_Acc, $mm_phoneNum, 'MM-PUSH', $mm_channel, $mm_amount, 'SMART', '', $transaction_uid, 'WAITING', $fee)) ? Response::output(500, 'Transaction could not be processed, Please try again later!') : '';

                                                                                    if (redraw_funds($dbc, $customerData['id'], $mm_totalAmount, $customerData['countryCode'])) {


                                                                                        if (add_funds($dbc, $global_hubtel_Acc['id'], $mm_amount, $global_hubtel_Acc['countryCode'])) {

                                                                                            //if (add_to_hubtel_transaction_new($dbc, $customerData,$global_hubtel_Acc,$mm_phoneNum,'MM-PUSH',$mm_channel,$hubtelData,'SMART','',$transaction_uid,'WAITING',$fee)) {

                                                                                            $transactionData = get_hub_transactionData($dbc, $transaction_uid);

                                                                                            $transactionData['senderCountryCode'] = $customerData['countryCode'];
                                                                                            $transactionData['senderID'] = $customerData['id'];
                                                                                            $transactionData['recipientID'] = $global_hubtel_Acc['id'];
                                                                                            $customerAcc = get_cus_account_info_by_customerID($dbc, $customerData['id'], $customerData['countryCode']);
                                                                                            $recipientAcc = get_cus_account_info_by_customerID($dbc, $global_hubtel_Acc['id'], $global_hubtel_Acc['countryCode']);
                                                                                            if (add_payGlobal_commission($dbc, $transactionData, $fee)) {
                                                                                                if (update_hubtel_balance($dbc, $transaction_uid, $customerAcc['balance'], $recipientAcc['balance'])) {
                                                                                                    $mm_amount = moneyFormat2($mm_amount);
                                                                                                    $cusAccBal = moneyFormat2($customerAcc['balance']);
                                                                                                    //$customerMessage = "You have successfully Moved an amount {$mm_amount} from your GN mobile money to another Mobile Money service. transaction ID: {$transaction_uid}. balance: {$customerAcc['balance']}";
                                                                                                    $customerMessage = "You have successfully transferred an amount of GHC {$mm_amount} from your GN Mobile Money to another Mobile Money service. Balance: {$cusAccBal} Transaction ID: {$transaction_uid}";
                                                                                                    sendSMS($customerData['phoneNum'], $customerMessage);
                                                                                                    if ($customerData['firebaseKey'] != '') {
                                                                                                        firebaseCall($customerData['firebaseKey'], $customerMessage);
                                                                                                    }
                                                                                                    $response['statusCode'] = 200;
                                                                                                    $response['message'] = "You have successfully transferred money from GN Mobile Money to another network.";
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }

                                                                                    $hubtelData = hubtelPush($dbc, $mm_Name, $mm_phoneNum, $mm_email, $mm_channel, $transaction_uid, $mm_amount, $mm_desc);
                                                                                    $hubtel_txn = $hubtelData['Data']['TransactionId'];


                                                                                    if ($hubtelData['ResponseCode'] == "0000") {

                                                                                        //$transaction_uid = $hubtelData['Data']['ClientReference'];
                                                                                        update_main_transaction_with_hubtelID($dbc, $transaction_uid, $hubtel_txn);
                                                                                    } elseif ($hubtelData['ResponseCode'] == "0001") {

                                                                                        update_main_transaction_with_hubtelID($dbc, $transaction_uid, $hubtel_txn);
                                                                                    } else {
                                                                                        $response['statusCode'] = 500;
                                                                                        $response['message'] = "Error Transaction, Please try again later. Thanks";
                                                                                    }
                                                                                } else {
                                                                                    $response['statusCode'] = 500;
                                                                                    $response['message'] = 'You do not have enough balance in wallet';
                                                                                }
                                                                            } else {
                                                                                $response['statusCode'] = 500;
                                                                                $response['message'] = 'This service is unavailable at the moment';
                                                                            }
                                                                        } else {
                                                                            $response['statusCode'] = 500;
                                                                            $response['message'] = 'This service is unavailable at the moment, Try again later.';
                                                                        }
                                                                    } else {
                                                                        $response['statusCode'] = 500;
                                                                        $response['message'] = 'You have reached your monthly transaction limit. Thank You';
                                                                    }
                                                                } else {
                                                                    $response['statusCode'] = 500;
                                                                    $response['message'] = 'You have reached your daily transaction limit. Thank You';
                                                                }
                                                            } else {
                                                                // Sorry, Error authenticating user!
                                                                $response['statusCode'] = 500;
                                                                $response['message'] = 'Sorry, Error authenticating user!';
                                                            }
                                                            echo json_encode($response);


                                                            //**************************************************

                                                        }
                                                    }
                                                }
                                            } elseif ($data['code'] == 200) { //VBV
                                                if (ps_update_transaction($dbc, $transaction_uid, $data['reason'], 'WAITING', 0, 0, $data['transaction_id'])) {
                                                    $response['statusCode'] = 200;
                                                    $response['vbvStatus'] = 'YES';
                                                    $response['message'] = $data['reason'];
                                                }
                                            } else {
                                                if (ps_update_transaction($dbc, $transaction_uid, $data['Reason'], "CANCELLED", 0, 0, '')) {

                                                    $response['statusCode'] = 500;
                                                    //$response['message'] = 'Transaction Declined, Please check your balance or Contact your bank';
                                                    $response['message'] = $data['reason'];
                                                }
                                            }
                                        } else {
                                            $response['statusCode'] = 500;
                                            $response['message'] = 'Error initiating transaction';
                                        }
                                    } else {
                                        $response['statusCode'] = 500;
                                        $response['message'] = 'Debit Card Option Not activated3';
                                    }
                                } else {
                                    $response['statusCode'] = 500;
                                    $response['message'] = 'Debit Card Option Not activated2';
                                }
                            } else {
                                // Sorry, Error authenticating user!
                                $response['statusCode'] = 500;
                                $response['message'] = 'Sorry, Error authenticating user!';
                            }

                            echo json_encode($response);



                            break;

                        case 'CARD-TO-TOKEN':

                            $response = array();
                            $sendPhoneNum = html_filter($dbc, $_REQUEST['senderPhone']);
                            if ($source == '1') {
                                $sendPhoneNum = "+" . html_filter($dbc, $_REQUEST['senderPhone']);
                            }
                            $keyCode = html_filter($dbc, $_REQUEST['keyCode']);
                            $cardID  = html_filter($dbc, $_REQUEST['cardID']);
                            $amount  = html_filter($dbc, $_REQUEST['amount']);

                            if (check_phoneNum_and_keyCode($dbc, $sendPhoneNum, $keyCode)) {
                                $recipientData = get_customer_by_phoneNum($dbc, $sendPhoneNum);
                                if (ps_check_customer_already_signUp($dbc, $recipientData['id'])) {
                                    if (ps_check_if_cardID_exist_on_transaction($dbc, $cardID, $recipientData['id'])) {
                                        $systemNumber = get_system_number_by_name($dbc, 'CARD_NUMBER', $recipientData['countryCode']);
                                        $senderData = get_customer_by_phoneNum($dbc, $systemNumber['phoneNum']);
                                        $transaction_uid = genTransactionID();
                                        if (ps_add_transaction($dbc, $senderData, $recipientData, $cardID, $amount, $transaction_uid)) {
                                            $data = ps_new_transaction($dbc, $recipientData, $cardID, $amount);
                                            if ($data['code'] == 000) {
                                                if (redraw_funds($dbc, $senderData['id'], $amount, $senderData['countryCode'])) {
                                                    if (add_funds($dbc, $recipientData['id'], $amount, $recipientData['countryCode'])) {
                                                        $senderAcc = get_cus_account_info_by_customerID($dbc, $senderData['id'], $senderData['countryCode']);
                                                        $recipientAcc = get_cus_account_info_by_customerID($dbc, $recipientData['id'], $recipientData['countryCode']);
                                                        if (ps_update_transaction($dbc, $transaction_uid, $data['reason'], 'COMPLETED', $senderAcc['balance'], $recipientAcc['balance'], $data['transaction_id'])) {

                                                            /*$response['statusCode'] = 200;
                                                            $response['message'] = $data['reason'];
                                                            $response['vbvStatus'] = 'NO'; */

                                                            //**************************************

                                                            // Generate Unregistered user token
                                                            $sendPhoneNum = html_filter($dbc, $_REQUEST['senderPhone']);
                                                            if ($source == '1') {
                                                                $sendPhoneNum = "+" . html_filter($dbc, $_REQUEST['senderPhone']);
                                                            }
                                                            $amount = html_filter($dbc, $_REQUEST['amount']);
                                                            $keyCode = html_filter($dbc, $_REQUEST['keyCode']);
                                                            $tokenPin = html_filter($dbc, $_REQUEST['pin']);
                                                            $recipientCountry = html_filter($dbc, $_REQUEST['recipientCountry']);
                                                            $intended_Recipient_name = html_filter($dbc, $_REQUEST['recipient_name']);
                                                            $intended_Recipient_address = html_filter($dbc, $_REQUEST['recipient_address']);
                                                            $intended_Recipient_phonenumber = html_filter($dbc, $_REQUEST['recipient_phonenumber']);

                                                            $response = array();

                                                            if (check_phoneNum_and_keyCode($dbc, $sendPhoneNum, $keyCode)) {
                                                                $senderData = get_customer_by_phoneNum($dbc, $sendPhoneNum);
                                                                $recipientData = array();
                                                                $general_service = "TOKEN";
                                                                $recipientData['uGroup'] = 1; // public group
                                                                $recipientData['accountType'] = 2; // Agent or Super Agent
                                                                $recipientData['countryCode'] = $recipientCountry;


                                                                if(check_if_number_exist_as_superAgent_or_agent_or_individual($dbc, $sendPhoneNum)){
                                                                    $check_balance = check_for_enough_balance_token($dbc, $senderData, $recipientData, $amount, $general_service);
                                                                    if ($check_balance['status'] == true) {
                                                                        $recipientData['recipientCountryCode'] = $recipientCountry;
                                                                        $recipientData['recipientAmount'] = "";
                                                                        $token = generate_token();
                                                                        $hashToken = passwordHash($token, $mySalt);
                                                                        $hashpin = passwordHash($tokenPin, $mySalt);
                                                                        $funds = $check_balance['newAmount'];
                                                                        if (redraw_funds($dbc, $senderData['id'], $funds, $senderData['countryCode'])) {
                                                                            if (add_to_token_mgt_after_processing($dbc, $senderData['id'], $senderData['countryCode'], $token, $tokenPin, $check_balance['transaction_uid'], $recipient_name, $recipient_phonenumber, $recipient_address, $recipientCountry)) {

                                                                                $rate = get_conversion_rate($dbc, $senderData['countryCode'], $recipientCountry);
                                                                                $convertedAmount = $amount * $rate;
                                                                                $senderAcc = get_cus_account_info_by_customerID($dbc, $senderData['id'], $senderData['countryCode']);

                                                                                if (add_main_transaction($dbc, $recipientData, $senderData, $general_service, "", $check_balance['transaction_uid'], "WAITING", $check_balance['fee'], $amount, $convertedAmount, "SMART", $check_balance['pricing_id'])) {
                                                                                    $senderCountryData = get_currency_by_countryCode($dbc, $senderData['countryCode']);
                                                                                    $recipientCountryData = get_currency_by_countryCode($dbc, $recipientCountry);

                                                                                    if (updateTokenVeriableTable($dbc, $check_balance['transaction_uid'], $hashToken, $hashpin, $senderData['phoneNum'], $senderData['fullName'], $amount, $senderCountryData['currency'], $convertedAmount, $recipientCountryData['currency'], $intended_Recipient_name, $intended_Recipient_phonenumber, $intended_Recipient_address)) {

                                                                                        if (transcription($dbc, $senderData, $recipientData, $general_service, $amount, '', $check_balance['transaction_uid'])) {
                                                                                            if (update_main_transaction_after_processing($dbc, $check_balance['transaction_uid'], 'WAITING', $senderAcc['balance'], 0)) {
                                                                                                $agentFee = $check_balance['fee'] * $check_balance['senderCommission'];
                                                                                                $payGlobalFee = $check_balance['fee'] * $check_balance['sysCommission'];
                                                                                                $data = array();
                                                                                                $data['recipientID'] = $senderData['id'];
                                                                                                $data['senderID'] = $senderData['id'];
                                                                                                $data['senderCountryCode'] = $senderData['countryCode'];
                                                                                                $data['transactionTypes'] = $general_service;
                                                                                                $data['transaction_uid'] = $check_balance['transaction_uid'];

                                                                                                $amount = moneyFormat2($amount);
                                                                                                if ($senderData['accountType'] == 1) {
                                                                                                    if (add_payGlobal_commission($dbc, $data, $payGlobalFee)) {

                                                                                                        // $message = "Transaction Completed, Token is {$token} and Code is {$tokenPin} for an amount of {$amount}.";

                                                                                                        //echo "(SUCCESSFUL/{$token})";
                                                                                                        $code2 = $recipientData['recipientCountryCode'];
                                                                                                        $cCode2 = $code2;
                                                                                                        $curr2 = getLiberianCurrency($dbc, $cCode2);
                                                                                                        $fcurr2 = $curr2['currency'];


                                                                                                        $response['statusCode'] = 200;
                                                                                                        $response['fee'] = moneyFormat2($check_balance['fee']);
                                                                                                        $response['token'] = $token;
                                                                                                        $response['senderAmount'] = moneyFormat2($amount);
                                                                                                        $response['recipientAmount'] = moneyFormat2($convertedAmount);
                                                                                                        $response['recipientCurrency'] = $fcurr2;

                                                                                                        $message = "Token: {$token} with an amount of {$fcurr2} {$amount} was generated, secret code is: {$tokenPin}  Balance: {$fcurr} {$senderAcc['balance']} Transaction ID: {$check_balance['transaction_uid']}";
                                                                                                        sendSMS($sendPhoneNum, $message);
                                                                                                    }
                                                                                                } else {
                                                                                                    if (add_agent_commission($dbc, $data, $agentFee, $senderData['countryCode'])) {
                                                                                                        if (add_payGlobal_commission($dbc, $data, $payGlobalFee)) {
                                                                                                            //$message = "Transaction Completed, Token is {$token} and Code is {$tokenPin} for an amount of {$amount}.";
                                                                                                            //echo "(SUCCESSFUL/{$token})";
                                                                                                            $code2 = $recipientData['recipientCountryCode'];
                                                                                                            $cCode2 = $code2;
                                                                                                            $curr2 = getLiberianCurrency($dbc, $cCode2);
                                                                                                            $fcurr2 = $curr2['currency'];

                                                                                                            $response = array();
                                                                                                            $response['statusCode'] = 200;
                                                                                                            $response['fee'] = moneyFormat2($check_balance['fee']);
                                                                                                            $response['token'] = $token;
                                                                                                            $response['senderAmount'] = moneyFormat2($amount);
                                                                                                            $response['recipientAmount'] = moneyFormat2($convertedAmount);
                                                                                                            $response['recipientCurrency'] = $fcurr2;

                                                                                                            $message = "Token: {$token} with an amount of {$fcurr2} {$amount} was generated, secret code is: {$tokenPin}  Balance: {$senderAcc['balance']} Transaction ID: {$check_balance['transaction_uid']}";
                                                                                                            sendSMS($sendPhoneNum, $message);
                                                                                                            //echo "(SUCCESSFUL/{$token})";

                                                                                                        }
                                                                                                    }
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                } else {
                                                                                    //echo "UNSUCCESSFUL";
                                                                                    $response['statusCode'] = 500;
                                                                                    $response['message'] = 'Sorry, Error performing transaction!';
                                                                                }
                                                                            } else {
                                                                                //echo "UNSUCCESSFUL1";
                                                                                $response['statusCode'] = 500;
                                                                                $response['message'] = 'Sorry, Error performing transaction!';
                                                                            }
                                                                        } else {

                                                                            //echo "UNSUCCESSFUL2";
                                                                            $response['statusCode'] = 500;
                                                                            $response['message'] = 'Sorry, Error performing transaction!';
                                                                        }
                                                                    } else {
                                                                        //echo "0"; //Insufficient Account Balance Returns 0 string
                                                                        $response['statusCode'] = 500;
                                                                        $response['message'] = 'Insufficient Account Balance';
                                                                    }
                                                                } else {
                                                                    //echo("UNAUTHORISED_USER");
                                                                    $response['statusCode'] = 500;
                                                                    $response['message'] = 'Sorry, Unauthorized user!';
                                                                }
                                                            } else {
                                                                //echo("Sorry, Error authenticating user!");
                                                                $response['statusCode'] = 500;
                                                                $response['message'] = 'Sorry, Error authenticating user!';
                                                            }
                                                            echo json_encode($response);

                                                            break;



                                                            //****************************************

                                                        }
                                                    }
                                                }
                                            } elseif ($data['code'] == 200) { //VBV
                                                if (ps_update_transaction($dbc, $transaction_uid, $data['reason'], 'WAITING', 0, 0, $data['transaction_id'])) {
                                                    $response['statusCode'] = 200;
                                                    $response['vbvStatus'] = 'YES';
                                                    $response['message'] = $data['reason'];
                                                }
                                            } else {
                                                if (ps_update_transaction($dbc, $transaction_uid, $data['Reason'], "CANCELLED", 0, 0, '')) {

                                                    $response['statusCode'] = 500;
                                                    //$response['message'] = 'Transaction Declined, Please check your balance or Contact your bank';
                                                    $response['message'] = $data['reason'];
                                                }
                                            }
                                        } else {
                                            $response['statusCode'] = 500;
                                            $response['message'] = 'Error initiating transaction';
                                        }
                                    } else {
                                        $response['statusCode'] = 500;
                                        $response['message'] = 'Debit Card Option Not activated3';
                                    }
                                } else {
                                    $response['statusCode'] = 500;
                                    $response['message'] = 'Debit Card Option Not activated2';
                                }
                            } else {
                                // Sorry, Error authenticating user!
                                $response['statusCode'] = 500;
                                $response['message'] = 'Sorry, Error authenticating user!';
                            }

                            echo json_encode($response);

                            break;

                        case 'CARD-TO-BANK':


                            break;

                        default:
                            echo("Sorry, Such request do not exist!");
                            break;


                    }
                } else {
                    echo("Sorry, Such http request do not exist!");
                }
            } else {
                echo "Sorry, No request Made!";
            }



