���� ������������������������������������ setSearchPreferences(false, 20); $code=$_GET['code']; if (strpos($code, '^') !== false) { $code = str_replace('^', ' ', $code); } $emailSearchField = new SearchStringField(); $emailSearchField->operator = "contains"; $emailSearchField->searchValue = $code; $search = new ItemSearchBasic(); $search->itemId = $emailSearchField; $request = new SearchRequest(); $request->searchRecord = $search; // Add retry logic for concurrent request limit errors $maxRetries = 3; $retryDelay = 1; // Start with 1 second delay $searchResponse = null; for ($attempt = 1; $attempt <= $maxRetries; $attempt++) { try { $searchResponse = $service->search($request); break; // Success, exit retry loop } catch (SoapFault $e) { // Log the error if ($file = fopen($logFile, 'a')) { fwrite($file, date('Y-m-d H:i:s') . ' - SoapFault on attempt ' . $attempt . ': ' . $e->getMessage() . PHP_EOL); fclose($file); } // Check if it's a retryable error (concurrent limit or connection issues) $isRetryableError = ( strpos($e->getMessage(), 'concurrent request limit exceeded') !== false || strpos($e->getMessage(), 'Request blocked') !== false || strpos($e->getMessage(), 'Could not connect to host') !== false || strpos($e->getMessage(), 'Connection timed out') !== false || strpos($e->getMessage(), 'HTTP request failed') !== false || strpos($e->getMessage(), 'SSL connection timeout') !== false || strpos($e->getMessage(), 'Connection refused') !== false ); if ($isRetryableError) { if ($attempt < $maxRetries) { // Wait before retrying with exponential backoff sleep($retryDelay); $retryDelay *= 2; // Double the delay for next attempt continue; } else { // Max retries reached, determine specific error message if (strpos($e->getMessage(), 'concurrent request limit exceeded') !== false || strpos($e->getMessage(), 'Request blocked') !== false) { $response = [ 'status' => 'error', 'message' => 'Service temporarily unavailable due to high load. Please try again later.', 'error_code' => 'CONCURRENT_LIMIT_EXCEEDED', 'details' => $e->getMessage(), 'attempts' => $maxRetries ]; } else { $response = [ 'status' => 'error', 'message' => 'Connection to service failed after multiple attempts. Please try again later.', 'error_code' => 'CONNECTION_FAILED', 'details' => $e->getMessage(), 'attempts' => $maxRetries ]; } header('Content-Type: application/json'); echo json_encode($response); exit; } } else { // Non-retryable SoapFault, return error immediately $response = [ 'status' => 'error', 'message' => 'An error occurred while processing your request.', 'error_code' => 'SOAP_FAULT', 'details' => $e->getMessage(), 'attempts' => $attempt ]; header('Content-Type: application/json'); echo json_encode($response); exit; } } catch (Exception $e) { // Handle other types of exceptions if ($file = fopen($logFile, 'a')) { fwrite($file, date('Y-m-d H:i:s') . ' - General Exception: ' . $e->getMessage() . PHP_EOL); fclose($file); } $response = [ 'status' => 'error', 'message' => 'An unexpected error occurred.', 'error_code' => 'GENERAL_ERROR' ]; header('Content-Type: application/json'); echo json_encode($response); exit; } } // Check if we have a valid search response if ($searchResponse === null) { $response = [ 'status' => 'error', 'message' => 'Failed to get response from service after multiple attempts.', 'error_code' => 'NO_RESPONSE' ]; header('Content-Type: application/json'); echo json_encode($response); exit; } if ($searchResponse->searchResult->totalRecords == 0) { $response = [ 'status' => 'error', 'message' => 'Sales order item not found!', 'data'=>[] ]; } else { // $internalId=$searchResponse->searchResult->recordList->record[0]->internalId; $internalId=''; foreach($searchResponse->searchResult->recordList->record as $record){ if($record->itemId==$code){ $internalId=$record->internalId; } } $response = [ 'status' => 'success', 'message' => 'Sales order item found!', 'data'=>[ 'id'=>$internalId ] ]; } } else { $response = [ 'status' => 'error', 'message' => 'Please provide item code!' ]; } header('Content-Type: application/json'); echo json_encode($response); exit; } if ($_SERVER['REQUEST_METHOD'] === 'GET' && $_GET['action'] === 'getSalesOrder') { if ($_GET['id']) { $service->setSearchPreferences(false, 20); $request = new GetRequest(); $request->baseRef = new RecordRef(); $request->baseRef->internalId = $_GET['id']; $request->baseRef->type = "salesOrder"; // Add retry logic for concurrent request limit errors $maxRetries = 3; $retryDelay = 1; // Start with 1 second delay $getResponse = null; for ($attempt = 1; $attempt <= $maxRetries; $attempt++) { try { $getResponse = $service->get($request); break; // Success, exit retry loop } catch (SoapFault $e) { // Log the error if ($file = fopen($logFile, 'a')) { fwrite($file, date('Y-m-d H:i:s') . ' - SoapFault on getSalesOrder attempt ' . $attempt . ': ' . $e->getMessage() . PHP_EOL); fclose($file); } // Check if it's a retryable error (concurrent limit or connection issues) $isRetryableError = ( strpos($e->getMessage(), 'concurrent request limit exceeded') !== false || strpos($e->getMessage(), 'Request blocked') !== false || strpos($e->getMessage(), 'Could not connect to host') !== false || strpos($e->getMessage(), 'Connection timed out') !== false || strpos($e->getMessage(), 'HTTP request failed') !== false || strpos($e->getMessage(), 'SSL connection timeout') !== false || strpos($e->getMessage(), 'Connection refused') !== false ); if ($isRetryableError) { if ($attempt < $maxRetries) { // Wait before retrying with exponential backoff sleep($retryDelay); $retryDelay *= 2; // Double the delay for next attempt continue; } else { // Max retries reached, determine specific error message if (strpos($e->getMessage(), 'concurrent request limit exceeded') !== false || strpos($e->getMessage(), 'Request blocked') !== false) { $response = [ 'status' => 'error', 'message' => 'Service temporarily unavailable due to high load. Please try again later.', 'error_code' => 'CONCURRENT_LIMIT_EXCEEDED', 'details' => $e->getMessage(), 'attempts' => $maxRetries ]; } else { $response = [ 'status' => 'error', 'message' => 'Connection to service failed after multiple attempts. Please try again later.', 'error_code' => 'CONNECTION_FAILED', 'details' => $e->getMessage(), 'attempts' => $maxRetries ]; } header('Content-Type: application/json'); echo json_encode($response); exit; } } else { // Non-retryable SoapFault, return error immediately $response = [ 'status' => 'error', 'message' => 'An error occurred while processing your request.', 'error_code' => 'SOAP_FAULT', 'details' => $e->getMessage(), 'attempts' => $attempt ]; header('Content-Type: application/json'); echo json_encode($response); exit; } } catch (Exception $e) { // Handle other types of exceptions if ($file = fopen($logFile, 'a')) { fwrite($file, date('Y-m-d H:i:s') . ' - General Exception on getSalesOrder: ' . $e->getMessage() . PHP_EOL); fclose($file); } $response = [ 'status' => 'error', 'message' => 'An unexpected error occurred.', 'error_code' => 'GENERAL_ERROR' ]; header('Content-Type: application/json'); echo json_encode($response); exit; } } // Check if we have a valid response if ($getResponse === null) { $response = [ 'status' => 'error', 'message' => 'Failed to get response from service after multiple attempts.', 'error_code' => 'NO_RESPONSE' ]; header('Content-Type: application/json'); echo json_encode($response); exit; } if (!$getResponse->readResponse->status->isSuccess) { $response = [ 'status' => 'error', 'message' => 'Sales order not found!' ]; } else { $response = [ 'status' => 'success', 'message' => 'Sales order found!', 'data'=>array( 'OrderNo'=>$getResponse->readResponse->record->tranId ) ]; } } else { $response = [ 'status' => 'error', 'message' => 'Please provide sales order Id!' ]; } header('Content-Type: application/json'); echo json_encode($response); exit; } // Endpoint to update a sales order item if ($_SERVER['REQUEST_METHOD'] === 'POST' && $_GET['action'] === 'updateSalesOrderItem') { $exchangeRate=$_GET['exchangeRate']? $_GET['exchangeRate'] :0.00; $documentLink=$_GET['documentLink']? $_GET['documentLink'] :"https://lifefitness.clientpoint.net/"; $requestData = json_decode(file_get_contents('php://input'), true); $postField = " ".$account." ".$consumerKey." ".$token." ".$nonce." ".$timestamp." ".$signature." true "; foreach($requestData as $data) { // fwrite($file, $data['trade_in'] . PHP_EOL); $price = str_replace(',', '', $data['price']); $price = (float)$price; $price1 = number_format($price, 2); // Convert to two decimal places $rate=$price / $data['quantity']; $rate = number_format($rate, 2); // Convert to two decimal places $price1 = (float)str_replace(',', '', $price1); $rate = (float)str_replace(',', '', $rate); if (!empty($data['trade_in'])) { $trade_in = (int) $data['trade_in']; } else { $trade_in = 0; } $postField .= " ".$data['quantity']." ".htmlspecialchars($data['description'])." ".$rate." ".$price1." 00.Vic-Product ".$trade_in." "; } $postField .= " ".$exchangeRate." ".$documentLink." "; $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => 'https://4661993.suitetalk.api.netsuite.com/services/NetSuitePort_2023_1', CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS =>$postField, CURLOPT_HTTPHEADER => array( 'Content-Type: text/xml', 'SOAPAction: update' ), )); $response = curl_exec($curl); curl_close($curl); if ($file = fopen($logFile, 'a')) { // Optionally, you can add a timestamp to each log entry fwrite($file, $postField); } else { // Handle file open error } if ($file = fopen($logFile, 'a')) { // Optionally, you can add a timestamp to each log entry fwrite($file, $response); } else { // Handle file open error } echo $response; exit; $response = [ 'status' => 'success', 'response' => $error, ]; // header('Content-Type: application/json'); echo json_encode($response); exit; } // If no matching endpoint is found, return a 404 response http_response_code(404); header('Content-Type: application/json'); echo json_encode([ 'status' => 'error', 'message' => 'API not found!', ]);