<?php session_start();
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'PHPMailer/Exception.php';
require 'PHPMailer/PHPMailer.php';
require 'PHPMailer/SMTP.php';

//print_r($_SESSION);
include("includes/db_config.php"); ?>
<?php if(isset($_POST['payment']))
{ 
    $logo = "https://orangegroceries.com/img/logo.png";
    $logo_width = "100%";
    $logo_height = "";
    // All CSS
    // Body
    $body_title = "Orange Groceries";
    $width_of_mail_body = "80%"; // Please write in % percentage for better view...
    $padding_of_mail_body = "20px";
    $body_background_color = "";
    $body_text_color = "";
    // Header
    $header_background_color = "";
    $header_text_color = "";
    $header_font_size = "";
    $header_font_family = "";
    // Footer
    $footer_background_color = "#2F3B59";
    $footer_text_color = "white";
    $footer_font_size = "10px";
    $footer_font_family = "monospace";
    // Button
    $button_background_color = "#4db7fe";
    $button_text_color = "white";
    $button_font_size = "20px";
    $button_font_family = "monospace";
    $button_padding = "15px";
    $button_border_radius = "10px";
    // Highlight
    $highlight_color = "#4db7fe";
    $highlight_font_size = "25px";
    $highlight_font_family = "monospace";
    // Heading
    $heading_color = "#4db7fe";
    $heading_font_size = "25px";
    $heading_font_family = "monospace";
    // Paragraph
    $paragraph_color = "";
    $paragraph_font_size = "14px";
    $paragraph_font_family = "monospace";    
$id=$_GET['id'];   
extract($_POST);
     $sql_pt="select * from user_payment where id='".$id."'";
     $result_pt = mysqli_query($conn, $sql_pt);
     $res_pt= mysqli_fetch_array($result_pt);
     $plan_id=$res_pt['cart_id'];
     $rid = explode(",",$plan_id);
     $user_id=$_SESSION['id'];
    foreach($rid as $pm_id){
     $status="1";
    $stat="0";
    $sql_paytm ="UPDATE  addto_cart SET payment_status='$status',invoice_id='$stat' WHERE user_id='".$user_id."' and id='".$pm_id."'"; 
    $result_ptm=mysqli_query($conn,$sql_paytm) or die(mysqli_error());
    }
    date_default_timezone_set('Asia/Kolkata');
    $created_at = date( 'Y-m-d h:i:s A', time () );
    $payment_status="completed";
    $payment_method="Cash On Delivery";
	$statuss=1;
    $sql_pay ="UPDATE  user_payment SET payment_method='$payment_method',pay_status='$payment_status',payable_status='$statuss',created_date='$created_at' WHERE id='".$id."'";
	 echo $sql_pay;
    $res_ptm=mysqli_query($conn,$sql_pay) or die(mysqli_error()); 
    if($res_ptm){
  $id=$_GET['id'];  
  $sql_ss=mysqli_query($conn, "select * from user_payment where id='".$id."'");
    $rowss=mysqli_fetch_array($sql_ss);
    $sql1=mysqli_query($conn, "select * from user_profile where id=".$_SESSION['id']);
    $row1=mysqli_fetch_array($sql1);
    $total_price = $rowss['total_price'];
    $user_id = $_SESSION['id'];
    $email = $row1['email'];
    $name = $row1['fname'];
    $date = date("d-m-Y");
    $from_team = "Orange Groceries";
    $from_website = "https://orangegroceries.com/index.php";
    $to = $email;
    $subject = "Order Placed And Payment Successfully Completed";
 
   $message = "
                <html>
                    <head>
                        <title>$body_title</title>
                    </head>
                    <body style='background-color: $body_background_color; color: $body_text_color;'>
                        <center>
                            <div style='width: $width_of_mail_body; padding: $padding_of_mail_body;' >
                                <br/><br/>
                                <p style='color: $paragraph_color; font-size: $paragraph_font_size; font-family: $paragraph_font_family;'> 
                                    <span style='color: $highlight_color; font-size: $highlight_font_size; font-family: $highlight_font_family;'>
                                        Hi Team,
                                    </span> 
                                    <br/>
                                    Order Placed By <span style='color: $highlight_color; font-size: $highlight_font_size; font-family: $highlight_font_family;'> $name</span>, with RS.$total_price , Payment method Is Cash On Delivery ..<br/> !!!
                                </p>
                            </div>
                        </center>
                    </body>
                </html>";
$email = new PHPMailer();
$email->Body      =  ($message);
$email->SetFrom('support@orangegroceries.com', 'Orange Groceries'); //Name is optional
$email->Subject   = $name . " Submitted an Assignment";
$email->IsHTML(true);
$email->Body      =  ($message);
$email->AddAddress('support@orangegroceries.com' );
   if (! $email->Send()) {
       //echo "<script>alert('Problem in sending email!');</script>";
    } else {
      header("location:invoice.php?id=".$_GET['id']);
    }
         
    } else{
        echo "
            <script> 
                alert('Your Payment Successfully completed, but your pannel is not activated please contact to our support team to activate your pannel!!!'); 
                location.replace('index.php');
            </script>";
    }
} ?>
<!DOCTYPE html>
<html lang="en">
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content=" ">
    <meta name="author" content="">
    <title>Orange Groceries</title>
    <?php include("includes/css.php");?>
</head>

<body>
    <?php include("includes/header.php");?>


    <section class="page-info section-padding border-bottom topthumb">
        <div class="container-fluid">
            <div class="row">
                <div class="col-md-12">
                    <a href="index.php"> <span class="mdi mdi-home"></span> &nbsp; Home</a> <span class="mdi mdi-chevron-right"></span> <a href="#">Payment</a>
                </div>
            </div>
        </div>
    </section>
    <section class="checkout-page section-padding">
        <div class="container">
            <div class="row">

                <div class="col-md-12">
                    <h2 class="sb-heads"><i class="fa fa-asterisk"></i>Payment Method</h2>
                    <div class="contentText pt-20">
