status !== PaymentStatus::PENDING) { return; } try { // Obtener el booking para acceder al restaurant_id $booking = $this->booking; if (!$booking) { return; } // Crear instancia del gateway Square if (!class_exists('\Alexr\Payments\GatewaySquare')) { return; } $gateway = new \Alexr\Payments\GatewaySquare($booking->restaurant_id); // Obtener el order_id desde settings $order_id = null; // Prioridad 1: Buscar en settings['square']['payment_link']['order_id'] if (isset($this->settings['square']['payment_link']['order_id'])) { $order_id = $this->settings['square']['payment_link']['order_id']; } if (!$order_id) { error_log("Square payment check: No order ID found for payment {$this->id}"); return; } // Consultar el estado actual en Square usando el método del gateway $square_payment = $gateway->get_square_payment_from_order_id($order_id); if (!$square_payment) { error_log("Square payment check: Could not retrieve payment for order {$order_id} from Square API"); return; } $previous_status = $this->status; $payment_updated = false; $new_status_text = ''; // Verificar el estado del pago if (isset($square_payment['status'])) { switch (strtoupper($square_payment['status'])) { case 'COMPLETED': case 'PAID': error_log("Square payment status check: Payment {$this->id} is now COMPLETED, updating status"); $this->status = PaymentStatus::SUCCEEDED; // Actualizar el booking $booking->status = $booking->paymentStatus; $booking->save(); // Enviar notificaciones $booking->sendBookingEmail(); $booking->sendSmsNotification(); $booking->sendWhatsappNotification(); $payment_updated = true; $new_status_text = 'COMPLETED'; break; case 'APPROVED': // Mantener como pending, pero podría necesitar captura $this->status = PaymentStatus::PENDING; $payment_updated = true; $new_status_text = 'APPROVED'; break; case 'CANCELLED': case 'CANCELED': error_log("Square payment status check: Payment {$this->id} is CANCELLED, updating status"); $this->status = PaymentStatus::CANCELLED; $payment_updated = true; $new_status_text = 'CANCELLED'; break; case 'FAILED': error_log("Square payment status check: Payment {$this->id} is FAILED, updating status"); $this->status = PaymentStatus::FAILED; $payment_updated = true; $new_status_text = 'FAILED'; break; case 'PENDING': // Mantener como pending $new_status_text = 'PENDING'; $payment_updated = true; break; } } if ($payment_updated) { // Actualizar settings con la información actualizada $settings = $this->settings ?? []; // Actualizar los datos del payment $settings['payment'] = $square_payment; // Agregar información de la verificación $settings['status_check'] = [ 'checked_at' => date('Y-m-d H:i:s'), 'previous_status' => $previous_status, 'new_status' => $this->status, 'square_status' => $square_payment['status'], 'source' => 'manual_check' ]; $this->settings = $settings; $this->save(); error_log("Square payment successfully updated: booking {$booking->uuid}, status: {$new_status_text}"); } } catch (\Exception $e) { error_log('Square payment status check error: ' . $e->getMessage()); } } /** * Obtiene los datos de Square formateados para mostrar */ public function getSquareDataFormattedAttribute() { // Verificar estado del pago antes de mostrar los datos $this->checkSquarePaymentStatus(); $settings = $this->settings; if (!isset($settings['square'])) { return '
No Square data available
'; } $message = "
"; // Mostrar mensaje si se actualizó el estado if (isset($settings['status_check'])) { $statusCheck = $settings['status_check']; $message .= "
"; $message .= "Status Updated: Payment status was automatically updated on {$statusCheck['checked_at']} from {$statusCheck['previous_status']} to {$statusCheck['new_status']} (Square: {$statusCheck['square_status']})"; $message .= "
"; } // Datos del payment actualizado si existe if (isset($settings['payment'])) { $payment = $settings['payment']; $message .= "

Square Payment Information

"; $message .= "
Payment ID: {$payment['id']}
"; $message .= "
Status: " . strtoupper($payment['status']) . "
"; $message .= "
Receipt Number: {$payment['receipt_number']}
"; // Información del monto if (isset($payment['amount_money'])) { $amount = $payment['amount_money']; $message .= "
Amount: " . number_format($amount['amount'] / 100, 2) . " {$amount['currency']}
"; } if (isset($payment['total_money'])) { $total = $payment['total_money']; $message .= "
Total: " . number_format($total['amount'] / 100, 2) . " {$total['currency']}
"; } // Fechas importantes if (isset($payment['created_at'])) { $message .= "
Created At: " . date('Y-m-d H:i:s', strtotime($payment['created_at'])) . "
"; } if (isset($payment['updated_at'])) { $message .= "
Updated At: " . date('Y-m-d H:i:s', strtotime($payment['updated_at'])) . "
"; } // Información del método de pago if (isset($payment['source_type'])) { $message .= "
Source Type: " . strtoupper($payment['source_type']) . "
"; } // Detalles externos si existen if (isset($payment['external_details'])) { $details = $payment['external_details']; $message .= "

Payment Details

"; if (isset($details['type'])) { $message .= "
Payment Type: " . strtoupper($details['type']) . "
"; } if (isset($details['source'])) { $message .= "
Source: {$details['source']}
"; } } // Información de la aplicación if (isset($payment['application_details'])) { $app = $payment['application_details']; $message .= "

Application Details

"; if (isset($app['square_product'])) { $message .= "
Square Product: {$app['square_product']}
"; } if (isset($app['application_id'])) { $message .= "
Application ID: {$app['application_id']}
"; } } // Capacidades del pago if (isset($payment['capabilities']) && !empty($payment['capabilities'])) { $message .= "

Payment Capabilities

"; $message .= "
Capabilities: " . implode(', ', $payment['capabilities']) . "
"; } // Order ID y Location ID if (isset($payment['order_id'])) { $message .= "
Order ID: {$payment['order_id']}
"; } if (isset($payment['location_id'])) { $message .= "
Location ID: {$payment['location_id']}
"; } // Receipt URL si está disponible if (isset($payment['receipt_url'])) { $message .= "
View Receipt >>
"; } } // Información del Payment Link if (isset($settings['square']['payment_link'])) { $paymentLink = $settings['square']['payment_link']; $message .= "

Payment Link Information

"; $message .= "
Link ID: {$paymentLink['id']}
"; $message .= "
Description: {$paymentLink['description']}
"; $message .= "
Order ID: {$paymentLink['order_id']}
"; if (isset($paymentLink['created_at'])) { $message .= "
Created At: " . date('Y-m-d H:i:s', strtotime($paymentLink['created_at'])) . "
"; } // Enlaces del payment link if (isset($paymentLink['url'])) { $message .= "
Payment Link >>
"; } if (isset($paymentLink['long_url'])) { $message .= "
Square Dashboard >>
"; } } // Información de la orden si existe if (isset($settings['square']['related_resources']['orders'][0])) { $order = $settings['square']['related_resources']['orders'][0]; $message .= "

Order Information

"; $message .= "
Order ID: {$order['id']}
"; $message .= "
State: " . strtoupper($order['state']) . "
"; $message .= "
Location ID: {$order['location_id']}
"; // Información del monto total if (isset($order['total_money'])) { $total = $order['total_money']; $message .= "
Total Amount: " . number_format($total['amount'] / 100, 2) . " {$total['currency']}
"; } if (isset($order['net_amount_due_money'])) { $netDue = $order['net_amount_due_money']; $message .= "
Net Amount Due: " . number_format($netDue['amount'] / 100, 2) . " {$netDue['currency']}
"; } // Información de los line items if (isset($order['line_items']) && !empty($order['line_items'])) { $lineItem = $order['line_items'][0]; $message .= "

Line Item Details

"; $message .= "
Item Name: {$lineItem['name']}
"; $message .= "
Quantity: {$lineItem['quantity']}
"; $message .= "
Item Type: " . strtoupper($lineItem['item_type']) . "
"; if (isset($lineItem['total_money'])) { $itemTotal = $lineItem['total_money']; $message .= "
Item Total: " . number_format($itemTotal['amount'] / 100, 2) . " {$itemTotal['currency']}
"; } } // Fechas de la orden if (isset($order['created_at'])) { $message .= "
Order Created: " . date('Y-m-d H:i:s', strtotime($order['created_at'])) . "
"; } if (isset($order['updated_at'])) { $message .= "
Order Updated: " . date('Y-m-d H:i:s', strtotime($order['updated_at'])) . "
"; } } // Información del modelo Payment $message .= "

Payment Model Information

"; $message .= "
Payment ID: {$this->id}
"; $message .= "
Current Status: " . strtoupper($this->status) . "
"; $message .= "
Amount: " . number_format($this->amount / 100, 2) . " " . strtoupper($this->currency) . "
"; $message .= "
Payment Type: " . strtoupper($this->payment_type) . "
"; // Mostrar datos en crudo en un área colapsable para debugging $message .= "

Raw Data

"; $message .= "
"; $message .= "Show Raw Settings"; $message .= "
"; $message .= "
" . htmlspecialchars(json_encode($settings, JSON_PRETTY_PRINT)) . "
"; $message .= "
"; $message .= "
"; $message .= "
"; return $message; } }