70 lines
1.7 KiB
PHP
70 lines
1.7 KiB
PHP
<?php
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
require_once 'vendor/autoload.php';
|
|
require_once 'includes/api_keys.php';
|
|
|
|
if (!isset($_SESSION['user_id'])) {
|
|
header("Location: login.php");
|
|
exit();
|
|
}
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
header("Location: checkout.php");
|
|
exit();
|
|
}
|
|
|
|
\Stripe\Stripe::setApiKey($stripeSecretKey);
|
|
|
|
$user_id = $_SESSION['user_id'];
|
|
$pdo = db();
|
|
|
|
// Fetch cart items
|
|
$stmt = $pdo->prepare("SELECT mi.name, mi.price, c.quantity FROM cart c JOIN menu_items mi ON c.menu_item_id = mi.id WHERE c.user_id = ?");
|
|
$stmt->execute([$user_id]);
|
|
$cart_items = $stmt->fetchAll();
|
|
|
|
if (empty($cart_items)) {
|
|
header("Location: cart.php");
|
|
exit();
|
|
}
|
|
|
|
$line_items = [];
|
|
foreach ($cart_items as $item) {
|
|
$line_items[] = [
|
|
'price_data' => [
|
|
'currency' => 'usd',
|
|
'product_data' => [
|
|
'name' => $item['name'],
|
|
],
|
|
'unit_amount' => $item['price'] * 100, // Price in cents
|
|
],
|
|
'quantity' => $item['quantity'],
|
|
];
|
|
}
|
|
|
|
// Add delivery fee
|
|
$delivery_fee = 5.00;
|
|
$line_items[] = [
|
|
'price_data' => [
|
|
'currency' => 'usd',
|
|
'product_data' => [
|
|
'name' => 'Delivery Fee',
|
|
],
|
|
'unit_amount' => $delivery_fee * 100,
|
|
],
|
|
'quantity' => 1,
|
|
];
|
|
|
|
|
|
$checkout_session = \Stripe\Checkout\Session::create([
|
|
'payment_method_types' => ['card'],
|
|
'line_items' => $line_items,
|
|
'mode' => 'payment',
|
|
'success_url' => 'http://' . $_SERVER['HTTP_HOST'] . '/payment-success.php?session_id={CHECKOUT_SESSION_ID}',
|
|
'cancel_url' => 'http://' . $_SERVER['HTTP_HOST'] . '/payment-cancel.php',
|
|
]);
|
|
|
|
header("HTTP/1.1 303 See Other");
|
|
header("Location: " . $checkout_session->url);
|