<?php
include_once('../system/lib/config.php');
$system = new System();

$ReferenceID    = $system -> getDataPost('Reference', true, false, false, true);
$Status         = $system -> getDataPost('Status', true);
$lang           = $_REQUEST['lang'];

//error_log paths
$apiPath 		= dirname(SYSTEM_PATH).DIRECTORY_SEPARATOR.'api';
$responseUrl 	= $apiPath.DIRECTORY_SEPARATOR.'Logs'.DIRECTORY_SEPARATOR.'H2P'.DIRECTORY_SEPARATOR.'deposit_response';
$responseDbUrl 	= $apiPath.DIRECTORY_SEPARATOR.'Logs'.DIRECTORY_SEPARATOR.'H2P'.DIRECTORY_SEPARATOR.'deposit_db_response';

//check if directory exists if not, create directory
if(!file_exists($responseUrl)) {
    mkdir($responseUrl, 0777, true);
}

if(!file_exists($responseDbUrl)) {
    mkdir($responseDbUrl, 0777, true);
}

$sqlObject = new mysql($system->getConfigValue(SECTION_DB,HOST_NAME),$system->getConfigValue(SECTION_DB,USER_NAME),$system->getConfigValue(SECTION_DB,LOGIN_PASS),$system->getConfigValue(SECTION_DB,TARGET_DB_NAME),'','UTF8');
$system -> transactionNumber = $system -> getDataPost("Reference");
//// GET data from t_deposit
$sqlstrR = 'select * from t_deposit where transaction_number = "'.$ReferenceID.'"';
$resultR = $sqlObject -> query($sqlstrR);
$rowR = $sqlObject -> fetch_assoc($resultR);
if(count($rowR) == 0) {
  //// GET data from temporary
  $sqlstr = 'select * from t_deposit_help2pay_temporary where transaction_number = "'.$ReferenceID.'"';
  $result = $sqlObject -> query($sqlstr);
  $row = $sqlObject -> fetch_assoc($result);
  if(count($row) > 0){
    if($Status == "000" || $Status == "006") {
      $sqlstr2 = 'select * from t_deposit where transaction_number = "'.$ReferenceID.'"';
      $result2 = $sqlObject -> query($sqlstr2);
      $row2 = $sqlObject -> fetch_assoc(2);
      if($row2 == NULL){
          
          $comment = "Bank:".$row['deposit_bank']
                    .",Merchant:".$system  -> getDataPost('Merchant', true)
                    .",Status:".$system  -> getDataPost('Status', true)
                    .",Currency:".$system  -> getDataPost('Currency', true)
                    .",Amount:".$system  -> getDataPost('Amount', true)
                    .",Datetime.".$system  -> getDataPost('Datetime', true)
                    .",Customer:".$system  -> getDataPost('Customer', true)
                    .",Language:".$system  -> getDataPost('Language', true)
                    .",Reference:".$system  -> getDataPost('Reference', true)
                    .",ID:".$system  -> getDataPost('ID', true)
                    .",Key:".$system  -> getDataPost('Key', true)
                    .",Note:".$system  -> getDataPost('Note', true);
          
            $HELP2PAY['Model'] = array();
            $HELP2PAY['Model']['transaction_number'] = $row['transaction_number'];
            $HELP2PAY['Model']['user_account'] = $row['user_account'];
            $HELP2PAY['Model']['amount'] = $row['amount'];
            $HELP2PAY['Model']['currency'] = $row['currency'];
            $HELP2PAY['Model']['method'] = $row['method'];
            $HELP2PAY['Model']['isSendMail'] = '0';
            $HELP2PAY['Model']['deposit_date'] = $row['deposit_date'];
            $HELP2PAY['Model']['deposit_bank'] = "Local Bank Transfer(Southeast Asia)";
            $HELP2PAY['Model']['fee'] = '0';
            $HELP2PAY['Model']['message'] = "Local Bank Transfer(Southeast Asia)";
            $HELP2PAY['Model']['comment'] = $comment;
            $HELP2PAY['Model']['create_time'] = $row['create_time'];
            $HELP2PAY['Model']['process_user'] = '9999';
            $HELP2PAY['Model']['old_filename'] = "";
            $HELP2PAY['Model']['new_filename'] = "";
            $HELP2PAY['Model']['type'] = '0';
            $HELP2PAY['Model']['err_flg'] = '0';
            $HELP2PAY['Model']['msecond'] = "0";
            $sqlObject = new mysql($system->getConfigValue(SECTION_DB,HOST_NAME),$system->getConfigValue(SECTION_DB,USER_NAME),$system->getConfigValue(SECTION_DB,LOGIN_PASS),$system->getConfigValue(SECTION_DB,TARGET_DB_NAME),'','UTF8');
            $sql = "insert into t_deposit";
            //进deposit表的sql方法
            $strsql = $sqlObject->Insertsql($HELP2PAY['Model']);
            $sql .= $strsql;
            $sqlObject->query($sql);
            // EMAIL PART
            $rowForUser = $system -> getRowData($system -> getAccountCommon($row['user_account']));
            $params = array();
            $params[] = $row['user_account'];
            $params[]	= $system -> getColumnData($rowForUser, 'first_name')." ".$system -> getColumnData($rowForUser, 'last_name');
            $params[]	= $row['transaction_number'];
            $params[]	= $row['create_time'];
            $params[]	= $row['currency'];
            $params[]	= $system -> getAmountString($row['amount'], $row['currency']);
            $params[]	= $row['currency'];
            $params[]	= $system -> getAmountString(($row['amount'] + $row['fee']),  $row['currency']);
            $params[] = $system -> getColumnData($rowForUser, 'country');
            // SEND on Template
            if($lang == "id") {
              $language = "id";
            } else {
              $language = "en";
            }
            $system -> sendMailByTmp($language.DIRECTORY_SEPARATOR.'help2pay_deposit_info.xml'
                        , $params
                        , $system -> getColumnData($rowForUser, COLUMN_MAIL)
                        , VAR_CS_MAIL_ADDRESS
                        , $system -> getColumnData($rowForUser, 'language'));
    
            $system -> accessModifyCommon('UPDATE_DEPOSIT_ISSENDMAIL',array($ReferenceID));
      }
    }
  }
}

//log the transaction details
error_log(date("[Y-m-d H:i:s]")."\r\n".$ReferenceID.' -deposit_response:' ."\r\n". $Status ."\r\n", 3, $responseUrl.DIRECTORY_SEPARATOR.'Log_'.date("Y-m-d").'.log');
chmod($responseUrl.DIRECTORY_SEPARATOR.'Log_'.date("Y-m-d").'.log',511);

echo "0";
?>
