Magento Amoazon order import


#1

I found code for importing my amazon orders on line but my cron email said I had errors:
public_html/store/app/code/community/TP/Amazon/Model/Observer.php: line 1: ?PHP: No such file or directory
public_html/store/app/code/community/TP/Amazon/Model/Observer.php: line 3: class: command not found
public_html/store/app/code/community/TP/Amazon/Model/Observer.php: line 196: unexpected EOF while looking for matching `’’
public_html/store/app/code/community/TP/Amazon/Model/Observer.php: line 200: syntax error: unexpected end of file
I don’t know enough to fix the errors. Here is the code:

<?php class TP_Amazon_Model_Observer { // CHANGE THE FOLLOWING SERVICE TO URL SO IT'S THE CORRECT COUNTRY, THIS URL IS FOR GERMANY protected $_serviceUrl = 'https://mws.amazonservices.com/Orders/2011-01-01'; protected $_accessKeyId = 'YOUR_ACCESS_KEY_ID'; protected $_secretAccessKey = 'YOUR_SECRET_ACCESS_KEY'; protected $_applicationName = 'YOUR_APPLICATION_NAME'; protected $_applicationVersion = '1.0.0'; protected $_merchantId = 'YOUR_MERCHANT_ID'; protected $_marketplaceId = 'YOUR_MARKETPLACE_ID'; /** * Get amazon orders */ public function getAmazonOrders() { $config = array( 'ServiceURL' => $this->_serviceUrl, 'ProxyHost' => null, 'ProxyPort' => -1, 'MaxErrorRetry' => 3, ); $client = new MarketplaceWebServiceOrders_Client($this->_accessKeyId, $this->_secretAccessKey, $this->_applicationName, $this->_applicationVersion, $config); $request = new MarketplaceWebServiceOrders_Model_ListOrdersRequest(); $request->setSellerId($this->_merchantId); // List all orders udpated after a certain date $request->setCreatedAfter(new DateTime(Zend_Date::now()->subDay(1)->get('y-M-d H:m:s'), new DateTimeZone('UTC'))); // Set the marketplaces queried in this ListOrdersRequest $marketplaceIdList = new MarketplaceWebServiceOrders_Model_MarketplaceIdList(); $marketplaceIdList->setId(array($this->_marketplaceId)); $request->setMarketplaceId($marketplaceIdList); // Set the order statuses for this ListOrdersRequest (optional) // $orderStatuses = new MarketplaceWebServiceOrders_Model_OrderStatusList(); // $orderStatuses->setStatus(array('Shipped')); // $request->setOrderStatus($orderStatuses); $response = $client->listOrders($request); if ($response->isSetListOrdersResult()) { $listOrdersResult = $response->getListOrdersResult(); if ($listOrdersResult->isSetOrders()) { $orders = $listOrdersResult->getOrders(); $memberList = $orders->getOrder(); /** * Each order */ foreach ($memberList as $member) { /** * Check if order is already imported */ if (Mage::getModel('tpamazon/order')->loadByAttribute('amazon_order_id', $member->getAmazonOrderId()) === false) { $order = Mage::getModel('tpamazon/order'); $saveOrder = false; if ($member->isSetAmazonOrderId()) { $order->setAmazonOrderId($member->getAmazonOrderId()); $saveOrder = true; } if ($member->isSetPurchaseDate()) { $order->setPurchaseDate($member->getPurchaseDate()); } if ($member->isSetLastUpdateDate()) { $order->setLastUpdateDate($member->getLastUpdateDate()); } if ($member->isSetOrderStatus()) { $order->setOrderStatus($member->getOrderStatus()); } if ($member->isSetFulfillmentChannel()) { $order->setFulfillmentChannel($member->getFulfillmentChannel()); } if ($saveOrder) { $order->setSynchronized(0); $order->save(); $request = new MarketplaceWebServiceOrders_Model_ListOrderItemsRequest(); $request->setSellerId($this->_merchantId); $request->setAmazonOrderId($order->getAmazonOrderId()); try { $response = $client->listOrderItems($request); if ($response->isSetListOrderItemsResult()) { $listOrderItemsResult = $response->getListOrderItemsResult(); if ($listOrderItemsResult->isSetOrderItems()) { $orderItems = $listOrderItemsResult->getOrderItems(); $memberList = $orderItems->getOrderItem(); $countItems = count($memberList); $doneItems = 0; foreach ($memberList as $member) { $orderItem = Mage::getModel('tpamazon/order_item'); $orderItem->setTpAmazonOrderId($order->getId()); if ($member->isSetASIN()) { $orderItem->setAsin($member->getASIN()); } if ($member->isSetQuantityOrdered()) { $orderItem->setQuantity($member->getQuantityOrdered()); } else { $orderItem->setQuantity(1); } try { $orderItem->save(); $doneItems++; } catch(Exception $e) { } } /** * If items could not be imported, delete order to try it next run again */ if($doneItems < $countItems) { echo 'Deleting order' . $order->getAmazonOrderId() . ' because not every item could be saved.' . chr(10); $order->delete(); } } } } catch(MarketplaceWebServiceOrders_Exception $e) { /** * If items could not be imported, delete order to try it next run again */ echo 'Deleting order' . $order->getAmazonOrderId() . ': ' . $e->getMessage() . chr(10); $order->delete(); exit; } } } } } } } public function synchronizeAmazonOrders() { $orders = Mage::getModel('tpamazon/order')->getCollection()->addFieldToFilter('synchronized', false); foreach($orders as $order) { echo '---------------------------------------' . chr(10); if($order->getOrderStatus != 'Canceled') { $items = $order->getItems(); $countItems = count($items); $itemsDone = 0; foreach($items as $item) { $product = Mage::getModel('catalog/product')->loadByAttribute('sku', $item->getAsin()); /* @var $product Mage_Catalog_Model_Product */ if($product !== false) { $stockApi = Mage::getModel('cataloginventory/stock_item_api'); /* @var $stockApi Mage_CatalogInventory_Model_Stock_Item_Api */ $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product->getId()); /* @var $stockItem Mage_CatalogInventory_Model_Stock_Item */ // Get products quantity $qty = $stockItem->getQty(); if($qty === null) { echo 'Error: Could not load products quantity' . chr(10); break; } $stockApi->update($product->getId(), array('qty' => $qty-$item->getQuantity())); echo 'Reduced ' . $item->getAsin() . ' by ' . $item->getQuantity() . chr(10); $itemsDone++; } } if($itemsDone < $countItems) { echo 'Error: not every item could be synchronized on order: ' . $order->getAmazonOrderId() . chr(10); } $order->setSynchronized(1); $order->save(); echo 'Order ' . $order->getAmazonOrderId() . ' synchronized.' . chr(10); } else { echo 'Skipped order : ' . $order->getAmazonOrderId() . ' because it is cancelled ' . chr(10); } echo '---------------------------------------' . chr(10); } } }