<?php 
    include "Admin/config/setup.php"; 
    session_start();
    $bid = $_SESSION['buyer_id'];
    $customer_type = $_SESSION['buyer_type'];
    $sql = $config->query("SELECT * FROM payment_settings WHERE payment_gateway = 'Instamojo' AND status = 1 " );
    $res = $sql->fetch_assoc();
    $key = $res['payment_key'];
    $token = $res['payment_token'];
    $payment_gateway = $res['payment_gateway'];
    $url = "https://www.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'];
            
            if($customer_type == 1){
                $sql = "INSERT INTO buypack_master(user_id, transaction_id, buy_amount, payment_mode, buy_on, buy_status) VALUES('$bid', '$payment_id', '$unit_price', '$payment_mode', '$created_at', 2)";
                if($config->query($sql) === TRUE){
                    $insert_id = $config->insert_id;
                    $subsql = "SELECT * FROM ewallet_master WHERE user_id = '$bid' " ;
                    $result = $config->query($subsql);
                    $subrow = $result->fetch_assoc();
                    $wal_id = $subrow['user_id'];
                    $wallet = $subrow['total_amount'] + $unit_price;
        
                    $sub_sql = $config->query("UPDATE ewallet_master SET total_amount = '$wallet' WHERE user_id = '$wal_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)" );

                    $_SESSION['check'] = 'Payment Successful';
                    $_SESSION['alert'] = 'success';
                    header('Location:applied_history.php');
                }else{
                    $_SESSION['check'] = 'Something Went Wrong.Contact Support.';
                    $_SESSION['alert'] = 'warning';
                    header('Location:applied_history.php');
                }
            }else if($customer_type == 2){
                
            }
        }
        // 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:applied_history.php');
        }else if($customer_type == 2){
                
        }
    }


?>