/**
  * GIVEN An <sdkApi> that will thrown an <exception> of <exceptionType> when making a request.
  * WHEN A request is made.
  * THEN The <exception> will be caught.
  * AND An exception of <expectedExceptionType> will be thrown.
  *
  * @param string
  * @param string
  * @dataProvider provideSdkExceptions
  */
 public function testSdkExceptionHandling($exceptionType, $expectedExceptionType)
 {
     $exception = new $exceptionType(__METHOD__ . ': Test Exception');
     $this->api->method('send')->will($this->throwException($exception));
     $this->setExpectedException($expectedExceptionType);
     EcomDev_Utils_Reflection::invokeRestrictedMethod($this->allocator, 'makeRequest', [$this->api]);
 }
 /**
  * Sending the payload request and returning the response.
  *
  * @return IPayload | null
  */
 protected function _sendRequest()
 {
     $logger = $this->_logger;
     $logContext = $this->_logContext;
     $response = null;
     try {
         $response = $this->_api->setRequestBody($this->_request)->send()->getResponseBody();
     } catch (InvalidPayload $e) {
         $logMessage = "Invalid payload for {$this->_getPayloadName()}. See exception log for more details.";
         $logger->warning($logMessage, $logContext->getMetaData(__CLASS__, ['exception_message' => $e->getMessage()]));
         $logger->logException($e, $logContext->getMetaData(__CLASS__, [], $e));
     } catch (NetworkError $e) {
         $logMessage = "Caught a network error sending {$this->_getPayloadName()}. See exception log for more details.";
         $logger->warning($logMessage, $logContext->getMetaData(__CLASS__, ['exception_message' => $e->getMessage()]));
         $logger->logException($e, $logContext->getMetaData(__CLASS__, [], $e));
     } catch (UnsupportedOperation $e) {
         $logMessage = "{$this->_getPayloadName()} operation is unsupported in the current configuration. See exception log for more details.";
         $logger->critical($logMessage, $logContext->getMetaData(__CLASS__, ['exception_message' => $e->getMessage()]));
         $logger->logException($e, $logContext->getMetaData(__CLASS__, [], $e));
     } catch (UnsupportedHttpAction $e) {
         $logMessage = "{$this->_getPayloadName()} request is configured with an unsupported HTTP action. See exception log for more details.";
         $logger->critical($logMessage, $logContext->getMetaData(__CLASS__, ['exception_message' => $e->getMessage()]));
         $logger->logException($e, $logContext->getMetaData(__CLASS__, [], $e));
     } catch (Exception $e) {
         $logMessage = "Encountered unexpected exception sending {$this->_getPayloadName()}. See exception log for more details.";
         $logger->warning($logMessage, $logContext->getMetaData(__CLASS__, ['exception_message' => $e->getMessage()]));
         $logger->logException($e, $logContext->getMetaData(__CLASS__, [], $e));
     }
     return $response;
 }
 /**
  * Send the order create request to the api.
  *
  * @return self
  */
 protected function _send()
 {
     Mage::dispatchEvent($this->_beforeOrderSendEvent, ['order' => $this->_order, 'payload' => $this->_payload]);
     try {
         $reply = $this->_api->setRequestBody($this->_payload)->send()->getResponseBody();
     } catch (NetworkException $e) {
         $this->_logger->warning('Caught a network error sending order create. Will retry later.', $this->_logContext->getMetaData(__CLASS__, [], $e));
         return $this;
     } catch (UnsupportedOperation $e) {
         $this->_logger->critical('[%s] Order create request saved, but not sent. See exception log.', $this->_logContext->getMetaData(__CLASS__, [], $e));
         return $this;
     } catch (UnsupportedHttpAction $e) {
         $this->_logger->critical('[%s] Order create request saved, but not sent. See exception log.', $this->_logContext->getMetaData(__CLASS__, [], $e));
         return $this;
     } catch (Exception $e) {
         throw $this->_logUnhandledException($e);
     }
     if ($reply->isSuccessful()) {
         $this->_order->setStatus(self::STATUS_SENT);
         Mage::dispatchEvent($this->_successfulOrderCreateEvent, ['order' => $this->_order]);
     } else {
         throw $this->_logUnhandledException();
     }
     return $this;
 }
 /**
  * Send the order create request to the api.
  *
  * @return self
  */
 protected function _send()
 {
     Mage::dispatchEvent($this->_beforeOrderSendEvent, ['order' => $this->_order, 'payload' => $this->_payload]);
     $logger = $this->_logger;
     $logContext = $this->_logContext;
     try {
         $reply = $this->_api->setRequestBody($this->_payload)->send()->getResponseBody();
     } catch (NetworkError $e) {
         $logger->warning('Caught a network error sending order create. See exception log for more details.', $logContext->getMetaData(__CLASS__, ['exception_message' => $e->getMessage()]));
         $logger->logException($e, $logContext->getMetaData(__CLASS__, [], $e));
         return $this;
     } catch (UnsupportedOperation $e) {
         $logger->critical('The order create operation is unsupported in the current configuration. Order saved, but not sent. See exception log for more details.', $logContext->getMetaData(__CLASS__, ['exception_message' => $e->getMessage()]));
         $logger->logException($e, $logContext->getMetaData(__CLASS__, [], $e));
         return $this;
     } catch (UnsupportedHttpAction $e) {
         $logger->critical('The order create operation is configured with an unsupported HTTP action. Order saved, but not sent. See exception log for more details.', $logContext->getMetaData(__CLASS__, ['exception_message' => $e->getMessage()]));
         $logger->logException($e, $logContext->getMetaData(__CLASS__, [], $e));
         return $this;
     } catch (Exception $e) {
         throw $this->_logUnhandledException($e);
     }
     if ($reply->isSuccessful()) {
         $this->_order->setStatus(self::STATUS_SENT);
         Mage::dispatchEvent($this->_successfulOrderCreateEvent, ['order' => $this->_order]);
     } else {
         throw $this->_logUnhandledException();
     }
     return $this;
 }
 /**
  * prepare to send the request
  *
  * @param ILookupRequest
  * @param string
  * @param string
  * @return ILookupRequest
  */
 protected function prepareApiForSend()
 {
     $request = $this->api->getRequestBody();
     $this->buildOutRequest($request);
     $this->logRequest($request, 'Gift card tender type request');
     $this->api->setRequestBody($request);
     return $request;
 }
 /**
  * Sending the payload request and returning the response.
  *
  * @return IPayload | null
  */
 protected function _sendRequest()
 {
     $response = null;
     try {
         $response = $this->_api->setRequestBody($this->_request)->send()->getResponseBody();
     } catch (Exception $e) {
         $this->_processException($e);
     }
     return $response;
 }
 /**
  * GIVEN An <sdkApi> that will thrown an <exception> of <exceptionType> when making a request.
  * WHEN A request is made.
  * THEN The <exception> will be caught.
  *
  * @param string
  * @dataProvider provideSdkExceptions
  */
 public function testSdkExceptionHandling($exceptionType)
 {
     $exception = new $exceptionType(__METHOD__ . ': Test Exception');
     // Assert that the send method is called and that it will throw
     // an exception. This is the closest thing to a meaningful assertion
     // this test can make.
     $this->api->expects($this->once())->method('send')->will($this->throwException($exception));
     $reservation = Mage::getModel('ebayenterprise_inventory/allocation_reservation');
     // Create deallocator mock to be tested. Mocked methods allow only
     // exception handling to be tested without depending upon methods needed
     // to create the API instance or fill out the payloads.
     $deallocator = $this->getModelMock('ebayenterprise_inventory/allocation_deallocator', ['prepareApi', 'prepareRequest'], false, [['log_context' => $this->logContext]]);
     $deallocator->method('prepareApi')->will($this->returnValue($this->api));
     $deallocator->method('prepareRequest')->will($this->returnSelf());
     // Nothing obvious to assert other than the side-effect of the send
     // method being called. All exceptions should be caught and suppressed.
     // The method doesn't return anything, just triggers side-effects.
     // Trigger the method and just make sure the send method gets called
     // and no exceptions arise.
     $deallocator->rollback($reservation);
 }
 /**
  * Send the request via the sdk
  *
  * @param  IBidirectionalApi
  * @return Payload
  *
  * @throws EbayEnterprise_PayPal_Exception
  * @throws EbayEnterprise_PayPal_Exception_Network
  */
 protected function sendRequest(IBidirectionalApi $sdk)
 {
     try {
         $sdk->send();
         $reply = $sdk->getResponseBody();
         return $reply;
     } catch (InvalidPayload $e) {
         $logMessage = 'PayPal payload invalid. See exception log for details.';
         $this->logger->warning($logMessage, $this->logContext->getMetaData(__CLASS__));
         $this->logger->logException($e, $this->logContext->getMetaData(__CLASS__, [], $e));
     } catch (NetworkError $e) {
         $logMessage = 'PayPal request encountered a network error. See exception log for details.';
         $this->logger->warning($logMessage, $this->logContext->getMetaData(__CLASS__));
         $this->logger->logException($e, $this->logContext->getMetaData(__CLASS__, [], $e));
     }
     $e = Mage::exception('EbayEnterprise_PayPal', $this->helper->__(static::EBAYENTERPRISE_PAYPAL_API_FAILED));
     $this->logger->logException($e, $this->logContext->getMetaData(__CLASS__, [], $e));
     throw $e;
 }
 /**
  * @param array $initParams Must have these keys:
  *                          - 'api' => IBidirectionalApi
  *                          - 'order_id' => string
  */
 public function __construct(array $initParams)
 {
     list($this->_api, $this->_orderId) = $this->_checkTypes($initParams['api'], $initParams['order_id']);
     $this->_payload = $this->_api->getRequestBody();
 }
 protected function extractResponseData(IBidirectionalApi $api)
 {
     $response = $api->getResponseBody();
     return $this->extractResult($response)->extractResponseAddresses($response);
 }
 /**
  * Update payment objects with details of the auth request and response. Validate
  * that a successful response was received.
  * @param ApiIBidirectionalApi $api
  * @param Varien_Object        $payment
  * @return self
  */
 protected function _handleApiResponse(Api\IBidirectionalApi $api, Varien_Object $payment)
 {
     $request = $api->getRequestBody();
     $response = $api->getResponseBody();
     return $this->_updatePaymentsWithAuthData($payment, $request, $response)->_validateResponse($response);
 }
 /**
  * @param array $initParams Must have these keys:
  *                          - 'api' => IBidirectionalApi
  *                          - 'order' => Mage_Sales_Model_Order
  */
 public function __construct(array $initParams)
 {
     list($this->_api, $this->_order, $this->_orderHelper) = $this->_checkTypes($initParams['api'], $initParams['order'], $this->_nullCoalesce($initParams, 'order_helper', Mage::helper('ebayenterprise_order')));
     $this->_payload = $this->_api->getRequestBody();
 }
 /**
  * Prepare a request payload for the API to validate the address.
  *
  * @param Mage_Customer_Model_Address_Abstract
  * @param IBidirectionalApi
  * @return \eBayEnterprise\RetailOrderManagement\Payload\Address\IValidationRequest
  */
 protected function _prepareApiForAddressRequest(Mage_Customer_Model_Address_Abstract $address, IBidirectionalApi $api)
 {
     return $api->setRequestBody($this->_getValidationRequest($address, $api)->prepareRequest()->getRequest());
 }
 /**
  * @param array
  */
 public function __construct(array $args = [])
 {
     list($this->_helper, $this->_address, $this->_api) = $this->_checkTypes($this->_nullCoalesce($args, 'helper', Mage::helper('ebayenterprise_address')), $args['address'], $args['api']);
     $this->_requestPayload = $this->_api->getRequestBody();
 }
 /**
  * process the data from the response into simpler objects
  *
  * @param IBidirectionalApi
  * @return EbayEnterprise_Inventory_Model_Allocation_Result
  */
 protected function prepareResult(IBidirectionalApi $api)
 {
     $result = $this->exportResultData($api->getResponseBody());
     return $result;
 }
 /**
  * Extract quantity results from the API response body.
  *
  * @param IBidirectionalApi
  * @param Mage_Sales_Model_Order_Quote_Item[]
  * @return EbayEnterprise_Inventory_Model_Quantity_Results
  */
 protected function _extractResponseResults(IBidirectionalApi $api, array $items)
 {
     try {
         $responseBody = $api->getResponseBody();
     } catch (UnsupportedOperation $e) {
         // This exception handling is probably not necessary but
         // is technically possible. If the sdk flow of
         // getRequest->setRequest->send->getResponse is followed,
         // which is is by the one public method of this class, this
         // exception should never be thrown in this instance. If it
         // were to be thrown at all by the SDK, it would have already
         // happened during the "send" step.
         $this->_logger->critical('Inventory quantity service response unsupported by SDK.', $this->_logContext->getMetaData(__CLASS__, [], $e));
         throw $this->_failQuantityCollection();
     }
     $responseParser = $this->_inventoryQuantityFactory->createResponseParser($responseBody);
     return $this->_inventoryQuantityFactory->createQuantityResults($responseParser->getQuantityResults(), $items);
 }
 /**
  * Check for the gift card redeem to have been voided. If it was, update the
  * gift card with response data. If not, thrown an exception, indicating the
  * request failed.
  * @param  Api\IBidirectionalApi $api
  * @return self
  * @throws EbayEnterprise_GiftCard_Exception If the redeem was not voided
  */
 protected function _handleVoidResponse(Api\IBidirectionalApi $api)
 {
     $response = $api->getResponseBody();
     if (!$response->wasVoided()) {
         throw Mage::exception('EbayEnterprise_GiftCard', self::VOID_REQUEST_FAILED_MESSAGE);
     }
     return $this->_extractPayloadAccountUniqueId($response)->setBalanceAmount($this->getBalanceAmount() + $this->getAmountRedeemed())->setAmountRedeemed(0.0)->setIsRedeemed(false);
 }
 /**
  * prepare the request to be sent
  *
  * return IBidirectionalApi
  */
 protected function prepareRequest(IBidirectionalApi $api, EbayEnterprise_Inventory_Model_Allocation_Reservation $reservation)
 {
     $request = $api->getRequestBody();
     $request->setReservationId($reservation->getId())->setRequestId(uniqid());
     $api->setRequestBody($request);
     return $this;
 }
 /**
  * Extract tax records from the API response body for the quote.
  *
  * @param IBidirectionalApi
  * @param Mage_Sales_Model_Order_Quote
  * @return EbayEnterprise_Tax_Model_Result
  */
 protected function _extractResponseResults(IBidirectionalApi $api, Mage_Sales_Model_Quote $quote)
 {
     try {
         $responseBody = $api->getResponseBody();
     } catch (UnsupportedOperation $e) {
         // This exception handling is probably not necessary but
         // is technically possible. If the sdk flow of
         // getRequest->setRequest->send->getResponse is followed,
         // which is is by the one public method of this class, this
         // exception should never be thrown in this instance. If it
         // were to be thrown at all by the SDK, it would have already
         // happened during the "send" step.
         $this->logger->critical('Tax quote service response unsupported by SDK.', $this->logContext->getMetaData(__CLASS__, [], $e));
         throw $this->_failTaxCollection();
     }
     $responseParser = $this->taxFactory->createResponseQuoteParser($responseBody, $quote);
     return $this->taxFactory->createTaxResults($responseParser->getTaxRecords(), $responseParser->getTaxDuties(), $responseParser->getTaxFees());
 }
 /**
  * Send the request via the sdk
  *
  * @param  IBidirectionalApi
  * @return Payload
  *
  * @throws EbayEnterprise_PayPal_Exception
  * @throws UnsupportedOperation
  * @throws UnsupportedHttpAction
  * @throws Exception
  */
 protected function sendRequest(IBidirectionalApi $sdk)
 {
     $logger = $this->logger;
     $logContext = $this->logContext;
     try {
         $sdk->send();
         $reply = $sdk->getResponseBody();
         return $reply;
     } catch (InvalidPayload $e) {
         $logMessage = 'Invalid payload for PayPal request. See exception log for more details.';
         $logger->warning($logMessage, $logContext->getMetaData(__CLASS__, ['exception_message' => $e->getMessage()]));
         $logger->logException($e, $logContext->getMetaData(__CLASS__, [], $e));
     } catch (NetworkError $e) {
         $logMessage = 'Caught network error sending the PayPal request. See exception log for more details.';
         $logger->warning($logMessage, $logContext->getMetaData(__CLASS__, ['exception_message' => $e->getMessage()]));
         $logger->logException($e, $logContext->getMetaData(__CLASS__, [], $e));
     } catch (UnsupportedOperation $e) {
         $logMessage = 'The PayPal operation is unsupported in the current configuration. See exception log for more details.';
         $logger->warning($logMessage, $logContext->getMetaData(__CLASS__, ['exception_message' => $e->getMessage()]));
         $logger->logException($e, $logContext->getMetaData(__CLASS__, [], $e));
         throw $e;
     } catch (UnsupportedHttpAction $e) {
         $logMessage = 'The PayPal operation is configured with an unsupported HTTP action. See exception log for more details.';
         $logger->warning($logMessage, $logContext->getMetaData(__CLASS__, ['exception_message' => $e->getMessage()]));
         $logger->logException($e, $logContext->getMetaData(__CLASS__, [], $e));
         throw $e;
     } catch (Exception $e) {
         $logMessage = 'Encountered unexpected exception from PayPal operation. See exception log for more details.';
         $logger->warning($logMessage, $logContext->getMetaData(__CLASS__, ['exception_message' => $e->getMessage()]));
         $logger->logException($e, $logContext->getMetaData(__CLASS__, [], $e));
         throw $e;
     }
     $e = Mage::exception('EbayEnterprise_PayPal', $this->helper->__(static::EBAYENTERPRISE_PAYPAL_API_FAILED));
     $logger->logException($e, $logContext->getMetaData(__CLASS__, [], $e));
     throw $e;
 }
 /**
  * @param array $initParams Must have these keys:
  *                          - 'api' => IBidirectionalApi
  *                          - 'customer_id' => string
  */
 public function __construct(array $initParams)
 {
     list($this->_api, $this->_customerId, $this->_orderId) = $this->_checkTypes($initParams['api'], $initParams['customer_id'], $this->_nullCoalesce($initParams, 'order_id', null));
     $this->_payload = $this->_api->getRequestBody();
 }