Ejemplo n.º 1
0
 /**
  *
  * @param string $message
  * @param int $code
  * @throws Creativestyle_CheckoutByAmazon_Exception
  */
 public function throwException($message, $code = 0, $params = array())
 {
     $exception = Mage::exception('Creativestyle_CheckoutByAmazon', $message, $code);
     if (!$this->isExceptionLoggingLocked()) {
         Creativestyle_CheckoutByAmazon_Model_Logger::logException($message, $code ? $code : 0, $exception->getTraceAsString(), isset($params['area']) ? $params['area'] : 'General', isset($params['request_id']) ? $params['request_id'] : null, isset($params['orders']) ? $params['orders'] : null);
     }
     throw $exception;
 }
 public function saveOrderAction()
 {
     if ($this->_expireAjax()) {
         return;
     }
     $result = array();
     try {
         Mage::helper('checkoutbyamazon')->lockExceptionLogging();
         if ($requiredAgreements = Mage::helper('checkout')->getRequiredAgreementIds()) {
             $postedAgreements = array_keys($this->getRequest()->getPost('agreement', array()));
             if ($diff = array_diff($requiredAgreements, $postedAgreements)) {
                 $result['success'] = false;
                 $result['error'] = true;
                 $result['error_messages'] = $this->__('Please agree to all Terms and Conditions before placing the order.');
                 $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
                 return;
             }
         }
         Mage::getSingleton('customer/session')->getCustomer()->setId(null)->setGroupId(Mage_Customer_Model_Group::NOT_LOGGED_IN_ID)->setEmail($this->_getOnepage()->getQuote()->getBillingAddress()->getEmail());
         $this->_getOnepage()->getQuote()->getPayment()->importData($this->_getPaymentMethod());
         $this->_getOnepage()->getQuote()->setCustomerIsGuest(true)->save();
         $this->_getOnepage()->saveOrder();
         $lastOrderId = $this->_getOnepage()->getCheckout()->getLastOrderId();
         $order = Mage::getModel('sales/order')->load($lastOrderId);
         // unsuccesful order save
         if (!$order->getId()) {
             Mage::helper('checkoutbyamazon')->throwException('Cannot load order with given ID', 0, array('area' => 'Inline Checkout'));
         }
         Mage::helper('checkoutbyamazon')->setAmazonProcessingOrders($order);
         $purchaseContractId = $this->getRequest()->getParam('purchaseContractId', null);
         $itemList = Mage::getModel('checkoutbyamazon/api_model_checkout_itemList');
         foreach ($order->getItemsCollection() as $item) {
             $productId = $this->_getProductId($item);
             if (!$productId) {
                 continue;
             }
             $product = Mage::getModel('catalog/product')->load($productId);
             $itemUnitPrice = $item->getBasePriceInclTax();
             if (!$itemUnitPrice) {
                 $itemUnitPrice = ($item->getBaseRowTotal() + $item->getBaseTaxAmount()) / $item->getQtyOrdered();
             }
             $amazonItem = Mage::getModel('checkoutbyamazon/api_model_checkout_purchaseItem', array('MerchantItemId' => $item->getId(), 'SKU' => Mage::helper('checkoutbyamazon')->sanitizeSku($item->getSku()), 'Title' => $item->getName(), 'UnitPrice' => array('Amount' => Mage::helper('checkoutbyamazon')->sanitizePrice($itemUnitPrice)), 'Quantity' => round($item->getQtyOrdered())));
             $amazonItem->setItemTax($this->_getAmazonPrice($item->getBaseTaxAmount() / $item->getQtyOrdered()));
             $amazonItem->setItemShipping($this->_getAmazonPrice(0));
             $itemList->addItem($amazonItem);
             unset($product);
         }
         $amazonCharges = Mage::getModel('checkoutbyamazon/api_model_checkout_charges');
         if (abs($order->getBaseDiscountAmount()) > 0) {
             $amazonPromotion = Mage::getModel('checkoutbyamazon/api_model_checkout_promotion', array('PromotionId' => trim($order->getCouponCode()) ? trim($order->getCouponCode()) : Mage::helper('checkoutbyamazon')->__('Discount'), 'Description' => trim($order->getDiscountDescription()) ? trim($order->getDiscountDescription()) : Mage::helper('checkoutbyamazon')->__('Discount'), 'Discount' => array('Amount' => abs($order->getBaseDiscountAmount()))));
             $promotionList = Mage::getModel('checkoutbyamazon/api_model_checkout_promotionList');
             $promotionList->setPromotion(array($amazonPromotion));
             $amazonCharges->setPromotions($promotionList);
         }
         $amazonCharges->setTax($this->_getAmazonPrice($order->getBaseTaxAmount()));
         $amazonCharges->setShipping($this->_getAmazonPrice($order->getBaseShippingAmount() + $order->getBaseShippingTaxAmount()));
         // send items data to Amazon
         $setItemsResult = $this->_getAmazonCheckoutApi()->setItems($purchaseContractId, $itemList);
         if (!$setItemsResult) {
             Mage::helper('checkoutbyamazon')->throwException('Error when performing SetItems call', 0, array('area' => 'Inline Checkout'));
         }
         $setContractChargesResult = $this->_getAmazonCheckoutApi()->setContractCharges($purchaseContractId, $amazonCharges);
         if (!$setContractChargesResult) {
             Mage::helper('checkoutbyamazon')->throwException('Error when performing SetContractCharges call', 0, array('area' => 'Inline Checkout'));
         }
         $amazonOrderIdList = $this->_getAmazonCheckoutApi()->completeOrder($purchaseContractId);
         if (!is_null($amazonOrderIdList)) {
             if (is_array($amazonOrderIdList)) {
                 $amazonOrderId = implode(',', $amazonOrderIdList);
             } else {
                 $amazonOrderId = $amazonOrderIdList;
             }
             $order->getPayment()->setLastTransId($amazonOrderId)->save();
         } else {
             Mage::helper('checkoutbyamazon')->throwException('Error when performing CompletePurchaseContract call', 0, array('area' => 'Inline Checkout'));
         }
         $this->_getOnepage()->getCheckout()->setAmazonPurchaseContractId(null);
         $result['success'] = true;
         $result['error'] = false;
         Mage::helper('checkoutbyamazon')->unsetAmazonProcessingOrders();
         Mage::helper('checkoutbyamazon')->unlockExceptionLogging();
     } catch (Mage_Core_Exception $e) {
         Mage::helper('checkoutbyamazon')->unlockExceptionLogging();
         if (isset($order) && is_object($order) && is_callable(array($order, 'getId')) && $order->getId()) {
             $order->cancel()->save();
         }
         Creativestyle_CheckoutByAmazon_Model_Logger::logException($e->getMessage(), $e->getCode(), $e->getTraceAsString(), 'Inline Checkout', null, isset($order) && is_object($order) && is_callable(array($order, 'getId')) && $order->getId() ? $order->getId() : null);
         $result['success'] = false;
         $result['error'] = true;
         $result['error_messages'] = $e->getMessage();
         if ($gotoSection = $this->_getOnepage()->getCheckout()->getGotoSection()) {
             $result['goto_section'] = $gotoSection;
             $this->_getOnepage()->getCheckout()->setGotoSection(null);
         }
         if ($updateSection = $this->_getOnepage()->getCheckout()->getUpdateSection()) {
             if (isset($this->_sectionUpdateFunctions[$updateSection])) {
                 $updateSectionFunction = $this->_sectionUpdateFunctions[$updateSection];
                 $result['update_section'] = array('name' => $updateSection, 'html' => $this->{$updateSectionFunction}());
             }
             $this->_getOnepage()->getCheckout()->setUpdateSection(null);
         }
         $this->_getOnepage()->getQuote()->save();
     } catch (Exception $e) {
         Mage::helper('checkoutbyamazon')->unlockExceptionLogging();
         if (isset($order) && is_object($order) && is_callable(array($order, 'getId')) && $order->getId()) {
             $order->cancel()->save();
         }
         Creativestyle_CheckoutByAmazon_Model_Logger::logException($e->getMessage(), $e->getCode(), $e->getTraceAsString(), 'Inline Checkout', null, isset($order) && is_object($order) && is_callable(array($order, 'getId')) && $order->getId() ? $order->getId() : null);
         $result['success'] = false;
         $result['error'] = true;
         $result['error_messages'] = Mage::helper('checkout')->__('There was an error processing your order. Please contact us or try again later.');
         $this->_getOnepage()->getQuote()->save();
     }
     $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
 }
