// WHATSAPP FUNCTIONS START // add_action('gform_after_submission', 'send_whatsapp_message', 10, 2); function send_whatsapp_message($entry, $form) { // Replace '1' with your actual Gravity Forms form ID if ($form['id'] != 1) { // Replace with your form ID return; } // Generate a unique booking ID $booking_id = uniqid('booking_'); // Extract form field values using the actual field IDs $restaurant = rgar($entry, '1'); // Field ID for Restaurant $reservation_date = rgar($entry, '7'); // Field ID for Date of Reservation $reservation_time = rgar($entry, '8'); // Field ID for Time of Reservation $booking_name = rgar($entry, '9'); // Field ID for Booking Name $number_persons = rgar($entry, '10'); // Field ID for Number of Persons $phone_number = rgar($entry, '5'); // Field ID for Booking Contact Phone Number $email = rgar($entry, '6'); // Field ID for Booking Email // Convert date format from YYYY-MM-DD to DD/MM/YYYY $date_object = DateTime::createFromFormat('Y-m-d', $reservation_date); $formatted_date = $date_object->format('d/m/Y'); // Normalize the phone number $normalized_phone_number = normalize_phone_number($phone_number); // Store the booking ID in the entry metadata gform_update_meta($entry['id'], 'booking_id', $booking_id); // Message for the WhatsApp group $group_message_text = "Booking ID: $booking_id\n"; $group_message_text .= "Restaurant: $restaurant\n"; $group_message_text .= "Date of Reservation: $formatted_date\n"; // Use formatted date $group_message_text .= "Time of Reservation: $reservation_time\n"; $group_message_text .= "Booking Name: $booking_name\n"; $group_message_text .= "Persons: $number_persons\n"; $group_message_text .= "Contact Phone Number: $phone_number\n"; $group_message_text .= "Email: $email\n\n"; $group_message_text .= "Please reply with 'accept $booking_id' or 'decline $booking_id' to confirm or reject this booking."; // Message for the customer $customer_message_text = "Hi! Thank you for your booking with Korsan Restaurants. Your booking is tentatively confirmed - if for any reason your booking cannot be accommodated on the requested night, our restaurant manager will get in touch with you.\n\n"; $customer_message_text .= "Your booking details are as follows:\n\n"; $customer_message_text .= "Restaurant: $restaurant\n"; $customer_message_text .= "Date of Reservation: $formatted_date\n"; $customer_message_text .= "Time of Reservation: $reservation_time\n"; $customer_message_text .= "Booking Name: $booking_name\n"; $customer_message_text .= "Persons: $number_persons\n"; $customer_message_text .= "Contact Phone Number: $phone_number\n"; $customer_message_text .= "Email: $email\n\n"; $customer_message_text .= "We look forward to welcoming you at $restaurant!"; // Log for debugging error_log("Group message: $group_message_text"); error_log("Customer message: $customer_message_text"); // Send message to WhatsApp group $nodeurl = 'https://api.kpnotify.com/send'; $data_group = [ 'receiver' => '120363317428466192@g.us', // WhatsApp Group ID 'msgtext' => $group_message_text, // Message text from form submission 'token' => 'nvebldL2g90qB1ZV5zTi', ]; $ch = curl_init(); curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/x-www-form-urlencoded']); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data_group)); curl_setopt($ch, CURLOPT_URL, $nodeurl); curl_setopt($ch, CURLOPT_TIMEOUT, 30); $response_group = curl_exec($ch); curl_close($ch); // Log response for debugging error_log("Group response: " . print_r($response_group, true)); // Send message to the customer $data_customer = [ 'receiver' => $normalized_phone_number, // Customer's phone number in international format 'msgtext' => $customer_message_text, // Message text to customer 'token' => 'nvebldL2g90qB1ZV5zTi', ]; $ch = curl_init(); curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/x-www-form-urlencoded']); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data_customer)); curl_setopt($ch, CURLOPT_URL, $nodeurl); curl_setopt($ch, CURLOPT_TIMEOUT, 30); $response_customer = curl_exec($ch); curl_close($ch); // Log response for debugging error_log("Customer response: " . print_r($response_customer, true)); // Optional: log the response for debugging GFCommon::log_debug(__METHOD__ . '(): group response => ' . print_r($response_group, true)); GFCommon::log_debug(__METHOD__ . '(): customer response => ' . print_r($response_customer, true)); } function normalize_phone_number($phone_number) { // Remove all non-numeric characters except '+' $phone_number = preg_replace('/[^\d+]/', '', $phone_number); // If the phone number starts with '+', assume it's already in international format if (substr($phone_number, 0, 1) === '+') { return $phone_number; } // If the phone number starts with '00', replace it with '+' if (substr($phone_number, 0, 2) === '00') { return '+' . substr($phone_number, 2); } // If the phone number starts with '0', replace it with the default country code (e.g., '+44') if (substr($phone_number, 0, 1) === '0') { // You can change '+44' to your default country code if needed return '+44' . substr($phone_number, 1); } // Default case (assume international format without the '+') return '+' . $phone_number; } function get_booking_details($booking_id) { // Get the entry ID based on the booking ID from Gravity Forms $search_criteria = [ 'field_filters' => [ [ 'key' => 'booking_id', 'value' => $booking_id, ], ], ]; $entries = GFAPI::get_entries(0, $search_criteria); if (empty($entries)) { return null; } $entry = $entries[0]; // Assuming booking ID is unique, get the first entry // Extract booking details from the entry return [ 'booking_id' => $booking_id, 'restaurant' => rgar($entry, '1'), 'reservation_date' => rgar($entry, '7'), 'reservation_time' => rgar($entry, '8'), 'booking_name' => rgar($entry, '9'), 'number_persons' => rgar($entry, '10'), 'phone_number' => rgar($entry, '5'), 'email' => rgar($entry, '6'), ]; } // WHATSAPP FUNCTIONS END // // REST API ENDPOINT START // add_action('rest_api_init', function () { register_rest_route('korsan/v1', '/booking-details/(?P[a-zA-Z0-9_]+)', array( 'methods' => 'GET', 'callback' => 'korsan_get_booking_details', )); }); if (!function_exists('korsan_get_booking_details')) { function korsan_get_booking_details(WP_REST_Request $request) { $booking_id = $request['booking_id']; // Get the entry ID based on the booking ID from Gravity Forms $search_criteria = [ 'field_filters' => [ [ 'key' => 'booking_id', 'value' => $booking_id, ], ], ]; $entries = GFAPI::get_entries(0, $search_criteria); if (empty($entries)) { return new WP_Error('no_booking', 'Booking not found', array('status' => 404)); } $entry = $entries[0]; // Assuming booking ID is unique, get the first entry // Extract booking details from the entry $booking_details = [ 'booking_id' => $booking_id, 'restaurant' => rgar($entry, '1'), 'reservation_date' => rgar($entry, '7'), 'reservation_time' => rgar($entry, '8'), 'booking_name' => rgar($entry, '9'), 'number_persons' => rgar($entry, '10'), 'phone_number' => rgar($entry, '5'), 'email' => rgar($entry, '6'), ]; return $booking_details; } } // REST API ENDPOINT END //