<?php 
    include "Admin/config/setup.php"; 
    session_start();
    $uid = $_SESSION['user_id'] ;
    $eid = $_SESSION['pay_event'];
    $customer_type = 1;
    $sql = $config->query("SELECT * FROM payment_settings WHERE payment_gateway = 'Instamojo' AND status = 1 " );
    $res = $sql->fetch_assoc();
    $payment_gateway = $res['payment_gateway'];
     $key = $res['payment_key'];
     $token = $res['payment_token'];
     $url = "https://www.instamojo.com/api/1.1/";
     // $key = 'test_137ac1dd89632138cb65392802a';
     // $token = 'test_5c89ac4b65e8a65e31e3132aa0d';
     // $url ="https://test.instamojo.com/api/1.1/";
    include "instamojo/src/Instamojo.php";
    $api = new Instamojo\Instamojo($key, $token, $url);
    $pay_id = $_GET['payment_request_id'];
    try {
        $response = $api->paymentRequestStatus($pay_id);
        if($response['payments'][0]['status']=='Credit'){
            $payment_order_id = $response['id'];
            $payment_id = $response['payments'][0]['payment_id'];
            $payment_mode = 'Online';
            $buy_amount = $response['payments'][0]['amount'];
            $name = $response['payments'][0]['buyer_name'];
            $phone = $response['payments'][0]['buyer_phone'];
            $email = $response['payments'][0]['buyer_email'];
            $unit_price = $response['payments'][0]['unit_price'];
            $fees = $response['payments'][0]['fees'];
            $created_at = $response['payments'][0]['created_at'];
            $purpose = 'Event';
            
            $entry_sql = $config->query("SELECT * FROM event_post WHERE event_id = '$eid' ");
            $entry_res = $entry_sql->fetch_assoc();
            $entry = $entry_res['entry'];
            
            $chksql = $config->query("SELECT * FROM players WHERE user_id = '$uid' " );
            
            $sql = "INSERT INTO buypack_master(user_id, transaction_id, purpose, event_id, buy_amount, payment_mode, buy_on, buy_status) VALUES('$uid', '$payment_id', '$purpose', '$eid', '$unit_price', '$payment_mode', '$created_at', 2)";
            if($config->query($sql) === TRUE){
                $insert_id = $config->insert_id;
                $order_sql = $config->query("INSERT INTO online_transactions(order_id, cutomer_name, customer_type, email, phone, ref_id, payment_id, payment_gateway, paid_amount, unit_price, fees, paid_on, status)
                    VALUES('$insert_id', '$name', '$customer_type', '$email', '$phone', '$payment_order_id', '$payment_id', '$payment_gateway', '$buy_amount', '$unit_price', '$fees', '$created_at', 1)" );
            
                foreach($chksql as $chk){
                    $ply_id = $chk['id'];
                    if(array_search($ply_id,$_SESSION['pid'][$ply_id])){
                        if($entry == 'paid'){
                            $comm_a = $_SESSION['pid'][$ply_id]['comm_a'];
                            $entry_fees = $_SESSION['pid'][$ply_id]['amt'];
                            $entry_type = $_SESSION['pid'][$ply_id]['cate'];
                            $sql = $config->query("INSERT INTO apply_request(user_id, event_id, player_id, entry, entry_type, entry_fees, apply_status,comm_amt,pay_id)
                            VALUES('$uid', '$eid', '$ply_id','$entry','$entry_type', '$entry_fees', 1,'$comm_a','$insert_id') ");
                        }else if($entry == 'multi'){
                            $comm_a = $_SESSION['pid'][$ply_id]['comm_a'];
                            $entry_fees = $_SESSION['pid'][$ply_id]['amt'];
                            $entry_type = $_SESSION['pid'][$ply_id]['cate'];
                            $sql = $config->query("INSERT INTO apply_request(user_id, event_id, player_id, entry, entry_type, entry_fees, apply_status,comm_amt,pay_id)
                            VALUES('$uid', '$eid', '$ply_id','$entry', '$entry_type', '$entry_fees', 1,'$comm_a','$insert_id') ");
                        }
                    }   
                }
                $_SESSION['check'] = 'Applied Successful';
                $_SESSION['alert'] = 'success';
                unset($_SESSION['price']);
                unset($_SESSION['pid']);
                header('Location:tournaments.php');
            }else{
                $_SESSION['check'] = 'Something Went Wrong.Contact Support.';
                $_SESSION['alert'] = 'warning';
                header('Location:tournaments.php');
            }
        }
        // echo '<pre>'; print_r($response); echo '</pre>';
    }
    catch (Exception $e) {
        print('Error: ' . $e->getMessage());
        if($customer_type == 1){
            $_SESSION['check'] = 'Payment Failed';
            $_SESSION['alert'] = 'error';
            header('Location:tournaments.php');
        }else if($customer_type == 2){
                
        }
    }


?>