status !== PaymentStatus::PENDING) {
return;
}
try {
// Obtener el booking para acceder al restaurant_id
$booking = $this->booking;
if (!$booking) {
return;
}
// Crear instancia del gateway Mollie
if (!class_exists('\Alexr\Payments\GatewayMollie')) {
return;
}
$gateway = new \Alexr\Payments\GatewayMollie($booking->restaurant_id);
// Obtener el payment_id desde settings
$payment_id = null;
// Prioridad 1: Buscar en settings['mollie']['id']
if (isset($this->settings['mollie']['id'])) {
$payment_id = $this->settings['mollie']['id'];
}
// Prioridad 2: Buscar en settings['payment']['id']
elseif (isset($this->settings['payment']['id'])) {
$payment_id = $this->settings['payment']['id'];
}
if (!$payment_id) {
error_log("Mollie payment check: No payment ID found for payment {$this->id}");
return;
}
// Consultar el estado actual en Mollie
$mollie_payment = $gateway->get_mollie_payment($payment_id);
if (!$mollie_payment) {
error_log("Mollie payment check: Could not retrieve payment {$payment_id} from Mollie API");
return;
}
$previous_status = $this->status;
$payment_updated = false;
// Verificar el estado del pago
if ($mollie_payment->isPaid()) {
error_log("Mollie payment status check: Payment {$this->id} is now PAID, updating status");
$this->status = PaymentStatus::SUCCEEDED;
$booking->status = $booking->paymentStatus;
$payment_updated = true;
// Enviar notificaciones
$booking->save();
$booking->sendBookingEmail();
$booking->sendSmsNotification();
$booking->sendWhatsappNotification();
$new_status_text = 'PAID';
}
elseif ($mollie_payment->isPending()) {
// Mantener como pending, pero actualizar los datos
$this->status = PaymentStatus::PENDING;
$booking->status = BookingStatus::PENDING_PAYMENT;
$booking->save();
$payment_updated = true;
$new_status_text = 'PENDING';
}
elseif ($mollie_payment->isExpired()) {
error_log("Mollie payment status check: Payment {$this->id} is EXPIRED, updating status");
$this->status = PaymentStatus::CANCELLED;
$payment_updated = true;
$new_status_text = 'EXPIRED';
}
elseif ($mollie_payment->isCanceled()) {
error_log("Mollie payment status check: Payment {$this->id} is CANCELED, updating status");
$this->status = PaymentStatus::CANCELLED;
$payment_updated = true;
$new_status_text = 'CANCELED';
}
elseif ($mollie_payment->isFailed()) {
error_log("Mollie payment status check: Payment {$this->id} is FAILED, updating status");
$this->status = PaymentStatus::FAILED;
$payment_updated = true;
$new_status_text = 'FAILED';
}
if ($payment_updated) {
// Actualizar settings con la información actualizada
$settings = $this->settings ?? [];
// Actualizar los datos del payment
$settings['payment'] = json_decode(json_encode($mollie_payment), true);
// 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,
'mollie_status' => $mollie_payment->status,
'source' => 'manual_check'
];
$this->settings = $settings;
$this->save();
error_log("Mollie payment successfully updated: booking {$booking->uuid}, status: {$new_status_text}");
}
} catch (\Exception $e) {
error_log('Mollie payment status check error: ' . $e->getMessage());
}
}
/**
* Obtiene los datos de Mollie formateados para mostrar
*/
public function getMollieDataFormattedAttribute()
{
// Verificar estado del pago antes de mostrar los datos
$this->checkMolliePaymentStatus();
$settings = $this->settings;
if (!isset($settings['mollie'])) {
return '
";
// 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']} (Mollie: {$statusCheck['mollie_status']})";
$message .= "
";
}
// Datos del payment actualizado si existe
if (isset($settings['payment'])) {
$payment = $settings['payment'];
$message .= "
Payment Information
";
$message .= "
Payment ID: {$payment['id']}
";
$message .= "
Status: " . strtoupper($payment['status']) . "
";
$message .= "
Mode: " . strtoupper($payment['mode']) . "
";
// Información del monto
if (isset($payment['amount'])) {
$message .= "
Amount: {$payment['amount']['value']} {$payment['amount']['currency']}
";
}
if (isset($payment['settlementAmount']) && $payment['settlementAmount']) {
$message .= "
Settlement Amount: {$payment['settlementAmount']['value']} {$payment['settlementAmount']['currency']}
";
}
if (isset($payment['amountRefunded']) && $payment['amountRefunded']) {
$message .= "
Amount Refunded: {$payment['amountRefunded']['value']} {$payment['amountRefunded']['currency']}
";
}
if (isset($payment['amountRemaining']) && $payment['amountRemaining']) {
$message .= "
Amount Remaining: {$payment['amountRemaining']['value']} {$payment['amountRemaining']['currency']}
";
}
// Información de método de pago
if (isset($payment['method']) && $payment['method']) {
$message .= "
Payment Method: " . strtoupper($payment['method']) . "
";
}
// Fechas importantes
if (isset($payment['createdAt'])) {
$message .= "
Created At: " . date('Y-m-d H:i:s', strtotime($payment['createdAt'])) . "
";
}
if (isset($payment['paidAt']) && $payment['paidAt']) {
$message .= "
Paid At: " . date('Y-m-d H:i:s', strtotime($payment['paidAt'])) . "
";
}
if (isset($payment['canceledAt']) && $payment['canceledAt']) {
$message .= "
Canceled At: " . date('Y-m-d H:i:s', strtotime($payment['canceledAt'])) . "
";
}
if (isset($payment['expiresAt']) && $payment['expiresAt']) {
$message .= "
Expires At: " . date('Y-m-d H:i:s', strtotime($payment['expiresAt'])) . "
";
}
if (isset($payment['failedAt']) && $payment['failedAt']) {
$message .= "
Failed At: " . date('Y-m-d H:i:s', strtotime($payment['failedAt'])) . "
";
}
// Detalles de la tarjeta si existen
if (isset($payment['details']) && $payment['details']) {
$details = $payment['details'];
$message .= "
Card Details
";
if (isset($details['cardNumber'])) {
$message .= "
Card Number: **** **** **** {$details['cardNumber']}
";
}
if (isset($details['cardHolder'])) {
$message .= "
Card Holder: {$details['cardHolder']}
";
}
if (isset($details['cardLabel'])) {
$message .= "
Card Label: {$details['cardLabel']}
";
}
if (isset($details['cardAudience'])) {
$message .= "
Card Audience: " . strtoupper($details['cardAudience']) . "
";
}
if (isset($details['cardCountryCode'])) {
$message .= "
Card Country: {$details['cardCountryCode']}
";
}
if (isset($details['cardToken'])) {
$message .= "
Card Token: {$details['cardToken']}
";
}
}
// Información adicional
$message .= "
Additional Information
";
$message .= "
Description: {$payment['description']}
";
if (isset($payment['locale']) && $payment['locale']) {
$message .= "
Locale: {$payment['locale']}
";
}
if (isset($payment['countryCode']) && $payment['countryCode']) {
$message .= "
Country Code: {$payment['countryCode']}
";
}
if (isset($payment['profileId'])) {
$message .= "
Profile ID: {$payment['profileId']}
";
}
if (isset($payment['sequenceType'])) {
$message .= "
Sequence Type: " . strtoupper($payment['sequenceType']) . "
";
}
// Enlaces útiles
if (isset($payment['_links'])) {
$links = $payment['_links'];
$message .= "
Links
";
if (isset($links['dashboard']['href'])) {
$message .= "
";
}
if (isset($links['checkout']['href'])) {
$message .= "
";
}
if (isset($links['changePaymentState']['href'])) {
$message .= "
";
}
}
}
// Datos iniciales de Mollie si no hay payment actualizado
else {
$mollie = $settings['mollie'];
$message .= "
Initial Mollie Data
";
$message .= "
Payment ID: {$mollie['id']}
";
$message .= "
Status: " . strtoupper($mollie['status']) . "
";
$message .= "
Mode: " . strtoupper($mollie['mode']) . "
";
if (isset($mollie['amount'])) {
$message .= "
Amount: {$mollie['amount']['value']} {$mollie['amount']['currency']}
";
}
if (isset($mollie['description'])) {
$message .= "
Description: {$mollie['description']}
";
}
if (isset($mollie['createdAt'])) {
$message .= "
Created At: " . date('Y-m-d H:i:s', strtotime($mollie['createdAt'])) . "
";
}
if (isset($mollie['expiresAt']) && $mollie['expiresAt']) {
$message .= "
Expires At: " . date('Y-m-d H:i:s', strtotime($mollie['expiresAt'])) . "
";
}
// Enlaces
if (isset($mollie['_links']['checkout']['href'])) {
$message .= "
";
}
if (isset($mollie['_links']['dashboard']['href'])) {
$message .= "
";
}
}
// 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;
}
}