Ejemplo n.º 3
0
 public function sendRefundNotify($payment)
 {
     $result = null;
     $order = $payment->getOrder();
     $creditmemo = $payment->getCreditmemo();
     if ($this->_isAmazonPaymentMethod($order->getPayment()->getMethod()) && $order->getPayment()->getLastTransId()) {
         $filePath = $this->_queueDir['refund'] . DS . $this->_getTimestamp() . '_order_' . $order->getIncrementId() . '.xml';
         $envelope = new SimpleXMLElement('<AmazonEnvelope></AmazonEnvelope>');
         $envelope->Header->DocumentVersion = '1.01';
         $envelope->Header->MerchantIdentifier = self::getConfigData('merchant_id');
         $envelope->MessageType = 'OrderAdjustment';
         $envelope->Message[0]->MessageID = 1;
         $envelope->Message[0]->OrderAdjustment->AmazonOrderID = $payment->getLastTransId();
         $envelope->Message[0]->OrderAdjustment->MerchantOrderID = $order->getIncrementId();
         $i = 0;
         foreach ($creditmemo->getAllItems() as $item) {
             $j = 0;
             $envelope->Message[0]->OrderAdjustment->AdjustedItem[$i]->MerchantOrderItemID = $item->getId();
             $envelope->Message[0]->OrderAdjustment->AdjustedItem[$i]->AdjustmentReason = Creativestyle_CheckoutByAmazon_Model_Api_Model_Marketplace_Feeds_Abstract::ADJUSTMENT_REASON_CUSTOMER_RETURN;
             $envelope->Message[0]->OrderAdjustment->AdjustedItem[$i]->ItemPriceAdjustments->Component[$j]->Type = 'Principal';
             $principalAmount = $envelope->Message[0]->OrderAdjustment->AdjustedItem[$i]->ItemPriceAdjustments->Component[$j]->addChild('Amount', Mage::helper('checkoutbyamazon')->sanitizePrice($item->getBaseRowTotalInclTax() - $item->getBaseDiscountAmount()));
             $principalAmount->addAttribute('currency', $creditmemo->getBaseCurrencyCode());
             if (!$i) {
                 if ($creditmemo->getBaseShippingAmount() > 0) {
                     $envelope->Message[0]->OrderAdjustment->AdjustedItem[$i]->ItemPriceAdjustments->Component[++$j]->Type = 'Shipping';
                     $shippingAmount = $envelope->Message[0]->OrderAdjustment->AdjustedItem[$i]->ItemPriceAdjustments->Component[$j]->addChild('Amount', Mage::helper('checkoutbyamazon')->sanitizePrice($creditmemo->getBaseShippingAmount()));
                     $shippingAmount->addAttribute('currency', $creditmemo->getBaseCurrencyCode());
                 }
             }
             $i++;
         }
         $fileSaveHandle = fopen($filePath, 'a');
         fwrite($fileSaveHandle, trim($envelope->asXML()));
         fclose($fileSaveHandle);
         if (!self::getConfigData('feed_batching')) {
             $result = $this->_submitFeed(Creativestyle_CheckoutByAmazon_Model_Api_Model_Marketplace_Feeds_Abstract::FEED_TYPE_POST_PAYMENT_ADJUSTMENT_DATA, $filePath);
             Creativestyle_CheckoutByAmazon_Model_Logger::logFeed(Creativestyle_CheckoutByAmazon_Model_Api_Model_Marketplace_Feeds_Abstract::FEED_TYPE_POST_PAYMENT_ADJUSTMENT_DATA, $result, $order, trim($envelope->asXML()), $order->getStore()->getId());
         }
     }
     return $result;
 }
