options = new AmzFulfillment_Options(); $this->appName = sprintf('%s on %s', AMZFULFILLMENT_PLUGIN_ID, preg_replace("/^https?:\/\//", '', site_url())); $this->appVersion = AmzFulfillment_Core::getPluginVersion(); $this->orderPrefix = 'woocommerce:'; } /** * createFulfillmentOrder * * @param string $orderId * @param FBAOutboundServiceMWS_Model_Address $address * @param FBAOutboundServiceMWS_Model_CreateFulfillmentOrderItemList $itemsList * @param string $notes * @return FBAOutboundServiceMWS_Model_CreateFulfillmentOrderResponse */ public function createFulfillmentOrder($orderId, $address, $itemsList, $notes = '') { $this->options->load(); if(!$this->options->hasAmazonCredentials()) { throw new Exception(sprintf('Amazon % failed: No mws api credentials configured', __FUNCTION__)); } $request = new FBAOutboundServiceMWS_Model_CreateFulfillmentOrderRequest(); $request->setSellerId($this->options->getMerchantId()); $request->setShippingSpeedCategory("Standard"); $request->setDestinationAddress($address); $request->setItems($itemsList); $request->setSellerFulfillmentOrderId($this->orderPrefix . $orderId); $request->setDisplayableOrderId($this->orderPrefix . $orderId); $request->setDisplayableOrderDateTime(date("c", strtotime("now"))); if($notes != '') { $request->setDisplayableOrderComment("Order Notes " . $notes); } else { $request->setDisplayableOrderComment("No notes"); } if ($this->options->getHold()) { $request->setFulfillmentAction("Hold"); } else { $request->setFulfillmentAction("Ship"); } $request->setFulfillmentPolicy('FillOrKill'); return $this->getOutboundClient()->CreateFulfillmentOrder($request); } /** * cancelFulfillmentOrder * * @param string $orderId * @return FBAOutboundServiceMWS_Model_CancelFulfillmentOrderResponse */ public function cancelFulfillmentOrder($orderId) { $this->options->load(); if(!$this->options->hasAmazonCredentials()) { throw new Exception(sprintf('Amazon % failed: No mws api credentials configured', __FUNCTION__)); } $request = new FBAOutboundServiceMWS_Model_CancelFulfillmentOrderRequest(); $request->setSellerId($this->options->getMerchantId()); $request->setSellerFulfillmentOrderId($this->orderPrefix . $orderId); return $this->getOutboundClient()->CancelFulfillmentOrder($request); } /** * getFulfillmentOrder * * @param int $orderId * @throws Exception * @return FBAOutboundServiceMWS_Model_GetFulfillmentOrderResponse */ public function getFulfillmentOrder($orderId) { $this->options->load(); if(!$this->options->hasAmazonCredentials()) { throw new Exception(sprintf('Amazon % failed: No mws api credentials configured', __FUNCTION__)); } $request = new FBAOutboundServiceMWS_Model_GetFulfillmentOrderRequest(); $request->setMarketplace($this->options->getMarketplace()->getMarketplaceId()); $request->setSellerId($this->options->getMerchantId()); $request->setSellerFulfillmentOrderId($this->orderPrefix . $orderId); return $this->getOutboundClient()->getFulfillmentOrder($request); } /** * getAllFulfillmentOrders * * @param string $date * @throws Exception * @return FBAOutboundServiceMWS_Model_FulfillmentOrder[] */ public function getAllFulfillmentOrders($date = NULL) { $this->options->load(); if(!$this->options->hasAmazonCredentials()) { throw new Exception(sprintf('Amazon % failed: No mws api credentials configured', __FUNCTION__)); } $orders = array(); $response = $this->listAllFulfillmentOrders($date); foreach($response->getListAllFulfillmentOrdersResult()->getFulfillmentOrders()->getmember() as $order) { $orders[] = $order; } while($response->getListAllFulfillmentOrdersResult()->isSetNextToken()) { $nextToken = $response->getListAllFulfillmentOrdersResult()->getNextToken(); $response = $this->listAllFulfillmentOrdersByNextToken($nextToken); foreach($response->getListAllFulfillmentOrdersByNextTokenResult()->getFulfillmentOrders()->getmember() as $order) { $orders[] = $order; } } return $orders; } /** * listAllFulfillmentOrders * * @param string $date * @return FBAOutboundServiceMWS_Model_ListAllFulfillmentOrdersResponse */ private function listAllFulfillmentOrders($date = NULL) { $request = new FBAOutboundServiceMWS_Model_ListAllFulfillmentOrdersRequest(); $request->setMarketplace($this->options->getMarketplace()->getMarketplaceId()); $request->setSellerId($this->options->getMerchantId()); if($date !== NULL) { $request->setQueryStartDateTime($date); } return $this->getOutboundClient()->listAllFulfillmentOrders($request); } /** * listAllFulfillmentOrdersByNextToken * * @param string $nextToken * @return FBAOutboundServiceMWS_Model_ListAllFulfillmentOrdersByNextTokenResponse */ private function listAllFulfillmentOrdersByNextToken($nextToken) { $request = new FBAOutboundServiceMWS_Model_ListAllFulfillmentOrdersByNextTokenRequest(); $request->setMarketplace($this->options->getMarketplace()->getMarketplaceId()); $request->setSellerId($this->options->getMerchantId()); $request->setNextToken($nextToken); return $this->getOutboundClient()->listAllFulfillmentOrdersByNextToken($request); } /** * getAllInventoryItems * * @param array $sellerSkus * @return array */ public function getAllInventoryItems($sellerSkus) { $this->options->load(); if(!$this->options->hasAmazonCredentials()) { throw new Exception(sprintf('Amazon % failed: No mws api credentials configured', __FUNCTION__)); } $items = array(); $response = $this->listInventorySupply($sellerSkus); foreach($response->getListInventorySupplyResult()->getInventorySupplyList()->getmember() as $supplyItem) { $items[] = $supplyItem; } while($response->getListInventorySupplyResult()->isSetNextToken()) { $nextToken = $response->getListInventorySupplyResult()->getNextToken(); $response = $this->listInventorySupplyByNextToken($nextToken); foreach($response->getListInventorySupplyByNextTokenResult()->getInventorySupplyList()->getmember() as $supplyItem) { $items[] = $supplyItem; } } return $items; } /** * listInventorySupply * * @param array $sellerSkus * @return FBAInventoryServiceMWS_Model_ListInventorySupplyResponse */ private function listInventorySupply($sellerSkus) { $request = new FBAInventoryServiceMWS_Model_ListInventorySupplyRequest(); $request->setMarketplace($this->options->getMarketplace()->getMarketplaceId()); $request->setSellerId($this->options->getMerchantId()); $skuList = new FBAInventoryServiceMWS_Model_SellerSkuList(); $skuList->setmember($sellerSkus); $request->setSellerSkus($skuList); return $this->getInventoryClient()->ListInventorySupply($request); } /** * listInventorySupplyByNextToken * * @param string $nextToken * @return FBAInventoryServiceMWS_Model_ListInventorySupplyByNextTokenResponse */ private function listInventorySupplyByNextToken($nextToken) { $request = new FBAInventoryServiceMWS_Model_ListInventorySupplyByNextTokenRequest(); $request->setMarketplace($this->options->getMarketplace()->getMarketplaceId()); $request->setSellerId($this->options->getMerchantId()); $request->setNextToken($nextToken); return $this->getInventoryClient()->ListInventorySupplyByNextToken($request); } /** * getPackageTrackingDetails * * @param int $packageNumber * @return FBAOutboundServiceMWS_Model_GetPackageTrackingDetailsResponse */ public function getPackageTrackingDetails($packageNumber) { $this->options->load(); if(!$this->options->hasAmazonCredentials()) { throw new Exception(sprintf('Amazon % failed: No mws api credentials configured', __FUNCTION__)); } $request = new FBAOutboundServiceMWS_Model_GetPackageTrackingDetailsRequest(); $request->setSellerId($this->options->getMerchantId()); $request->setPackageNumber($packageNumber); return $this->getOutboundClient()->getPackageTrackingDetails($request); } /** * toXmlDate * * Convert date / timestamp to XML date (format 2016-02-11T00:00:00-06:00) for amazon api * * @param string|int $time * @throws InvalidArgumentException * @return string */ public function toXmlDate($time) { if(is_string($time)) { $date = $time; $time = strtotime($date); if($time <= 0) { throw new InvalidArgumentException('Invalid time: ' . $date); } } if(!is_integer($time)) { throw new InvalidArgumentException('Invalid time: ' . $time); } return date('Y-m-d', $time) . 'T' . date('H:i:sP', $time); } /** * toDate * * Convert XML date (format 2016-02-11T00:00:00-06:00) to date * * @param string $xmlDate * @throws InvalidArgumentException * @return string */ public function toDate($xmlDate) { if(!is_string($xmlDate) || strlen($xmlDate) < 19) { throw new InvalidArgumentException('Invalid time: ' . $xmlDate); } return str_replace('T', ' ', substr($xmlDate, 0, 19)); } /** * getOrderIdWithoutPrefix * * @param string $orderId * @return string or NULL if prefix is not found */ public function getOrderIdWithoutPrefix($orderId) { if(strpos($orderId, $this->orderPrefix) !== 0) { return NULL; } else { return str_replace($this->orderPrefix, '', $orderId); } } /** * getInventoryClient * * @throws Exception * @return FBAInventoryServiceMWS_Client */ private function getInventoryClient() { if($this->inventoryClient == null) { $this->options->load(); if(!$this->options->hasAmazonCredentials()) { throw new Exception('No amazon MWS credentials'); } $this->inventoryClient= new FBAInventoryServiceMWS_Client( $this->options->getAccessKeyId(), $this->options->getSecretAccessKeyId(), $this->appName, $this->appVersion, array ('ServiceURL' => $this->options->getMarketplace()->getEndpointUri() . self::FULFILLMENT_INVENTORY_API)); } return $this->inventoryClient; } /** * getOutboundClient * * @throws Exception * @return FBAOutboundServiceMWS_Client */ private function getOutboundClient() { if($this->outboundClient == null) { $this->options->load(); if(!$this->options->hasAmazonCredentials()) { throw new Exception('No amazon MWS credentials'); } $this->outboundClient = new FBAOutboundServiceMWS_Client( $this->options->getAccessKeyId(), $this->options->getSecretAccessKeyId(), array ('ServiceURL' => $this->options->getMarketplace()->getEndpointUri(). self::FULFILLMENT_OUTBOUND_SHIPMENT_API), $this->appName, $this->appVersion); } return $this->outboundClient; } }