99 lines
3.5 KiB
PHP
99 lines
3.5 KiB
PHP
<?php
|
|
require_once 'db/config.php';
|
|
|
|
if (!isset($_GET['product_id'])) {
|
|
header('Location: products.php');
|
|
exit();
|
|
}
|
|
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare("SELECT p.id, p.name, p.description, p.price, t.name as translated_name, t.description as translated_description FROM products p LEFT JOIN translations t ON p.id = t.product_id AND t.language_code = 'en' WHERE p.id = ?");
|
|
$stmt->execute([$_GET['product_id']]);
|
|
$product = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if (empty($product)) {
|
|
header('Location: products.php');
|
|
exit();
|
|
}
|
|
|
|
$stmt = $pdo->query("SELECT stripe_publishable_key FROM settings ORDER BY id DESC LIMIT 1");
|
|
$settings = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
$stripe_publishable_key = $settings['stripe_publishable_key'] ?? '';
|
|
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Checkout</title>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<script src="https://js.stripe.com/v3/"></script>
|
|
</head>
|
|
<body>
|
|
<div class="container mt-5">
|
|
<h1>Checkout</h1>
|
|
<div class="row">
|
|
<div class="col-md-6">
|
|
<div class="card">
|
|
<div class="card-body">
|
|
<h5 class="card-title"><?php echo htmlspecialchars($product['translated_name'] ?: $product['name']); ?></h5>
|
|
<p class="card-text"><?php echo htmlspecialchars($product['translated_description'] ?: $product['description']); ?></p>
|
|
<p class="card-text"><strong>Price: $<?php echo htmlspecialchars($product['price']); ?></strong></p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="col-md-6">
|
|
<form id="payment-form">
|
|
<div id="card-element">
|
|
<!-- A Stripe Element will be inserted here. -->
|
|
</div>
|
|
<button id="submit" class="btn btn-primary mt-3">Pay</button>
|
|
<div id="error-message" role="alert"></div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<script>
|
|
const stripe = Stripe('<?php echo $stripe_publishable_key; ?>');
|
|
const paymentForm = document.getElementById('payment-form');
|
|
const productId = <?php echo $product['id']; ?>;
|
|
|
|
let elements;
|
|
|
|
initialize();
|
|
|
|
async function initialize() {
|
|
const { clientSecret } = await fetch('api/payments/create-payment-intent.php', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ product_id: productId }),
|
|
}).then((r) => r.json());
|
|
|
|
elements = stripe.elements({ clientSecret });
|
|
|
|
const cardElement = elements.create('card');
|
|
cardElement.mount('#card-element');
|
|
}
|
|
|
|
paymentForm.addEventListener('submit', async (e) => {
|
|
e.preventDefault();
|
|
|
|
const { error } = await stripe.confirmCardPayment(elements.getClientSecret(), {
|
|
payment_method: {
|
|
card: elements.getElement('card'),
|
|
},
|
|
});
|
|
|
|
if (error) {
|
|
const messageContainer = document.getElementById('error-message');
|
|
messageContainer.textContent = error.message;
|
|
} else {
|
|
// Payment succeeded
|
|
window.location.href = 'payment-success.php';
|
|
}
|
|
});
|
|
</script>
|
|
</body>
|
|
</html>
|