<?php
include("config.php");

$date=date("d-m-Y");
$msdid="MSDH".rand(1111111111,9999999999);
$pnr=rand(1111111111,9999999999);
$hotel=$_POST['hotel'];
$uid=$_POST['uid'];
$name=$_POST['name'];
$email=$_POST['email'];
$checkin=$_POST['checkin'];
$checkout=$_POST['checkout'];
$rcount=$_POST['roomcount'];
$adult=$_POST['adult'];
$child=$_POST['child'];
$num=$_POST['number'];
$cname=$_POST['cname'];
if($cname=="")
{
    $cname="N/A";
}
$caddress=$_POST['caddress'];
if($caddress=="")
{
    $caddress="N/A";
}
$gst=$_POST['gst'];
if($gst=="")
{
    $gst="N/A";
}
$total=$_POST['total'];
$price=$_POST['value'];

$paymode=$_POST['paymode'];
$room=$_POST['room'];


$date1=date_create($checkin);
$date2=date_create($checkout);
$datediff=date_diff($date1,$date2);
$diff=$datediff->format("%a");
if($diff==0)
{
    $diff=1;
}
$ftotal=$total*$diff*$rcount;

$hmail=mysqli_query($db,"select * from hotels where id='$hotel' ");
$row=mysqli_fetch_assoc($hmail);


$mail=$row['mail'];
$hname=$row['name'];

$guest=$adult+$child;

$rnamesql=mysqli_query($db,"select price,roomtype from roomtype where id='$room' ");
list($rprice,$rname)=mysqli_fetch_row($rnamesql);

$rprice=$rprice*$diff*$rcount;

$tax=($rprice*12)/100;
$hgross=$rprice+$tax;
$hcomm=$_POST['comm'];
$comm=($rprice*$hcomm)/100;
$commgst=($comm*18)/100;
$tcomm=$comm+$commgst;
$topay=$hgross-$tcomm;
$gstval=$total-$price;




/*$msg="We have received a booking request from you. All the charges you have to pay to the hotel are given below.
    
    Company Name: ".$cname."
    Company Address: ".$caddress."
    GST No.: ".$gst."
    
    Your Murshidabad Hotels Booking ID is ".$msdid."
    PNR: ".$pnr."
    
    Booking Details: From ".$checkin." to ".$checkout."
    
    Your Payment Breakdown is:
    
    Hotel Price = Rs ".$hprice."
    MSDH commision = Rs 100
    GST = Rs ".$gstval."
    Total = Rs ".$total."
    
    This is the total amount you have to pay to the hotel at the time of checkin.
    Complimentary Services:
    > Complimentary Breakfast is available.
    > Complimentary Early Check in is available for up to 3 hours from the standard check-in time. This service is subject to availability.
    
    

";

$msg="

<html>
<head></head>
<body>
ss

</body>
</html>


";*/




/*$msg2="We have received a booking request for your hotel. Below are the details of your booking.

    Name: ".$name."
    Email: ".$email."
    Phone: ".$num."
    
    Company Name: ".$cname."
    Company Address: ".$caddress."
    GST No.: ".$gst."
    
    Your Murshidabad Hotels Booking ID is ".$msdid."
    PNR: ".$pnr."
    
    Booking Details: From ".$checkin." to ".$checkout."
    
    Your Payment Breakdown is:
    
    Hotel Price = Rs ".$hprice."
    MSDH commision = Rs 100
    GST = Rs ".$gstval."
    Total = Rs ".$total."
    
    Charge this amount from the guest at the time of checkin.
    
    Complimentary Services:
    > Complimentary Breakfast is available.
    > Complimentary Early Check in is available for up to 3 hours from the standard check-in time. This service is subject to availability.
    
    For further details, log on to admin panel: https://murshidabadhotels.com/mainpage/admin
    

";*/


$to = $email; 
$from = $mail; 
$fromName = $name; 
 
$subject = "Room Confirmation Message"; 

