<?php

require('config.php');
session_start();
//db connection
$conn = mysqli_connect($host, $username, $password, $dbname);

require('razorpay-php/Razorpay.php');
use Razorpay\Api\Api;
use Razorpay\Api\Errors\SignatureVerificationError;

$success = true;

$error = 'Payment Failed';

if (empty($_POST['razorpay_payment_id']) == false) {
    $api = new Api($keyId, $keySecret);

    try {
        // Please note that the razorpay order ID must
        // come from a trusted source (session here, but
        // could be database or something else)
        $attributes = [
            'razorpay_order_id'   => $_SESSION['razorpay_order_id'],
            'razorpay_payment_id' => $_POST['razorpay_payment_id'],
            // 'razorpay_signature' => $_POST['razorpay_signature']
        ];

        // $api->utility->verifyPaymentSignature($attributes);
    } catch (SignatureVerificationError $e) {
        $success = false;
        $error   = 'Razorpay Error : ' . $e->getMessage();
    }
}

if ($success == true) {
    try {
        $db = new PDO('mysql:host=localhost;dbname=ructszmy_vibespublica;charset=utf8', 'root', '');
    } catch (Exception $e) {
        echo 'Error has Occured';
    }

    $ORDERID = $_POST['ORDERID'];
    $stmt    = $db->query("select * from transcationbook where OrderId='$ORDERID'");
    while ($row = $stmt->fetch()) {
        echo $row['name'] . '<br><b>Address:</b>' . $row['Address'] . '<br><b>Contact Details:</b>' . $row['contactDetail'] ;
        $Cid = $row['Cid'];
    }
    ?>
    </div>
    <div>
    <h4>Payment Info:</h4>
    
    <?php
    if (isset($_POST) && count($_POST) > 0) {
        echo 'Order ID = ' . $_POST['ORDERID'] . '<br/>';

        echo 'Transcation ID = ' . $_POST['TXNID'] . '<br/>';
        echo 'Transcation Status = ' . $_POST['STATUS'] . '<br/>';
        echo 'Transcation Amount =  ' . $_POST['TXNAMOUNT'] . '<br/>';
        echo 'Currency = ' . $_POST['CURRENCY'] . '<br/>';
        echo 'Transcation Date = ' . $_POST['TXNDATE'] . '<br/>';
        echo 'Payment Mode =' . $_POST['PAYMENTMODE'] . '<br/>';

        //$n1=$_POST['ORDERID'];
        $MID         = $_POST['MID'];
        $TXNID       = $_POST['TXNID'];
        $STATUS      = $_POST['STATUS'];
        $TXNAMOUNT   = $_POST['TXNAMOUNT'];
        $CURRENCY    = $_POST['CURRENCY'];
        $TXNDATE     = $_POST['TXNDATE'];
        $PAYMENTMODE = $_POST['PAYMENTMODE'];
        $BANKNAME    = $_POST['BANKNAME'];
        $BANKTXNID   = $_POST['BANKTXNID'];
        $details     = 'order Id =' . $ORDERID . '<br>PAYTM_MERCHANT_MID = ' . $MID . '<br>Trascation ID = ' . $TXNID . '<br>Trascation STATUS = ' . $STATUS .
         '<br>Trascation Amount =  ' . $TXNAMOUNT . '<br>CURRENCY = ' . $CURRENCY . '<br>Trascation Date = ' . $TXNDATE . '<br>PAYMENT MODE =' . $PAYMENTMODE .
         '<br>BANK NAME =' . $BANKNAME . '<br>BANKTXN ID = ' . $BANKTXNID;
        $exp = date('Y-m-d', strtotime('+1 week'));

        if ($_POST['STATUS'] == 'TXN_SUCCESS') {
            $statusTXN = 'Order Confirmed';
        } else {
            $statusTXN = 'Order Cancelled';
        }
        try {
            $db = new PDO('mysql:host=localhost;dbname=ructszmy_vibespublica;charset=utf8', 'root', '');
        } catch (Exception $e) {
            echo 'Error has Occured';
        }
        $stmt = $db->prepare("UPDATE  transcationbook SET PaymentInfo='$details' ,txnStatus='$STATUS' , exceptDate='$exp'  , OrderStatus='$statusTXN' , DeliveryDate=now() where  OrderId='$ORDERID' ");
        $stmt->execute();
    }
    ?>
    </div>
    </div> <!-- Billing content -->
    <?php
    if ($_POST['STATUS'] == 'TXN_SUCCESS') {?>
    
    
    
    <div class="order-summary">
    <h2><b>Order Summary:</b></h2>
    <?php
    $stmt = $db->query("select * from cart where cid= $Cid ");
        while ($row = $stmt->fetch()) {
            $pid      = $row['Pid'];
            $quantity = $row['Quantity'];
            $st       = $db->prepare("insert into orderlist values('$ORDERID','$Cid','$pid','$quantity') ");
            $st->execute();
        }
        $stmt = $db->prepare("Delete from cart where Cid= $Cid ");
        $stmt->execute();
        ?>
    
    
    <table class="table table-hover ">
     <tr>
        <td class="col-sm-8 col-md-6"><b> Product </b> </td>
        <td><b>Quantity</b></td>
        <td><b>Price</b></td>
        <td colspan="2"><b>Total</b></td>
    </tr>
    <?php

        $stmt = $db->query("select product.title AS Title ,product.price AS price,product.description AS description,
        product.image AS image, product.price* orderlist.Quantity AS totalAmount,
        orderlist.Quantity AS Quantity
        FROM  orderlist LEFT JOIN product ON  orderlist.Pid=product.id WHERE  orderlist.OrderID='$ORDERID' ");
        while ($row = $stmt->fetch()) {
            ?>
    <tr>
      <td class="col-sm-8 col-md-6">
                            <div class="media">
                                <a class="thumbnail pull-left" href="#"> <img src="../../pic/book/<?php echo $row['image'] . ' ' ?>" alt="" style="width:72px; height: 72px;"></a>
                                <div style="padding-left: 10px;" class="media-body">
                                    <h4 class="media-heading"><a href="#"><?php echo $row['Title'] . ' ' ?></a></h4>
                                    <p><?php echo substr($row['description'], 0, 60) . '...'; ?></p>
                                </div>
                            </div>
        </td>
    <td><P><?php echo $row['Quantity'] ?>Pies</p></td>
        <td><?php echo $row['price'] . ' ' ?></td>
        <td colspan="2"><?php echo $row['totalAmount'] ?></td>
    </tr>
    
    
        <?php }?>
    </table>
    </div>
    
    <div class="order-status">
    <h2><b>Order Status :</b></h2>
    </div>
    <div class="status-container">
    <div  class="content1">
    <div class="content-icons">
    <i class="fa fa-thumbs-o-up fa-2x green"></i><span class="join green"></span>
    <i class="fa fa-tags fa-2x i gray"></i><span class="join"></span>
    <i class="fa fa-truck fa-2x i fa-flip-horizontal gray"></i><span class="join"></span>
    <i class="fa fa-check-square-o fa-2x i gray" ></i>
    </div>
    <div class="content-title">
    <span>Order Confirmed<br>And Approved</span>
    <span>Packed</span><span>Shipped</span><span>Delivered</span>
    </div>
    </div>
    <div class="content2">
    <h5>
    Expected Arrival : <?php echo $exp; ?><br>
    Order Status : <?php echo $statusTXN; ?><br>
    </h5>
    </div>
    
    </div>
    <p class="note">Note: Order Will be Delivered Within 7 days. You can cancel order from Profile Section.</p>
    <?php }?>
    
    
    
    <?php
} else {
    echo '<b>Checksum mismatched.</b>';
    //Process transaction as suspicious.
}
?>
    <div class="back-btn"><a href="../index.php" class="redirect" ><i class="fa fa-spinner fa-pulse  fa-fw"></i>&nbsp;Back to Home Page</a></div>
    
    </div><!--main -container end-->
    
    <script>
    function myFunction(){
    window.print();
    }
    </script>
