<?php
class Ikajo extends CFormModel
{
	public $type = 'ikajo';
    private $typeOnServer = 21;
	public $ccDetails = true;

    public $action;
    public $url;

    private $key;
    private $pass;

    public $payment = 'CC';
    public $data;
    public $sign;

    public $amount;
    public $description;
    public $account;
    public $currency;

    public $email;
    public $order;
    public $card;
    public $status;

    public $ext5;

    private $currencies = array('USD');

	public function rules(){
		return array(
            array('amount, description, currency', 'required', 'on'=>'deposit', 'message'=> Yii::t("main","36_cannot_be_blank")),

            array('email, order, card, sign, status, description, amount, currency, ext5', 'required', 'on'=>'response', 'message'=> Yii::t("main","36_cannot_be_blank")),
//            array('refrence_no_real, status, additional_memo', 'safe', 'on'=>'response', 'message'=> Yii::t("main","36_cannot_be_blank")),

            array('amount, account, currency', 'required', 'on'=>'return', 'message'=> Yii::t("main","36_cannot_be_blank")),
		);
	}

	public function init()
	{
        $this->action = 'https://secure.fxpaymentservices.com/payment/auth';
        $this->key = 'BRHDULC3SN';
        $this->pass = 'yZubDf3cbBG0ru5b1TF1xny0a7eJCd2y';
	}

    public function getAttribute($name){
        return $this->$name;
    }

    public function attributeLabels(){
        return array(
            'item_name' => Yii::t("main",'157_account'),
            'price' => Yii::t("funds",'9_amount'),
        );
    }

    private function generateSign()
    {
        //var_dump(strrev($this->key), strrev($this->payment), strrev($this->data), strrev($this->url), strrev($this->pass));die();
        $sign = md5(strtoupper( strrev($this->key).
            strrev($this->payment).
            strrev($this->data).
            strrev($this->url).
            strrev($this->pass)
        ));

        return $sign;
    }

    public function initData($data)
    {
        //var_dump($data);die();

        $data2 = array();
        $http = (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == 'on') ? 'https:' : 'http:';

        $Registration = new Registration('Individual');
        $Registration->initIndv();
        $countries_code_two = $Registration->GetCountry('two');

        $data2['data']['ext1'] = $this->amount = $data['amount'];
        $data2['data']['ext2'] = $this->description = (int)$data['transferTo'];
        $data2['data']['ext3'] = $this->currency = $data['currency'];
        $data2['data']['ext4'] = $data2['data']['order'] = $this->order =  uniqid();
        $data2['data']['ext5'] = Yii::app()->params['type'];

        $data2['data']['first_name'] = $Registration->firstName;
        $data2['data']['last_name'] = $Registration->lastName;
        $data2['data']['city'] = $Registration->city;
        $data2['data']['address'] = $Registration->address; //added by Arsen 24/01/2020
        $data2['data']['country'] = $countries_code_two[$Registration->country];
        $data2['data']['phone'] = $Registration->phone;
        $data2['data']['email'] = $Registration->email;
        $data2['data']['lang'] = 'en';

        $data2['data']['payment'] = $this->payment;
        $data2['data']['key'] = $this->key;
        $data2['data']['url'] = $this->url = $http.'//'.$_SERVER["SERVER_NAME"].'/ikajoTrans.html?isUser&success&account='.$this->description.'&amount='.$this->amount.'&currency='.$this->currency;
        $data2['data']['error_url'] = $http.'//'.$_SERVER["SERVER_NAME"].'/ikajoTrans.html?isUser&cancel';
        $data2['data']['data'] = $this->data = base64_encode(json_encode(array('amount' => number_format($this->amount, 2, '.', ''),'description' => $this->description, 'currency' => $this->currency)));

        $data2['data']['sign'] = $this->generateSign();



        $data2['type'] = $this->type;
        $data2['action'] = $this->action;

        $data2["message"] = Yii::t("funds","446_you_will_redir_secure_payment");

        return $data2;
    }