$htmlContent2='
<html>
<head>
<title>Booking Voucher for Hotel</title>
</head>
<body>

    <h3>We have received a booking request for your hotel The details are given below:</h3>

    <table style="width:600px;margin-bottom:10px;">
        <tr>
        <td style="width:35%;">
        Booked With<br>
        <b>Murshidabad Hotels</b>
        <br>
        Your Online Hotel Booking Partner
        </td>
        <td style="width:30%;">
        <center>
        <label style="font-size:20px;">Booking Voucher</label>
        </center>
        </td>
        <td style="width:35%;text-align:right;">
        Booking ID: <b>'.$msdid.'</b><br>
        PNR: <b>'.$pnr.'</b><br>
        (Booked on <b>'.$date.'</b>)
        
        
        </td>
        </tr>
        </table>
        
        <div style="width:600px;;border:1px solid black;border-radius:10px;height:auto;">
        <br>
        <br>
            <table style="width:90%;margin-left:5%;>
            <tr>
            <td style="width:75%;margin-bottom:20px;">
                <label style="font-size:30px;"><b>'.$row['name'].'</b></label>
                <br>
                <br>
                <label style="font-szie:10px;">'.$row['location'].'</label>
                <br>
                <i class="fa fa-phone" aria-hidden="true"></i> '.$row['phone'].'
                
                
            </td>
            <td style="width:25%;">
            <label style="font-size:10px;">Booking Status</label>
            <label style="font-size:25px">CONFIRMED</label>
            </td>
            </tr>
            </table>
            
            <div style="width:100%;border:1px solid black;">
            </div>
            
            <div style="display:flex;width:90%;margin-left:5%;border-bottom:1px solid black;">
                <div style="width:20%;margin:10px 0;">
                    <b>'.$diff.'-Night Stay</b>
                </div>
                <div style="width:40%;margin:10px 0;">
                    <label style="font-size:10px;">Check-in</label>
                    <br>
                    <b>'.$checkin.'</b>
                    <br>After 12:00 PM
                </div>
                <div style="width:40%;margin:10px 0;">
                    <label style="font-size:10px;">Check-out</label>
                    <br>
                    <b>'.$checkout.'</b>
                    <br>Before 11:00 AM
                </div>
            </div>
            
            <div style="display:flex;width:90%;margin-left:5%;border-bottom:1px solid black;">
                <div style="width:20%;margin:10px 0;">
                    <b>'.$guest.' Guest</b>
                </div>
                <div style="width:80%;margin:10px 0;">
                    <label><b>Mr. '.$name.'</b></label>
                    <br>
                    <br>
                    <b>'.$email.',&nbsp;91'.$num.'</b>
                    
                </div>
                
                
            </div>
            
            <div style="display:flex;width:90%;margin-left:5%;border-bottom:1px solid black;">
                <div style="width:20%;margin:10px 0;">
                    <b>'.$rcount.' Room(s)</b>
                </div>
                <div style="width:80%;margin:10px 0;">
                    <label><b>'.ucwords($rname).'</b></label>
                    <br>
                    <br>
                    <label>Breakfast</label>
                    <br>
                    <label>'.$adult.' Adult</label>
                    
                </div>
                
                
            </div>
            
            <div style="display:flex;width:90%;margin-left:5%;">
                <div style="width:20%;margin:10px 0;">
                    <b>Payment Breakdown:</b>
                </div>
                <div style="width:80%;margin:10px 0;">
                    <label>Hotel Charge: <b>INR '.$rprice.'</b></label>
                    <br>
                    <label>Hotel Tax(12%): <b>INR '.$tax.'</b></label>
                    <br>
                    <label>Hotel Gross: <b>INR '.$hgross.'</b></label>
                    <br>
                    <label>MSDH Commission(<b>'.$hcomm.'%</b>)(TAC)(including GST): <b>INR '.$tcomm.'</b></label>
                    <br>
                    <label>MSDH to pay Hotel: <b>INR '.$topay.'</b></label>
                    <br>
                    
                    
                    
                    
                </div>
                
                
            </div>
            
            
           
            
        </div>
        
</body>
</html>
';
 
