<?php
/**
 * Created by PhpStorm.
 * User: varun_2
 * Date: 27-02-2016
 * Time: 15:37
 */

namespace App\Http\Controllers\Action;


use App\Http\Controllers\Auth\User;
use DB ;

class Payment
{
    private $order_id ;
    private $amount ;
    private $currency ;
    private $payment_gateway ;
    private $type ;
    private $log_id ;
    private $mem_id ;
    private $status ;

    private $data ;

    function __construct( $amount, $currency, $payment_gateway, $type, $memid, $data )
    {
        $this->amount = $amount ;
        $this->currency = $currency ;
        $this->payment_gateway = $payment_gateway ;
        $this->type = $type ;
        $this->mem_id = $memid ;
        $this->data = $data ;
    }

    /**
     * For logging payment before payment paid
     */
    public function log_payment_before( $order_id , $log_id )
    {
        $this->order_id = $order_id ;
        $this->log_id = $log_id ;

        $res['account'] = $this->payment_gateway ;
        $res['log_id'] = $this->log_id ;
        $res['order_id'] = $this->order_id  ;
        $res['amount'] = $this->amount ;
        $res['currency'] = $this->currency ;
        $res['type'] = $this->type ;
        $this->status = $res['status'] = 'pending' ;
        $res['data'] = json_encode( $this->data ) ;
        DB::table('payment_log')
            ->insert($res) ;
    }

    /**
     * Verify payment gateway amount send and log after payment if successful
     * @return array|bool
     */
    public function verify_payment( $order_id  )
    {
        $this->order_id = $order_id ;
        $data = DB::table('payment_log')
            ->select('amount' , 'currency' , 'status', 'type' , 'log_id' , 'data')
            ->where('order_id' , '=' , $this->order_id )
            ->first();

        $this->data = json_decode($data['data'] , true ) ;

        $this->type = $data['type'] ;

        if(is_array($data) && count($data))
        {
            $this->log_id = $data['log_id'] ;
            if( strcasecmp( $this->payment_gateway , 'ccavenue') == 0 )
            {
                if( $data["amount"] == $this->amount )
                {
                    /**
                     * Payment successful
                     */
                    if($this->type == 'event')
                    {
                        if($this->log_after_payment_delegate())
                            return true ;
                        else
                            return array(
                                "status" => "1311" ,
                                "message" => "Payment Unsuccessful"
                            ) ;
                    }
                    elseif ($this->type == 'user')
                    {
                        if($this->log_after_user_registration())
                            return true ;
                        else
                            return array(
                                "status" => "1312" ,
                                "message" => "Payment Unsuccessful"
                            ) ;
                    }
                    else
                        return array(
                           "status#" => "1313" ,
                            "message#" => "payment module not found"
                        ) ;
                    return array(
                        "status#" => "1" ,
                    ) ;
                }
                /**
                 * Payment successful but with non-similarity
                 */
                else
                {
                    DB::table('payment_log')
                        ->update(["status" => "hack"])
                        ->where('order_id' , '=' , $this->order_id ) ;
                    return array(
                        "status#" => "1317" ,
                        "message" => "Same amount not paid"
                    ) ;
                }

            }
            elseif (strcasecmp( $this->payment_gateway , 'paypal') == 0 )
            {
//                echo "In paypal" ;
                if( $data["amount"] == $this->amount && $data['currency'] == $this->currency && $data["status"] == 'pending' )
                {
                    /**
                     * Payment successful
                     */
                    if($this->type == 'event')
                    {

                        if($this->log_after_payment_delegate())
                            return true ;
                        else
                            return array(
                                "status" => "1314" ,
                                "message" => "Payment Unsuccessful"
                            ) ;
                    }
                    elseif ($this->type == 'user')
                    {
                        if($this->log_after_user_registration())
                            return true ;
                        else
                            return array(
                                "status" => "1315" ,
                                "message" => "Payment Unsuccessful"
                            ) ;
                    }
                    else
                        return array(
                            "status" => "1316" ,
                            "message" => "payment module not found"
                        ) ;

                    return array(
                        "status" => "100"
                    ) ;

                }
                else
                {
                    DB::table('payment_log')
                        ->where('order_id' , '=' , $this->order_id )
                        ->update(["status" => "hack"]);

                    return array(
                        "status" => "1317" ,
                        "message" => "Not similar payment amount"
                    ) ;
                }

                /**
                 * Payment successful but with non-similarity
                 */
            }
            elseif( strcasecmp($this->payment_gateway , 'inapp') == 0 )
            {
                if( $data["amount"] == $this->amount && $data['currency'] == $this->currency )
                {
                    /**
                     * Payment successful
                     */
                    if ($this->type == 'user')
                    {
                        if($this->log_after_user_registration())
                            return true ;
                        else
                            return array(
                                "status" => "1318" ,
                                "message" => "Payment Unsuccessful"
                            ) ;
                    }

                    else
                        return array(
                            "status" => "1319" ,
                            "message" => "payment module not found"
                        ) ;

                    return array(
                        "status" => "100"
                    ) ;
                }
                else
                    return array(
                        "status" => "1317" ,
                        "message" => "Same amount not paid"
                    ) ;


                /**
                 * Payment successful but with non-similarity
                 */
            }
        }
        else
            return array(
                "status" => "1330" ,
                "message" => "OrderID not found"
            );
    }


    /**
     * Todo:
     * 1. Verified for event => paypal (done)
     * 2. Verified for event => ccavenue
     * 3. Verified for user => inapp
     * 4. verified for user => paypal
     * 5. verified for user => ccavenue
     */
    private function log_after_payment_delegate()
    {

        require ("/home/steelmin/public_html/mf.php");
        include ("/home/steelmin/public_html/hf.php");

        require ($_ENV['COMMON_ROOT_PATH'].'/config1.php');

//        echo "before get new" ;
        $final_inv_no = getnewInvNo('T', 'Events');
        $Order_Id = $this->order_id ;

//        echo "after get new" ;

        $u = new User($this->mem_id) ;
        $user = $u.get($this->mem_id);



        /**
         * For IpAddress
         */
        $this->data['ip'] = $_SERVER['REMOTE_ADDR'];
        if (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)) {
            $this->data['ip'] = array_pop(explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']));
        }

        onSuccessfulTranscation("online", $Order_Id, $final_inv_no, $user['name'], $user['email'], $this->mem_id, $user['phoneno'], $this->log_id ,"NEFT/Online", strtoupper($this->payment_gateway) .": ".$this->order_id , $this->currency , $this->data );

//        echo "after on success" ;
        require ($_ENV['COMMON_ROOT_PATH'].'/desconfig.php' );

        DB::table('payment_log')
            ->where('log_id' , '=' , $this->log_id )
            ->update(['status' => "success"]);
        return true ;
    }

    private function log_after_user_registration()
    {
        include_once ($_ENV['COMMON_ROOT_PATH'].'/reg_process_steps.php') ;
        $return = processDetailsAfterPymt($this->mem_id, $this->log_id, $this->data, $this->status , 'mobile' , $this->payment_gateway , $this->amount );

        if($return['message'] == 'Payment done Succesfully')
        {
            DB::table('payment_log')
                ->where('log_id' , '=' , $this->log_id )
                ->update(['status' => "success"]);
            return true ;
        }
        else
            return array(
                "status#" => "0" ,
                "message#" => "Payment failed"
            ) ;
    }

}