Search code examples
phpmagentomagento-soap-api

Magento export orders to 3PL soap api issue


I have been reimplementing this script for a client after upgrade and I'm stuck (perhaps the brain is fried). Most of it is none of your concern. The part I'm having an issue with is here:

try

         {

            $customer_exists = $client->call($sess_id, 'sales_order.info', $ShippedOrderId);
         }

         catch (exception $e)

         {

            echo 'Exception: ' . $e . "<br/>";

         }

It's throwing the following exception:

"Exception: SoapFault exception: [100] Requested order not exists. in /home/ab71714/public_html/exporter/importer-sftp.php:140 Stack trace: #0 /home/ab71714/public_html/exporter/importer-sftp.php(140): SoapClient->__call('call', Array) #1 /home/ab71714/public_html/exporter/importer-sftp.php(140): SoapClient->call('9cafc83722b06f2...', 'sales_order.inf...', '100015822') #2 {main}"

The whole thing is following. Any help with be enormously appreciated!

     <?php 

   ini_set('error_reporting', E_ALL); 

   ini_set('display_errors', 1); 


var_dump(extension_loaded('ssh2'));
   // URL to Magento SOAP API

   $client = new SoapClient('http://animalnecessity.com/api/soap?wsdl'); 

   $apiuser = ''; 

   $apikey = ''; 



   // FTP info

   $ftp_server = 'ftp.3plworldwide.com';

   $ftp_user_name = '';

   $ftp_user_pass = '';



   // Address to mail report to

   $mail_to = '';



   $mail_content = "Checking for 3PL shipping import files on " . date('m/d/Y') . "\n\n";



   /*** Begin FTP ***/



   // Set up SFTP connection

   $conn_id = ssh2_connect($ftp_server, 22);

   $login_result = ssh2_auth_password($conn_id, $ftp_user_name, $ftp_user_pass);

   $sftp = ssh2_sftp($conn_id);



   // Check connection results

   if ((!$conn_id) || (!$login_result)) { 

       $mail_content .= "SFTP connection has failed.\n";

       $mail_content .= "Attempted to connect to $ftp_server under user '$ftp_user_name', password '$ftp_user_pass'.\n\n"; 

   } else {

       $mail_content .= "Successfully connected to $ftp_server with an SFTP secure connection, under user '$ftp_user_name', password '$ftp_user_pass'.\n\n"; 

   }



   // Download the files in the SHIPMENTS folder

   $download = true;

   $dirHandle = opendir("ssh2.sftp://$sftp/SHIPMENTS/");

   if ($dirHandle)

   {

      $filelist = '';

      $downarray = array();

      while (false !== ($file = readdir($dirHandle)))

      {

         if (strpos($file, 'OCU_SHP_') !== false)

         {

            // Choose only shipment files up to 7 days back

            $datetag = substr($file, 8, 6);

            $year = substr($datetag, 0, 2);

            $month = substr($datetag, 2, 2);

            $day = substr($datetag, 4, 2);

            if (!is_numeric($year)) { continue; }

            $datestamp = mktime(0, 0, 0, $month, $day, $year);

            if ($datestamp > strtotime('-7 days'))

            {

               $fileName = 'SHIPMENTS/' . $file;

               // Remote stream

               if (!$remoteStream = @fopen("ssh2.sftp://$sftp/$fileName", 'r'))

               {

                  $mail_content .= "Unable to open remote file: $fileName\n";

                  $download = false;

               } 

               // Local stream

               if (!$localStream = @fopen("./$fileName", 'w'))

               {

                  $mail_content .= "Unable to open local file for writing: ./$fileName\n";

                  $download = false;

               }

               // Write from our remote stream to our local stream

               $read = 0;

               $fileSize = filesize("ssh2.sftp://$sftp/$fileName");

               while ($read < $fileSize && ($buffer = fread($remoteStream, $fileSize - $read)))

               {

                  // Increase our bytes read

                  $read += strlen($buffer);

                  // Write to our local file

                  if (fwrite($localStream, $buffer) === FALSE)

                  {

                      $mail_content .= "Unable to write to local file: /localdir/$fileName\n";

                      $download = false;

                  }

               }

               // Close our streams

               fclose($localStream);

               fclose($remoteStream);

               // Update file lists for mail content and imports

               $filelist .= $file . "\n";

               array_push($downarray, './SHIPMENTS/' . $file);

            }

         } 

      }

   }



   // check download status

   if (!$download) { 

       $mail_content .= "Some file download(s) failed.\n\n";

   }

   else

   {

      $mail_content .= "Successfully downloaded all of the following files from $ftp_server:\n\n";

      $mail_content .= $filelist . "\n";

   }



   /*** End FTP ***/



   /*** Begin Import ***/



   // Read data from downloaded files into array

   $lines = array();

   foreach ($downarray as $file)

   {

      $linesnew = file($file);

      $lines = array_merge($lines, $linesnew);

   }

   var_dump($lines);

   // Connect to Magento Web Services API

   $sess_id = $client->login($apiuser, $apikey); 


   $mail_content .= "Importing the following shipments:\n\n===\n\n";

   $dupecount = 0;

   $importcount = 0;

   $result = $client->call($sess_id, 'sales_order.list');
      echo ($result[1]["order_id"]);

      //var_dump($result);


   foreach ($lines as $line)

   {

      set_time_limit(60);



      $fields = explode("\t", $line);

      $ShippedOrderId = $fields[0];
        echo $ShippedOrderId . "<br/>";
      if (strlen($ShippedOrderId) == 4) { $ShippedOrderId = '10000' . $ShippedOrderId; }



      if (is_numeric($ShippedOrderId))

      {

         // Check if customer exists

         $customer_exists = null;

         try

         {

            $customer_exists = $client->call($sess_id, 'sales_order.info', $ShippedOrderId);
         }

         catch (exception $e)

         {

            echo 'Exception: ' . $e . "<br/>";

         }

         if (($customer_exists != null) && ($customer_exists != ''))

         { 

            echo 'In with ' . $ShippedOrderId . "<br/>";

            $order_info = $client->call($sess_id, 'sales_order.info', $ShippedOrderId);

             ?><pre><?php print_r($order_info); ?></pre><?php 



            // Skip order if already completed or shipped

            if (($order_info['status'] != 'processing') && ($order_info['status'] != 'pending'))

            {

               $dupecount++;

               continue;

            }



            $comment = '<b><br>*** Order has shipped. ***</b><br/><br/>' . 

                       '<b>3PL order number:</b> ' .  $fields[1] . '<br/>' . 

                       '<b>Weight:</b> ' .  $fields[2] . '<br/>' . 

                       '<b>Shipped via:</b> ' .  $fields[3] . '<br/>' . 

                       '<b>Tracking number:</b> ' .  $fields[4] . '<br/>' . 

                       '<b>Ship date:</b> ' .  $fields[5] . '<br/>' . 

                       '<b>Postage:</b> ' .  $fields[6] . '<br/>' . 

                       '<b>Fulfillment:</b> ' .  $fields[7] . '<br/>' . 

                       '<b>Per packslip:</b> ' .  $fields[8];



            // Make shipment and add tracking number

            if ($fields[3] == 'UPS-RESIDENTIAL') { $shippedby = 'ups'; $shipname = 'UPS Ground'; }

            elseif ($fields[3] == 'UPS-2') { $shippedby = 'ups'; $shipname = 'UPS 2nd Day Air'; }

            elseif ($fields[3] == 'UPS-OVERNIGHT') { $shippedby = 'ups'; $shipname = 'UPS Next Day Air Saver'; }

            elseif ($fields[3] == 'USPS-PRI') { $shippedby = 'usps'; $shipname = 'USPS Priority'; }

            elseif ($fields[3] == 'CANADA') { $shippedby = 'custom'; $shipname = 'MSI Canada (Standard) '; }

            elseif ($fields[3] == 'MSITRACK') { $shippedby = 'custom'; $shipname = 'MSI Canada (Express)'; }

            else { $shippedby = 'custom'; }

            // Attempt to create the order, notify on failure

            try { $newShipmentId = $client->call($sess_id, 'sales_order_shipment.create', array($ShippedOrderId, array(), $comment, true, false, $shippedby, $shipname, $fields[4])); }

            catch (Exception $e) { echo 'Shipment creation failed on order '. $ShippedOrderId . ': ', $e->getMessage(); }

            // Add comment to order with all the info

            $client->call($sess_id, 'sales_order.addComment', array($ShippedOrderId, 'complete',  $comment,  false));



            $mail_content .= $line . "\n";

            $importcount++;

         }

      }

   }



   $mail_content .= "\n===\n\n";



   $mail_content .= $dupecount . " shipments already in cart.\n\n" . $importcount . " new shipments imported on " . date('m/d/Y') . " at " . date('g:ia', strtotime('+1 hour')) . " EST.\n\n";



   // Close API session

   $client->endSession($sess_id);



   mail($mail_to, 'Magento Shipping Import for ' . date('m/d/Y'), $mail_content);

   mail($mail_to2, 'Magento Shipping Import for ' . date('m/d/Y'), $mail_content);

   mail($mail_to3, 'Magento Shipping Import for ' . date('m/d/Y'), $mail_content);

   mail($mail_to4, 'Magento Shipping Import for ' . date('m/d/Y'), $mail_content);

   //mail($mail_to5, 'Magento Shipping Import for ' . date('m/d/Y'), $mail_content);



