mirror of
https://github.com/myronblair/parkerslingshotrentals
synced 2026-06-30 17:50:31 -05:00
140 lines
7.5 KiB
PHP
140 lines
7.5 KiB
PHP
<?php
|
|
/**
|
|
* Parker County Slingshot Rentals — Booking Request Handler
|
|
*/
|
|
|
|
header('Content-Type: application/json');
|
|
header('Access-Control-Allow-Origin: https://parkerslingshotrentals.com');
|
|
header('Access-Control-Allow-Methods: POST, OPTIONS');
|
|
header('Access-Control-Allow-Headers: Content-Type');
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') { http_response_code(204); exit; }
|
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
|
http_response_code(405);
|
|
echo json_encode(['success' => false, 'error' => 'Method not allowed']);
|
|
exit;
|
|
}
|
|
|
|
// ── CONFIG ────────────────────────────────────────────────────────────
|
|
define('SENDGRID_API_KEY', 'SG.YOUR_KEY_HERE'); // <-- replace with your SendGrid API key
|
|
define('MAIL_FROM', 'noreply@parkerslingshotrentals.com');
|
|
define('MAIL_FROM_NAME', 'Parker County Slingshot Rentals');
|
|
define('ADMIN_EMAIL', 'info@parkerslingshotrentals.com'); // where booking alerts go
|
|
// ─────────────────────────────────────────────────────────────────────
|
|
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
if (!$input) { $input = $_POST; }
|
|
|
|
$name = trim(strip_tags($input['name'] ?? ''));
|
|
$email = trim(strip_tags($input['email'] ?? ''));
|
|
$phone = trim(strip_tags($input['phone'] ?? ''));
|
|
$package = trim(strip_tags($input['package'] ?? ''));
|
|
$date = trim(strip_tags($input['date'] ?? ''));
|
|
$message = trim(strip_tags($input['message'] ?? ''));
|
|
|
|
// Basic validation
|
|
if (!$name || !$email || !$package || !$date) {
|
|
http_response_code(400);
|
|
echo json_encode(['success' => false, 'error' => 'Name, email, package, and date are required.']);
|
|
exit;
|
|
}
|
|
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
http_response_code(400);
|
|
echo json_encode(['success' => false, 'error' => 'Invalid email address.']);
|
|
exit;
|
|
}
|
|
|
|
$packages = [
|
|
'half-day' => 'Half Day (4 hrs) — $99',
|
|
'full-day' => 'Full Day (8 hrs) — $169',
|
|
'weekend' => 'Weekend (48 hrs) — $299',
|
|
];
|
|
$packageLabel = $packages[$package] ?? ucfirst($package);
|
|
$dateFormatted = date('F j, Y', strtotime($date));
|
|
|
|
// ── SEND ADMIN ALERT ──────────────────────────────────────────────────
|
|
$adminHtml = '
|
|
<div style="max-width:600px;margin:0 auto;font-family:Arial,sans-serif;">
|
|
<div style="background:#f97316;padding:24px;text-align:center;">
|
|
<h1 style="color:#fff;margin:0;font-size:22px;">New Booking Request!</h1>
|
|
<p style="color:rgba(255,255,255,.85);margin:4px 0 0;font-size:14px;">Parker County Slingshot Rentals</p>
|
|
</div>
|
|
<div style="padding:28px;background:#fff;border:1px solid #e5e7eb;">
|
|
<table style="width:100%;border-collapse:collapse;font-size:15px;">
|
|
<tr><td style="padding:10px 0;color:#6b7280;width:100px;">Name</td>
|
|
<td style="padding:10px 0;font-weight:600;">' . htmlspecialchars($name) . '</td></tr>
|
|
<tr><td style="padding:10px 0;color:#6b7280;">Email</td>
|
|
<td style="padding:10px 0;"><a href="mailto:' . htmlspecialchars($email) . '" style="color:#f97316;">' . htmlspecialchars($email) . '</a></td></tr>
|
|
<tr><td style="padding:10px 0;color:#6b7280;">Phone</td>
|
|
<td style="padding:10px 0;">' . (htmlspecialchars($phone) ?: '<em style="color:#9ca3af;">not provided</em>') . '</td></tr>
|
|
<tr><td style="padding:10px 0;color:#6b7280;">Package</td>
|
|
<td style="padding:10px 0;font-weight:700;color:#f97316;">' . htmlspecialchars($packageLabel) . '</td></tr>
|
|
<tr><td style="padding:10px 0;color:#6b7280;">Date</td>
|
|
<td style="padding:10px 0;font-weight:600;">' . htmlspecialchars($dateFormatted) . '</td></tr>
|
|
</table>
|
|
' . ($message ? '<div style="margin-top:16px;padding:16px;background:#fff7ed;border-radius:8px;border-left:4px solid #f97316;"><p style="margin:0;font-size:14px;color:#374151;line-height:1.6;">' . nl2br(htmlspecialchars($message)) . '</p></div>' : '') . '
|
|
<p style="margin-top:20px;font-size:13px;color:#9ca3af;">Submitted ' . date('F j, Y \a\t g:i A') . ' CT</p>
|
|
</div>
|
|
</div>';
|
|
|
|
// ── SEND CUSTOMER CONFIRMATION ────────────────────────────────────────
|
|
$confirmHtml = '
|
|
<div style="max-width:600px;margin:0 auto;font-family:Arial,sans-serif;">
|
|
<div style="background:#0d0d0d;padding:24px;text-align:center;">
|
|
<h1 style="color:#f97316;margin:0;font-size:22px;">Parker County Slingshot Rentals</h1>
|
|
</div>
|
|
<div style="padding:32px;background:#fff;">
|
|
<h2 style="color:#0d0d0d;margin-top:0;">Booking Request Received!</h2>
|
|
<p style="color:#374151;line-height:1.6;">Hey ' . htmlspecialchars($name) . ', we got your request and will confirm availability within a few hours.</p>
|
|
<div style="background:#fff7ed;border:1px solid #fed7aa;border-radius:10px;padding:20px;margin:24px 0;">
|
|
<h3 style="margin-top:0;color:#f97316;font-size:16px;">Your Request Summary</h3>
|
|
<p style="margin:4px 0;font-size:14px;color:#374151;"><strong>Package:</strong> ' . htmlspecialchars($packageLabel) . '</p>
|
|
<p style="margin:4px 0;font-size:14px;color:#374151;"><strong>Requested Date:</strong> ' . htmlspecialchars($dateFormatted) . '</p>
|
|
</div>
|
|
<p style="color:#374151;line-height:1.6;">We\'ll reach out to you at <strong>' . htmlspecialchars($email) . '</strong>' . ($phone ? ' or <strong>' . htmlspecialchars($phone) . '</strong>' : '') . ' to confirm your ride.</p>
|
|
<p style="color:#374151;line-height:1.6;">Questions? Call or text us at <strong>(817) 555-0199</strong>.</p>
|
|
<p style="color:#374151;line-height:1.6;">Ride on,<br><strong>The Parker County Slingshot Team</strong></p>
|
|
</div>
|
|
<div style="background:#f3f4f6;padding:16px;text-align:center;">
|
|
<p style="margin:0;font-size:12px;color:#9ca3af;">© ' . date('Y') . ' Parker County Slingshot Rentals — Weatherford, TX</p>
|
|
</div>
|
|
</div>';
|
|
|
|
function sendgridSend(string $toEmail, string $toName, string $subject, string $html): bool {
|
|
$payload = json_encode([
|
|
'personalizations' => [['to' => [['email' => $toEmail, 'name' => $toName]]]],
|
|
'from' => ['email' => MAIL_FROM, 'name' => MAIL_FROM_NAME],
|
|
'subject' => $subject,
|
|
'content' => [['type' => 'text/html', 'value' => $html]],
|
|
]);
|
|
|
|
$ch = curl_init('https://api.sendgrid.com/v3/mail/send');
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_POST => true,
|
|
CURLOPT_POSTFIELDS => $payload,
|
|
CURLOPT_HTTPHEADER => [
|
|
'Authorization: Bearer ' . SENDGRID_API_KEY,
|
|
'Content-Type: application/json',
|
|
],
|
|
CURLOPT_TIMEOUT => 20,
|
|
CURLOPT_SSL_VERIFYPEER => false,
|
|
]);
|
|
$response = curl_exec($ch);
|
|
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
curl_close($ch);
|
|
return $code === 202;
|
|
}
|
|
|
|
$apiKey = SENDGRID_API_KEY;
|
|
if ($apiKey && strpos($apiKey, 'YOUR_KEY') === false) {
|
|
sendgridSend(ADMIN_EMAIL, 'Parker Slingshot Admin',
|
|
"New Booking Request: {$name} — {$packageLabel} on {$dateFormatted}", $adminHtml);
|
|
sendgridSend($email, $name,
|
|
"Booking Request Confirmed — Parker County Slingshot", $confirmHtml);
|
|
} else {
|
|
error_log('[Parker Slingshot] SENDGRID_API_KEY not configured');
|
|
}
|
|
|
|
echo json_encode(['success' => true, 'message' => 'Booking request received! We\'ll be in touch shortly.']);
|