Skip to content

Commit

Permalink
Merge pull request magento#2656 from magento-tsg/2.3-develop-pr21
Browse files Browse the repository at this point in the history
[TSG] Upporting for 2.3 (pr21) (2.3.0)
  • Loading branch information
Alexander Akimov authored Jun 6, 2018
2 parents c62d922 + 0687df0 commit d3a849c
Show file tree
Hide file tree
Showing 119 changed files with 2,685 additions and 31,608 deletions.
75 changes: 57 additions & 18 deletions app/code/Magento/Authorizenet/Model/Directpost.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
*/
namespace Magento\Authorizenet\Model;

use Magento\Framework\HTTP\ZendClientFactory;
use Magento\Framework\App\ObjectManager;
use Magento\Payment\Model\Method\ConfigInterface;
use Magento\Payment\Model\Method\TransparentInterface;
use Magento\Sales\Model\Order\Email\Sender\OrderSender;

/**
* Authorize.net DirectPost payment method model.
Expand Down Expand Up @@ -102,7 +101,7 @@ class Directpost extends \Magento\Authorizenet\Model\Authorizenet implements Tra
protected $response;

/**
* @var OrderSender
* @var \Magento\Sales\Model\Order\Email\Sender\OrderSender
*/
protected $orderSender;

Expand All @@ -123,6 +122,16 @@ class Directpost extends \Magento\Authorizenet\Model\Authorizenet implements Tra
*/
private $psrLogger;

/**
* @var \Magento\Sales\Api\PaymentFailuresInterface
*/
private $paymentFailures;

/**
* @var \Magento\Sales\Model\Order
*/
private $order;

