MMCT TEAM
Server IP : 103.53.40.154  /  Your IP : 3.144.93.14
Web Server : Apache
System : Linux md-in-35.webhostbox.net 4.19.286-203.ELK.el7.x86_64 #1 SMP Wed Jun 14 04:33:55 CDT 2023 x86_64
User : ppcad7no ( 715)
PHP Version : 8.2.25
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON
Directory (0750) :  /home2/ppcad7no/rajwadahouse.com/

[  Home  ][  C0mmand  ][  Upload File  ]

Current File : /home2/ppcad7no/rajwadahouse.com/checkout.php
<?php
session_start();
include_once('site-main-query.php');
if(isset($_SESSION['user'])) {
    $user_id = $_SESSION['user']['cus_id'];
}else{
    $user_id ='';
}
if ($_SERVER["REQUEST_METHOD"] === "POST") {
    if (isset($_POST['submit_btn1'])) {
        // Collect the form data
        $name = $_POST['name'];
        $phone = $_POST['phone'];
        $email = $_POST['email'];
        $state = $_POST['state'];
        $pin = $_POST['pin'];
        $payment_method = $_POST['payment_method'];
        $address = $_POST['address'];
        $book_by = $user_id;
        // $post_by = $_POST['post_by'];
        $offer_rate = isset($_POST['product_prices']) ? array_sum($_POST['product_prices']) : 0;
        $quantities = isset($_POST['product_quantities']) ? implode(',', $_POST['product_quantities']) : '';
        $property_name = isset($_POST['product_names']) ? implode(',', $_POST['product_names']) : '';
        date_default_timezone_set("Asia/Kolkata");
        $currentTime = date("Y-m-d H:i:s");
        // $checkin_date = $_POST['checkin_date'];
        // $checkout_date = $_POST['checkout_date'];

        // Insert order details into the database
        $query = "INSERT INTO `tbl_booking`(`booking_name`, `state`, `pin`, `address`, `booking_for`, `contact`, `booking_email`, `booking_rate`, `checkin_date`, `quantities`, `payment_status`, `booking_status`, `book_by`) 
                  VALUES ('$name','$state', '$pin', '$address', '$property_name', '$phone', '$email', '$offer_rate', '$currentTime', '$quantities', 'Pending', 'Pending','$book_by')";
        $result = mysqli_query($db, $query) or die("Query unsuccessful");
        if ($result) {
            $order_id = mysqli_insert_id($db);

            // Proceed to Cashfree Payment Gateway if "Pay Now" is selected
            if ($payment_method == 'pay_now') {
                // Cashfree API endpoint
                $url = 'https://api.cashfree.com/pg/orders';
            
                // Data to be sent in the request body
                $data = [
                    'customer_details' => [
                        'cart_details' => null,
                        'customer_name' => $name,
                        'customer_id' =>  "CUID" . $order_id,
                        'customer_email' => $email,
                        'customer_phone' => $phone,
                        'customer_uid' => null
                    ],
                    'order_id' =>  "OID" . $order_id . "_" . time(),
                    'order_amount' => $offer_rate,
                    'order_currency' => 'INR',
                    'order_note' => "Booking for $property_name",
                    'order_meta' => [
                        'return_url' => "https://www.rajwadahouse.com/thankyou.php?order_id=$order_id",
                        'notify_url' => 'https://www.rajwadahouse.com/payment-notification.php',
                        'payment_methods' => 'cc,dc,upi,nb,paylater',
                    ],
                    'order_expiry_time' => date('Y-m-d\TH:i:s+05:30', strtotime('+29 days')),

                ];
            
                $jsonData = json_encode($data);
                $ch = curl_init($url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_POST, true);
                curl_setopt($ch, CURLOPT_HTTPHEADER, [
                    'accept: application/json',
                    'content-type: application/json',
                    'x-api-version: 2023-08-01',
                    'x-client-id: ' . '765274b8c8bfb2d1bf9eab1b86472567',
                    'x-client-secret: ' . 'cfsk_ma_prod_84fcf23bb4688770c6a52b943b274634_6922276d'
                ]);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData); 
                $response = curl_exec($ch);
                
                if (curl_errno($ch)) {
                    echo 'Error:' . curl_error($ch);
                } else {
                    $result = json_decode($response, true);
            
                    // Debugging - Print the entire response
                    // echo "<pre>";
                    // print_r($result);
                    // echo "</pre>";
                    // exit;
            
                    // Check if the payment session ID is available
                    if (isset($result['payment_session_id'])) {
                        $paymentSessionId = $result['payment_session_id'];
                        echo "<script>
                        document.addEventListener('DOMContentLoaded', function() {
                            var paymentSessionId = '$paymentSessionId';
                            console.log('Payment Session ID:', paymentSessionId);
                    
                            const cashfree = Cashfree({
                                mode: 'production',
                            });
                    
                            let checkoutOptions = {
                                paymentSessionId: paymentSessionId,
                                redirectTarget: '_self', 
                            };
                    
                            try {
                                cashfree.checkout(checkoutOptions);
                                console.log('Checkout initiated');
                            } catch (error) {
                                console.error('Error during checkout:', error);
                                alert('An error occurred during payment. Please try again.');
                            }
                            
                        });
                        </script>";
                    } else {
                        echo "Error: Unable to initiate payment. Please try again.";
                    }
                }
                curl_close($ch);
            } else {
                // For COD (Cash on Delivery) or Pay Later
                echo "<script>
                    window.alert('Order placed successfully! We will contact you soon.');
                    window.location.href = '$wspath' + 'thankyou.html';
                    </script>";
            }
        } else {
            echo "Error in processing your request. Please try again later.";
        }
    }
}
?>

