// Enable error reporting for debugging
// ini_set('display_errors', 1);
// ini_set('display_startup_errors', 1);
// error_reporting(E_ALL);if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Retrieve the form page URL from the submitted form data
$form_page_url = isset($_POST['form_page_url']) ? sanitize_text_field($_POST['form_page_url']) : 'URL not provided';
// Collect and sanitize form data
$first_name = isset($_POST['first_name']) ? htmlspecialchars(trim($_POST['first_name'])) : '';
$last_name = isset($_POST['last_name']) ? htmlspecialchars(trim($_POST['last_name'])) : '';
$email = isset($_POST['email']) ? filter_var(trim($_POST['email']), FILTER_SANITIZE_EMAIL) : '';
$phone = isset($_POST['phone']) ? htmlspecialchars(trim($_POST['phone'])) : '';
// Sanitize and convert the ship date
$ship_date_raw = isset($_POST['ship_date']) ? htmlspecialchars(trim($_POST['ship_date'])) : '';
$ship_date = DateTime::createFromFormat('Y-m-d', $ship_date_raw) ? DateTime::createFromFormat('Y-m-d', $ship_date_raw)->format('m/d/Y') : '';$transport_type = isset($_POST['transport_type']) ? (int)$_POST['transport_type'] : 0;
$origin_city = isset($_POST['origin_city']) ? htmlspecialchars(trim($_POST['origin_city'])) : '';
$origin_country = isset($_POST['origin_country']) ? htmlspecialchars(trim($_POST['origin_country'])) : '';
$origin_postal_code = isset($_POST['origin_postal_code']) ? htmlspecialchars(trim($_POST['origin_postal_code'])) : '';
$origin_state = isset($_POST['origin_state']) ? htmlspecialchars(trim($_POST['origin_state'])) : '';
$destination_city = isset($_POST['destination_city']) ? htmlspecialchars(trim($_POST['destination_city'])) : '';
$destination_country = isset($_POST['destination_country']) ? htmlspecialchars(trim($_POST['destination_country'])) : '';
$destination_postal_code = isset($_POST['destination_postal_code']) ? htmlspecialchars(trim($_POST['destination_postal_code'])) : '';
$destination_state = isset($_POST['destination_state']) ? htmlspecialchars(trim($_POST['destination_state'])) : '';// Vehicle data (assuming these are arrays for multiple vehicles)
$vehicle_inop = $_POST['vehicle_inop']; // 0 or 1 for each vehicle
$vehicle_make = $_POST['vehicle_make'];
$vehicle_model = $_POST['vehicle_model'];
$vehicle_model_year = $_POST['vehicle_model_year'];
$vehicle_type = $_POST['vehicle_type'];
// $vehicle_width = $_POST['vehicle_width'];
// $vehicle_height = $_POST['vehicle_height'];
// $vehicle_length = $_POST['vehicle_length'];
// $vehicle_weight = $_POST['vehicle_weight'];
// $vehicle_weight_measure = $_POST['vehicle_weight_measure'];// Construct the vehicles array properly
$vehicles = [];
for ($i = 0; $i < count($vehicle_make); $i++) {
$vehicles[] = [
'vehicle_inop' => (int) $vehicle_inop[$i], // Ensure it's 0 or 1 //Lead Vehicle inop
'vehicles_inop' => $vehicle_inop[0], // Ensure it's 0 or 1 //Pricing Vehicle inop
'vehicle_make' => $vehicle_make[$i],
'vehicle_model' => $vehicle_model[$i],
'vehicle_model_year' => (int) $vehicle_model_year[$i],
'vehicle_type' => $vehicle_type[$i], //Lead Vehicle type
'vehicle_types' => $vehicle_type[$i], //Pricing Vehicle type
// 'vehicle_width' => $vehicle_width[$i],
// 'vehicle_height' => $vehicle_height[$i],
// 'vehicle_length' => $vehicle_length[$i],
// 'vehicle_weight' => (int) $vehicle_weight[$i],
// 'vehicle_weight_measure' => $vehicle_weight_measure[$i],
];
}// Validation (ensure all required fields are filled out)
if (empty($first_name) || empty($email) || empty($phone) || empty($ship_date) || empty($origin_postal_code) || empty($origin_state) || empty($transport_type) || empty($destination_postal_code) || empty($destination_state) || empty($vehicle_make) || empty($vehicle_model) || empty($vehicle_model_year) || empty($vehicle_inop) || empty($vehicle_type)) {
echo 'Please fill in all required fields.';
exit;
}
// Prepare email content
$to = 'leads@bestamericanautotransport.com';
$subject = 'New Lead Submission';
$message = "A new lead has been submitted with the following details:\n\n";
$message .= "First Name: $first_name\n";
$message .= "Last Name: $last_name\n";
$message .= "Email: $email\n";
$message .= "Phone: $phone\n";
$message .= "Ship Date: $ship_date\n";
$message .= "Transport Type: $transport_type\n";
$message .= "Origin: $origin_city, $origin_state, $origin_postal_code\n";
$message .= "Destination: $destination_city, $destination_state, $destination_postal_code\n";
$message .= "Vehicle(s) Information:\n";
foreach ($vehicles as $vehicle) {
$message .= "- {$vehicle['vehicle_make']} {$vehicle['vehicle_model']} ({$vehicle['vehicle_model_year']} ) {$vehicle['vehicles_inop']}\n";
}
// Get current page URL
$message .= "\nForm Page URL: $form_page_url\n";$headers = [
'Content-Type: text/plain; charset=UTF-8',
'From: Best American Auto Transport ',
];// Send email
if (wp_mail($to, $subject, $message, $headers)) {
// echo 'Email sent successfully.';
} else {
echo 'Failed to send email.';
}
// API endpoint and AuthKey
$Lead_api_url = 'https://api.batscrm.com/leads';
$pricing_api_url = 'https://api.batscrm.com/ss/production/price';
$auth_key ='acb28345b20145049f38924f78e35ed2';// Prepare data to be sent to the API
$Lead_data = [
'AuthKey' => '2BB9D357-C32F-475F-92E6-B13E0FD81EBC',
'first_name' => $first_name,
'last_name' => $last_name,
'email' => $email,
'phone' => $phone,
'ship_date' => $ship_date,
'transport_type' => $transport_type,
'origin_city' => $origin_city,
'origin_country' => $origin_country,
'origin_postal_code' => $origin_postal_code,
'origin_state' => $origin_state,
'destination_city' => $destination_city,
'destination_country' => $destination_country,
'destination_postal_code' => $destination_postal_code,
'destination_state' => $destination_state,
'Vehicles' => $vehicles // Add vehicles array here
];
$api1_response = callApi($Lead_api_url, $Lead_data);
// echo $api1_response;
// Loop through vehicles and get pricing for each
// Loop through vehicles and prepare the vehicle types
$vehicle_types = []; // Collect vehicle types in an arrayfor ($i = 0; $i < count($vehicles); $i++) {
$vehicle_types[] = $vehicles[$i]['vehicle_types']; // Add vehicle make as vehicle type
$vehicles_inop[] = $vehicles[$i]['vehicles_inop']; // Add vehicle make as vehicle type
}// Convert the array of vehicle types to a comma-separated string
$vehicle_types_string = implode(',', $vehicle_types);// Prepare the pricing data for the API
$pricing_data = [
'partner_id' => 'e3db8ed5-1746-4e74-806c-6ecc56bed9e9',
'origin_state' => $origin_state, // Example valid state code
'origin_postal_code' => $origin_postal_code, // Valid postal code
'destination_state' => $destination_state, // Example valid state code
'destination_postal_code' => $destination_postal_code, // Valid postal code
'vehicle_types' => $vehicle_types_string, // Check if this is a valid format
'transport_type' => $transport_type, // Ensure this is a valid transport type
'vehicles_inop' => (int)$vehicles_inop[0] // Assuming you want to send the highest inop status
];// Print the constructed pricing data (for debugging)
// echo '
' . print_r(json_encode($pricing_data), true) . '';// Send the data to the pricing API$pricing_response = callApinew($pricing_api_url, json_encode($pricing_data));$pricing_result = json_decode($pricing_response, true);}function callApi($url, $data) {// print_r($data);// Initialize cURL $ch = curl_init($url);// Set cURL options curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', 'AuthKey: acb28345b20145049f38924f78e35ed2', // API authentication ]); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); // Convert the data array to JSON// Execute cURL request $response = curl_exec($ch); $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); // Get the HTTP status code// Check for cURL errors if (curl_errno($ch)) { echo 'cURL Error: ' . curl_error($ch); } else { // Decode JSON response $response_data = json_decode($response, true); // Check if the response is valid if ($http_code === 200) { // echo 'Form submitted successfully!'; } elseif ($http_code === 422) { echo 'Validation Error: '; } else { echo 'Unknown error. HTTP Status Code: ' . $http_code;// echo '
' . print_r($pricing_data, true) . ''; } } // Close cURL curl_close($ch);}function callApinew($url, $data) { // Initialize cURL $ch = curl_init($url); // Set cURL options curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', 'AuthKey: acb28345b20145049f38924f78e35ed2', // API authentication ]); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); // Convert the data array to JSON// Execute cURL request $response = curl_exec($ch); $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); // Get the HTTP status code// Check for cURL errors if (curl_errno($ch)) { echo 'cURL Error: ' . curl_error($ch); } else { // Decode JSON response $response_data = json_decode($response, true); $form_data = json_decode($data, true); // $form_value = json_decode($value, true); // Check if the response is valid if ($http_code === 200) { echo '
';
echo '
';} elseif ($http_code === 422) {
echo 'Validation Error: ';
} else {
echo 'Unknown error. HTTP Status Code: ' . $http_code;
}
}
// Close cURL
curl_close($ch);
}?>'; // Start flexbox for layout// Form Data Table
echo '
'; // Close flexbox
echo '';
echo '';
// foreach ($form_data as $key => $value) {
echo '
';
echo '
'; // Close form data section// Response Data Table
echo 'Shipping Details
'; echo '| Price Ref ID | '; // echo '' . $response_data['price_ref_id'] . ' | '; echo '||||
|---|---|---|---|---|---|
| ' . htmlspecialchars($key) . ' | '; // echo '' . htmlspecialchars($value) . ' | '; echo 'Origin State | '; echo '' . $form_data['origin_state'] . ' | '; echo '||
| Origin Zip Code | '; echo '' . $form_data['origin_postal_code'] . ' | '; echo '||||
| Destination State | '; echo '' . $form_data['destination_state'] . ' | '; echo '||||
| Destination Zip Code | '; echo '' . $form_data['destination_postal_code'] . ' | '; echo '||||
| Vehicle Types | '; echo '' . $form_data['vehicle_types'] . ' | '; echo '||||
| Transport Type | '; if ($form_data['transport_type'] == 1) { echo 'Open | '; } elseif ($form_data['transport_type'] == 2) { echo 'Enclosed | '; } elseif ($form_data['transport_type'] == 3) { echo 'Driveaway | '; } else { echo 'Unknown | '; // fallback in case the value doesn't match } echo '|
| Vehicles INOP | '; // if ($form_data['vehicles_inop'] == 1) { // echo 'Yes | '; // } elseif ($form_data['vehicles_inop'] == 0) { // echo 'No | '; // } else { // echo 'Unknown | '; // in case the value is not 1 or 0 // } echo 'Vehicle Condition | ' . ($form_data['vehicles_inop'] == 1 ? 'Inoperable' : ($form_data['vehicles_inop'] == 0 ? 'Operable' : 'Unknown')) . ' | ';echo '
';
// echo '';
// // foreach ($response_data as $key => $value) {
// echo '
';
echo '
'; // Close response data section
echo 'Total Shipping Cost
'; // echo '';
// echo '
';
echo 'Your quote has been e-mailed to you. // Ready to book? Hooray! NO PAYMENT REQUIRED to book your shipment.
'; // echo '';
// echo '
';echo 'Total Price
'; // echo ' // $' . $response_data['price'] . // ''; echo 'Thank You for Requesting a Quote! One of Our Agents Will Call You Shortly with the BEST Market Rates and EXCLUSIVE Offers to Get Your Vehicle Shipped HASSLE-FREE!
'; // echo 'Once the order is assigned to a carrier, a partial payment will be charged, the balance will be due in cash on delivery.
'; echo '
BOOK with One of Our Friendly CUSTOMER SERVICE Agents Today!
CALL for UNBEATABLE Market Rates TODAY!
| ' . htmlspecialchars($key) . ' | '; // // echo '' . htmlspecialchars($value) . ' | '; // echo 'Price | '; // echo ''. $response_data['price'] . ' | '; // echo '
|---|---|---|---|
| Price Ref ID | '; // echo ''. $response_data['price_ref_id'] . ' | '; // echo '