36716-vm/create-portal-session.php
2025-12-07 05:00:42 +00:00

32 lines
814 B
PHP

<?php
session_start();
require_once 'db/config.php';
require_once 'stripe/init.php';
if (!isset($_SESSION['user_id']) || $_SESSION['user_role'] !== 'client') {
header('Location: login.php');
exit;
}
$user_id = $_SESSION['user_id'];
$stmt = db()->prepare("SELECT stripe_customer_id FROM clients WHERE id = ?");
$stmt->execute([$user_id]);
$client = $stmt->fetch();
if (!$client || !$client['stripe_customer_id']) {
// This should not happen if the user has a subscription
header('Location: subscribe.php');
exit;
}
$return_url = 'http://' . $_SERVER['HTTP_HOST'] . '/manage-subscription.php';
$portalSession = \Stripe\BillingPortal\Session::create([
'customer' => $client['stripe_customer_id'],
'return_url' => $return_url,
]);
header("Location: " . $portalSession->url);
exit();