<?php
    include "Admin/config/setup.php";
    session_start();
    if(isset($_POST['pay'])){
        $purpose = $_POST['purpose'];
        $amount = $_POST['amount'];
        $buyer_name = $_POST['name'];
        $_SESSION['buyer_id'] = $_POST['bid'];
        $_SESSION['buyer_type'] = $_POST['type'];
        $email = $_POST['email'];
        $phone = $_POST['phone'];
        $sql = $config->query("SELECT * FROM payment_settings WHERE status = 1 " );
        $res = $sql->fetch_assoc();
        if($res['payment_gateway'] == 'Instamojo'){
            $key = $res['payment_key'];
            $token = $res['payment_token'];
            $url = "https://www.instamojo.com/api/1.1/";
            include "instamojo/src/Instamojo.php";
            $api = new Instamojo\Instamojo($key, $token, $url);
            try {
                $response = $api->paymentRequestCreate(array(
                    "purpose" => $purpose,
                    "amount" => $amount,
                    "buyer_name" => $buyer_name,
                    "send_email" => true,
                    "email" => $email,
                    "send_sms" => true,
                    "phone" => $phone,
                    "allow_repeated_payments" => 'false',
                    "redirect_url" => "https://www.addmyentry.com/success.php"
                    ));
                    $url = $response['longurl'];
                    header('Location:'.$url);
                    exit();
                // print_r($response);
            }
            catch (Exception $e) {
                print('Error: ' . $e->getMessage());
            }
        }
    }else if(isset($_POST['apply'])){
        $eid = $_SESSION['pay_event'] = $_POST['pay_event'];
        $uid = $_SESSION['user_id'] ;
        $username = $_SESSION['user_name'] ;
        $email = $_SESSION['user_email'] ;
        $phone = $_SESSION['user_phone'] ;
        
        $entry_sql = $config->query("SELECT * FROM event_post WHERE event_id = '$eid' ");
        $entry_res = $entry_sql->fetch_assoc();
        $entry = $entry_res['entry'];
        if($entry == 'paid'){
            $amount = $_SESSION['price'];
        }else if($entry == 'multi'){
            $amount = $_SESSION['price'];
        }
        
        $purpose = 'Apply Tournament';
        $sql = $config->query("SELECT * FROM payment_settings WHERE status = 1 " );
        $res = $sql->fetch_assoc();
        if($res['payment_gateway'] == 'Instamojo'){
            $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);
            try {
                $response = $api->paymentRequestCreate(array(
                    "purpose" => $purpose,
                    "amount" => $amount,
                    "buyer_name" => $username,
                    "send_email" => true,
                    "email" => $email,
                    "send_sms" => true,
                    "phone" => $phone,
                    "allow_repeated_payments" => 'false',
                    "redirect_url" => "https://www.addmyentry.com/events-apply-success.php"
                    ));
                    $url = $response['longurl'];
                    header('Location:'.$url);
                    exit();
                // print_r($response);
            }
            catch (Exception $e) {
                print('Error: ' . $e->getMessage());
            }
        }
    }

?>