<?php
include("./config/db_config.php");
$logQuery = "SELECT * FROM information_schema.tables WHERE table_schema =  '".$db_name."' AND table_name = 'mobile_payment' LIMIT 1";
$select = mysqli_query($conn, $logQuery);
$result = array(
    'success' => false,
    'message' => 'Something went wrong'
);
if (mysqli_num_rows($select) > 0) 
{
    if(!isset($_POST['onlinePaymentId']))
    {
        $_POST = json_decode(file_get_contents("php://input"), true);   
    }
    $activeGateway = isset($_POST['activeGateway']) ? trim($_POST['activeGateway']) : '';
    $onlinePaymentId = isset($_POST['onlinePaymentId']) ? trim($_POST['onlinePaymentId']) : '';
    $paymentStatus = isset($_POST['paymentStatus']) ? trim($_POST['paymentStatus']) : '';
    $receiptStatus = isset($_POST['receiptStatus']) ? trim($_POST['receiptStatus']) : '';
    $txnId = isset($_POST['txnId']) ? trim($_POST['txnId']) : '';

    if(isset($_POST['onlinePaymentId'])  && isset($_POST['paymentStatus']) && isset($_POST['receiptStatus']) && isset($_POST['activeGateway']) && !empty($_POST['activeGateway']))
    {
        if($activeGateway =='network')
        {
            $sql = "SELECT * FROM `mobile_payment` WHERE onlinePaymentId = '".$onlinePaymentId."'";
            $select = mysqli_query($conn, $sql);
            if(mysqli_num_rows($select) > 0)
            {
                $rowData = mysqli_fetch_assoc($select);
                $query = "UPDATE mobile_payment SET paymentStatus = '".$paymentStatus."', receiptStatus = '".$receiptStatus."', txnId = '".$txnId."' where id = ".$rowData['id'] ." order by id desc limit 1";
                mysqli_query($conn, $query);
                
                $result = array(
                    'success' => true,
                    'message' => 'Record Successfully Updated'
                );
            }
            else
            {
                $result = array(
                    'success' => false,
                    'message' => 'Invalid transaction ID'
                );
            }
        }
        
    }
    else
    {
        $result = array(
            'success' => false,
            'message' => 'Invalid Parameter'
        );
    }
}
else
{
    $result = array(
        'success' => false,
        'message' => 'Mobile data functionality not implemented'
    );
}

echo json_encode($result);