Ejemplo n.º 4
0
 public function logApiCall($observer)
 {
     $requestMethod = 'GET';
     $host = $observer->getEvent()->getHost();
     $action = $observer->getEvent()->getAction();
     $headers = $observer->getEvent()->getHeaders();
     $get = $observer->getEvent()->getGet();
     $post = $observer->getEvent()->getPost();
     $file = $observer->getEvent()->getFile();
     $responseCode = $observer->getEvent()->getResponseCode();
     $response = $observer->getEvent()->getResponse();
     if ($file) {
         $requestMethod = 'FILE';
     } elseif ($post) {
         $requestMethod = 'POST';
     }
     Creativestyle_CheckoutByAmazon_Model_Logger::logApiCall($host, $action, null, $requestMethod, $headers, $get, $post, $file, $responseCode, $response);
     return $this;
 }
 public function iopnAction()
 {
     if (Mage::helper('checkoutbyamazon')->getConfigData('iopn_active')) {
         $notificationType = $this->getRequest()->getPost('NotificationType', null);
         try {
             $params = $this->_getIopnRequestParams();
             switch ($notificationType) {
                 case 'NewOrderNotification':
                     $result = Mage::getModel('checkoutbyamazon/api_iopn')->newOrderNotification($params);
                     if (isset($result['code']) && $result['code'] == 200) {
                         $order = $result['order'];
                         $this->_getAmazonManager()->newOrder($order);
                         $amazonOrderId = $order->issetAmazonOrderID() ? $order->getAmazonOrderID() : null;
                         Creativestyle_CheckoutByAmazon_Model_Logger::logIopn($notificationType, isset($params['UUID']) ? $params['UUID'] : '', $amazonOrderId ? $this->_getOrderId($amazonOrderId) : null, isset($params['NotificationData']) ? $params['NotificationData'] : '', 200);
                         return $this->_sendResponse(200);
                     } else {
                         Mage::helper('checkoutbyamazon')->throwException(isset($result['message']) ? $result['message'] : 'Internal error', isset($result['code']) ? $result['code'] : 500, array('area' => 'Amazon IOPN'));
                     }
                     break;
                 case 'OrderCancelledNotification':
                     $result = Mage::getModel('checkoutbyamazon/api_iopn')->orderCancelledNotification($params);
                     if (isset($result['code']) && $result['code'] == 200) {
                         $order = $result['order'];
                         $this->_getAmazonManager()->cancelOrder($order);
                         $amazonOrderId = $order->issetAmazonOrderID() ? $order->getAmazonOrderID() : null;
                         Creativestyle_CheckoutByAmazon_Model_Logger::logIopn($notificationType, isset($params['UUID']) ? $params['UUID'] : '', $amazonOrderId ? $this->_getOrderId($amazonOrderId) : null, isset($params['NotificationData']) ? $params['NotificationData'] : '', 200);
                         return $this->_sendResponse(200);
                     } else {
                         Mage::helper('checkoutbyamazon')->throwException(isset($result['message']) ? $result['message'] : 'Internal error', isset($result['code']) ? $result['code'] : 500, array('area' => 'Amazon IOPN'));
                     }
                     break;
                 case 'OrderReadyToShipNotification':
                     $result = Mage::getModel('checkoutbyamazon/api_iopn')->orderReadyToShipNotification($params);
                     if (isset($result['code']) && $result['code'] == 200) {
                         $order = $result['order'];
                         $this->_getAmazonManager()->readyToShipOrder($order);
                         $amazonOrderId = $order->issetAmazonOrderID() ? $order->getAmazonOrderID() : null;
                         Creativestyle_CheckoutByAmazon_Model_Logger::logIopn($notificationType, isset($params['UUID']) ? $params['UUID'] : '', $amazonOrderId ? $this->_getOrderId($amazonOrderId) : null, isset($params['NotificationData']) ? $params['NotificationData'] : '', 200);
                         return $this->_sendResponse(200);
                     } else {
                         Mage::helper('checkoutbyamazon')->throwException(isset($result['message']) ? $result['message'] : 'Internal error', isset($result['code']) ? $result['code'] : 500, array('area' => 'Amazon IOPN'));
                     }
                     break;
                 default:
                     $this->norouteAction();
                     return;
             }
         } catch (Exception $e) {
             $amazonOrderId = null;
             if (isset($result['order']) && is_object($result['order'])) {
                 $order = $result['order'];
                 $amazonOrderId = $order->issetAmazonOrderID() ? $order->getAmazonOrderID() : null;
             }
             Creativestyle_CheckoutByAmazon_Model_Logger::logIopn($notificationType, isset($params['UUID']) ? $params['UUID'] : '', $amazonOrderId ? $this->_getOrderId($amazonOrderId) : null, isset($params['NotificationData']) ? $params['NotificationData'] : '', $e->getCode());
             if (!$e instanceof Creativestyle_CheckoutByAmazon_Exception) {
                 Creativestyle_CheckoutByAmazon_Model_Logger::logException($e->getMessage(), $e->getCode(), $e->getTraceAsString(), 'Amazon IOPN', isset($params['UUID']) ? $params['UUID'] : '', isset($result['order']) ? $this->_getOrderId($result['order']) : null);
             }
             $this->_sendResponse($e->getCode(), $e->getMessage());
         }
     } else {
         $this->norouteAction();
         return;
     }
 }