<!--
                        <p class="subcaption bold">Select Your Payment Type</p>
-->
                        <h2>
                            <ul class="row text-black">
                                <?php $id=$_GET['id'];   
                  $sql_ptm="select * from user_payment where id='".$id."'";
                  $result_ptm = mysqli_query($conn, $sql_ptm);
                  $res_ptm= mysqli_fetch_array($result_ptm); 
                  $total_price=$res_ptm['total_price']; 
                  $pay_id=$res_ptm['id']; 
                  ?>
                                <li class="col-md-12 bg-white p-10" style="border:1px solid #ccc;margin-bottom:20px;">
                                    <form name="checkout_address" action="" method="post">
                                        <label for="radio_button_7" style="font-size:20px;"><img src="img/cod.png" width="32px">Cash On Delivery</label>
                                        <button class="float-right btn" style="background-color:#fa8042; border-radius:10px; color:#fff;margin-top:10px;" name="payment" type="submit">Pay ₹ <?php echo $res_ptm['total_price']; ?> On Delivery</button>
                                    </form>
                                </li>

                                <!--   <li class="col-md-12 bg-white p-10" style="border:1px solid #ccc; margin-bottom:20px">
                <form action="" name="frmTransaction" id="frmTransaction" method="POST" accept-charset="utf-8">
                                                                <label for="radio_button_5"><img src="img/card.png" width="32px"> Credit / Debit Card / UPI</label>
                                <button class="float-right btn btn-success" type="button" class="btn btn-success btn-lg pull-right" id="rzp-button1" style="margin-top:10px;">Continue To Payment</button>
                                <div style="display: none;">
                                            <?php 
                                                //$random_number = rand(111111,999999);
                                                //$random_OrderId = "GXQsdoHyK9Hb6e";
                                             ?>
                                             <input type="hidden" class="form-control" id="merchantRefNo" name="merchantRefNo" value="<?php echo $random_OrderId; ?>">
                                             <input type="hidden" class="form-control" id="outletId" name="outletId" value="0"  readonly="true">
                                             <input type="hidden" class="form-control" id="apiVersion" name="apiVersion" value="2.0.0" readonly>
                                             <input type="hidden" class="form-control" id="currencyCode" name="currencyCode" value="INR" readonly>
                                              <input type="hidden" class="form-control" id="locale" name="locale" value="EN-US" readonly>

                                             <?php
                                               // $amount=$total_price*100;  
                                                //echo "<pre>";print_r($amount);die;
                                             ?>
                                             <input type="hidden" class="form-control" id="amount" name="amount" value="<?php echo $amount;?>"> 
                                            <input type="number" id="paymentMode" name="paymentMode" value="1" readonly="true" > 
                                            <input type="text" id="cardNumber" name="cardNumber" value="" readonly="true" > 
                                            <input type="text" id="cardHolderName" name="cardHolderName" value="" readonly="true" > 
                                            <input type="text" id="cardBrand" name="cardBrand" value="" readonly="true" > 
                                            <input type="text" id="cardExpiry" name="cardExpiry" value="" readonly="true" > 
                                            <input type="text" id="cardType" name="cardType" value="" readonly="true" > 
                                            <input type="text" id="cardCvv" name="cardCvv" value="" readonly="true" > 
                                            <input type="text" id="paymentCode" name="paymentCode" value="" readonly="true" > </div>
                            </form>             </li>-->
                                <!-- <li class="col-md-12 bg-white p-10" style="border:1px solid #ccc; margin-bottom:20px">
                <input type="radio" value="Net Banking" name="radio_4" id="radio_button_6">
                <label for="radio_button_6"><img src="img/netbanking.png" width="32px"> Net Banking</label> 
                <button class="float-right btn btn-success" style="margin-top:10px;" type="button">Continue To Payment</button>
              </li>   -->
                            </ul>
                        </h2>
                    </div>
                </div>

            </div>
        </div>
    </section>
    <?php include("includes/footer.php");?>
    <?php include("includes/js.php");?>
    <script src="https://checkout.razorpay.com/v1/checkout.js"></script>
    <form name='razorpayform' action="verify.php" method="POST">
        <input type="text" name="razorpay_payment_id" id="razorpay_payment_id">
        <input type="text" name="razorpay_signature" id="razorpay_signature">
        <input type="text" name="paym_id" value="<?php echo $pay_id;?>" id="paym_id">
    </form>
    <script>
        // Checkout details as a json
        var options = <?php echo $json?>;
        /**
         * The entire list of Checkout fields is available at
         * https://docs.razorpay.com/docs/checkout-form#checkout-fields
         */
        options.handler = function(response) {
            document.getElementById('razorpay_payment_id').value = response.razorpay_payment_id;
            document.getElementById('razorpay_signature').value = response.razorpay_signature;
            document.razorpayform.submit();
        };

        // Boolean whether to show image inside a white frame. (default: true)
        options.theme.image_padding = false;

        options.modal = {
            ondismiss: function() {
                console.log("This code runs when the popup is closed");
            },
            // Boolean indicating whether pressing escape key 
            // should close the checkout form. (default: true)
            escape: true,
            // Boolean indicating whether clicking translucent blank
            // space outside checkout form should close the form. (default: false)
            backdropclose: false
        };

        var rzp = new Razorpay(options);

        document.getElementById('rzp-button1').onclick = function(e) {
            rzp.open();
            e.preventDefault();
        }

    </script>
</body>

</html>