/**
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
Expand All @@ -134,18 +143,19 @@ class Directpost extends \Magento\Authorizenet\Model\Authorizenet implements Tra
* @param \Magento\Framework\Module\ModuleListInterface $moduleList
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
* @param \Magento\Authorizenet\Helper\Data $dataHelper
* @param Directpost\Request\Factory $requestFactory
* @param Directpost\Response\Factory $responseFactory
* @param \Magento\Authorizenet\Model\Directpost\Request\Factory $requestFactory
* @param \Magento\Authorizenet\Model\Directpost\Response\Factory $responseFactory
* @param \Magento\Authorizenet\Model\TransactionService $transactionService
* @param \Magento\Framework\HTTP\ZendClientFactory $httpClientFactory
* @param \Magento\Sales\Model\OrderFactory $orderFactory
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
* @param OrderSender $orderSender
* @param \Magento\Sales\Model\Order\Email\Sender\OrderSender $orderSender
* @param \Magento\Sales\Api\TransactionRepositoryInterface $transactionRepository
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
* @param array $data
* @param \Magento\Sales\Api\PaymentFailuresInterface|null $paymentFailures
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
Expand All @@ -161,16 +171,17 @@ public function __construct(
\Magento\Authorizenet\Helper\Data $dataHelper,
\Magento\Authorizenet\Model\Directpost\Request\Factory $requestFactory,
\Magento\Authorizenet\Model\Directpost\Response\Factory $responseFactory,
TransactionService $transactionService,
ZendClientFactory $httpClientFactory,
\Magento\Authorizenet\Model\TransactionService $transactionService,
\Magento\Framework\HTTP\ZendClientFactory $httpClientFactory,
\Magento\Sales\Model\OrderFactory $orderFactory,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
\Magento\Sales\Model\Order\Email\Sender\OrderSender $orderSender,
\Magento\Sales\Api\TransactionRepositoryInterface $transactionRepository,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = []
array $data = [],
\Magento\Sales\Api\PaymentFailuresInterface $paymentFailures = null
) {
$this->orderFactory = $orderFactory;
$this->storeManager = $storeManager;
Expand All @@ -179,6 +190,8 @@ public function __construct(
$this->orderSender = $orderSender;
$this->transactionRepository = $transactionRepository;
$this->_code = static::METHOD_CODE;
$this->paymentFailures = $paymentFailures ? : ObjectManager::getInstance()
->get(\Magento\Sales\Api\PaymentFailuresInterface::class);

parent::__construct(
$context,
Expand Down Expand Up @@ -561,13 +574,10 @@ public function process(array $responseData)
$this->validateResponse();

$response = $this->getResponse();
//operate with order
$orderIncrementId = $response->getXInvoiceNum();
$responseText = $this->dataHelper->wrapGatewayError($response->getXResponseReasonText());
$isError = false;
if ($orderIncrementId) {
/* @var $order \Magento\Sales\Model\Order */
$order = $this->orderFactory->create()->loadByIncrementId($orderIncrementId);
if ($this->getOrderIncrementId()) {
$order = $this->getOrderFromResponse();
//check payment method
$payment = $order->getPayment();
if (!$payment || $payment->getMethod() != $this->getCode()) {
Expand Down Expand Up @@ -632,9 +642,10 @@ public function checkResponseCode()
return true;
case self::RESPONSE_CODE_DECLINED:
case self::RESPONSE_CODE_ERROR:
throw new \Magento\Framework\Exception\LocalizedException(
$this->dataHelper->wrapGatewayError($this->getResponse()->getXResponseReasonText())
);
$errorMessage = $this->dataHelper->wrapGatewayError($this->getResponse()->getXResponseReasonText());
$order = $this->getOrderFromResponse();
$this->paymentFailures->handle((int)$order->getQuoteId(), $errorMessage);
throw new \Magento\Framework\Exception\LocalizedException($errorMessage);
default:
throw new \Magento\Framework\Exception\LocalizedException(
__('There was a payment authorization error.')
Expand Down Expand Up @@ -988,12 +999,40 @@ protected function getTransactionResponse($transactionId)
private function getPsrLogger()
{
if (null === $this->psrLogger) {
$this->psrLogger = \Magento\Framework\App\ObjectManager::getInstance()
$this->psrLogger = ObjectManager::getInstance()
->get(\Psr\Log\LoggerInterface::class);
}
return $this->psrLogger;
}

/**
* Fetch order by increment id from response.
*
* @return \Magento\Sales\Model\Order
*/
private function getOrderFromResponse(): \Magento\Sales\Model\Order
{
if (!$this->order) {
$this->order = $this->orderFactory->create();

if ($incrementId = $this->getOrderIncrementId()) {
$this->order = $this->order->loadByIncrementId($incrementId);
}
}

return $this->order;
}

/**
* Fetch order increment id from response.
*
* @return string
*/
private function getOrderIncrementId(): string
{
return $this->getResponse()->getXInvoiceNum();
}

/**
* Checks if filter action is Report Only. Transactions that trigger this filter are processed as normal,
* but are also reported in the Merchant Interface as triggering this filter.
Expand Down
51 changes: 44 additions & 7 deletions app/code/Magento/Authorizenet/Test/Unit/Model/DirectpostTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
namespace Magento\Authorizenet\Test\Unit\Model;

use Magento\Sales\Api\PaymentFailuresInterface;
use Magento\Framework\Simplexml\Element;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
use Magento\Authorizenet\Model\Directpost;
Expand Down Expand Up @@ -74,6 +75,14 @@ class DirectpostTest extends \PHPUnit\Framework\TestCase
*/
protected $requestFactory;

/**
* @var PaymentFailuresInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $paymentFailures;

/**
* @inheritdoc
*/
protected function setUp()
{
$this->scopeConfigMock = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)
Expand Down Expand Up @@ -104,6 +113,12 @@ protected function setUp()
->setMethods(['getTransactionDetails'])
->getMock();

$this->paymentFailures = $this->getMockBuilder(
PaymentFailuresInterface::class
)
->disableOriginalConstructor()
->getMock();

$this->requestFactory = $this->getRequestFactoryMock();
$httpClientFactoryMock = $this->getHttpClientFactoryMock();

Expand All @@ -117,7 +132,8 @@ protected function setUp()
'responseFactory' => $this->responseFactoryMock,
'transactionRepository' => $this->transactionRepositoryMock,
'transactionService' => $this->transactionServiceMock,
'httpClientFactory' => $httpClientFactoryMock
'httpClientFactory' => $httpClientFactoryMock,
'paymentFailures' => $this->paymentFailures,
]
);
}
Expand Down Expand Up @@ -313,12 +329,16 @@ public function checkResponseCodeSuccessDataProvider()
}