    public function newTransaction($accountID, $value, $transactionID, $currency = 'USD', $Id = ''){
        $Info = Yii::app()->curl->get(Yii::app()->params['json_server'].'/API/Transactions/NewTransaction?guid='.Yii::app()->user->id.'&transactionID='.trim($transactionID).'&currency='.$currency.'&value='.$value.'&Id='.$Id.'&accountID='.$accountID.'&trType='.$this->typeOnServer);
        $f = fopen($_SERVER["DOCUMENT_ROOT"].'/protected/runtime/log/ikajo.txt',"a");
        fwrite($f, 'newIkajoTransaction '.date('Y-m-d H:i:s').'  '.Yii::app()->params['json_server'].'/API/Transactions/NewTransaction?guid='.Yii::app()->user->id.'&transactionID='.trim($transactionID).'&currency='.$currency.'&value='.$value.'&Id='.$Id.'&accountID='.$accountID.'&trType='.$this->typeOnServer.' ||||| '.json_encode($Info).'<br/>\n
		');

        /*$Info2 = Yii::app()->curl->get(Yii::app()->params['json_server_eforex'].'/API/Transactions/NewTransaction?guid='.Yii::app()->user->id.'&transactionID='.trim($transactionID).'&currency='.$currency.'&value='.$value.'&Id='.$Id.'&accountID='.$accountID.'&trType='.$this->typeOnServer);
        $f = fopen($_SERVER["DOCUMENT_ROOT"].'/protected/runtime/log/ikajo.txt',"a");
        fwrite($f, 'newIkajoTransaction '.date('Y-m-d H:i:s').'  '.Yii::app()->params['json_server_eforex'].'/API/Transactions/NewTransaction?guid='.Yii::app()->user->id.'&transactionID='.trim($transactionID).'&currency='.$currency.'&value='.$value.'&Id='.$Id.'&accountID='.$accountID.'&trType='.$this->typeOnServer.' ||||| '.json_encode($Info2).'<br/>\n
		');*/

        return  $Info;
    }

	public function ApplyTransaction(){

	    $server = (in_array($this->ext5, ['t2', 'eforex'])) ? Yii::app()->params['json_server_eforex'] : Yii::app()->params['json_server'];

	    //var_dump(Yii::app()->params['json_server'].'/API/Transactions/ApplyFxTransaction?errorcode='.(($this->status) ? 'SALE' : 'ERROR').'&transactionid='.$this->order.'&depValue=0&currency=');die();
	    $Info = Yii::app()->curl->get($server.'/API/Transactions/ApplyFxTransaction?errorcode='.(($this->status = 'SALE') ? 'SALE' : 'ERROR').'&transactionid='.$this->order.'&depValue='.$this->amount.'&currency='.$this->currency);
        //$Info = 'block';

        $f = fopen($_SERVER["DOCUMENT_ROOT"].'/protected/runtime/log/ikajo.txt',"a");
        fwrite($f, 'ApplyIkajoransaction '.date('Y-m-d H:i:s').' '.$server.'/API/Transactions/ApplyFxTransaction?errorcode='.(($this->status == 'SALE') ? 'SALE' : 'ERROR').'&transactionid='.$this->order.'&depValue='.$this->amount.'&currency='.$this->currency.' ||||| '.json_encode($Info).'<br/>\n
		');


	    /*$Info2 = Yii::app()->curl->get($server.'/API/Transactions/ApplyFxTransaction?errorcode='.(($this->status = 'SALE') ? 'SALE' : 'ERROR').'&transactionid='.$this->order.'&depValue='.$this->amount.'&currency='.$this->currency);
        $f = fopen($_SERVER["DOCUMENT_ROOT"].'/protected/runtime/log/ikajo.txt',"a");
        fwrite($f, 'ApplyIkajoransaction '.date('Y-m-d H:i:s').' '.$server.'/API/Transactions/ApplyFxTransaction?errorcode='.(($this->status == 'SALE') ? 'SALE' : 'ERROR').'&transactionid='.$this->order.'&depValue='.$this->amount.'&currency='.$this->currency.' ||||| '.json_encode($Info2).'<br/>\n
		');*/


        return  $Info;
	}

    public function checkTransactionStatus()
    {
        return $this->status == 'SALE';
	}

    public function checkResponse()
    {
        $sign =  md5(strtoupper(
            strrev($this->email) .
            $this->pass .
            $this->order .
            strrev(substr($this->card, 0, 6) . substr($this->card, -4))
        ));

        return $this->sign === $sign;
	}




}