prepare("SELECT * FROM bookings WHERE id = ? AND client_id = ?"); $stmt->execute([$booking_id, $client_id]); $booking = $stmt->fetch(); if ($booking) { if (!empty($booking['stripe_payment_intent_id'])) { try { $refund = \Stripe\Refund::create([ 'payment_intent' => $booking['stripe_payment_intent_id'], ]); if ($refund->status == 'succeeded') { // Optionally, you can store the refund ID in your database $stmt = db()->prepare("UPDATE bookings SET status = 'cancelled', payment_status = 'refunded' WHERE id = ?"); $stmt->execute([$booking_id]); } else { // Handle refund failure header('Location: dashboard.php?status=error'); exit; } } catch (\Stripe\Exception\ApiErrorException $e) { // Handle Stripe API errors error_log('Stripe API error: ' . $e->getMessage()); header('Location: dashboard.php?status=error'); exit; } } else { $stmt = db()->prepare("UPDATE bookings SET status = 'cancelled' WHERE id = ?"); $stmt->execute([$booking_id]); } // Notify coach $stmt = db()->prepare("SELECT co.email, co.name as coach_name, c.name as client_name, b.booking_time FROM bookings b JOIN coaches co ON b.coach_id = co.id JOIN clients c ON b.client_id = c.id WHERE b.id = ?"); $stmt->execute([$booking_id]); $booking_details = $stmt->fetch(); if ($booking_details) { $to = $booking_details['email']; $subject = 'Booking Cancellation & Refund'; $body = "

Hello " . htmlspecialchars($booking_details['coach_name']) . ",

"; $body .= "

The booking with " . htmlspecialchars($booking_details['client_name']) . " on " . htmlspecialchars(date('F j, Y, g:i a', strtotime($booking_details['booking_time']))) . " has been cancelled and a refund has been issued.

"; $body .= "

Thank you for using CoachConnect.

"; MailService::sendMail($to, $subject, $body, strip_tags($body)); } header('Location: dashboard.php?status=cancelled'); } else { header('Location: dashboard.php?status=error'); } exit;