36716-vm/payment-success.php
2025-12-07 05:00:42 +00:00

51 lines
1.9 KiB
PHP

<?php
session_start();
require_once 'db/config.php';
require_once 'stripe/init.php';
if (!isset($_SESSION['user_id']) || $_SESSION['user_role'] !== 'client' || !isset($_GET['session_id'])) {
header('Location: login.php');
exit;
}
try {
$checkout_session = \Stripe\Checkout\Session::retrieve($_GET['session_id']);
if ($checkout_session->payment_status === 'paid') {
$booking_details = $_SESSION['pending_booking'];
$pdo = db();
$stmt = $pdo->prepare("INSERT INTO bookings (coach_id, client_id, booking_time, stripe_payment_intent_id, payment_status) VALUES (?, ?, ?, ?, 'paid')");
$stmt->execute([$booking_details['coach_id'], $booking_details['client_id'], $booking_details['booking_time'], $checkout_session->payment_intent]);
// Send email notification to coach
require_once __DIR__ . '/mail/MailService.php';
$coach_stmt = $pdo->prepare("SELECT email FROM coaches WHERE id = ?");
$coach_stmt->execute([$booking_details['coach_id']]);
$coach = $coach_stmt->fetch();
$client_stmt = $pdo->prepare("SELECT name FROM clients WHERE id = ?");
$client_stmt->execute([$booking_details['client_id']]);
$client = $client_stmt->fetch();
if ($coach && $client) {
$to = $coach['email'];
$subject = 'New Booking Confirmation';
$message = "You have a new booking from {$client['name']} for {$booking_details['booking_time']}.";
MailService::sendMail($to, $subject, $message, $message);
}
// Clear the pending booking from the session
unset($_SESSION['pending_booking']);
header('Location: dashboard.php?booking=success');
exit;
} else {
header('Location: payment-cancel.php?reason=payment_not_complete');
exit;
}
} catch (\Stripe\Exception\ApiErrorException $e) {
// Handle error
header('Location: payment-cancel.php?reason=invalid_session');
exit;
}