Skip to content

Commit

Permalink
Merge pull request #3508 from magento-mpi/port-0412
Browse files Browse the repository at this point in the history
[MPI]-port-0412
  • Loading branch information
viktym authored Dec 12, 2018
2 parents 5a298ad + bd95e17 commit 3ff0161
Show file tree
Hide file tree
Showing 19 changed files with 734 additions and 146 deletions.
9 changes: 7 additions & 2 deletions app/code/Magento/Braintree/Controller/Paypal/PlaceOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Magento\Braintree\Model\Paypal\Helper;
use Magento\Checkout\Model\Session;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Exception\LocalizedException;
Expand All @@ -17,7 +18,7 @@
/**
* Class PlaceOrder
*/
class PlaceOrder extends AbstractAction
class PlaceOrder extends AbstractAction implements HttpPostActionInterface
{
/**
* @var Helper\OrderPlace
Expand Down Expand Up @@ -54,6 +55,7 @@ public function __construct(

/**
* @inheritdoc
*
* @throws LocalizedException
*/
public function execute()
Expand All @@ -71,7 +73,10 @@ public function execute()
return $resultRedirect->setPath('checkout/onepage/success', ['_secure' => true]);
} catch (\Exception $e) {
$this->logger->critical($e);
$this->messageManager->addExceptionMessage($e, $e->getMessage());
$this->messageManager->addExceptionMessage(
$e,
'The order #' . $quote->getReservedOrderId() . ' cannot be processed.'
);
}

return $resultRedirect->setPath('checkout/cart', ['_secure' => true]);
Expand Down
30 changes: 21 additions & 9 deletions app/code/Magento/Braintree/Model/Paypal/Helper/OrderPlace.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@

namespace Magento\Braintree\Model\Paypal\Helper;

use Magento\Quote\Model\Quote;
use Magento\Braintree\Model\Paypal\OrderCancellationService;
use Magento\Checkout\Api\AgreementsValidatorInterface;
use Magento\Checkout\Helper\Data;
use Magento\Checkout\Model\Type\Onepage;
use Magento\Customer\Model\Group;
use Magento\Customer\Model\Session;
use Magento\Checkout\Model\Type\Onepage;
use Magento\Quote\Api\CartManagementInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Checkout\Api\AgreementsValidatorInterface;
use Magento\Quote\Api\CartManagementInterface;
use Magento\Quote\Model\Quote;

/**
* Class OrderPlace
Expand Down Expand Up @@ -42,23 +43,29 @@ class OrderPlace extends AbstractHelper
private $checkoutHelper;

/**
* Constructor
*
* @var OrderCancellationService
*/
private $orderCancellationService;

/**
* @param CartManagementInterface $cartManagement
* @param AgreementsValidatorInterface $agreementsValidator
* @param Session $customerSession
* @param Data $checkoutHelper
* @param OrderCancellationService $orderCancellationService
*/
public function __construct(
CartManagementInterface $cartManagement,
AgreementsValidatorInterface $agreementsValidator,
Session $customerSession,
Data $checkoutHelper
Data $checkoutHelper,
OrderCancellationService $orderCancellationService
) {
$this->cartManagement = $cartManagement;
$this->agreementsValidator = $agreementsValidator;
$this->customerSession = $customerSession;
$this->checkoutHelper = $checkoutHelper;
$this->orderCancellationService = $orderCancellationService;
}

/**
Expand All @@ -67,7 +74,7 @@ public function __construct(
* @param Quote $quote
* @param array $agreement
* @return void
* @throws LocalizedException
* @throws \Exception
*/
public function execute(Quote $quote, array $agreement)
{
Expand All @@ -84,7 +91,12 @@ public function execute(Quote $quote, array $agreement)
$this->disabledQuoteAddressValidation($quote);

$quote->collectTotals();
$this->cartManagement->placeOrder($quote->getId());
try {
$this->cartManagement->placeOrder($quote->getId());
} catch (\Exception $e) {
$this->orderCancellationService->execute($quote->getReservedOrderId());
throw $e;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Braintree\Model\Paypal;

use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\OrderRepositoryInterface;

/**
* The service to cancel an order and void authorization transaction.
*/
class OrderCancellationService
{
/**
* @var OrderRepositoryInterface
*/
private $orderRepository;

/**
* @var SearchCriteriaBuilder
*/
private $searchCriteriaBuilder;

/**
* @param SearchCriteriaBuilder $searchCriteriaBuilder
* @param OrderRepositoryInterface $orderRepository
*/
public function __construct(
SearchCriteriaBuilder $searchCriteriaBuilder,
OrderRepositoryInterface $orderRepository
) {
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->orderRepository = $orderRepository;
}

/**
* Cancels an order and authorization transaction.
*
* @param string $incrementId
* @return bool
*/
public function execute(string $incrementId): bool
{
$order = $this->getOrder($incrementId);
if ($order === null) {
return false;
}

// `\Magento\Sales\Model\Service\OrderService::cancel` cannot be used for cancellation as the service uses
// the order repository with outdated payment method instance (ex. contains Vault instead of Braintree)
$order->cancel();
$this->orderRepository->save($order);
return true;
}

/**
* Gets order by increment ID.
*
* @param string $incrementId
* @return OrderInterface|null
*/
private function getOrder(string $incrementId)
{
$searchCriteria = $this->searchCriteriaBuilder->addFilter(OrderInterface::INCREMENT_ID, $incrementId)
->create();

$items = $this->orderRepository->getList($searchCriteria)
->getItems();

return array_pop($items);
}
}
81 changes: 81 additions & 0 deletions app/code/Magento/Braintree/Plugin/OrderCancellation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Braintree\Plugin;

use Magento\Braintree\Model\Paypal\OrderCancellationService;
use Magento\Braintree\Model\Ui\ConfigProvider;
use Magento\Braintree\Model\Ui\PayPal\ConfigProvider as PayPalConfigProvider;
use Magento\Framework\Exception\LocalizedException;
use Magento\Quote\Api\CartManagementInterface;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Api\Data\PaymentInterface;

/**
* Cancels an order and an authorization transaction.
*/
class OrderCancellation
{
/**
* @var OrderCancellationService
*/
private $orderCancellationService;

/**
* @var CartRepositoryInterface
*/
private $quoteRepository;

/**
* @param OrderCancellationService $orderCancellationService
* @param CartRepositoryInterface $quoteRepository
*/
public function __construct(
OrderCancellationService $orderCancellationService,
CartRepositoryInterface $quoteRepository
) {
$this->orderCancellationService = $orderCancellationService;
$this->quoteRepository = $quoteRepository;
}

/**
* Cancels an order if an exception occurs during the order creation.
*
* @param CartManagementInterface $subject
* @param \Closure $proceed
* @param int $cartId
* @param PaymentInterface $payment
* @return int
* @throws \Exception
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function aroundPlaceOrder(
CartManagementInterface $subject,
\Closure $proceed,
$cartId,
PaymentInterface $payment = null
) {
try {
return $proceed($cartId, $payment);
} catch (\Exception $e) {
$quote = $this->quoteRepository->get((int) $cartId);
$payment = $quote->getPayment();
$paymentCodes = [
ConfigProvider::CODE,
ConfigProvider::CC_VAULT_CODE,
PayPalConfigProvider::PAYPAL_CODE,
PayPalConfigProvider::PAYPAL_VAULT_CODE
];
if (in_array($payment->getMethod(), $paymentCodes)) {
$incrementId = $quote->getReservedOrderId();
$this->orderCancellationService->execute($incrementId);
}

throw $e;
}
}
}
Loading

0 comments on commit 3ff0161

Please sign in to comment.