Skip to content

Commit

Permalink
Merge pull request #266 from magento-mpi/MAGETWO-36037
Browse files Browse the repository at this point in the history
[MPI] Bugs
  • Loading branch information
vpelipenko committed May 5, 2015
2 parents 99dca3e + 7dc6ee9 commit 10f06c6
Show file tree
Hide file tree
Showing 2 changed files with 371 additions and 24 deletions.
73 changes: 49 additions & 24 deletions app/code/Magento/Checkout/Controller/Onepage/SaveOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
namespace Magento\Checkout\Controller\Onepage;

use Magento\Framework\Object;
use Magento\Framework\Exception\PaymentException;

class SaveOrder extends \Magento\Checkout\Controller\Onepage
Expand All @@ -26,16 +27,17 @@ public function execute()
return $this->_ajaxRedirectResponse();
}

$result = [];
$result = new Object();
try {
$agreementsValidator = $this->_objectManager->get('Magento\Checkout\Model\Agreements\AgreementsValidator');
if (!$agreementsValidator->isValid(array_keys($this->getRequest()->getPost('agreement', [])))) {
$result['success'] = false;
$result['error'] = true;
$result['error_messages'] = __(
'Please agree to all the terms and conditions before placing the order.'
$result->setData('success', false);
$result->setData('error', true);
$result->setData(
'error_messages',
__('Please agree to all the terms and conditions before placing the order.')
);
return $this->resultJsonFactory->create()->setData($result);
return $this->resultJsonFactory->create()->setData($result->getData());
}

$data = $this->getRequest()->getPost('payment', []);
Expand All @@ -54,55 +56,78 @@ public function execute()
$this->getOnepage()->saveOrder();

$redirectUrl = $this->getOnepage()->getCheckout()->getRedirectUrl();
$result['success'] = true;
$result['error'] = false;
$result->setData('success', true);
$result->setData('error', false);
} catch (PaymentException $e) {
$message = $e->getMessage();
if (!empty($message)) {
$result['error_messages'] = $message;
$result->setData('error_messages', $message);
}
$result['goto_section'] = 'payment';
$result['update_section'] = ['name' => 'payment-method', 'html' => $this->_getPaymentMethodsHtml()];
$result->setData('goto_section', 'payment');
$result->setData(
'update_section',
[
'name' => 'payment-method',
'html' => $this->_getPaymentMethodsHtml()
]
);
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$this->_objectManager->get('Psr\Log\LoggerInterface')->critical($e);
$this->_objectManager->get('Magento\Checkout\Helper\Data')
->sendPaymentFailedEmail($this->getOnepage()->getQuote(), $e->getMessage());
$result['success'] = false;
$result['error'] = true;
$result['error_messages'] = $e->getMessage();
$result->setData(
'success',
false
);
$result->setData('error', true);
$result->setData('error_messages', $e->getMessage());
$gotoSection = $this->getOnepage()->getCheckout()->getGotoSection();
if ($gotoSection) {
$result['goto_section'] = $gotoSection;
$result->setData('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'] = [
'name' => $updateSection,
'html' => $this->{$updateSectionFunction}(),
];
$result->setData(
'update_section',
[
'name' => $updateSection,
'html' => $this->{$updateSectionFunction}(),
]
);
}
$this->getOnepage()->getCheckout()->setUpdateSection(null);
}
} catch (\Exception $e) {
$this->_objectManager->get('Psr\Log\LoggerInterface')->critical($e);
$this->_objectManager->get('Magento\Checkout\Helper\Data')
->sendPaymentFailedEmail($this->getOnepage()->getQuote(), $e->getMessage());
$result['success'] = false;
$result['error'] = true;
$result['error_messages'] = __('Something went wrong processing your order. Please try again later.');
$result->setData('success', false);
$result->setData('error', true);
$result->setData(
'error_messages',
__('Something went wrong processing your order. Please try again later.')
);
}
/**
* 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;
$result->setData('redirect', $redirectUrl);
}

return $this->resultJsonFactory->create()->setData($result);
$this->_eventManager->dispatch(
'checkout_controller_onepage_saveOrder',
[
'result' => $result,
'action' => $this
]
);

return $this->resultJsonFactory->create()->setData($result->getData());
}
}
Loading

0 comments on commit 10f06c6

Please sign in to comment.