<?php

define("IN_SITE", true);
require_once(__DIR__."/../libs/db.php");
require_once(__DIR__."/../libs/lang.php");
require_once(__DIR__."/../libs/helper.php");
require_once(__DIR__."/../libs/database/users.php");
$User = new users();
$CMSNT = new DB();
$Mobile_Detect = new Mobile_Detect();
 
 

 if(isset($_POST['order_id']) && isset($_POST['billcode'])){

    $order_id = validate_alphanumeric($_POST['order_id'], 100);
    $billcode = validate_alphanumeric($_POST['billcode'], 100);
    $status = validate_int($_POST['status'], 0, 3);
    
    if ($order_id === false || $billcode === false || $status === false) {
        die('Invalid callback data');
    }

    if($row = $CMSNT->get_row_safe("SELECT * FROM `toyyibpay_transactions` WHERE `trans_id` = ? AND `status` = 0 AND `BillCode` = ?", [$order_id, $billcode])){

        if($status == 1){
            $isUpdate = $CMSNT->update('toyyibpay_transactions', [
                'status'    => 1,
                'update_date'   => gettime()
            ], " `id` = ?", [$row['id']]);
            if($isUpdate){
                $amount = $amount / 100;
                $received = $row['amount'] * $CMSNT->site('rate_toyyibpay');
                $User->AddCredits($row['user_id'], $received, 'Automatic top-up via Malaysian bank #'.$billcode);
            }
        }else if($status == 3){
            $CMSNT->update('toyyibpay_transactions', [
                'status'    => 2,
                'update_date'   => gettime()
            ], " `id` = ?", [$row['id']]);
        }

    }   


 }


 