status !== PaymentStatus::PENDING) { return; } try { // Obtener el booking para acceder al restaurant_id $booking = $this->booking; if (!$booking) { return; } // Crear instancia del gateway UPayments if (!class_exists('\Alexr\Payments\GatewayUpayments')) { return; } $gateway = new \Alexr\Payments\GatewayUpayments($booking->restaurant_id); // Obtener el payment_id (ref) desde el campo específico $payment_id = null; // Prioridad 1: Usar upayments_payment_id si existe (este es el 'ref') if ($this->upayments_payment_id) { $payment_id = $this->upayments_payment_id; } // Prioridad 2: Buscar en settings['callback']['ref'] elseif (isset($this->settings['callback']['ref'])) { $payment_id = $this->settings['callback']['ref']; } if (!$payment_id) { error_log("UPayments payment check: No payment ID found for payment {$this->id}"); return; } // Consultar el estado actual en UPayments $payment_status = $gateway->checkPaymentStatus($payment_id); if ($payment_status && isset($payment_status['data']['status'])) { $status = strtoupper($payment_status['data']['status']); if (in_array($status, ['CAPTURED', 'SUCCESS', 'PAID'])) { // El pago se ha completado, actualizar el estado error_log("UPayments payment status check: Payment {$this->id} is now {$status}, updating status"); $this->status = PaymentStatus::SUCCEEDED; // Actualizar también el campo específico si existe if (property_exists($this, 'upayments_status')) { $this->upayments_status = $status; } // Actualizar el booking $booking->status = $booking->paymentStatus; $booking->save(); // Enviar notificaciones $booking->sendBookingEmail(); $booking->sendSmsNotification(); $booking->sendWhatsappNotification(); // Actualizar settings con la información actualizada $settings = $this->settings ?? []; $settings['status_check'] = [ 'checked_at' => date('Y-m-d H:i:s'), 'previous_status' => 'PENDING', 'new_status' => $status, 'payment_data' => $payment_status['data'], 'source' => 'manual_check' ]; $this->settings = $settings; $this->save(); error_log("UPayments payment successfully updated: booking {$booking->uuid}, status: {$status}"); } elseif (in_array($status, ['NOT CAPTURED', 'NOT_CAPTURED', 'FAILED', 'FAILURE'])) { // El pago ha fallado, actualizar el estado $this->status = PaymentStatus::FAILED; if (property_exists($this, 'upayments_status')) { $this->upayments_status = $status; } $this->save(); error_log("UPayments payment status updated to {$status} for payment {$this->id}"); } elseif (in_array($status, ['EXPIRED', 'CANCELLED', 'CANCELED'])) { // El pago ha expirado o ha sido cancelado $this->status = PaymentStatus::CANCELLED; if (property_exists($this, 'upayments_status')) { $this->upayments_status = $status; } $this->save(); error_log("UPayments payment status updated to {$status} for payment {$this->id}"); } } } catch (\Exception $e) { error_log('UPayments payment status check error: ' . $e->getMessage()); } } /** * Obtiene los datos de UPayments formateados para mostrar */ public function getUpaymentsDataFormattedAttribute() { // Verificar estado del pago antes de mostrar los datos $this->checkUpaymentsPaymentStatus(); $settings = $this->settings; if (empty($settings)) { return '
No UPayments 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']}"; $message .= "
"; } // Datos del callback si existen if (isset($settings['callback'])) { $callback = $settings['callback']; $message .= "

Callback Information

"; $message .= "
Order ID: {$callback['order_id']}
"; $message .= "
Reference ID: {$callback['reference_id']}
"; $message .= "
Payment ID (ref): {$callback['ref']}
"; $message .= "
Result: {$callback['result']}
"; $message .= "
Status: {$callback['status']}
"; $message .= "
Callback Time: {$callback['callback_time']}
"; $message .= "
Source: {$callback['source']}
"; // Información adicional del callback if (isset($callback['track_id'])) { $message .= "
Track ID: {$callback['track_id']}
"; } if (isset($callback['payment_type'])) { $message .= "
Payment Type: {$callback['payment_type']}
"; } if (isset($callback['invoice_id'])) { $message .= "
Invoice ID: {$callback['invoice_id']}
"; } if (isset($callback['transaction_date'])) { $message .= "
Transaction Date: {$callback['transaction_date']}
"; } if (isset($callback['receipt_id'])) { $message .= "
Receipt ID: {$callback['receipt_id']}
"; } if (isset($callback['requested_order_id'])) { $message .= "
Requested Order ID: {$callback['requested_order_id']}
"; } } // Datos del webhook si existen if (isset($settings['webhook'])) { $webhook = $settings['webhook']; $message .= "

Webhook Data

"; if (isset($webhook['order_id'])) { $message .= "
Order ID: {$webhook['order_id']}
"; } if (isset($webhook['result'])) { $message .= "
Result: {$webhook['result']}
"; } if (isset($webhook['payment_id'])) { $message .= "
Payment ID: {$webhook['payment_id']}
"; } if (isset($webhook['track_id'])) { $message .= "
Track ID: {$webhook['track_id']}
"; } } // Datos del failed callback si existen if (isset($settings['failed_callback'])) { $failed = $settings['failed_callback']; $message .= "

Failed Payment Information

"; $message .= "
"; $message .= "
Order ID: {$failed['order_id']}
"; $message .= "
Reference ID: {$failed['reference_id']}
"; $message .= "
Result: {$failed['result']}
"; $message .= "
Status: {$failed['status']}
"; $message .= "
Failed At: {$failed['callback_time']}
"; if (isset($failed['track_id'])) { $message .= "
Track ID: {$failed['track_id']}
"; } if (isset($failed['ref'])) { $message .= "
Transaction Ref: {$failed['ref']}
"; } $message .= "
"; } // Datos del cancelled callback si existen if (isset($settings['cancelled_callback'])) { $cancelled = $settings['cancelled_callback']; $message .= "

Cancelled Payment Information

"; $message .= "
"; $message .= "
Order ID: {$cancelled['order_id']}
"; $message .= "
Reference ID: {$cancelled['reference_id']}
"; $message .= "
Result: {$cancelled['result']}
"; $message .= "
Status: {$cancelled['status']}
"; $message .= "
Cancelled At: {$cancelled['callback_time']}
"; $message .= "
"; } // Datos iniciales del charge si no hay callback if (isset($settings['charge']) && !isset($settings['callback'])) { $charge = $settings['charge']; $message .= "

Payment Link Information

"; $message .= "
Status: " . ($charge['status'] ? 'Created' : 'Failed') . "
"; $message .= "
Message: {$charge['message']}
"; if (isset($charge['data']['link'])) { $message .= "
View Payment Link >>
"; } if (isset($charge['order_id'])) { $message .= "
Order ID: {$charge['order_id']}
"; } if (isset($charge['reference_id'])) { $message .= "
Reference ID: {$charge['reference_id']}
"; } } // 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) . "
"; if ($this->upayments_order_id) { $message .= "
Order ID: {$this->upayments_order_id}
"; } if ($this->upayments_reference_id) { $message .= "
Reference ID: {$this->upayments_reference_id}
"; } if ($this->upayments_payment_id) { $message .= "
Payment ID (ref): {$this->upayments_payment_id}
"; } if ($this->upayments_status) { $message .= "
UPayments Status: " . strtoupper($this->upayments_status) . "
"; } // Botón para verificar estado manualmente if ($this->status === PaymentStatus::PENDING && $this->upayments_payment_id) { $message .= "
"; $message .= ""; $message .= "
"; } // 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; } }