public function saveOrderAction()

in eCommerce platforms/Adobe Magento/src/code/local/Microsoft/Dfp/controllers/Checkout/OnepageController.php [55:182]


	public function saveOrderAction()
	{
		if (!$this->_validateFormKey()) {
			$this->_redirect('*/*');
			return;
		}

		if ($this->_expireAjax()) {
			return;
		}

		$result = array();
		try {
			$requiredAgreements = Mage::helper('checkout')->getRequiredAgreementIds();
			if ($requiredAgreements) {
				$postedAgreements = array_keys($this->getRequest()->getPost('agreement', array()));
				$diff = array_diff($requiredAgreements, $postedAgreements);
				if ($diff) {
					$result['success'] = false;
					$result['error'] = true;
					$result['error_messages'] = $this->__('Please agree to all the terms and conditions before placing the order.');
					$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
					return;
				}
			}

			$data = $this->getRequest()->getPost('payment', array());
			if ($data) {
				$data['checks'] = Mage_Payment_Model_Method_Abstract::CHECK_USE_CHECKOUT
					| Mage_Payment_Model_Method_Abstract::CHECK_USE_FOR_COUNTRY
					| Mage_Payment_Model_Method_Abstract::CHECK_USE_FOR_CURRENCY
					| Mage_Payment_Model_Method_Abstract::CHECK_ORDER_TOTAL_MIN_MAX
					| Mage_Payment_Model_Method_Abstract::CHECK_ZERO_TOTAL;
				$this->getOnepage()->getQuote()->getPayment()->importData($data);
			}

			$this->getOnepage()->saveOrder();

			// *********************************************** DFP Changes START *************************************//
			try {
				$correlationId = $this->dfp->GUID();
				$order = Mage::getModel('sales/order')->load($this->getOnepage()->getQuote()->getId(), 'quote_id');
				$optionArray = Mage::getModel('dfp/config_source_assessmenttypes')->toOptionArray();
				$assessmentType = $optionArray[Mage::getStoreConfig('dfp/general/assessmenttype')]['label'];
				$this->dfp->log('************** Invoking Purchase API - START ************* ');
				$merchantRuleDecision = $this->invokePurchaseDFP($order, $correlationId, $assessmentType);
				$this->dfp->log('************** Invoking Purchase API - END ************* ');

				if (
					strtolower($merchantRuleDecision) == strtolower(Microsoft_Dfp_Helper_Data::MERCHANT_DECISION_REJECT) &&
					strtolower($assessmentType) == strtolower(Microsoft_Dfp_Helper_Data::ASSESSMENT_TYPE_PROTECT)
				) {
					$this->cancelOrder('Order is rejected by DFP.');
					$result['redirect'] = Mage::getUrl('checkout/onepage/failure');
					$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));

					return;
				}

				try {
					$this->dfp->log('************** Invoking Bank Event API - START ************* ');
					$this->invokeBankEventDFP("Auth", $correlationId);
					$this->invokeBankEventDFP("Charge", $correlationId);
					$this->dfp->log('************** Invoking Bank Event API - END ************* ');
				} catch (exception $e) {
					$this->dfp->log($e->getMessage());
				}
			} catch (exception $e) {
				$this->dfp->log($e->getMessage());
			}

			//DFP failing should not break checkout.
			$redirectUrl = $this->getOnepage()->getCheckout()->getRedirectUrl();
			$result['success'] = true;
			$result['error']   = false;

			// *********************************************** DFP Changes END *************************************// 
		} catch (Mage_Payment_Model_Info_Exception $e) {
			$message = $e->getMessage();
			if (!empty($message)) {
				$result['error_messages'] = $message;
			}
			$result['goto_section'] = 'payment';
			$result['update_section'] = array(
				'name' => 'payment-method',
				'html' => $this->_getPaymentMethodsHtml()
			);
		} catch (Mage_Core_Exception $e) {
			Mage::logException($e);
			Mage::helper('checkout')->sendPaymentFailedEmail($this->getOnepage()->getQuote(), $e->getMessage());
			$result['success'] = false;
			$result['error'] = true;
			$result['error_messages'] = $e->getMessage();

			$gotoSection = $this->getOnepage()->getCheckout()->getGotoSection();
			if ($gotoSection) {
				$result['goto_section'] = $gotoSection;
				$this->getOnepage()->getCheckout()->setGotoSection(null);
			}
			$updateSection = $this->getOnepage()->getCheckout()->getUpdateSection();
			if ($updateSection) {
				if (isset($this->_sectionUpdateFunctions[$updateSection])) {
					$updateSectionFunction = $this->_sectionUpdateFunctions[$updateSection];
					$result['update_section'] = array(
						'name' => $updateSection,
						'html' => $this->$updateSectionFunction()
					);
				}
				$this->getOnepage()->getCheckout()->setUpdateSection(null);
			}
		} catch (Exception $e) {
			Mage::logException($e);
			Mage::helper('checkout')->sendPaymentFailedEmail($this->getOnepage()->getQuote(), $e->getMessage());
			$result['success']  = false;
			$result['error']    = true;
			$result['error_messages'] = $this->__('There was an error processing your order. Please contact us or try again later.');
		}
		$this->getOnepage()->getQuote()->save();
		/**
		 * when there is redirect to third party, we don't want to save order yet.
		 * we will save the order in return action.
		 */
		if (isset($redirectUrl)) {
			$result['redirect'] = $redirectUrl;
		}

		$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
	}