prepare('SELECT * FROM subscriptions WHERE user_id = ? AND status = \'active\''); $stmt->execute([$_SESSION['user_id']]); $subscription = $stmt->fetch(); if ($subscription) { \Stripe\Stripe::setApiKey($stripe_config['stripe']['secret_key']); // Cancel the subscription in Stripe $stripe_subscription = \Stripe\Subscription::update($subscription['stripe_subscription_id'], ['cancel_at_period_end' => true]); // Update the status in the local database $stmt = $pdo->prepare("UPDATE subscriptions SET status = 'canceled' WHERE id = ?"); $stmt->execute([$subscription['id']]); $message = "Your subscription has been successfully canceled. You will have access until the end of the current billing period."; } else { $message = "No active subscription found to cancel."; $error = true; } } catch (\Stripe\Exception\ApiErrorException $e) { $message = "Error canceling subscription: " . $e->getMessage(); $error = true; } catch (PDOException $e) { $message = "Database error: " . $e->getMessage(); $error = true; } include 'templates/header.php'; ?>

Cancellation Failed

Subscription Canceled

Back to Profile