34853-vm/order_confirmation.php
Flatlogic Bot 9660f06ca5 full
2025-10-12 11:03:22 +00:00

116 lines
5.2 KiB
PHP

<?php
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/db/config.php';
\Stripe\Stripe::setApiKey('sk_test_51Hh9Y2L9s5P2Q8Z4sYgY8Z4sYgY8Z4sYgY8Z4sYgY8Z4sYgY8Z4sYgY8Z4sYgY8Z4sYgY8Z4sYgY8Z4');
include 'header.php';
$message = '';
$order_details = null;
$error = false;
if (empty($_GET['payment_intent'])) {
$error = true;
$message = "No payment intent provided.";
} else {
$payment_intent_id = $_GET['payment_intent'];
$pdo = db();
try {
// 1. Verify Payment with Stripe
$paymentIntent = \Stripe\PaymentIntent::retrieve($payment_intent_id);
if ($paymentIntent->status == 'succeeded') {
// 2. Find Local Order
$stmt = $pdo->prepare("SELECT * FROM orders WHERE stripe_payment_intent_id = ?");
$stmt->execute([$payment_intent_id]);
$order = $stmt->fetch(PDO::FETCH_OBJ);
if ($order) {
// 3. Update Order Status (if it's still pending)
if ($order->order_status == 'pending') {
$update_stmt = $pdo->prepare("UPDATE orders SET order_status = 'completed' WHERE id = ?");
$update_stmt->execute([$order->id]);
}
// 4. Fetch Order Details for Display
$details_stmt = $pdo->prepare(
"SELECT o.id as order_id, o.amount, c.name as customer_name, c.email, p.name as plan_name
FROM orders o
JOIN customers c ON o.customer_id = c.id
JOIN plans p ON o.plan_id = p.id
WHERE o.id = ?"
);
$details_stmt->execute([$order->id]);
$order_details = $details_stmt->fetch(PDO::FETCH_OBJ);
$message = "Thank you for your order! Your payment was successful.";
// 5. Send Confirmation Email (optional, but good practice)
require_once __DIR__ . '/mail/MailService.php';
$subject = 'Your Australia Broadband Internet Order Confirmation';
$html_body = "<h1>Welcome, " . htmlspecialchars($order_details->customer_name) . "!</h1>"
. "<p>Thank you for your order. Your new internet service is being processed.</p>"
. "<p><strong>Order Details:</strong></p>"
. "<ul>"
. "<li><strong>Order ID:</strong> " . htmlspecialchars($order_details->order_id) . "</li>"
. "<li><strong>Plan:</strong> " . htmlspecialchars($order_details->plan_name) . "</li>"
. "<li><strong>Amount Paid:</strong> $" . htmlspecialchars(number_format($order_details->amount, 2)) . "</li>"
. "</ul>"
. "<p>You will receive further updates from us shortly.</p>";
MailService::sendMail($order_details->email, $subject, $html_body);
} else {
$error = true;
// This is a critical error. Payment succeeded but we can't find the order.
error_log("CRITICAL: Payment succeeded for PI {$payment_intent_id} but no matching order found in DB.");
$message = "Your payment was successful, but we could not find your order. Please contact support immediately.";
}
} else {
$error = true;
$message = "Your payment was not successful. Please try again or contact support.";
}
} catch (Exception $e) {
$error = true;
error_log("Order confirmation error: " . $e->getMessage());
$message = "An error occurred while processing your order. Please contact support.";
}
}
?>
<div class="container mt-5">
<div class="row">
<div class="col-md-8 offset-md-2 text-center">
<?php if ($error): ?>
<div class="alert alert-danger">
<h1 class="alert-heading">Order Error</h1>
<p><?php echo htmlspecialchars($message); ?></p>
</div>
<?php else: ?>
<div class="alert alert-success">
<h1 class="alert-heading">Thank You!</h1>
<p><?php echo htmlspecialchars($message); ?></p>
</div>
<?php if ($order_details): ?>
<div class="card mt-4">
<div class="card-header">
Order Summary
</div>
<div class="card-body">
<p><strong>Order ID:</strong> <?php echo htmlspecialchars($order_details->order_id); ?></p>
<p><strong>Customer:</strong> <?php echo htmlspecialchars($order_details->customer_name); ?></p>
<p><strong>Plan:</strong> <?php echo htmlspecialchars($order_details->plan_name); ?></p>
<p><strong>Amount Paid:</strong> $<?php echo htmlspecialchars(number_format($order_details->amount, 2)); ?></p>
</div>
</div>
<?php endif; ?>
<?php endif; ?>
<a href="/" class="btn btn-primary mt-4">Back to Home</a>
</div>
</div>
</div>
<?php
include 'footer.php';
?>