<?php 
/*
    define("IN_SITE", true);
    require_once(__DIR__."/../../config/config.php");
    require_once(__DIR__."/../../config/function.php");
    $IULA= new IULA();
    
    if (isset($_GET['pass_cron'])) {
    // Tiếp tục xử lý nếu 'pass_cron' tồn tại trong mảng $_GET
    if ($IULA->site('pass_cron') != $_GET['pass_cron']) {
        die('Mật khẩu cron không đúng');
    }
   } else {
    die('Đéo cho cron');
  }
     if ($IULA->site('auto_momo_1s') == 'ON') {
         die('Admin bật auto momo 1s, không thể thực hiện cron này');
     }
     if (time() - $IULA->site('limit_cron_withdrawMomo') < 15)
     {
     die('Limit');
     }
     $IULA->update("options", ['value' => time()], " `name` = 'limit_cron_withdrawMomo' ");
     
     

    $limit_pending = $IULA->site('limit_withdraw') ??  1;
    $limit_allow = $IULA->site('max_rut_auto_momo') ?? 2000000;
    $query = "SELECT * FROM `wallet_orders_ruttien` WHERE  `status` = 'pending' AND `paygate_code` LIKE '%Momo%' ORDER BY ID ASC LIMIT 1";
    foreach ($IULA->get_list($query) as $withdraw) 
    {
    if($withdraw['status'] != 'pending')
    {
    exit();
    }
    if($withdraw['net_amount'] > $limit_allow)
    {
             $IULA->update("wallet_orders_ruttien", [
                'status'  => 'hold', 
                'message'   => 'Không thể chuyển đơn qua api do admin cài giới hạn rút Momo qua api là  '.$limit_allow ,  
                'client_note'   => 'Tạm giữ, vui lòng chờ xử lý lại',
                'updated_at'   => gettime(),
            ], " `id` = '".$withdraw['id']."' ");   
    exit();
    }
      $check_noidung = $IULA->get_row("SELECT * FROM `wallet_orders_ruttien` WHERE `description` = '".$withdraw['description']."' AND (`status` = 'completed' OR `status` = 'processing')");
     if($check_noidung)
     {
       $IULA->update("wallet_orders_ruttien", [
                'status'  => 'completed', 
                'message'   => 'Nội dung đơn hàng đã tồn tại và được chuyển thành công' ,  
                'client_note'   => 'ND',
                'updated_at'   => gettime(),
            ], " `id` = '".$withdraw['id']."' ");   
                exit();   
     }
    if($IULA->get_row("SELECT * FROM `paygates` WHERE `code` = '".$withdraw['paygate_code']."' ")['status']  != 1)
    {
             $IULA->update("wallet_orders_ruttien", [
                'status'  => 'hold', 
                'message'   => $withdraw['paygate_code'].' Đã bị tắt status - đơn tạm giữ',  
                'client_note'   => 'Tạm giữ, vui lòng chờ xử lý lại',
                'updated_at'   => gettime(),
            ], " `id` = '".$withdraw['id']."' ");
             exit();
    }
     $infoProviders = $IULA->get_row("SELECT * FROM `paygate_vendors` WHERE `code` = '".$withdraw['vendor']."' ");
     $Token = json_decode($infoProviders['configs'] ?? '', true);
     $Token = $Token['token'] ?? '';
     
     //BÁO THIẾU TOKEN 
     if( empty($Token))
     {
      $IULA->update("wallet_orders_ruttien", [
                'status'  => 'hold', 
                'message'   => 'NCC '.$withdraw['vendor'].' thiếu token, đơn không thể chuyển',  
                'client_note'   => 'Tạm giữ, vui lòng chờ xử lý lại',
                'updated_at'   => gettime(),
            ], " `id` = '".$withdraw['id']."' ");
             exit();    
     }
     
     
     
     $infoPaygates = $IULA->get_row("SELECT * FROM `paygates` WHERE `code` = '".$withdraw['paygate_code']."' ");
     $passwordMomo = json_decode($infoPaygates['configs']  ?? '', true);
     $passwordMomo = $passwordMomo['account_password'] ?? '';
     
     
     
     

    if ($withdraw['vendor'] == 'ApiWeb2m') 
    {
     if( empty($passwordMomo))
     {
      $IULA->update("wallet_orders_ruttien", [
                'status'  => 'hold', 
                'message'   => 'Vui lòng điền password vào cổng tt momo',  
                'client_note'   => 'Tạm giữ, vui lòng chờ xử lý lại',
                'updated_at'   => gettime(),
            ], " `id` = '".$withdraw['id']."' ");
             exit();    
     }    
        
        
    require_once(__DIR__ . "/../../frontend/paygate/providers/ApiWeb2m.php");
    $ApiWeb2m_help = new ApiWeb2m();
    $result = $ApiWeb2m_help->transferMomo($Token, $withdraw['account_num'], floatval($withdraw['net_amount']), $withdraw['description'], $passwordMomo);
    
    $status = 'hold';
    $message = $result['msg'] ?? 'Tạm giữ tiền do chuyển thất bại';
    $client_note = 'Tạm giữ';
    $payment_trans_id = '';
    if ($status && $result['status'] == 200) {
        $status = 'completed';
        $message = $result['msg'] ?? 'Chuyển tiền thành công';
        $client_note = 'Chuyển tiền thành công';
        $payment_trans_id   = $result['transId'] ?? '';
        $IULA->update("paygates", ['balance' => $result['balance'] ?? $infoPaygates['balance']], "`id` = '" . $infoPaygates['id'] . "'");
    }
    
    $IULA->update("wallet_orders_ruttien", [
        'status' => $status,
        'message' => $message,
        'client_note' => $client_note,
        'payment_trans_id'   => $payment_trans_id,
        'updated_at' => gettime(),
        'log' => json_encode($result) ?? $withdraw['log'],
    ], "`id` = '".$withdraw['id']."' ");
     exit();
  } 
  
  
  
  elseif ($withdraw['vendor'] == 'ApiV3Web2m')
  {
     if( empty($passwordMomo))
     {
      $IULA->update("wallet_orders_ruttien", [
                'status'  => 'hold', 
                'message'   => 'Vui lòng điền password vào cổng tt momo',  
                'client_note'   => 'Tạm giữ, vui lòng chờ xử lý lại',
                'updated_at'   => gettime(),
            ], " `id` = '".$withdraw['id']."' ");
             exit();    
     }    
    require_once(__DIR__ . "/../../frontend/paygate/providers/ApiV3Web2m.php");
    $ApiV3Web2m_help = new ApiV3Web2m();
    $result = $ApiV3Web2m_help->transferMomo($Token, $withdraw['account_num'], floatval($withdraw['net_amount']), $withdraw['description'], $passwordMomo);
    $status = 'hold';
    $message = $result['msg'] ?? 'Tạm giữ tiền do chuyển thất bại';
    $client_note = 'Tạm giữ';
    $payment_trans_id = '';
    if ($status && $result['status'] == 200) {
        $status = 'completed';
        $message = $result['msg'] ?? 'Chuyển tiền thành công';
        $client_note = 'Chuyển tiền thành công';
        $IULA->update("paygates", ['balance' => $result['balance'] ?? $infoPaygates['balance']], "`id` = '" . $infoPaygates['id'] . "'");
    }
    $IULA->update("wallet_orders_ruttien", [
        'status' => $status,
        'message' => $message,
        'client_note' => $client_note,
        'updated_at' => gettime(),
        'log' => json_encode($result) ?? $withdraw['log'],
        'payment_trans_id'   => $payment_trans_id,
    ], "`id` = '".$withdraw['id']."' ");
     exit();
  }




  elseif ($withdraw['vendor'] == 'ApiSieuthicode') 
  {
   //BÁO THIẾU TOKEN 
     if( empty($passwordMomo))
     {
      $IULA->update("wallet_orders_ruttien", [
                'status'  => 'hold', 
                'message'   => 'Vui lòng điền password vào cổng tt momo',  
                'client_note'   => 'Tạm giữ, vui lòng chờ xử lý lại',
                'updated_at'   => gettime(),
            ], " `id` = '".$withdraw['id']."' ");
             exit();    
     }       
    require_once(__DIR__ . "/../../frontend/paygate/providers/ApiSieuthicode.php");
    $ApiSieuthicode_help = new ApiSieuthicode();
    $result = $ApiSieuthicode_help->transferMomo($Token, $withdraw['account_num'], floatval($withdraw['net_amount']), $withdraw['description'], $passwordMomo);
    
    $status = 'hold';
    $message = $result['msg'] ?? 'Tạm giữ tiền do chuyển thất bại';
    $client_note = 'Tạm giữ';
    $payment_trans_id = '';
    if ($result['status'] == 2) {
        $status = 'completed';
        $message = $result['msg'] ?? 'Chuyển tiền thành công';
        $client_note = 'Chuyển tiền thành công';
        $IULA->update("paygates", ['balance' => $result['balance'] ?? $infoPaygates['balance']], "`id` = '" . $infoPaygates['id'] . "'");
    }
    
    $IULA->update("wallet_orders_ruttien", [
        'status' => $status,
        'message' => $message,
        'client_note' => $client_note,
        'updated_at' => gettime(),
        'payment_trans_id'   => $payment_trans_id,
        'log' => json_encode($result) ?? $withdraw['log'],
    ], "`id` = '".$withdraw['id']."' ");
     exit();
 } 
 
 
 
 elseif ($withdraw['vendor'] == 'ApiDichvudark')
 {
     if( empty($passwordMomo))
     {
      $IULA->update("wallet_orders_ruttien", [
                'status'  => 'hold', 
                'message'   => 'Vui lòng điền password vào cổng tt momo',  
                'client_note'   => 'Tạm giữ, vui lòng chờ xử lý lại',
                'updated_at'   => gettime(),
            ], " `id` = '".$withdraw['id']."' ");
             exit();    
     }    
    require_once(__DIR__ . "/../../frontend/paygate/providers/ApiDichvudark.php");
    $ApiDichvudark_help = new ApiDichvudark();
    $result = $ApiDichvudark_help->transferMomo($Token, $withdraw['account_num'], floatval($withdraw['net_amount']), $withdraw['description'], $passwordMomo);
    
    $status = 'hold';
    $message = $result['msg'] ?? 'Tạm giữ tiền do chuyển thất bại';
    $client_note = 'Tạm giữ';
    $payment_trans_id = '';
    if ($status && $result['status'] == 200) {
        $status = 'completed';
        $message = $result['msg'] ?? 'Chuyển tiền thành công';
        $client_note = 'Chuyển tiền thành công';
        $IULA->update("paygates", ['balance' => $result['balance'] ?? $infoPaygates['balance']], "`id` = '" . $infoPaygates['id'] . "'");
    }
    
    $IULA->update("wallet_orders_ruttien", [
        'status' => $status,
        'message' => $message,
        'client_note' => $client_note,
        'updated_at' => gettime(),
        'payment_trans_id'   => $payment_trans_id,
        'log' => json_encode($result) ?? $withdraw['log'],
    ], "`id` = '".$withdraw['id']."' ");
     exit();
 }


 elseif ($withdraw['vendor'] == 'ApiWebme')
 {
    require_once(__DIR__ . "/../../frontend/paygate/providers/ApiWebme.php");
    $ApiWebme_help = new ApiWebme($IULA);
     $result = $ApiWebme_help->transferWebme($Token, $withdraw['paygate_code'], $withdraw['account_num'], $withdraw['account_name'], floatval($withdraw['net_amount']), $withdraw['description']);
    
    $status = 'hold';
    $message = 'Lỗi';
    $client_note = 'Lỗi , chờ xly lại sau nhé';
    $payment_trans_id = '';
    if ($status && $result['status'] == 1) {
        $status = 'processing';
        $message   = check_status_ApiWebme($result['status'], $result['message'] ??  'Lỗi không xác định');
        $client_note = 'Đang xử lý đơn hàng';
        $payment_trans_id = $result['data']['order_code'] ;
    }
    
    $IULA->update("wallet_orders_ruttien", [
        'status' => $status,
        'message' => $message,
        'client_note' => $client_note,
        'updated_at' => gettime(),
        'payment_trans_id'   => $payment_trans_id,
        'log' => json_encode($result) ?? $withdraw['log'],
    ], "`id` = '".$withdraw['id']."' ");
     exit();
 }
 
 else 
  {
   $IULA->update("wallet_orders_ruttien", [
        'status' => 'hold',
        'message' => 'Ngân hàng rút không có cổng thanh toán, hệ thống tạm giữ tiền',
        'client_note' => 'Vui lòng chờ admin xử lý  đơn hàng',
        'updated_at' => gettime(),
        'payment_trans_id'   => '',
    ], "`id` = '".$withdraw['id']."' ");  
     exit();
  }
}
    
     echo 'hệ thống tự động xử lý đơn rút momo';
    */
    