$htmlContent = '
<html> 
    <head> 
        <title>Booking Voucher</title> 
        
    </head> 
    <body> 
    
        <table style="width:600px;">
        <tr>
        <td style="width:25%;">
        Booked With<br>
        <b>Murshidabad Hotels</b>
        <br>
        Your Online Hotel Booking Partner
        </td>
        <td style="width:35%;">
        <center>
        <b>Booking Voucher</b>
        </center>
        </td>
        <td style="width:40%;text-align:right;">
        Booking ID: <b>'.$msdid.'</b><br>
        PNR: <b>'.$pnr.'</b><br>
        (Booked on '.$date.')
        
        
        </td>
        </tr>
        </table>
        
        <div style="width:600px;;border:1px solid black;border-radius:10px;height:auto;">
        <br>
        <br>
        
            <table style="width:90%;margin-left:5%;>
            <tr>
            <td style="width:75%;margin-bottom:20px;">
                <label style="font-size:30px;"><b>'.$row['name'].'</b></label>
                <br>
                <br>
                <label style="font-szie:10px;">'.$row['location'].'</label>
                <br>
                <i class="fa fa-phone" aria-hidden="true"></i> '.$row['phone'].'
                
                
            </td>
            <td style="width:25%;">
            <label style="font-size:10px;">Booking Status</label>
            <label style="font-size:25px">CONFIRMED</label>
            </td>
            </tr>
            </table>
            
            <div style="width:100%;border:1px solid black;">
            </div>
            
            <div style="display:flex;width:90%;margin-left:5%;border-bottom:1px solid black;">
                <div style="width:20%;margin:10px 0;">
                    <b>'.$diff.'-Night Stay</b>
                </div>
                <div style="width:40%;margin:10px 0;">
                    <label style="font-size:10px;">Check-in</label>
                    <br>
                    <b>'.$checkin.'</b>
                    <br>After 12:00 PM
                </div>
                <div style="width:40%;margin:10px 0;">
                    <label style="font-size:10px;">Check-out</label>
                    <br>
                    <b>'.$checkout.'</b>
                    <br>Before 11:00 AM
                </div>
            </div>
            
            <div style="display:flex;width:90%;margin-left:5%;border-bottom:1px solid black;">
                <div style="width:20%;margin:10px 0;">
                    <b>'.$guest.' Guest</b>
                </div>
                <div style="width:80%;margin:10px 0;">
                    <label><b>Mr. '.$name.'</b></label>
                    <br>
                    <br>
                    <b>'.$email.',&nbsp;91'.$num.'</b>
                    
                </div>
                
                
            </div>
            
            <div style="display:flex;width:90%;margin-left:5%;border-bottom:1px solid black;">
                <div style="width:20%;margin:10px 0;">
                    <b>'.$rcount.' Room(s)</b>
                </div>
                <div style="width:80%;margin:10px 0;">
                    <label><b>'.ucwords($rname).'</b></label>
                    <br>
                    <br>
                    <label>Breakfast</label>
                    <br>
                    <label>'.$adult.' Adult</label>
                    
                </div>
                
                
            </div>
            
            <div style="display:flex;width:90%;margin-left:5%;">
                <div style="width:20%;margin:10px 0;">
                    <b>Amount</b>
                </div>
                <div style="width:80%;margin:10px 0;">
                    <label><b>INR '.$total.' x '.$diff.'<br>= '.$ftotal.'.00</b></label>
                    <br>
                    
                    
                </div>
                
                
            </div>
            
            
           
            
        </div>
        <br>
        <br>
        <b>Important Information</b>
        <ul>
        <li>Passport, Aadhar, Driving License or any other Govt. Id will be accepted as ID proof. No Office ID or PAN Card will be accepted.</li>
        <li>GST invoice can be collected directly from the property.</li>
        <br>
        
        
    
    
   
       
    </body> 
    </html>

';
 
// Set content-type header for sending HTML email 
$headers = "MIME-Version: 1.0" . "\r\n"; 
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; 
 
// Additional headers 
/*$headers .= "From: ".$fromName."<".$from.">" . "\r\n"; 
$headers .= "Cc: webworldtech1@gmail.com" . "\r\n"; 
$headers .= "Bcc: webworldtech1@gmail.com" . "\r\n"; */
 
// Send email 
/*mail($to, $subject, $htmlContent, $headers);
mail($mail,$subject,$htmlContent2, $headers);*/

#mysqli_query($db,"insert into booking(bdate,msdid,hotel,pnr,uid,name,email,company,caddress,gstin,checkin,checkout,rcount,adult,child,room,number,status,total) values('$date','$msdid','$hotel','$pnr','$uid','$name','$email','$cname','$caddress','$gst','$checkin','$checkout','$rcount','$adult','$child','$room','$num','pending','$total')");

