<?php
include_once('config.php');

class H2PWithdrawalWebhook extends System {
 
    private $transactionNum     = NO_STRING;
    private $transactionKey     = NO_STRING;
    private $memberCode         = NO_STRING;
    private $status             = NO_STRING;
    
    // directories
    private $successLog;
    private $errorLog;
    private $accessLog;
    
    public function __construct(){
        parent::__construct();
        
        $this -> setParameter();
        $this -> validation();
    }
    
    private function setParameter(){
        $this -> transactionNum     = $this -> getDataPost("TransactionID");
        $this -> transactionKey     = $this -> getDataPost("Key");
        $this -> memberCode         = $this -> getDataPost("MemberCode");
        $this -> status             = $this -> getDataPost("Status");
        
        $this -> successLog = "Logs/H2P_Withdrawal/callback/success";
        $this -> errorLog   = "Logs/H2P_Withdrawal/callback/error";
        $this -> accessLog  = "Logs/H2P_Withdrawal/callback/access";
    }
    
    private function validation(){
        if(!file_exists($this -> successLog)) {
            mkdir($this -> successLog, 0777, true);
        }
        
        if(!file_exists($this -> errorLog)) {
            mkdir($this -> errorLog, 0777, true);
        }
        
        if(!file_exists($this -> accessLog)) {
            mkdir($this -> accessLog, 0777, true);
        }
    }
    
    private function emailParams($status, $result){
        $params = [];
        
        $memberCode = $this -> getColumnData($result, COLUMN_USER_ACCOUNT);
        $amount     = $this -> getColumnData($result, COLUMN_AMOUNT);
        
        switch($status){
            case "000":
                $date = date('Y-m-d H:i:s');
                $newdate = strtotime ('-1 hour' , strtotime($date)) ;
                $newdate = date('Y-m-d H:i:s', $newdate);
                
                $params = [
                    $memberCode,
                    $this -> getUserNameCommon($memberCode),
                    $this -> getColumnData($result, COLUMN_CURRENCY),
                    number_format($amount, 2, ".", NO_STRING),
                    $newdate
                ];
                
                break;
            case "001":
                $params = [
                    $memberCode,
                    $this -> getUserNameCommon($memberCode)
                ];
                break;
        }
        
        return $params;
    }
    
    private function logEvent($message, $logType){
        $logDir = NO_STRING;
        
        switch($logType){
            case VAL_INT_1:
                $logDir = $this -> accessLog;
                break;
            case VAL_INT_2:
                $logDir = $this -> errorLog;
                break;
            case VAL_INT_3:
                $logDir = $this -> successLog;
                break;
        }
        
        $logDate        = date("Y-m-d");
        $logTimeStamp   = date("[Y-m-d H:i:s]");
        
        error_log(
            "{$logTimeStamp}\r\n{$message}\r\n\r\n",
            VAL_INT_3,
            "{$logDir}/Log_{$logDate}.log"
        );
    }
    
    public function listen(){
        $response = print_r($_POST, true);
        
        //log the access details
        $this -> logEvent("Accessed: {$response}", VAL_INT_1);
        
        if($this -> transactionNum != NO_STRING && $this -> transactionKey != NO_STRING){            
            $result = $this -> getRowData($this -> accessSelect('SELECT_WITHDRAW_H2P_TEMP', array($this -> transactionNum)));
            
            if($this -> isLoopData($result)){
                $resultData = print_r($result, true);
                $this -> logEvent("Withdrawal Transaction Data: {$resultData}", VAL_INT_1);
                
                $memberCode =    $this -> getColumnData($result, COLUMN_USER_ACCOUNT);
                $bankCode       = $this -> getColumnData($result, COLUMN_RECEIPT_BANK_NAME);
                $language       = $this -> getColumnData($result, COLUMN_LANGUAGE);
                
                $param = [];
                
                if($this -> memberCode == $memberCode){
                    $param[] = $this -> transactionNum;
                    $param[] = $response.'Bank_code:'.$bankCode;
                    
                    switch($this -> status){
                        case "000":
                            //update withdraw
                            $param[] = VAL_INT_2;
                            $this -> accessModifyCommon('UPDATE_WITHDRAW_H2P_STATUS', $param);
                            $this -> accessModifyCommon('UPDATE_WITHDRAW_H2P_TEMP_STATUS', $param);
                             
                            $this -> sendMailByTmp(
                                "{$language}/withdraw_h2p_success.xml",
                                $this -> emailParams($this -> status, $result),
                                $this -> getUserEMailCommon($memberCode),
                                VAR_CS_MAIL_ADDRESS
                            );
                            
                            $this -> logEvent("success status:\r\n\r\n{$response}", VAL_INT_3);
                            break;
                        case "001":
                            //update withdraw
                            $param[] = VAL_INT_5;
                            $this -> accessModifyCommon('UPDATE_WITHDRAW_H2P_STATUS', $param);
                            $this -> accessModifyCommon('UPDATE_WITHDRAW_H2P_TEMP_STATUS', $param);
                            
                            $this -> sendMailByTmp("{$language}/withdraw_h2p_fail.xml",
                                $this -> emailParams($this -> status, $result),
                                $this -> getUserEMailCommon($memberCode),
                                VAR_CS_MAIL_ADDRESS
                            );
                            
                            $this -> logEvent("nFailed Status:\r\n\r\n{$response}", VAL_INT_2);
                            break;
                        default:
                            //error, status not valid
                            $this -> logEvent("Invalid Status Code:\r\n\r\n{$response}\r\n{$this -> status}", VAL_INT_2);
                            break;
                    }
                }else{
                    //error, member code not valid
                    $this -> logEvent("Invalid Member Code: {$this -> memberCode}", VAL_INT_2);
                }   
            }else{
                //error, transaction num not valid
                $this -> logEvent("Invalid Transaction Number:\r\n\r\n{$this -> transactionNum}", VAL_INT_2);
            }
        }
    }
}

$withdrawal = new H2PWithdrawalWebhook();
$withdrawal -> listen();