/** * 抓取一段时间内的订单 * @param datetime $starttime * @param datetime $endtime * @return array * @author lzx */ public function getOrderLists($starttime, $endtime) { $allResponse = array(); $service = new MarketplaceWebServiceOrders_Client(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, APPLICATION_NAME, APPLICATION_VERSION, $this->config); $request = new MarketplaceWebServiceOrders_Model_ListOrdersRequest(); $request->setSellerId(MERCHANT_ID); $request->setMarketplaceId(MARKETPLACE_ID); $request->setCreatedAfter($starttime); $request->setCreatedBefore($endtime); $request->setOrderStatus(array("Unshipped", "PartiallyShipped")); $request->setMaxResultsPerPage(100); $response = $service->listOrders($request); $listOrdersResult = $response->getListOrdersResult(); $allResponse[] = $listOrdersResult; ######################### 分页抓取剩下的分页 start #################################### $while_index = 0; while ($listOrdersResult->isSetNextToken()) { $request = new MarketplaceWebServiceOrders_Model_ListOrdersByNextTokenRequest(); $request->setSellerId(MERCHANT_ID); $request->setNextToken($listOrdersResult->getNextToken()); $response = $service->ListOrdersByNextToken($request); $listOrdersResult = $response->getListOrdersByNextTokenResult(); $allResponse[] = $listOrdersResult; //ListOrders 和 ListOrdersByNextToken 操作的最大请求限额为 6 个防止意外循环 if ($while_index % 5 == 4) { sleep(61); //恢复速度1分钟 } //容错机制, 异常后自动弹出 if ($while_index > 50) { break; } $while_index++; } ######################### 分页抓取剩下的分页 end #################################### return $allResponse; }
/************************************************************************ * Setup request parameters and uncomment invoke to try out * sample for List Orders Action ***********************************************************************/ $request = new MarketplaceWebServiceOrders_Model_ListOrdersRequest(); $request->setSellerId(MERCHANT_ID); $interval_start = strtotime($start); $interval_end = strtotime($interval_time, $interval_start); $base_end = strtotime($end); while ($interval_end <= $base_end) { $marketplaceIdList = new MarketplaceWebServiceOrders_Model_MarketplaceIdList(); $marketplaceIdList->setId(array(MARKETPLACE_ID)); $request->setMarketplaceId($marketplaceIdList); $orderStatuses = new MarketplaceWebServiceOrders_Model_OrderStatusList(); $orderStatuses->setStatus(array('Unshipped', 'PartiallyShipped')); $request->setOrderStatus($orderStatuses); $start = date("Y-m-d H:i:s", $interval_start); $end = date("Y-m-d H:i:s", $interval_end); $astart = date('Y-m-d', strtotime($start)) . " " . date('H:i:s', strtotime($start)); $aend = date('Y-m-d', strtotime($end)) . " " . date('H:i:s', strtotime($end)); $interval_start = $interval_end; $interval_end = strtotime($interval_time, $interval_end); echo $astart . '<br>'; echo $aend; $request->setCreatedAfter(new DateTime($astart, new DateTimeZone('UTC'))); $request->setCreatedBefore(new DateTime($aend, new DateTimeZone('UTC'))); $errorstatus = invokeListOrders($service, $request); echo '$errorstatus:' . $errorstatus . "<br><br>"; } echo "finish"; /**
public function test() { $this->load->config('config_mws_api'); $amazon_app = $this->config->item('amazon_app'); //var_dump($amazon_app); foreach ($amazon_app as $amazon) { $serviceUrl = $amazon['serviceUrl']; $amazon_api = array('ServiceURL' => $serviceUrl, 'ProxyHost' => null, 'ProxyPort' => -1, 'MaxErrorRetry' => 3); $all_order_id = array(); $service = new MarketplaceWebServiceOrders_Client($amazon['AWS_ACCESS_KEY_ID'], $amazon['AWS_SECRET_ACCESS_KEY'], $this->config->item('APPLICATION_NAME'), $this->config->item('APPLICATION_VERSION'), $amazon_api); $request = new MarketplaceWebServiceOrders_Model_ListOrdersRequest(); $request->setSellerId($amazon['MERCHANT_ID']); //$request->setCreatedAfter(new DateTime('2012-12-17 00:00:00', new DateTimeZone('UTC'))); $beginning_time = $this->order_model->get_amazon_import_beginning_time(); $end_time = date('Y-m-d H:i:s', mktime(substr($beginning_time, 11, 2) + 24, substr($beginning_time, 14, 2), substr($beginning_time, 17, 2), substr($beginning_time, 5, 2), substr($beginning_time, 8, 2), substr($beginning_time, 0, 4))); $startdate = strtotime($end_time); $enddate = strtotime(str_replace("Z", "", str_replace("T", " ", get_utc_time('-20 minutes')))); if ($enddate - $startdate <= 0) { $end_time = get_utc_time('-20 minutes'); } $end_time = str_replace("Z", "", str_replace("T", " ", $end_time)); echo "beginning_time[utc]:" . $beginning_time; echo "end_time[utc]:" . $end_time; //die(); //$request->setLastUpdatedAfter(new DateTime('2013-01-21 08:43:14', new DateTimeZone('UTC'))); //$request->setLastUpdatedBefore(new DateTime('2013-01-21 11:10:25', new DateTimeZone('UTC'))); $request->setLastUpdatedAfter(new DateTime($beginning_time, new DateTimeZone('UTC'))); $request->setLastUpdatedBefore(new DateTime($end_time, new DateTimeZone('UTC'))); $orderStatuses = new MarketplaceWebServiceOrders_Model_OrderStatusList(); $orderStatuses->setStatus(array('Unshipped', 'PartiallyShipped')); $request->setOrderStatus($orderStatuses); $marketplaceIdList = new MarketplaceWebServiceOrders_Model_MarketplaceIdList(); $marketplaceIdList->setId(array($amazon['MARKETPLACE_ID'])); $request->setMarketplaceId($marketplaceIdList); $orderids = $this->invokeListOrders($service, $request); $all_order_id = $orderids[1]; //var_dump($orderids);die(); $next_token = $orderids[0]; while ($next_token != '') { $request = new MarketplaceWebServiceOrders_Model_ListOrdersByNextTokenRequest(); $request->setSellerId($amazon['MERCHANT_ID']); $request->setNextToken($orderids[0]); $orderids = $this->invokeListOrdersByNextToken($service, $request); $next_token = $orderids[0]; $all_order_id = array_merge($all_order_id, $orderids[1]); //var_dump($orderids); } //var_dump($all_order_id); foreach ($all_order_id as $order_id) { $order_info = array(); $items = array(); $data = array(); $amazon_pdf_data = array(); $amazon_ack_failed_data = array(); $request = new MarketplaceWebServiceOrders_Model_GetOrderRequest(); $request->setSellerId($amazon['MERCHANT_ID']); $request->setAmazonOrderId(array($order_id)); // object or array of parameters $order_info = $this->invokeGetOrder($service, $request); //var_dump($order_info); $request = new MarketplaceWebServiceOrders_Model_ListOrderItemsRequest(); $request->setSellerId($amazon['MERCHANT_ID']); $request->setAmazonOrderId($order_id); // object or array of parameters $items = $this->invokeListOrderItems($service, $request); //var_dump($items); if (!isset($order_info['AmazonOrderId'])) { $this->log_report($order_id); $amazon_ack_failed_data = array('amazonorderid' => $order_id, 'sellerid' => $amazon['MERCHANT_ID']); if (!$this->order_model->check_amazon_ack_failed_exists($order_id)) { $this->order_model->add_amazon_ack_failed($amazon_ack_failed_data); } continue; } $data = $this->_make_common_order_list_data($order_info, $items); //var_dump($data); if ($this->order_model->check_exists('order_list', array('transaction_id' => $data['transaction_id']))) { echo $data['transaction_id'] . "存在\n"; /* $invoice_begin=2372; $invoice_id=$invoice_begin+$this->order_model->fetch_all_amazon_count()+1; $amazon_pdf_data=array( 'amazonorderid'=>$order_info['AmazonOrderId'], 'sellerid'=>$amazon['MERCHANT_ID'], 'invoice_id'=>$invoice_id, ); if(!$this->order_model->check_wait_create_amazon_pdf_exists($order_id)) { $this->order_model->add_wait_create_amazon_pdf($amazon_pdf_data); }*/ } else { $this->order_model->add_order($data); $invoice_begin = 2372; $invoice_id = $invoice_begin + $this->order_model->fetch_all_amazon_count() + 1; $amazon_pdf_data = array('amazonorderid' => $order_info['AmazonOrderId'], 'sellerid' => $amazon['MERCHANT_ID'], 'invoice_id' => $invoice_id); if (!$this->order_model->check_wait_create_amazon_pdf_exists($order_id) && $order_info['AmazonOrderId'] != '') { $this->order_model->add_wait_create_amazon_pdf($amazon_pdf_data); } echo $data['transaction_id'] . "保存成功\n"; } sleep(2); } $this->order_model->update_amazon_import_beginning_time(array('value' => $end_time)); } }
define('MARKETPLACE_ID', 'A3PY9OQTG31F3H'); } $config = array('ServiceURL' => "https://mws.amazonservices.in/Orders/2013-09-01", 'ProxyHost' => null, 'ProxyPort' => -1, 'ProxyUsername' => null, 'ProxyPassword' => null, 'MaxErrorRetry' => 3); $service = new MarketplaceWebServiceOrders_Client(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, APPLICATION_NAME, APPLICATION_VERSION, $config); $LAST_PROCESSING = Mage::getStoreConfig('paywithamazon/general/last_reshipment_date'); if ($LAST_PROCESSING == '') { $days_ago = date('Y-m-d H:i:s', strtotime('-7 days', strtotime(date('Y-m-d H:i:s')))); Mage::getConfig()->saveConfig('paywithamazon/general/last_reshipment_date', $days_ago)->cleanCache(); $LAST_PROCESSING = Mage::getStoreConfig('paywithamazon/general/last_reshipment_date'); } $date = date('Y-m-d', strtotime($LAST_PROCESSING)) . 'T' . date('H:i:s', strtotime($LAST_PROCESSING)) . 'Z'; $request = new MarketplaceWebServiceOrders_Model_ListOrdersRequest(); $request->setSellerId(MERCHANT_ID); $request->setLastUpdatedAfter($date); $request->setMarketplaceId(MARKETPLACE_ID); $request->setOrderStatus(array('0' => 'Unshipped', '1' => 'PartiallyShipped')); try { $response = $service->ListOrders($request); $dom = new DOMDocument(); $dom->loadXML($response->toXML()); $dom->preserveWhiteSpace = false; $dom->formatOutput = true; $xml = simplexml_load_string($dom->saveXML()); $dataResponse = json_decode(json_encode($xml), 1); } catch (Exception $e) { } if (is_array($dataResponse) && !empty($dataResponse) && isset($dataResponse['ListOrdersResult']['Orders']['Order'])) { $orders = $dataResponse['ListOrdersResult']['Orders']['Order']; foreach ($orders as $key => $value) { if ($value['OrderStatus'] == 'Unshipped') { $AmazonOrderId = $value['AmazonOrderId'];
/** * List all orders updated after a certain date * * @param string $date * @param string $orderStatus * @link http://docs.developer.amazonservices.com/en_US/orders/2013-09-01/Orders_ListOrders.html * @link https://mws.amazonservices.ca/Orders/%s?Action=listOrders&SellerId=1&CreatedAfter=2016-02-23T14%3A06%3A41%2B0000&OrderStatus.Status.1=Pending&MarketplaceId.Id.1=1&AWSAccessKeyId=12124654645746765756&Timestamp=2016-02-23T14%3A06%3A41.000Z&Version=2013-09-01&SignatureVersion=2&SignatureMethod=HmacSHA256&Signature=g4xOYeHF5f9oIH8RPv5DIyw0QklnZmVDv10cKG3KdP8%3D * @return \MarketplaceWebServiceOrders_Model_ListOrdersResponse */ public function getOrders($date = 'NOW', array $orderStatuses) { try { // ini list order request $this->request = new ListOrderRequest(); // set marketplace id $this->request->setMarketplaceId($this->merchantConfig['marketplaceId']); // set seller id $this->request->setSellerId($this->merchantConfig['merchantId']); // set create order's date $this->request->setCreatedAfter(new \DateTime($date, new \DateTimeZone('UTC'))); // set the order statuses $this->request->setOrderStatus($orderStatuses); return $this->orderClient->listOrders($this->request); } catch (\MarketplaceWebServiceOrders_Exception $e) { throw new InternalServerErrorException($e->getErrorMessage(), InternalServerErrorException::CODE); } /** * List Orders Action Sample * ListOrders can be used to find orders that meet the specified criteria. * * @param MarketplaceWebServiceOrders_Interface $service instance of MarketplaceWebServiceOrders_Interface * @param mixed $request MarketplaceWebServiceOrders_Model_ListOrders or array of parameters // */ // function invokeListOrders(\MarketplaceWebServiceOrders_Interface $service, $request) // { // $response = $service->listOrders($request); // // var_dump($response); // exit; // try { // $response = $service->listOrders($request); // // echo ("Service Response\n"); // echo ("=============================================================================\n"); // // echo(" ListOrdersResponse\n"); // if ($response->isSetListOrdersResult()) { // echo(" ListOrdersResult\n"); // $listOrdersResult = $response->getListOrdersResult(); // if ($listOrdersResult->isSetNextToken()) // { // echo(" NextToken\n"); // echo(" " . $listOrdersResult->getNextToken() . "\n"); // } // if ($listOrdersResult->isSetCreatedBefore()) // { // echo(" CreatedBefore\n"); // echo(" " . $listOrdersResult->getCreatedBefore() . "\n"); // } // if ($listOrdersResult->isSetLastUpdatedBefore()) // { // echo(" LastUpdatedBefore\n"); // echo(" " . $listOrdersResult->getLastUpdatedBefore() . "\n"); // } // if ($listOrdersResult->isSetOrders()) { // echo(" Orders\n"); // $orders = $listOrdersResult->getOrders(); // $orderList = $orders->getOrder(); // foreach ($orderList as $order) { // echo(" Order\n"); // if ($order->isSetAmazonOrderId()) // { // echo(" AmazonOrderId\n"); // echo(" " . $order->getAmazonOrderId() . "\n"); // } // if ($order->isSetSellerOrderId()) // { // echo(" SellerOrderId\n"); // echo(" " . $order->getSellerOrderId() . "\n"); // } // if ($order->isSetPurchaseDate()) // { // echo(" PurchaseDate\n"); // echo(" " . $order->getPurchaseDate() . "\n"); // } // if ($order->isSetLastUpdateDate()) // { // echo(" LastUpdateDate\n"); // echo(" " . $order->getLastUpdateDate() . "\n"); // } // if ($order->isSetOrderStatus()) // { // echo(" OrderStatus\n"); // echo(" " . $order->getOrderStatus() . "\n"); // } // if ($order->isSetFulfillmentChannel()) // { // echo(" FulfillmentChannel\n"); // echo(" " . $order->getFulfillmentChannel() . "\n"); // } // if ($order->isSetSalesChannel()) // { // echo(" SalesChannel\n"); // echo(" " . $order->getSalesChannel() . "\n"); // } // if ($order->isSetOrderChannel()) // { // echo(" OrderChannel\n"); // echo(" " . $order->getOrderChannel() . "\n"); // } // if ($order->isSetShipServiceLevel()) // { // echo(" ShipServiceLevel\n"); // echo(" " . $order->getShipServiceLevel() . "\n"); // } // if ($order->isSetShippingAddress()) { // echo(" ShippingAddress\n"); // $shippingAddress = $order->getShippingAddress(); // if ($shippingAddress->isSetName()) // { // echo(" Name\n"); // echo(" " . $shippingAddress->getName() . "\n"); // } // if ($shippingAddress->isSetAddressLine1()) // { // echo(" AddressLine1\n"); // echo(" " . $shippingAddress->getAddressLine1() . "\n"); // } // if ($shippingAddress->isSetAddressLine2()) // { // echo(" AddressLine2\n"); // echo(" " . $shippingAddress->getAddressLine2() . "\n"); // } // if ($shippingAddress->isSetAddressLine3()) // { // echo(" AddressLine3\n"); // echo(" " . $shippingAddress->getAddressLine3() . "\n"); // } // if ($shippingAddress->isSetCity()) // { // echo(" City\n"); // echo(" " . $shippingAddress->getCity() . "\n"); // } // if ($shippingAddress->isSetCounty()) // { // echo(" County\n"); // echo(" " . $shippingAddress->getCounty() . "\n"); // } // if ($shippingAddress->isSetDistrict()) // { // echo(" District\n"); // echo(" " . $shippingAddress->getDistrict() . "\n"); // } // if ($shippingAddress->isSetStateOrRegion()) // { // echo(" StateOrRegion\n"); // echo(" " . $shippingAddress->getStateOrRegion() . "\n"); // } // if ($shippingAddress->isSetPostalCode()) // { // echo(" PostalCode\n"); // echo(" " . $shippingAddress->getPostalCode() . "\n"); // } // if ($shippingAddress->isSetCountryCode()) // { // echo(" CountryCode\n"); // echo(" " . $shippingAddress->getCountryCode() . "\n"); // } // if ($shippingAddress->isSetPhone()) // { // echo(" Phone\n"); // echo(" " . $shippingAddress->getPhone() . "\n"); // } // } // if ($order->isSetOrderTotal()) { // echo(" OrderTotal\n"); // $orderTotal = $order->getOrderTotal(); // if ($orderTotal->isSetCurrencyCode()) // { // echo(" CurrencyCode\n"); // echo(" " . $orderTotal->getCurrencyCode() . "\n"); // } // if ($orderTotal->isSetAmount()) // { // echo(" Amount\n"); // echo(" " . $orderTotal->getAmount() . "\n"); // } // } // if ($order->isSetNumberOfItemsShipped()) // { // echo(" NumberOfItemsShipped\n"); // echo(" " . $order->getNumberOfItemsShipped() . "\n"); // } // if ($order->isSetNumberOfItemsUnshipped()) // { // echo(" NumberOfItemsUnshipped\n"); // echo(" " . $order->getNumberOfItemsUnshipped() . "\n"); // } // if ($order->isSetPaymentExecutionDetail()) { // echo(" PaymentExecutionDetail\n"); // $paymentExecutionDetail = $order->getPaymentExecutionDetail(); // $paymentExecutionDetailItemList = $paymentExecutionDetail->getPaymentExecutionDetailItem(); // foreach ($paymentExecutionDetailItemList as $paymentExecutionDetailItem) { // echo(" PaymentExecutionDetailItem\n"); // if ($paymentExecutionDetailItem->isSetPayment()) { // echo(" Payment\n"); // $payment = $paymentExecutionDetailItem->getPayment(); // if ($payment->isSetCurrencyCode()) // { // echo(" CurrencyCode\n"); // echo(" " . $payment->getCurrencyCode() . "\n"); // } // if ($payment->isSetAmount()) // { // echo(" Amount\n"); // echo(" " . $payment->getAmount() . "\n"); // } // } // if ($paymentExecutionDetailItem->isSetPaymentMethod()) // { // echo(" PaymentMethod\n"); // echo(" " . $paymentExecutionDetailItem->getPaymentMethod() . "\n"); // } // } // } // if ($order->isSetPaymentMethod()) // { // echo(" PaymentMethod\n"); // echo(" " . $order->getPaymentMethod() . "\n"); // } // if ($order->isSetMarketplaceId()) // { // echo(" MarketplaceId\n"); // echo(" " . $order->getMarketplaceId() . "\n"); // } // if ($order->isSetBuyerEmail()) // { // echo(" BuyerEmail\n"); // echo(" " . $order->getBuyerEmail() . "\n"); // } // if ($order->isSetBuyerName()) // { // echo(" BuyerName\n"); // echo(" " . $order->getBuyerName() . "\n"); // } // if ($order->isSetShipmentServiceLevelCategory()) // { // echo(" ShipmentServiceLevelCategory\n"); // echo(" " . $order->getShipmentServiceLevelCategory() . "\n"); // } // if ($order->isSetShippedByAmazonTFM()) // { // echo(" ShippedByAmazonTFM\n"); // echo(" " . $order->getShippedByAmazonTFM() . "\n"); // } // if ($order->isSetTFMShipmentStatus()) // { // echo(" TFMShipmentStatus\n"); // echo(" " . $order->getTFMShipmentStatus() . "\n"); // } // } // } // } // if ($response->isSetResponseMetadata()) { // echo(" ResponseMetadata\n"); // $responseMetadata = $response->getResponseMetadata(); // if ($responseMetadata->isSetRequestId()) // { // echo(" RequestId\n"); // echo(" " . $responseMetadata->getRequestId() . "\n"); // } // } // // echo(" ResponseHeaderMetadata: " . $response->getResponseHeaderMetadata() . "\n"); // } catch (\MarketplaceWebServiceOrders_Exception $ex) { // echo("Caught Exception: " . $ex->getMessage() . "\n"); // echo("Response Status Code: " . $ex->getStatusCode() . "\n"); // echo("Error Code: " . $ex->getErrorCode() . "\n"); // echo("Error Type: " . $ex->getErrorType() . "\n"); // echo("Request ID: " . $ex->getRequestId() . "\n"); // echo("XML: " . $ex->getXML() . "\n"); // echo("ResponseHeaderMetadata: " . $ex->getResponseHeaderMetadata() . "\n"); // } // } // invokeListOrders($this->client, $this->request); exit; }