/**
* @param bool $responseCode
* Checks response failures behaviour.
*
* @param int $responseCode
* @param int $failuresHandlerCalls
* @return void
*
* @expectedException \Magento\Framework\Exception\LocalizedException
* @dataProvider checkResponseCodeFailureDataProvider
*/
public function testCheckResponseCodeFailure($responseCode)
public function testCheckResponseCodeFailure(int $responseCode, int $failuresHandlerCalls): void
{
$reasonText = 'reason text';

Expand All @@ -333,18 +353,35 @@ public function testCheckResponseCodeFailure($responseCode)
->with($reasonText)
->willReturn(__('Gateway error: %1', $reasonText));

$orderMock = $this->getMockBuilder(Order::class)
->disableOriginalConstructor()
->getMock();

$orderMock->expects($this->exactly($failuresHandlerCalls))
->method('getQuoteId')
->willReturn(1);

$this->paymentFailures->expects($this->exactly($failuresHandlerCalls))
->method('handle')
->with(1);

$reflection = new \ReflectionClass($this->directpost);
$order = $reflection->getProperty('order');
$order->setAccessible(true);
$order->setValue($this->directpost, $orderMock);

$this->directpost->checkResponseCode();
}

/**
* @return array
*/
public function checkResponseCodeFailureDataProvider()
public function checkResponseCodeFailureDataProvider(): array
{
return [
['responseCode' => Directpost::RESPONSE_CODE_DECLINED],
['responseCode' => Directpost::RESPONSE_CODE_ERROR],
['responseCode' => 999999]
['responseCode' => Directpost::RESPONSE_CODE_DECLINED, 1],
['responseCode' => Directpost::RESPONSE_CODE_ERROR, 1],
['responseCode' => 999999, 0],
];
}

Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/Braintree/Model/AvsEmsCodeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class AvsEmsCodeMapper implements PaymentVerificationInterface
*
* @var string
*/
private static $unavailableCode = 'U';
private static $unavailableCode = '';

/**
* List of mapping AVS codes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ public function testGetCodeWithException()
public function getCodeDataProvider()
{
return [
['avsZip' => null, 'avsStreet' => null, 'expected' => 'U'],
['avsZip' => null, 'avsStreet' => 'M', 'expected' => 'U'],
['avsZip' => 'M', 'avsStreet' => null, 'expected' => 'U'],
['avsZip' => 'M', 'avsStreet' => 'Unknown', 'expected' => 'U'],
['avsZip' => 'I', 'avsStreet' => 'A', 'expected' => 'U'],
['avsZip' => null, 'avsStreet' => null, 'expected' => ''],
['avsZip' => null, 'avsStreet' => 'M', 'expected' => ''],
['avsZip' => 'M', 'avsStreet' => null, 'expected' => ''],
['avsZip' => 'M', 'avsStreet' => 'Unknown', 'expected' => ''],
['avsZip' => 'I', 'avsStreet' => 'A', 'expected' => ''],
['avsZip' => 'M', 'avsStreet' => 'M', 'expected' => 'Y'],
['avsZip' => 'N', 'avsStreet' => 'M', 'expected' => 'A'],
['avsZip' => 'M', 'avsStreet' => 'N', 'expected' => 'Z'],
Expand Down
Loading

0 comments on commit d3a849c

Please sign in to comment.