<!doctype html>
<html class="no-js" lang="zxx">


<head>
    <meta charset="utf-8">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <title>Checkout</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    
    <?php include "top-links.php"; ?>


    <style>
    .contact-form-wrap .cheakout {
        width: 100%;
        height: 50px;
        border: 1px solid #dddddd;
        padding: 5px 20px;
        color: #929292;
        border-radius: 5px;
        height: 45px;
    }

    .contact-form-wrap textarea {
        width: 100%;
        height: 200px;
        border: 1px solid #eeeeee;
        padding: 10px 20px;
        color: #929292;
        resize: none;
        border-radius: 5px;
    }

    .contact-form-wrap .cheakout {
        width: 100%;
        height: 50px;
        border: 1px solid #dddddd;
        padding: 5px 20px;
        color: #929292;
        border-radius: 5px;
        height: 45px;
    }

    @media only screen and (min-width: 992px) {
        .contact-form-wrap {
            padding: 60px;
            background: #0e0e0e21;
        }
    }
    </style>
<script src="https://sdk.cashfree.com/js/v3/cashfree.js"></script>
</head>

<body>

    <div id="main-wrapper">

        <?php include "header.php"; ?>


        <div class="page-banner-section section bg-image" data-bg="assets/images/bg/breadcrumb.jpg">
            <div class="container">
                <div class="row">
                    <div class="col">

                        <div class="page-banner text-center">
                            <h1>Checkout</h1>
                            <ul class="page-breadcrumb">
                                <li><a href="index.html">Home</a></li>
                                <li>Checkout</li>
                            </ul>
                        </div>

                    </div>
                </div>
            </div>
        </div>

        <div class="container">
            <div class="contact-form-wrap mt-3 mb-3">

                <div class="row mb-5 mt-5">
                    <div class="col-md-6">
                        <div class="text-center">
                            <h3>Checkout</h3>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="text-center">
                            <a class="cart_summary-btn btn" href="<?php echo $wspath ?>cart.html">Cancel</a>
                        </div>
                    </div>
                </div>
                <form method="POST" action="proceed-razorpay.php">
                    <div class="text-center">
                        <?php if (isset($_SESSION['cart']) && !empty($_SESSION['cart'])): ?>
                        <?php
                            foreach ($_SESSION['cart'] as $key => $value) {
                                echo '<input type="hidden" name="product_names[]" value="' . $value['name'] . '">';
                                echo '<input type="hidden" name="product_quantities[]" value="' . $value['quantity'] . '">';
                                echo '<input type="hidden" name="product_prices[]" value="' . $value['price'] . '">';
                            }
                            ?>
                        <?php endif; ?>
                    </div>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="contact-form-style mb-20">
                                <input name="name" class="cheakout" placeholder="Name*" type="text" required>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="contact-form-style mb-20">
                                <input name="email" class="cheakout" placeholder="Email*" type="email" required>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="contact-form-style mb-20">
                                <input name="phone" class="cheakout" placeholder="Phone*" type="text" minlength="10" maxlength="10" onkeypress="return isNumberKey(event)" pattern=".{10,10}" onpaste="return false;" required>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="contact-form-style mb-20">
                                <input name="city" class="cheakout" placeholder="City*" type="text" required>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="contact-form-style mb-20">
                                <input name="state" class="cheakout" placeholder="State*" type="text" required>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="contact-form-style mb-20">
                                <input name="pin" class="cheakout" placeholder="Pincode*" type="text" required>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="contact-form-style">
                                <textarea name="address" placeholder="Type Your Address..." required></textarea>
                                <div class="col-md-6">
                                    <div class="contact-form-style">
                                        <input name="payment_method" type="radio" value="cod" required>
                                        <label style="color: #0e0e0e; font-weight: 600; font-size: 18px;">COD</label>
                                    </div>
                                    <div class="contact-form-style mb-20">
                                        <input name="payment_method" type="radio" value="pay_now" required>
                                        <label style="color: #0e0e0e; font-weight: 600; font-size: 18px;">Pay Now</label>
                                    </div>
                                </div>
                                <!-- <div class="row">
                                    <p>Click below to open the checkout page in current tab</p>
                                    <button id="renderBtn">Pay Now</button>
                                </div> -->
                                <input type="submit" name="submit_btn1" class="btn cnt-btn" value="Place Order">
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
        
        <?php include "footer.php"; ?>
        
    </div>

</body>

</html>

MMCT - 2023