$merchantKey='354b0963-27c1-49c3-9f05-543cba64eaaf';

$t=time();
$tid="MT".$t;
 $data=array(
     "merchantId" => "MURSHIDONLINE",
     "merchantTransactionId" => "MT".$t,
  "merchantUserId" => "MUID".$t,
  "amount" => $ftotal*100,
  "redirectUrl" => "https://murshidabadhotels.com/confirmBooking.php?date=$date&msdid=$msdid&pnr=$pnr&hotel=$hotel&uid=$uid&name=$name&email=$email&checkin=$checkin&checkout=$checkout&rcount=$rcount&adult=$adult&child=$child&num=$num&cname=$cname&caddress=$caddress&gst=$gst&total=$total&price=$price&paymode=$paymode&room=$room&tid=$tid",
  "redirectMode" => "GET",
  "callbackUrl" => "https://murshidabadhotels.com/booking.php?hotel=$hotel&uid=$uid&room=$room",
  "mobileNumber" => $num,
  "paymentInstrument" => array(
      "type" => "PAY_PAGE"
      )
  
     );
     
    $payloadMain=base64_encode(json_encode($data));
     $payload=$payloadMain."/pg/v1/pay".$merchantKey;
     
     $Checksum=hash('sha256',$payload);
   $Checksum=$Checksum.'###1';
  


$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://api.phonepe.com/apis/hermes/pg/v1/pay',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS =>'{
    "request": "'.$payloadMain.'"
}',
  CURLOPT_HTTPHEADER => array(
    'Content-Type: application/json',
    'X-Verify:'.$Checksum
  ),
));

$response = curl_exec($curl);

curl_close($curl);



curl_close($curl);



if ($err) {
  header("Location:booked.php?cURLError=$err");
} else {
  
$responseData = json_decode($response,true);
print_r($jsondata['data']['instrumentResponse']['redirectInfo']['url']);


  $url=$responseData['data']['instrumentResponse']['redirectInfo']['url'];
    header("Location:".$url);
  
}


echo $payloadMain."\n";
echo $Checksum;



/*$key="23597261facb472bc25b4161f5a65ef4";
$token="0da1cb4b1698812d4182f433df03f2ed";
$mojoUrl="test.instamojo.com";*/



/*bdate='.$date.'&msdid='.$msdid.'&hotel='.$hotel.'
.'&uid='.$uid*/



#'redirect_url' => 'https://murshidabadhotels.com/mainpage/booked.php?pnr='.$pnr,

#'redirect_url' => 'https://murshidabadhotels.com/booked.php?bdate='.$date.'&msdid='.$msdid.'&hotel='.$hotel.'&pnr='.$pnr.'&uid='.$uid.'&name='.$name.'&email='.$email.'&company='.$cname.'&caddress='.$caddress.'&gstin='.$gst.'&checkin='.$checkin.'&checkout='.$checkout.'&rcount='.$rcount.'&adult='.$adult.'&child='.$child.'&num='.$num.'&room='.$room.'&total='.$total.'&price='.$price

/*$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://www.instamojo.com/api/1.1/payment-requests/");
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER,
                        array("X-Api-Key:$key",
                                "X-Auth-Token:$token",
                                "Authorization: Bearer y70kak2K0Rg7J4PAL8sdW0MutnGJEl"));

$purpose="Booking at ".$hname;
$payload = Array(
  'purpose' => $purpose,
  'amount' => $ftotal,
  'buyer_name' => $name,
  'email' => $email,
  'phone' => $num,
  'redirect_url' => 'https://murshidabadhotels.com/booked.php?bdate='.$da.'&msdid='.$msdid.'&hotel='.$hotel.'&pnr='.$pnr.'&uid='.$uid.'&email='.$email.'&company='.$cname.'&caddress='.$caddress.'&gstin='.$gst.'&checkin='.$checkin.'&checkout='.$checkout.'&rcount='.$rcount.'&adult='.$adult.'&child='.$child.'&num='.$num.'&room='.$room.'&total='.$total.'&price='.$price,
  'send_email' => 'True',
  'webhook' => 'http://www.example.com/webhook/',
  'allow_repeated_payments' => 'False',
);

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($payload));
$response = curl_exec($ch);
curl_close($ch); 



$response=json_decode($response,true);


if($response['success'])
{
    $longurl=$response['payment_request']['longurl'];
    header("Location:$longurl");
    exit();
   
}*/