?>

<pre><?php echo $mail_content; ?></pre>

When I echo $shippedOrderId within the loop I get this (sample):

"The order numbers are: string(9) "100015822" Exception: SoapFault exception: [100] Requested order not exists. in /home/ab71714/public_html/exporter/importer-sftp.php:140 Stack trace: #0 /home/ab71714/public_html/exporter/importer-sftp.php(140): SoapClient->__call('call', Array) #1 /home/ab71714/public_html/exporter/importer-sftp.php(140): SoapClient->call('0f128ddbbbfc007...', 'sales_order.inf...', '100015822') #2 {main} The order numbers are: string(9) "100015842" Exception: SoapFault exception: [100] Requested order not exists. in /home/ab71714/public_html/exporter/importer-sftp.php:140 Stack trace: #0 /home/ab71714/public_html/exporter/importer-sftp.php(140): SoapClient->__call('call', Array) #1 /home/ab71714/public_html/exporter/importer-sftp.php(140): SoapClient->call('0f128ddbbbfc007...', 'sales_order.inf...', '100015842') #2 {main} The order numbers are: string(9) "100015878" "


Solution

  • This is what your calling (/app/code/core/Mage/Sales/Model/Order/Api.php) :

    public function info($orderIncrementId)
    

    Make sure you're using the increment id when setting $shippedOrderId:

    $ShippedOrderId = $order->getIncrementId();
    

    It seems that you are connecting successfully, you have an error from the API that the order doesn't exist. Not really sure what else it would be causing the problem. Try debugging the session_id and client params if that doesn't work.