/** * @param Mage_Sales_Model_Order * @return self */ public function void(Mage_Sales_Model_Order $order) { if ($this->_canVoid($order)) { try { $this->_getVoidApi()->doVoidOrder($order); } catch (EbayEnterprise_PayPal_Exception $e) { $logMessage = 'Void request failed. See exception log for details.'; $this->_logger->warning($logMessage, $this->_context->getMetaData(__CLASS__)); $this->_logger->logException($e, $this->_context->getMetaData(__CLASS__, [], $e)); } } return $this; }
/** * Process all of the products within a given store. * * @param Mage_Catalog_Model_Resource_Product_Collection $products products for a specific store * @param EbayEnterprise_Catalog_Model_Pim_Product_Collection $pimProducts collection of PIM Product instances * @param string $key * @param array $productIds * @return EbayEnterprise_Catalog_Model_Pim_Product_Collection $pimProducts collection of PIM Product instances */ protected function _processProductCollection(Mage_Catalog_Model_Resource_Product_Collection $products, EbayEnterprise_Catalog_Model_Pim_Product_Collection $pimProducts, array &$productIds = null) { $excludedProductIds = array(); $currentStoreId = $products->getStoreId(); $config = Mage::helper('eb2ccore')->getConfigModel($currentStoreId); $clientId = $config->clientId; $catalogId = $config->catalogId; foreach ($products->getItems() as $product) { $product->setStoreId($currentStoreId); $pimProduct = $pimProducts->getItemForProduct($product); if (!$pimProduct) { $pimProduct = Mage::getModel('ebayenterprise_catalog/pim_product', array('client_id' => $clientId, 'catalog_id' => $catalogId, 'sku' => $product->getSku())); $pimProducts->addItem($pimProduct); } try { $pimProduct->loadPimAttributesByProduct($product, $this->_doc, $this->_getFeedConfig(), $this->_getFeedAttributes($currentStoreId)); } catch (EbayEnterprise_Catalog_Model_Pim_Product_Validation_Exception $e) { $logData = ['sku' => $pimProduct->getSku()]; $logMessage = 'Product "{sku}" excluded from export.'; $this->_logger->warning($logMessage, $this->_context->getMetaData(__CLASS__, $logData)); $this->_logger->logException($e, $this->_context->getMetaData(__CLASS__, [], $e)); $excludedProductIds[] = $product->getId(); $pimProducts->deleteItem($pimProduct); } } if ($productIds) { $productIds = array_diff($productIds, $excludedProductIds); } return $pimProducts; }
/** * Save an EAV collection, disabling the indexer if the collection is * larger than a configured size. * * @param Mage_Eav_Model_Entity_Collection_Abstract * @return self */ public function saveCollectionStubIndexer(Mage_Eav_Model_Entity_Collection_Abstract $collection) { $config = $this->getConfigModel(); $stubIndexer = $config->maxPartialReindexSkus < $collection->getSize(); if ($stubIndexer) { // Stub the indexer so no indexing can take place during massive saves. $indexerKey = '_singleton/index/indexer'; $oldIndexer = $this->reregister($indexerKey, $this->indexerStub); } $failureCount = 0; $logData = ['product_count' => $collection->getSize()]; $logMessage = 'Saving {product_count} products with stubbed indexer.'; $this->logger->info($logMessage, $this->context->getMetaData(__CLASS__, $logData)); $failMessage = 'Failed to save product with sku {sku}.'; foreach ($collection as $item) { try { $item->save(); } catch (Exception $e) { $failureCount++; $failLogData = ['sku' => $item->getSku(), 'exception' => $e]; $this->logger->logException($e, $this->context->getMetaData(__CLASS__, $failLogData))->error($failMessage, $this->context->getMetaData(__CLASS__, $failLogData)); } } $logMessage = 'Finished saving {product_count} products with {failure_count} failures.'; $logData['failure_count'] = $failureCount; $this->logger->info($logMessage, $this->context->getMetaData(__CLASS__, $logData)); if ($stubIndexer) { $this->reregister($indexerKey, $oldIndexer); } return $this; }
/** * Test the method EbayEnterprise_MageLog_Helper_Data::logException when invoked * will call the EbayEnterprise_MageLog_Model_Logger::logException passing the proper * parameters. * @param Exception $exception * @param array $context * @dataProvider providerLogException */ public function testLogException(Exception $exception, array $context) { $logger = $this->getModelMock('ebayenterprise_magelog/logger', ['logException']); $logger->expects($this->once())->method('logException')->with($this->identicalTo($exception), $this->identicalTo($context))->will($this->returnSelf()); $this->replaceByMock('model', 'ebayenterprise_magelog/logger', $logger); EcomDev_Utils_Reflection::setRestrictedPropertyValue($this->_helper, '_logger', null); $this->assertSame($this->_helper, $this->_helper->logException($exception, $context)); }
/** * Redeem the gift card for the requested amount and return the amount that * was actually redeemed for the card. * @param EbayEnterprise_GiftCard_Model_IGiftcard $card * @param float $amount * @return self */ protected function _redeemVoidCard(EbayEnterprise_GiftCard_Model_IGiftcard $card) { try { $card->void(); } catch (EbayEnterprise_GiftCard_Exception $e) { $this->_logger->logException($e, $this->_context->getMetaData(__CLASS__, [], $e)); } return $card; }
/** * Unhandled exceptions cause the entire order not to get saved. * This is by design, so we don't report a false success or try * to keep sending an order that has no hope for success. * * @param Exception|null The exception to log or null for the default. * @return Exception The same (or default) exception after logging */ protected function _logUnhandledException(Exception $e = null) { if (!$e) { $errorMessage = $this->_helper->__(self::ORDER_CREATE_FAIL_MESSAGE); // Mage::exception adds '_Exception' to the end. $exceptionClassName = Mage::getConfig()->getModelClassName('ebayenterprise_order/create'); $e = Mage::exception($exceptionClassName, $errorMessage); } $this->_logger->warning('Encountered unexpected exception attempting to send order create. See exception log for more details.', $this->_logContext->getMetaData(__CLASS__, ['exception_message' => $e->getMessage()])); $this->_logger->logException($e, $this->_logContext->getMetaData(__CLASS__, [], $e)); return $e; }
/** * add a giftcard. * * @param string $cardNumber * @param string $pin * @return self */ protected function addGiftCard($cardNumber, $pin) { $giftcard = $this->container->getGiftCard($cardNumber)->setPin($pin); try { $this->helper->addGiftCardToOrder($giftcard, $this->container); $this->getSession()->addSuccess($this->helper->__(EbayEnterprise_GiftCard_Helper_Data::GIFT_CARD_ADD_SUCCESS, $cardNumber)); } catch (EbayEnterprise_GiftCard_Exception $e) { $this->getSession()->addError($this->helper->__($e->getMessage())); $this->logger->debug('Failed to add gift card to admin order. See exception log for more details.', $this->logContext->getMetaData(__CLASS__, ['exception_message' => $e->getMessage()])); $this->logger->logException($e, $this->logContext->getMetaData(__CLASS__, [], $e)); } return $this; }
/** * Before collecting item totals, check that all items * in the quote are available to be fulfilled. * * @param Varien_Event_Observer * @return self */ public function handleBeforeCollectTotals(Varien_Event_Observer $observer) { try { $quote = $observer->getEvent()->getQuote(); $this->quantityService->checkQuoteInventory($quote); } catch (EbayEnterprise_Inventory_Exception_Quantity_Collector_Exception $e) { $this->logger->logException($e, $this->logContext->getMetaData(__CLASS__, [], $e)); $this->logger->warning('Caught the following exception: {exception_message}.', $this->logContext->getMetaData(__CLASS__, ['exception_message' => $e->getMessage()])); } catch (EbayEnterprise_Inventory_Exception_Quantity_Unavailable_Exception $e) { $this->logger->logException($e, $this->logContext->getMetaData(__CLASS__, [], $e)); $this->logger->warning('Caught the following exception: {exception_message}.', $this->logContext->getMetaData(__CLASS__, ['exception_message' => $e->getMessage()])); if ($this->app->getStore()->isAdmin()) { // Handling admin exception here because it is not // being caught in the admin controller causing it // to display exception traces in the page. return $this->handleAdminOrderException($e, $quote); } // Continue to throw the exception in the frontend and let // the frontend controller handle it. throw $e; } return $this; }
/** * Place the order and recurring payment profiles when customer returned from paypal * Until this moment all quote data must be valid * * @param string $token * @param string $shippingMethodCode */ public function place($token, $shippingMethodCode = null) { $this->updateShippingMethod($shippingMethodCode); $isNewCustomer = $this->_prepareQuote(); $this->_ignoreAddressValidation(); $this->_quote->collectTotals(); $this->_getApi(); $payerId = $this->_quote->getPayment()->getAdditionalInformation(self::PAYMENT_INFO_PAYER_ID); $doExpressReply = $this->_api->doExpressCheckout($this->_quote, $token, $payerId); $doAuthorizationReply = $this->_api->doAuthorization($this->_quote); $this->_quote->getPayment()->importData(array_merge($doExpressReply, $doAuthorizationReply)); $service = Mage::getModel('sales/service_quote', $this->_quote); try { $service->submitAll(); // Any exceptions thrown from submitAll indicate an order that failed // to be created. In any such cases, the payment auth needs to be voided. } catch (Exception $e) { $this->_api->doVoidQuote($this->_quote); // Throw an exception for the controller to handle. Needs to indicate // the failure to complete the PayPal payment as the PayPal process // needs to be restarted once the auth was performed. throw Mage::exception('EbayEnterprise_PayPal', $this->_helper->__(EbayEnterprise_Paypal_Model_Express_Api::EBAYENTERPRISE_PAYPAL_API_FAILED)); } $this->_quote->save(); if ($isNewCustomer) { try { $this->_involveNewCustomer(); } catch (Exception $e) { $this->_logger->logException($e, $this->_context->getMetaData(__CLASS__, [], $e)); } } $order = $service->getOrder(); if (!$order) { return; } switch ($order->getState()) { // Even after placement, paypal can disallow authorize/capture case Mage_Sales_Model_Order::STATE_PENDING_PAYMENT: break; // regular placement, when everything is ok // regular placement, when everything is ok case Mage_Sales_Model_Order::STATE_PROCESSING: case Mage_Sales_Model_Order::STATE_COMPLETE: case Mage_Sales_Model_Order::STATE_PAYMENT_REVIEW: $order->sendNewOrderEmail(); break; } $this->_order = $order; Mage::dispatchEvent('checkout_submit_all_after', array('order' => $order, 'quote' => $this->_quote)); }
/** * Saving shipment and order in one transaction. * @param Mage_Sales_Model_Order_Shipment $shipment * @param string $incrementId * @return self */ protected function _saveShipment(Mage_Sales_Model_Order_Shipment $shipment, $incrementId) { $order = $shipment->getOrder(); $order->setIsInProcess(true); $transactionSave = Mage::getModel('core/resource_transaction')->addObject($shipment)->addObject($order); try { $transactionSave->save(); } catch (Exception $e) { $logData = ['increment_id' => $incrementId]; $logMessage = 'An error occurred saving shipment confirmation to order id ({increment_id}). See exception log for details.'; $this->_logger->error($logMessage, $this->_context->getMetaData(__CLASS__, $logData)); $this->_logger->logException($e, $this->_context->getMetaData(__CLASS__, [], $e)); } return $this; }
/** * fill out the request payload to send * * @param IBidirectionalApi * @param EbayEnterprise_Inventory_Model_Allocation_Item_Selector * @return self */ protected function prepareRequest(IBidirectionalApi $api, EbayEnterprise_Inventory_Model_Allocation_Item_Selector $selector) { $this->logger->debug('Building inventory allocation request reservation id {reservation_id}', $this->logContext->getMetaData(__CLASS__, ['reservation_id' => $this->reservation->getId()])); try { $request = $api->getRequestBody(); $builder = $this->createRequestBuilder($request, $selector, $this->reservation); $builder->buildOutRequest(); $api->setRequestBody($request); return $this; } catch (UnsupportedOperation $e) { $this->logger->critical('The inventory allocation operation is unsupported in the current configuration. See exception log for more details.', $this->logContext->getMetaData(__CLASS__, ['exception_message' => $e->getMessage()])); $this->logger->logException($e, $this->logContext->getMetaData(__CLASS__, [], $e)); } $this->handleAllocationFailure(); }
/** * Submit the order */ public function placeOrderAction() { try { $requiredAgreements = $this->_checkoutHelper->getRequiredAgreementIds(); if ($requiredAgreements) { $postedAgreements = array_keys($this->getRequest()->getPost('agreement', array())); if (array_diff($requiredAgreements, $postedAgreements)) { Mage::throwException($this->_helper->__('Please agree to all the terms and conditions before placing the order.')); } } $this->_initCheckout(); $this->_checkout->place($this->_initToken()); // prepare session to success or cancellation page $session = $this->_getCheckoutSession(); $session->clearHelperData(); // last successful quote $quoteId = $this->_getQuote()->getId(); $session->setLastQuoteId($quoteId)->setLastSuccessQuoteId($quoteId); // an order may be created $order = $this->_checkout->getOrder(); if ($order) { $session->setLastOrderId($order->getId())->setLastRealOrderId($order->getIncrementId()); } $this->_initToken(false); // no need in token anymore $this->_redirect('checkout/onepage/success'); return; } catch (EbayEnterprise_PayPal_Exception $e) { $this->_checkoutHelper->sendPaymentFailedEmail($this->_getQuote(), $e->getMessage()); $this->_logger->logException($e, $this->_context->getMetaData(__CLASS__, [], $e)); // If a PayPal exception is thrown while trying to place the order, // the PayPal transaction failed or was voided and customer needs to // begin PayPal flow again. Place error message in checkout session // so it will be displayed in the cart and redirect back to the cart // for the checkout flow to begin again. $this->_getCheckoutSession()->addError($e->getMessage()); $this->_redirect('checkout/cart'); } catch (Mage_Core_Exception $e) { $this->_checkoutHelper->sendPaymentFailedEmail($this->_getQuote(), $e->getMessage()); $this->_getSession()->addError($e->getMessage()); $this->_redirect('*/*/review'); } catch (Exception $e) { $this->_checkoutHelper->sendPaymentFailedEmail($this->_getQuote(), $this->__('Unable to place the order.')); $this->_getSession()->addError($this->__('Unable to place the order.')); $this->_logger->logException($e, $this->_context->getMetaData(__CLASS__, [], $e)); $this->_redirect('*/*/review'); } }
/** * Send the request via the SDK * @param Api\IBidirectionalApi $api * @return self * @throws EbayEnterprise_GiftCard_Exception If request cannot be made successfully */ protected function _sendRequest(Api\IBidirectionalApi $api) { try { $api->send(); } catch (Api\Exception\NetworkError $e) { $logMessage = 'Stored value request failed. See exception log for details.'; $this->_logger->warning($logMessage, $this->_context->getMetaData(__CLASS__)); $this->_logger->logException($e, $this->_context->getMetaData(__CLASS__, [], $e)); throw Mage::exception('EbayEnterprise_GiftCard_Exception_Network', $this->_helper->__(self::REQUEST_FAILED_MESSAGE)); } catch (Payload\Exception\InvalidPayload $e) { $logMessage = 'Invalid payload for stored value response. See exception log for details.'; $this->_logger->warning($logMessage, $this->_context->getMetaData(__CLASS__)); $this->_logger->logException($e, $this->_context->getMetaData(__CLASS__, [], $e)); throw Mage::exception('EbayEnterprise_GiftCard', $this->_helper->__(self::REQUEST_FAILED_MESSAGE)); } return $this; }
/** * Make a PayPal void request for the order identified by the order id. * * @param string * @param string * @return array * @throws EbayEnterprise_PayPal_Exception when the operation cannot be completed or fails. */ protected function doVoid($orderId, $currencyCode) { $sdk = $this->getSdk($this->config->apiOperationDoVoid); $payload = $sdk->getRequestBody(); $payload->setOrderId($orderId)->setRequestId($this->coreHelper->generateRequestId(self::PAYPAL_DOVOID_REQUEST_ID_PREFIX))->setCurrencyCode($currencyCode); Mage::dispatchEvent('ebayenterprise_paypal_do_void_before_send', ['payload' => $payload]); $sdk->setRequestBody($payload); $reply = $this->sendRequest($sdk); Mage::dispatchEvent('ebayenterprise_paypal_do_void_after_send', ['payload' => $reply]); $isVoided = $reply->isSuccess(); if (!$reply->isSuccess()) { $logMessage = 'PayPal DoVoid failed. See exception log for details.'; $this->logger->warning($logMessage, $this->logContext->getMetaData(__CLASS__)); $e = Mage::exception('EbayEnterprise_PayPal', $this->helper->__(static::EBAYENTERPRISE_PAYPAL_API_FAILED)); $this->logger->logException($e, $this->logContext->getMetaData(__CLASS__, [], $e)); throw $e; } return ['method' => EbayEnterprise_PayPal_Model_Method_Express::CODE, 'order_id' => $reply->getOrderId(), 'is_voided' => $isVoided]; }
/** * 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; }
/** * Perform the web request for address validation and return the response * @param Mage_Customer_Model_Address_Abstract $address * @return EbayEnterprise_Address_Model_Validation_Response|null */ protected function _makeRequestForAddress(Mage_Customer_Model_Address_Abstract $address) { $config = $this->_helper->getConfigModel(); $api = $this->_coreHelper->getSdkApi($config->apiService, $config->apiOperation); try { $this->_prepareApiForAddressRequest($address, $api); $api->send(); return $this->_getValidationResponse($api); } catch (NetworkError $e) { $logMessage = 'Address validation service returned empty response.'; $this->_logger->warning($logMessage, $this->_context->getMetaData(__CLASS__)); } catch (Exception $e) { $logMessage = 'Unexpected exception from SDK.'; $this->_logger->warning($logMessage, $this->_context->getMetaData(__CLASS__)); $this->_logger->logException($e, $this->_context->getMetaData(__CLASS__, [], $e)); throw $e; } return null; }
/** * Place the order and recurring payment profiles when customer returned from paypal * Until this moment all quote data must be valid * * @param string $token * @param string $shippingMethodCode */ public function place($token, $shippingMethodCode = null) { $this->updateShippingMethod($shippingMethodCode); $isNewCustomer = $this->_prepareQuote(); $this->_ignoreAddressValidation(); $this->_quote->collectTotals(); $this->_getApi(); $payerId = $this->_quote->getPayment()->getAdditionalInformation(self::PAYMENT_INFO_PAYER_ID); $doExpressReply = $this->_api->doExpressCheckout($this->_quote, $token, $payerId); $doAuthorizationReply = $this->_api->doAuthorization($this->_quote); $this->_quote->getPayment()->importData(array_merge($doExpressReply, $doAuthorizationReply)); $service = Mage::getModel('sales/service_quote', $this->_quote); $service->submitAll(); $this->_quote->save(); if ($isNewCustomer) { try { $this->_involveNewCustomer(); } catch (Exception $e) { $this->_logger->logException($e, $this->_context->getMetaData(__CLASS__, [], $e)); } } $order = $service->getOrder(); if (!$order) { return; } switch ($order->getState()) { // Even after placement, paypal can disallow authorize/capture case Mage_Sales_Model_Order::STATE_PENDING_PAYMENT: break; // regular placement, when everything is ok // regular placement, when everything is ok case Mage_Sales_Model_Order::STATE_PROCESSING: case Mage_Sales_Model_Order::STATE_COMPLETE: case Mage_Sales_Model_Order::STATE_PAYMENT_REVIEW: $order->sendNewOrderEmail(); break; } $this->_order = $order; Mage::dispatchEvent('checkout_submit_all_after', array('order' => $order, 'quote' => $this->_quote)); }
/** * Make the API request and handle any exceptions. * @param ApiIBidirectionalApi $api * @return self */ protected function _sendAuthRequest(Api\IBidirectionalApi $api) { try { $api->send(); } catch (Payload\Exception\InvalidPayload $e) { // Invalid payloads cannot be valid - log the error and fail the auth $logData = ['error_message' => $e->getMessage()]; $logMessage = 'Credit card auth payload invalid: {error_message}'; $this->_logger->warning($logMessage, $this->_context->getMetaData(__CLASS__, $logData)); $this->_logger->logException($e, $this->_context->getMetaData(__CLASS__, [], $e)); $this->_failPaymentAuth(self::CREDITCARD_AUTH_FAILED_MESSAGE); } catch (Api\Exception\NetworkError $e) { // Can't accept an auth request that could not be made successfully - log // the error and fail the auth. $logData = ['error_message' => $e->getMessage()]; $logMessage = 'Credit card auth request failed: {error_message}'; $this->_logger->warning($logMessage, $this->_context->getMetaData(__CLASS__, $logData)); $this->_logger->logException($e, $this->_context->getMetaData(__CLASS__, [], $e)); $this->_failPaymentAuth(self::CREDITCARD_AUTH_FAILED_MESSAGE); } return $this; }
/** * Submit the order */ public function placeOrderAction() { try { $requiredAgreements = Mage::helper('checkout')->getRequiredAgreementIds(); if ($requiredAgreements) { $postedAgreements = array_keys($this->getRequest()->getPost('agreement', array())); if (array_diff($requiredAgreements, $postedAgreements)) { Mage::throwException($this->_helper->__('Please agree to all the terms and conditions before placing the order.')); } } $this->_initCheckout(); $this->_checkout->place($this->_initToken()); // prepare session to success or cancellation page $session = $this->_getCheckoutSession(); $session->clearHelperData(); // last successful quote $quoteId = $this->_getQuote()->getId(); $session->setLastQuoteId($quoteId)->setLastSuccessQuoteId($quoteId); // an order may be created $order = $this->_checkout->getOrder(); if ($order) { $session->setLastOrderId($order->getId())->setLastRealOrderId($order->getIncrementId()); } $this->_initToken(false); // no need in token anymore $this->_redirect('checkout/onepage/success'); return; } catch (Mage_Core_Exception $e) { Mage::helper('checkout')->sendPaymentFailedEmail($this->_getQuote(), $e->getMessage()); $this->_getSession()->addError($e->getMessage()); $this->_redirect('*/*/review'); } catch (Exception $e) { Mage::helper('checkout')->sendPaymentFailedEmail($this->_getQuote(), $this->__('Unable to place the order.')); $this->_getSession()->addError($this->__('Unable to place the order.')); $this->_logger->logException($e, $this->_context->getMetaData(__CLASS__, [], $e)); $this->_redirect('*/*/review'); } }
/** * Save the credit memo and the related order and invoice * * @param Mage_Sales_Model_Order_Creditmemo $creditmemo */ protected function _saveCreditMemo(Mage_Sales_Model_Order_Creditmemo $creditmemo) { $creditmemo = $this->_fixupTotals($creditmemo); $logData = ['order_id' => $creditmemo->getOrderId()]; try { $creditmemo->register(); } catch (Mage_Core_Exception $e) { $logMessage = 'Could not refund order {order_id}'; $this->_logger->warning($logMessage, $this->_context->getMetaData(__CLASS__, $logData)); $this->_logger->logException($e, $this->_context->getMetaData(__CLASS__, [], $e)); } $transactionSave = Mage::getModel('core/resource_transaction')->addObject($creditmemo)->addObject($creditmemo->getOrder()); if ($creditmemo->getInvoice()) { $transactionSave->addObject($creditmemo->getInvoice()); } try { $transactionSave->save(); } catch (Exception $e) { $logMessage = 'Could not save credit memo for order {order_id}'; $this->_logger->warning($logMessage, $this->_context->getMetaData(__CLASS__, $logData)); $this->_logger->logException($e, $this->_context->getMetaData(__CLASS__, [], $e)); } }