"error", "message" => "No payload received"]); exit; } /** * ------------------------------------------------------------- * JUGAD: Phone Number Sanitizer (Extract Last 10 Digits) * ------------------------------------------------------------- */ function clean_phone_number($phone) { if (empty($phone)) return ''; // 1. Sirf numbers/digits ko rakho, bakki sab (spaces, +, -, (), etc.) hata do $clean = preg_replace('/[^0-9]/', '', $phone); // 2. Agar length 10 se badi hai (jaise 919876543210 ya 09876543210), toh last 10 digits lo if (strlen($clean) >= 10) { return substr($clean, -10); } return $clean; // Agar kam digit hai toh original cleaned return karega } // Data mapping (Shopify Draft Order -> NeoDove Lead Payload) $customer = isset($draft_order['customer']) ? $draft_order['customer'] : []; $shipping = isset($draft_order['shipping_address']) ? $draft_order['shipping_address'] : []; $billing = isset($draft_order['billing_address']) ? $draft_order['billing_address'] : []; // Raw Phone Number fetch karein (Priority: Shipping > Customer > Billing) $raw_phone = ''; if (!empty($shipping['phone'])) { $raw_phone = $shipping['phone']; } elseif (!empty($customer['phone'])) { $raw_phone = $customer['phone']; } elseif (!empty($billing['phone'])) { $raw_phone = $billing['phone']; } // Phone number ko clean karke EXACT 10-Digits nikalein $formatted_phone = clean_phone_number($raw_phone); // Line items extract karein $line_items = []; if (isset($draft_order['line_items']) && is_array($draft_order['line_items'])) { foreach ($draft_order['line_items'] as $item) { $line_items[] = [ 'title' => isset($item['title']) ? $item['title'] : '', 'quantity' => isset($item['quantity']) ? $item['quantity'] : 1, 'price' => isset($item['price']) ? $item['price'] : '0.00' ]; } } // NeoDove Final Payload $neodove_payload = [ 'order_id' => isset($draft_order['id']) ? $draft_order['id'] : '', 'name' => isset($draft_order['name']) ? $draft_order['name'] : '', 'customer_first_name' => !empty($customer['first_name']) ? $customer['first_name'] : (!empty($shipping['first_name']) ? $shipping['first_name'] : ''), 'customer_last_name' => !empty($customer['last_name']) ? $customer['last_name'] : (!empty($shipping['last_name']) ? $shipping['last_name'] : ''), 'email' => !empty($draft_order['email']) ? $draft_order['email'] : (!empty($customer['email']) ? $customer['email'] : ''), 'phone' => $formatted_phone, // 👈 Pure 10-Digit Mobile Number 'total_price' => isset($draft_order['total_price']) ? $draft_order['total_price'] : '0.00', 'currency' => isset($draft_order['currency']) ? $draft_order['currency'] : 'INR', 'status' => isset($draft_order['status']) ? $draft_order['status'] : 'open', 'line_items' => $line_items, 'shipping_address' => [ 'address1' => isset($shipping['address1']) ? $shipping['address1'] : '', 'city' => isset($shipping['city']) ? $shipping['city'] : '', 'province' => isset($shipping['province']) ? $shipping['province'] : '', 'zip' => isset($shipping['zip']) ? $shipping['zip'] : '', 'country' => isset($shipping['country']) ? $shipping['country'] : '' ], 'created_at' => isset($draft_order['created_at']) ? $draft_order['created_at'] : '' ]; // cURL se NeoDove API ko POST request bhejein $ch = curl_init($neodove_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($neodove_payload)); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json' ]); $response = curl_exec($ch); $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); // Shopify ko 200 OK return karein http_response_code(200); echo json_encode([ 'status' => 'success', 'cleaned_phone_sent' => $formatted_phone, 'neodove_response_code' => $http_code ]); ?>