| Server IP : 13.126.101.145 / Your IP : 216.73.217.37 Web Server : Apache/2.4.52 (Ubuntu) System : Linux ip-11-115-0-196 6.8.0-1039-aws #41~22.04.1-Ubuntu SMP Thu Sep 11 10:54:48 UTC 2025 x86_64 User : www-data ( 33) PHP Version : 8.3.17 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /var/www/html/rentals_updated/wp-content/plugins/api-phone/ |
Upload File : |
<?php
// /// ----------- payment -----------------
// class WC_Purpose_Payment_Options {
// /**
// * Add payment options dropdown on checkout page
// */
// public function add_payment_options_dropdown() {
// $cart_items = WC()->cart->get_cart();
// $order_total = WC()->cart->get_total('');
// $purpose_sales = false;
// $product_price = 0;
// // // Check if any product has purpose_of_listing = "Sales"
// // foreach ($cart_items as $cart_item) {
// // $product_id = $cart_item['product_id'];
// // $purpose = get_post_meta($product_id, 'purpose_of_listing', true);
// // if ($purpose === 'Sales') {
// // $purpose_sales = true;
// // }
// // // Get product price for calculations
// // $product = wc_get_product($product_id);
// // $product_price = $product->get_regular_price();
// // }
// foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
// $product_id = $cart_item['product_id'];
// $purpose = get_post_meta($product_id, 'purpose_of_listing', true);
// if ($purpose === 'Sales') {
// $purpose_sales = true;
// }
// // Get product price for calculations
// $product = wc_get_product($product_id);
// $product_price = $product->get_regular_price();
// // Check for RFQ data if needed
// if (isset($cart_item['rfq_data'])) {
// $start_date = $cart_item['rfq_data']['start_date'];
// $end_date = $cart_item['rfq_data']['end_date'];
// $shift = $cart_item['rfq_data']['shift'];
// $location = $cart_item['rfq_data']['location'];
// $application = $cart_item['rfq_data']['application'];
// $base_price = $cart_item['rfq_data']['base_price'];
// }
// }
// // Clean order total (remove currency symbol and formatting)
// $order_total_clean = preg_replace('/[^\d.]/', '', $order_total);
// // Determine payment options based on purpose and total
// $payment_options = array();
// $default_option = 'full'; // Always default to full payment initially
// if ($purpose_sales) {
// $payment_options['full'] = '100% full payment';
// if ($order_total_clean > 1000000) { // 10,00,000
// $payment_options['half'] = '50% payment';
// $payment_options['2LC'] = '2 Lakh payment';
// } else {
// $payment_options['2LC'] = '2 Lakh payment';
// }
// } else {
// $payment_options['full'] = '100% full payment';
// if ($order_total_clean > (90 * $product_price)) {
// $payment_options['2-Months'] = '2 Months payment';
// }
// }
// // SAP Logic to caliculate number of months
// if(!empty($start_date)&&!empty($end_date)) {
// // Convert date strings to DateTime objects
// $start = new DateTime($start_date);
// $end = new DateTime($end_date);
// // Calculate total days between start and end dates
// $total_days = $start->diff($end)->days + 1; // +1 to include end date
// // Calculate full months
// $full_months = 0;
// $current = clone $start;
// while ($current <= $end) {
// // Get the number of days in the current month
// $days_in_month = $current->format('t');
// // Calculate days in this specific month for the rental period
// $month_start = max($current, $start);
// $month_end = min(new DateTime($current->format('Y-m-t')), $end);
// $days_this_month = $month_start->diff($month_end)->days + 1;
// // Check if days in this month are more than 20
// if ($days_this_month > 20) {
// $full_months++;
// }
// // Move to next month
// $current->modify('first day of next month');
// }
// // return $full_months;
// }
// // Calculate payment amounts
// $payment_amounts = array();
// $payment_amounts['full'] = $order_total_clean;
// if (isset($payment_options['half'])) {
// $payment_amounts['half'] = $order_total_clean * 0.5;
// }
// if (isset($payment_options['2LC'])) {
// $payment_amounts['2LC'] = 200000;
// }
// if (isset($payment_options['2-Months'])) {
// // $payment_amounts['2-Months'] = 60 * $product_price;
// // $average_total = $order_total_clean / $full_months;
// // $payment_amounts['2-Months'] = 2 * $average_total;
// $payment_amounts['2-Months'] = ($order_total_clean / $full_months) * 2;
// }
// // Store data in session for later use
// WC()->session->set('wc_payment_options', $payment_options);
// WC()->session->set('wc_payment_amounts', $payment_amounts);
// WC()->session->set('wc_original_total', $order_total_clean);
// // Check if user has previously made a selection
// $selected_option = WC()->session->get('wc_payment_option');
// if (!$selected_option || !isset($payment_options[$selected_option])) {
// WC()->session->set('wc_payment_option', $default_option);
// $selected_option = $default_option;
// }
// // Set a flag to indicate user hasn't explicitly chosen an option yet
// if (!WC()->session->get('wc_user_selected_payment')) {
// WC()->session->set('wc_user_selected_payment', false);
// }
// }
// /**
// * AJAX handler for updating payment option in session
// */
// function ajax_update_payment_option() {
// // Check nonce for security
// check_ajax_referer('update_payment_option_nonce', 'security');
// if (isset($_POST['payment_option'])) {
// $payment_option = sanitize_text_field($_POST['payment_option']);
// WC()->session->set('wc_payment_option', $payment_option);
// // Set flag that user has made a selection
// if (isset($_POST['user_selected']) && $_POST['user_selected'] === 'true') {
// WC()->session->set('wc_user_selected_payment', true);
// }
// // Update cart total
// WC()->cart->calculate_totals();
// wp_send_json_success();
// }
// wp_send_json_error();
// die();
// }
// /**
// * Modify displayed cart total
// */
// public function modify_displayed_total($total) {
// // Only modify on checkout page and if user has made a selection
// if (!is_checkout() || !WC()->session->get('wc_user_selected_payment')) {
// return $total;
// }
// $payment_option = WC()->session->get('wc_payment_option');
// $payment_amounts = WC()->session->get('wc_payment_amounts');
// if (!empty($payment_option) && !empty($payment_amounts) && isset($payment_amounts[$payment_option])) {
// // return wc_price($payment_amounts[$payment_option]);
// }
// return $total;
// }
// /**
// * Modify cart total for payment gateways
// */
// public function modify_cart_total($total, $cart) {
// // Only modify on checkout page and if user has made a selection
// if (!is_checkout() || !WC()->session->get('wc_user_selected_payment')) {
// return $total;
// }
// $payment_option = WC()->session->get('wc_payment_option');
// $payment_amounts = WC()->session->get('wc_payment_amounts');
// if (!empty($payment_option) && !empty($payment_amounts) && isset($payment_amounts[$payment_option])) {
// return $payment_amounts[$payment_option];
// }
// return $total;
// }
// /**
// * Add notice about modified payment amount
// */
// public function add_payment_notice() {
// // Only show notice if user has made a selection
// if (!WC()->session->get('wc_user_selected_payment')) {
// return;
// }
// $payment_option = WC()->session->get('wc_payment_option');
// $payment_amounts = WC()->session->get('wc_payment_amounts');
// $original_total = WC()->session->get('wc_original_total');
// if (!empty($payment_option) && $payment_option !== 'full' &&
// !empty($payment_amounts) && isset($payment_amounts[$payment_option]) &&
// !empty($original_total) && $payment_amounts[$payment_option] < $original_total) {
// wc_print_notice(
// sprintf(
// __('Based on your selected payment option, your initial payment will be %s instead of the full amount %s.', 'woocommerce'),
// wc_price($payment_amounts[$payment_option]),
// wc_price($original_total)
// ),
// 'notice'
// );
// }
// }
// /**
// * Reset payment session when leaving checkout or cart contents change
// */
// public function reset_payment_session_on_cart_change() {
// // Reset session variables if cart is modified or emptied
// if (WC()->cart->is_empty()) {
// $this->clear_custom_payment_session();
// }
// }
// /**
// * Reset payment session when navigating away from checkout
// */
// public function reset_payment_session_on_page_change() {
// // Check if we're currently on checkout page
// if (is_checkout()) {
// // Do nothing if on checkout
// return;
// }
// // If not on checkout, clear the session
// $this->clear_custom_payment_session();
// }
// /**
// * Clear custom payment session variables
// */
// public function clear_custom_payment_session() {
// // Clear all our custom session variables
// WC()->session->set('wc_payment_options', null);
// WC()->session->set('wc_payment_amounts', null);
// WC()->session->set('wc_original_total', null);
// WC()->session->set('wc_payment_option', null);
// WC()->session->set('wc_user_selected_payment', null);
// // Log that we cleared the session
// error_log("Cleared payment session data on page change or cart modification");
// }
// public function save_payment_option_to_order($order, $data) {
// // Get payment option from POST or session
// if (isset($_POST['payment_option'])) {
// $payment_option = sanitize_text_field($_POST['payment_option']);
// } else {
// $payment_option = WC()->session->get('wc_payment_option');
// }
// // Save payment option
// if (!empty($payment_option)) {
// $order->update_meta_data('_payment_option', $payment_option);
// }
// // Get original total and payable amount
// $original_total = $order->get_total();
// $payable_amount = null;
// if (isset($_POST['payable_amount'])) {
// $payable_amount = sanitize_text_field($_POST['payable_amount']);
// } else {
// $payment_amounts = WC()->session->get('wc_payment_amounts');
// if (!empty($payment_option) && !empty($payment_amounts) && isset($payment_amounts[$payment_option])) {
// $payable_amount = $payment_amounts[$payment_option];
// }
// }
// // Store original total for later restoration
// $order->update_meta_data('_temp_original_total', $original_total);
// if (!empty($payable_amount)) {
// $order->update_meta_data('_payable_amount', $payable_amount);
// // Only modify the order total if user has explicitly selected a non-full payment option
// $user_selected = WC()->session->get('wc_user_selected_payment');
// if ($payment_option !== 'full' && $user_selected) {
// $order->set_total($payable_amount);
// }
// }
// // Get and save original total from cart
// $cart_original_total = null;
// if (isset($_POST['original_total'])) {
// $cart_original_total = sanitize_text_field($_POST['original_total']);
// } else {
// $cart_original_total = WC()->session->get('wc_original_total');
// }
// if (!empty($cart_original_total)) {
// $order->update_meta_data('_original_total', $cart_original_total);
// }
// // Calculate and save balance amount
// if (!empty($payable_amount) && !empty($cart_original_total)) {
// $balance_amount = $cart_original_total - $payable_amount;
// $order->update_meta_data('_balance_amount', $balance_amount);
// } elseif (isset($_POST['balance_amount'])) {
// $order->update_meta_data('_balance_amount', sanitize_text_field($_POST['balance_amount']));
// }
// }
// /**
// * Restore original total after payment is completed
// */
// public function restore_original_total($order_id) {
// $order = wc_get_order($order_id);
// if (!$order) {
// return;
// }
// // Get temporary stored original total
// $temp_original_total = $order->get_meta('_temp_original_total');
// $payment_option = $order->get_meta('_payment_option');
// // Only restore if not full payment and we have a stored value
// if ($payment_option !== 'full' && !empty($temp_original_total)) {
// // Update the order total back to original
// // $order->set_total($temp_original_total);
// // $order->save();
// // Once restored, we can remove the temporary meta
// $order->delete_meta_data('_temp_original_total');
// $order->save();
// }
// }
// /**
// * Display payment details on thank you page
// */
// public function display_payment_details_on_thankyou($order_id) {
// $order = wc_get_order($order_id);
// if (!$order) {
// error_log("Order #$order_id could not be loaded in display_payment_details_on_thankyou");
// return;
// }
// $payment_option = $order->get_meta('_payment_option');
// $payable_amount = $order->get_meta('_payable_amount');
// $balance_amount = $order->get_meta('_balance_amount');
// $original_total = $order->get_meta('_original_total');
// // If original total is not saved, get from temp or current total
// if (empty($original_total)) {
// $temp_original_total = $order->get_meta('_temp_original_total');
// if (!empty($temp_original_total)) {
// $original_total = $temp_original_total;
// } else {
// $original_total = $order->get_total();
// }
// }
// // If no payable amount saved, use order total
// if (empty($payable_amount)) {
// $payable_amount = $order->get_total();
// }
// if (!empty($payment_option)) {
// // UPDATE REQUEST QUOTE DATABASE
// global $wpdb;
// // Get order ID
// $order_id = $order->get_id();
// // Get customer ID
// $customer_id = $order->get_customer_id();
// $order_total = $order->get_total();
// if (empty($customer_id)) {
// error_log("No customer ID for Order #$order_id - skipping RFQ update");
// // Proceed with guest orders without RFQ update
// } else {
// // Loop through each order item
// foreach ($order->get_items() as $item) {
// $product_id = 0; // $item->get_product_id();
// $item_total = 0 ;//= floatval($item->get_total()); // Get this specific line item's total
// // Prepare and execute the update query
// $query = $wpdb->prepare(
// "UPDATE {$wpdb->prefix}request_quote
// SET order_id = %d
// WHERE user_id = %d
// AND product_id = %d
// AND CAST(calculated_price AS DECIMAL(10,2)) = %f
// ORDER BY id DESC LIMIT 1",
// $order_id,
// $customer_id,
// $product_id,
// $order_total
// );
// $result = $wpdb->query($query);
// if ($result === false) {
// error_log("Database error updating RFQ for Order #$order_id: " . $wpdb->last_error);
// error_log("Failed Query: " . $query);
// } elseif ($wpdb->rows_affected > 0) {
// error_log("Successfully updated RFQ for Order #$order_id | User: $customer_id | Product: $product_id | Price: $item_total");
// } else {
// error_log("No RFQ record found to update for Order #$order_id | User: $customer_id | Product: $product_id | Price: $item_total");
// error_log("Query executed: " . $query);
// }
// }
// }
// // Update RFQ details to Order meta
// $table_name = $wpdb->prefix . 'request_quote';
// $result = $wpdb->get_row(
// $wpdb->prepare("SELECT * FROM $table_name WHERE order_id = %d", $order_id),
// ARRAY_A
// );
// if ($result === false) {
// error_log("Database error fetching RFQ for Order #$order_id: " . $wpdb->last_error);
// } elseif ($result) {
// // Map RFQ data to order meta
// $order->update_meta_data('rental_amount', $result['calculated_price']);
// $order->update_meta_data('start_date', $result['start_date']);
// $order->update_meta_data('end_date', $result['end_date']);
// $order->update_meta_data('_number_shifts_required', $result['shift']);
// $order->update_meta_data('model_name', $result['product_name']);
// $order->save();
// error_log("Successfully updated rental meta for Order #$order_id");
// } else {
// error_log("No RFQ record found in $table_name for Order #$order_id");
// }
// // Update Quotation Data
// if ($result) {
// $wc_quotation_table = $wpdb->prefix . 'quotation';
// $rfq_id = $result['id'];
// // Check if rfq_id already exists in wc_quotation
// $existing_entry = $wpdb->get_var(
// $wpdb->prepare("SELECT COUNT(*) FROM $wc_quotation_table WHERE rfq_id = %d", $rfq_id)
// );
// if ($existing_entry == 0) { // If no entry exists, insert new data
// $data = [
// 'user_name' => $result['user_name'],
// 'user_email' => $result['user_email'],
// 'start_date' => $result['start_date'],
// 'end_date' => $result['end_date'],
// 'shift' => $result['shift'],
// 'product_id' => $result['product_id'],
// 'product_name' => $result['product_name'],
// 'user_id' => $result['user_id'],
// 'rfq_id' => $rfq_id,
// 'final_price' => $result['calculated_price'], // Ensuring final_price isn't duplicated
// 'calculated_price' => $result['calculated_price'],
// 'region' => $result['region'],
// 'location' => $result['location'],
// 'qt_status' => 'Accepted',
// ];
// // Insert data into wc_quotation table
// $inserted = $wpdb->insert($wc_quotation_table, $data);
// if ($inserted) {
// error_log("Data inserted successfully into wc_quotation.");
// } else {
// error_log("Failed to insert data: " . $wpdb->last_error);
// }
// } else {
// error_log("Data already exists for rfq_id: $rfq_id");
// }
// } else {
// error_log("No data found for order_id: $order_id");
// }
// foreach ($order->get_items() as $item) {
// $product_id ;//= $item->get_product_id();
// $purpose = get_post_meta($product_id, 'purpose_of_listing', true);
// $order_total = get_post_meta($order_id, '_order_total', true);
// $rental_amount = get_post_meta($order_id, 'rental_amount', true);
// if ($purpose === 'Sales') {
// if($rental_amount){
// $order->update_meta_data('sale_amount', $order_total, true);
// $order->delete_meta_data('rental_amount'); // Delete the meta key
// $order->save();
// }
// else{
// $order->update_meta_data('sale_amount', $order_total, true);
// $order->save();
// }
// break;
// }
// }
// $product = wc_get_product( $product_id );
// $price = $product ? $product->get_regular_price() : 0;
// switch ($payment_option) {
// case 'full':
// echo esc_html__('100% Full Payment', 'woocommerce');
// break;
// case 'half':
// echo esc_html__('50% Payment', 'woocommerce');
// break;
// case '2LC':
// echo esc_html__('₹2,00,000 Payment', 'woocommerce');
// break;
// case '2-Months':
// echo esc_html__('2 Months Payment', 'woocommerce');
// break;
// default:
// echo esc_html($payment_option);
// }
// echo '</p>';
// $order->set_total($original_total);
// $order->save();
// // else{
// // echo '</div><div class="custom-div right-column">';
// // echo '<h2 style="font-size: 30px;">' . esc_html__('Buy Equipment Terms', 'woocommerce') . '</h2>';
// // // echo '<p><strong>' . esc_html__('Start Date:', 'woocommerce') . '</strong> ' . $result['start_date'] . '</p>';
// // // echo '<p><strong>' . esc_html__('End Date:', 'woocommerce') . '</strong> ' . $result['end_date'] . '</p>';
// // // echo '<p><strong>' . esc_html__('Shift:', 'woocommerce') . '</strong> ' . $result['shift'] . '</p>';
// // // echo '<p><strong>' . esc_html__('Location:', 'woocommerce') . '</strong> ' . $result['location'] . '</p>';
// // // echo '<p><strong>' . esc_html__('Base Price for 8HR:', 'woocommerce') . '</strong> ' . $price . '</p>';
// // echo '</div></div>';
// // }
// }
// }
// /**
// * Add meta box to order admin page
// */
// public function add_payment_details_meta_box() {
// add_meta_box(
// 'wc_payment_details',
// __('Payment Option Details', 'woocommerce'),
// array($this, 'display_payment_details_meta_box'),
// 'shop_order',
// 'side',
// 'default'
// );
// }
// /**
// * Display payment details in admin meta box
// */
// public function display_payment_details_meta_box($post) {
// $order = wc_get_order($post->ID);
// if (!$order) {
// return;
// }
// $payment_option = $order->get_meta('_payment_option');
// $payable_amount = $order->get_meta('_payable_amount');
// $balance_amount = $order->get_meta('_balance_amount');
// $original_total = $order->get_meta('_original_total');
// $pyment_gateway = $order->get_meta('_payment_method');
// $transaction_id = $order->get_meta('_transaction_id');
// // If original total is not saved, use current order total
// if (empty($original_total)) {
// $original_total = $order->get_total();
// }
// // If no payable amount saved, use order total
// if (empty($payable_amount)) {
// $payable_amount = $order->get_total();
// }
// if (!empty($payment_option)) {
// echo '<p><strong>' . esc_html__('Payment Option:', 'woocommerce') . '</strong> ';
// switch ($payment_option) {
// case 'full':
// echo esc_html__('100% Full Payment', 'woocommerce');
// break;
// case 'half':
// echo esc_html__('50% Payment', 'woocommerce');
// break;
// case '2LC':
// echo esc_html__('₹2,00,000 Payment', 'woocommerce');
// break;
// case '2-Months':
// echo esc_html__('2 Months Payment', 'woocommerce');
// break;
// default:
// echo esc_html($payment_option);
// }
// echo '</p>';
// // Always show original total
// echo '<p><strong>' . esc_html__('Payment Gateway:', 'woocommerce') . '</strong> ' . $pyment_gateway . '</p>';
// echo '<p><strong>' . esc_html__('Transaction ID:', 'woocommerce') . '</strong> ' . $transaction_id . '</p>';
// echo '<p><strong>' . esc_html__('Original Total:', 'woocommerce') . '</strong> ' . wc_price($original_total) . '</p>';
// echo '<p><strong>' . esc_html__('Paid Amount:', 'woocommerce') . '</strong> ' . wc_price($payable_amount) . '</p>';
// if (!empty($balance_amount) && $balance_amount > 0) {
// echo '<p><strong>' . esc_html__('Balance Amount:', 'woocommerce') . '</strong> ' . wc_price($balance_amount) . '</p>';
// }
// } else {
// echo '<p>' . esc_html__('No custom payment option selected for this order.', 'woocommerce') . '</p>';
// }
// }
// /**
// * Clear WooCommerce session data after order completion or cancellation
// */
// public function clear_payment_session($order_id) {
// // Clear all our custom session variables
// WC()->session->set('wc_payment_options', null);
// WC()->session->set('wc_payment_amounts', null);
// WC()->session->set('wc_original_total', null);
// WC()->session->set('wc_payment_option', null);
// WC()->session->set('wc_user_selected_payment', null);
// // Log that we cleared the session
// error_log("Cleared payment session data for Order #$order_id");
// }
// /**
// * Add terms & conditions checkboxes for different purpose products
// */
// public function add_purpose_terms_checkboxes() {
// $cart_items = WC()->cart->get_cart();
// $purposes = array();
// // Collect all unique purposes from cart items
// foreach ($cart_items as $cart_item) {
// $product_id = $cart_item['product_id'];
// $purpose = get_post_meta($product_id, 'purpose_of_listing', true);
// if (!empty($purpose)) {
// $purposes[$purpose] = true;
// }
// }
// }
// /**
// * Validate the terms checkboxes on checkout submission
// */
// public function validate_purpose_terms_checkboxes() {
// $cart_items = WC()->cart->get_cart();
// $purposes = array();
// // Collect all unique purposes from cart items
// foreach ($cart_items as $cart_item) {
// $product_id = $cart_item['product_id'];
// $purpose = get_post_meta($product_id, 'purpose_of_listing', true);
// if (!empty($purpose)) {
// $purposes[$purpose] = true;
// }
// }
// // Validate each required checkbox
// $error_shown = false;
// if (isset($purposes['Sales']) && !isset($_POST['purpose_terms_sales'])) {
// wc_add_notice(__('Please read and accept the sales terms and conditions to proceed.', 'woocommerce'), 'error');
// $error_shown = true;
// }
// if (isset($purposes['Renting']) && !isset($_POST['purpose_terms_renting'])) {
// wc_add_notice(__('Please read and accept the renting terms and conditions to proceed.', 'woocommerce'), 'error');
// $error_shown = true;
// }
// // Check other purposes
// $other_purposes = array_diff(array_keys($purposes), ['Sales', 'Renting']);
// if (!empty($other_purposes) && !isset($_POST['purpose_terms_other'])) {
// wc_add_notice(__('Please read and accept the general terms and conditions to proceed.', 'woocommerce'), 'error');
// $error_shown = true;
// }
// }
// /**
// * Add JavaScript to handle checkbox validation
// */
// // public function purpose_terms_scripts() {
// // if (!is_checkout()) {
// // return;
// // }
// // }
// // Validate Cart Count and restrict the Checkout of more than 1 product.
// // Restrict WooCommerce checkout to only one product at a time
// // add_action('woocommerce_check_cart_items', 'restrict_cart_to_one_item');
// function restrict_cart_to_one_item() {
// if (WC()->cart->get_cart_contents_count() > 1) {
// wc_add_notice(__('Only one equipment is allowed to checkout at a time', 'woocommerce'), 'error');
// }
// }
// function restrict_single_item_checkout() {
// if (is_admin()) {
// return;
// }
// // Only run on checkout page
// if (is_checkout() && !is_wc_endpoint_url()) {
// // Get cart contents count
// $cart_count = WC()->cart->get_cart_contents_count();
// // If more than one item in cart
// if ($cart_count > 1) {
// // Add notice
// wc_add_notice('Only one equipment is allowed to checkout at a time', 'error');
// // Redirect to cart page
// wp_redirect(wc_get_cart_url());
// exit;
// }
// }
// }
// }
// // // add_action('template_redirect', 'restrict_single_item_checkout');
// // // Initialize the plugin
// // new WC_Purpose_Payment_Options();