$parsed['provider'] ?? 'unknown', 'restaurant_id' => $restaurantId, 'message_id' => $message->id, 'conversation_id' => $conversation->id, 'booking_id' => $message->booking_id, 'button_payload' => $parsed['button_payload'] ?? null, 'button_text' => $parsed['button_text'] ?? null, 'interactive_type' => $parsed['interactive_type'] ?? null, ]); }*/ } /** * Handle confirm_booking button response * * @param array $parsed Parsed message data * @param int $restaurantId Restaurant ID * @param WhatsappMessage $message The WhatsApp message * @param WhatsappConversation $conversation The conversation */ public static function handleConfirmBooking( array $parsed, int $restaurantId, WhatsappMessage $message, WhatsappConversation $conversation ): void { // TODO: Implement booking confirmation logic // This should: // 1. Find the booking (using $message->booking_id or matching by phone/date) // 2. Update booking status to confirmed // 3. Optionally send a confirmation message back $booking = Booking::find($message->booking_id); if ($booking) { $booking->status = BookingStatus::CONFIRMED; $booking->save(); $booking->sendEmailConfirmed($booking->language); $booking->sendEmailAdminConfirmed(); $booking->sendWhatsappConfirmed($booking->language); $booking->restaurant->notify(new \Alexr\Notifications\BookingConfirmedByUser($booking)); // Send custom reply message if configured self::sendReconfirmationReply($restaurantId, $booking, 'confirm'); } /*if (function_exists('ray')) { ray('WhatsApp Button: confirm_booking', [ 'provider' => $parsed['provider'] ?? 'unknown', 'restaurant_id' => $restaurantId, 'message_id' => $message->id, 'conversation_id' => $conversation->id, 'booking_id' => $message->booking_id, 'button_payload' => $parsed['button_payload'] ?? null, ]); }*/ error_log('[WhatsApp] Button confirm_booking received for restaurant ' . $restaurantId); // Fire action for custom handling do_action('alexr_whatsapp_booking_confirmed', $message->booking_id, $restaurantId, $message, $conversation); } /** * Handle cancel_booking button response * * @param array $parsed Parsed message data * @param int $restaurantId Restaurant ID * @param WhatsappMessage $message The WhatsApp message * @param WhatsappConversation $conversation The conversation */ public static function handleCancelBooking( array $parsed, int $restaurantId, WhatsappMessage $message, WhatsappConversation $conversation ): void { // TODO: Implement booking cancellation logic // This should: // 1. Find the booking // 2. Update booking status to cancelled // 3. Optionally send a cancellation confirmation message $booking = Booking::find($message->booking_id); if ($booking) { $booking->status = BookingStatus::CANCELLED; $booking->save(); $booking->sendEmailCancelled($booking->language); $booking->sendEmailAdminCancelled(); $booking->sendWhatsappCancelled($booking->language); $booking->restaurant->notify(new \Alexr\Notifications\BookingCancelledByUser($booking)); // Send custom reply message if configured self::sendReconfirmationReply($restaurantId, $booking, 'cancel'); } /*if (function_exists('ray')) { ray('WhatsApp Button: cancel_booking', [ 'provider' => $parsed['provider'] ?? 'unknown', 'restaurant_id' => $restaurantId, 'message_id' => $message->id, 'conversation_id' => $conversation->id, 'booking_id' => $message->booking_id, 'button_payload' => $parsed['button_payload'] ?? null, ]); }*/ error_log('[WhatsApp] Button cancel_booking received for restaurant ' . $restaurantId); // Fire action for custom handling do_action('alexr_whatsapp_booking_cancelled', $message->booking_id, $restaurantId, $message, $conversation); } /** * Send a custom reply message from a WhatsappReconfirmation setting * after the customer clicks confirm or cancel. * * @param int $restaurantId * @param Booking $booking * @param string $action 'confirm' or 'cancel' */ protected static function sendReconfirmationReply(int $restaurantId, Booking $booking, string $action): void { $lang = $booking->language ?? 'en'; // Find all reconfirmations for this restaurant $reconfirmations = WhatsappReconfirmation::where('restaurant_id', $restaurantId)->get(); if (!$reconfirmations) { return; } // Get customer phone $customerPhone = ($booking->dial_code ?? '') . ($booking->phone ?? ''); $customerPhone = preg_replace('/\s+/', '', $customerPhone); if (empty($customerPhone)) { return; } if (strpos($customerPhone, '+') !== 0) { $customerPhone = '+' . $customerPhone; } // Check each reconfirmation for a reply message foreach ($reconfirmations as $reconfirmation) { if (!$reconfirmation->isEnabled()) { continue; } $replyMessage = null; if ($action === 'confirm') { $replyMessage = $reconfirmation->getReplyOnConfirm($lang); } elseif ($action === 'cancel') { $replyMessage = $reconfirmation->getReplyOnCancel($lang); } if ($replyMessage) { self::sendResponse($restaurantId, $customerPhone, $replyMessage, $booking->id); // Only send from the first matching reconfirmation return; } } } /** * Register a custom button handler * * @param string $buttonPayload The button payload to handle (e.g., 'my_custom_action') * @param callable $handler The handler function * @param int $priority Hook priority */ public static function registerHandler(string $buttonPayload, callable $handler, int $priority = 10): void { $sanitizedPayload = sanitize_key($buttonPayload); add_action('alexr_whatsapp_button_' . $sanitizedPayload, $handler, $priority, 4); } /** * Send a response message after button action * * @param int $restaurantId * @param string $toPhone * @param string $message * @param int|null $bookingId * @return array */ public static function sendResponse( int $restaurantId, string $toPhone, string $message, ?int $bookingId = null ): array { $manager = WhatsappManager::forRestaurant($restaurantId); return $manager->sendMessage($toPhone, $message, $bookingId); } } // Register the handlers WhatsappButtonHandler::register();