/*include("pay-config.php");

require('testapp/razorpay-php/Razorpay.php');
session_start();

// Create the Razorpay Order

use Razorpay\Api\Api;

$api = new Api($keyId, $keySecret);

//
// We create an razorpay order using orders api
// Docs: https://docs.razorpay.com/docs/orders
//
$orderData = [
    'receipt'         => 1111,
    'amount'          => $ftotal * 100, // 2000 rupees in paise
    'currency'        => 'INR',
    'payment_capture' => 1 // auto capture
];

$razorpayOrder = $api->order->create($orderData);

$razorpayOrderId = $razorpayOrder['id'];

$_SESSION['razorpay_order_id'] = $razorpayOrderId;

$displayAmount = $amount = $orderData['amount'];

if ($displayCurrency !== 'INR')
{
    $url = "https://api.fixer.io/latest?symbols=$displayCurrency&base=INR";
    $exchange = json_decode(file_get_contents($url), true);

    $displayAmount = $exchange['rates'][$displayCurrency] * $amount / 100;
}

$checkout = 'automatic';

if (isset($_GET['checkout']) and in_array($_GET['checkout'], ['automatic', 'manual'], true))
{
    $checkout = $_GET['checkout'];
}

$data = [
    "key"               => $keyId,
    "amount"            => $amount,
    "name"              => $name,
    "description"       => "Booking at $hname",
    "image"             => "https://s29.postimg.org/r6dj1g85z/daft_punk.jpg",
    "prefill"           => [
    "name"              => $name,
    "email"             => $email,
    "contact"           => $num,
    ],
    "notes"             => [
    "address"           => "Hello World",
    "merchant_order_id" => "12312321",
    ],
    "theme"             => [
    "color"             => "#F37254"
    ],
    "order_id"          => $razorpayOrderId,
];

if ($displayCurrency !== 'INR')
{
    $data['display_currency']  = $displayCurrency;
    $data['display_amount']    = $displayAmount;
}

$json = json_encode($data);

require("testapp/checkout/{$checkout}.php");*/


/*include("stripe-php-master/init.php");
$publishableKey="
pk_test_51NWEltSIcyC3Hb3tYMnKPZyYtZ7PnyUd6ZZcZX5luIGd8EMHRs6hMuvBRAwciq5MlmvD6NBMm0yp5OUkt9oxCI1Y008NbejKFh
";

$stripeSecretKey="
sk_test_51NWEltSIcyC3Hb3tllBIpTn48AQQ0DzRWqBzU33rv3oX01c388YosB7f6FXWnjebS4OcM2pX1zIhmbNjEUFyH5O100mNGPeqqJ
";

\Stripe\Stripe::setApiKey($stripeSecretKey);
header('Content-Type: application/json');

$YOUR_DOMAIN = 'https://murshidabadhotels.com/';

$checkout_session = \Stripe\Checkout\Session::create([
  'line_items' => [[
    # Provide the exact Price ID (e.g. pr_1234) of the product you want to sell
    'price' => $ftotal,
    'quantity' => 1,
  ]],
  'mode' => 'payment',
  'success_url' => $YOUR_DOMAIN . '/success.php',
  'cancel_url' => $YOUR_DOMAIN . '/cancel.php',
]);

header("HTTP/1.1 303 See Other");
header("Location: " . $checkout_session->url);*/

?>
<!-- <html>
    <head>
        <title>Payment Page</title>
         <meta content="width=device-width, initial-scale=1.0" name="viewport">
    </head>
    <body>
        <form action="booked.php" action="POST">
        <script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
                                    
                                data-key="pk_test_51NWEltSIcyC3Hb3tYMnKPZyYtZ7PnyUd6ZZcZX5luIGd8EMHRs6hMuvBRAwciq5MlmvD6NBMm0yp5OUkt9oxCI1Y008NbejKFh"
                                data-amount="<?php echo $ftotal*100;?>"
                                data-name="Pay to Confirm"
                                data-description="Booking @ <?php echo $hname;?>"
                                data-image="img/logo.png"
                                data-currency="inr"
                                data-email="murshidabadhotels@gmail.com"
                                >
                                    
                                </script>
                                </form>
    </body>
</html> -->

