Skip to content

Commit

Permalink
Merge pull request magento#4395 from magento-mpi/MPI_PR_2019_06_21
Browse files Browse the repository at this point in the history
[MPI] Bugfixes
  • Loading branch information
viktym authored Jun 23, 2019
2 parents 9c7a51d + a7a0e54 commit 1e58f70
Show file tree
Hide file tree
Showing 24 changed files with 883 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ public function getErrorCodes($response): array
$result[] = $error->code;
}

if (isset($response->transaction) && $response->transaction->status === 'gateway_rejected') {
$result[] = $response->transaction->gatewayRejectionReason;
}

if (isset($response->transaction) && $response->transaction->status === 'processor_declined') {
$result[] = $response->transaction->processorResponseCode;
}

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

namespace Magento\Braintree\Test\Unit\Gateway\Validator;

use Braintree\Result\Error;
use Magento\Braintree\Gateway\Validator\ErrorCodeProvider;
use PHPUnit\Framework\TestCase;

/**
* Class ErrorCodeProviderTest
*/
class ErrorCodeProviderTest extends TestCase
{
/**
* @var ErrorCodeProvider
*/
private $model;

/**
* Checks a extracting error codes from response.
*
* @param array $errors
* @param array $transaction
* @param array $expectedResult
* @return void
* @dataProvider getErrorCodeDataProvider
*/
public function testGetErrorCodes(array $errors, array $transaction, array $expectedResult): void
{
$response = new Error(
[
'errors' => ['errors' => $errors],
'transaction' => $transaction,
]
);
$this->model = new ErrorCodeProvider();
$actual = $this->model->getErrorCodes($response);

$this->assertSame($expectedResult, $actual);
}

/**
* Gets list of errors variations.
*
* @return array
*/
public function getErrorCodeDataProvider(): array
{
return [
[
'errors' => [
['code' => 91734],
['code' => 91504]
],
'transaction' => [
'status' => 'success',
],
'expectedResult' => ['91734', '91504']
],
[
'errors' => [],
'transaction' => [
'status' => 'processor_declined',
'processorResponseCode' => '1000'
],
'expectedResult' => ['1000']
],
[
'errors' => [],
'transaction' => [
'status' => 'processor_declined',
'processorResponseCode' => '1000'
],
'expectedResult' => ['1000']
],
[
'errors' => [
['code' => 91734],
['code' => 91504]
],
'transaction' => [
'status' => 'processor_declined',
'processorResponseCode' => '1000'
],
'expectedResult' => ['91734', '91504', '1000']
],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
use Magento\Payment\Gateway\Validator\ResultInterfaceFactory;
use PHPUnit_Framework_MockObject_MockObject as MockObject;

/**
* Class GeneralResponseValidatorTest
*/
class GeneralResponseValidatorTest extends \PHPUnit\Framework\TestCase
{
/**
Expand Down Expand Up @@ -61,11 +64,13 @@ public function testValidate(array $validationSubject, bool $isValid, $messages,
$result = new Result($isValid, $messages);

$this->resultInterfaceFactory->method('create')
->with([
'isValid' => $isValid,
'failsDescription' => $messages,
'errorCodes' => $errorCodes
])
->with(
[
'isValid' => $isValid,
'failsDescription' => $messages,
'errorCodes' => $errorCodes
]
)
->willReturn($result);

$actual = $this->responseValidator->validate($validationSubject);
Expand All @@ -82,9 +87,11 @@ public function dataProviderTestValidate()
{
$successTransaction = new \stdClass();
$successTransaction->success = true;
$successTransaction->status = 'authorized';

$failureTransaction = new \stdClass();
$failureTransaction->success = false;
$failureTransaction->status = 'declined';
$failureTransaction->message = 'Transaction was failed.';

$errors = [
Expand All @@ -93,10 +100,10 @@ public function dataProviderTestValidate()
'code' => 81804,
'attribute' => 'base',
'message' => 'Cannot process transaction.'
]
],
]
];
$errorTransaction = new Error(['errors' => $errors]);
$errorTransaction = new Error(['errors' => $errors, 'transaction' => ['status' => 'declined']]);

return [
[
Expand Down
1 change: 1 addition & 0 deletions app/code/Magento/Braintree/etc/braintree_error_mapping.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<message code="81716" translate="true">Credit card number must be 12-19 digits.</message>
<message code="81723" translate="true">Cardholder name is too long.</message>
<message code="81736" translate="true">CVV verification failed.</message>
<message code="cvv" translate="true">CVV verification failed.</message>
<message code="81737" translate="true">Postal code verification failed.</message>
<message code="81750" translate="true">Credit card number is prohibited.</message>
<message code="81801" translate="true">Addresses must have at least one field filled in.</message>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ define([
'Magento_Ui/js/model/messageList',
'mage/translate',
'Magento_Checkout/js/model/full-screen-loader',
'Magento_Checkout/js/action/set-payment-information',
'Magento_Checkout/js/action/set-payment-information-extended',
'Magento_Checkout/js/model/payment/additional-validators',
'Magento_Braintree/js/view/payment/validator-handler'
], function (
Expand All @@ -22,7 +22,7 @@ define([
messageList,
$t,
fullScreenLoader,
setPaymentInformationAction,
setPaymentInformationExtended,
additionalValidators,
validatorManager
) {
Expand Down Expand Up @@ -51,9 +51,10 @@ define([
if (additionalValidators.validate()) {
fullScreenLoader.startLoader();
$.when(
setPaymentInformationAction(
setPaymentInformationExtended(
this.messageContainer,
this.getData()
this.getData(),
true
)
).done(this.done.bind(this))
.fail(this.fail.bind(this));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ define([
'jquery',
'underscore',
'Magento_Braintree/js/view/payment/method-renderer/paypal',
'Magento_Checkout/js/action/set-payment-information',
'Magento_Checkout/js/action/set-payment-information-extended',
'Magento_Checkout/js/model/payment/additional-validators',
'Magento_Checkout/js/model/full-screen-loader'
], function (
$,
_,
Component,
setPaymentInformationAction,
setPaymentInformationExtended,
additionalValidators,
fullScreenLoader
) {
Expand Down Expand Up @@ -131,9 +131,10 @@ define([
placeOrder: function () {
fullScreenLoader.startLoader();
$.when(
setPaymentInformationAction(
setPaymentInformationExtended(
this.messageContainer,
this.getData()
this.getData(),
true
)
).done(this.done.bind(this))
.fail(this.fail.bind(this));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogInventory\Observer;

use Magento\Catalog\Api\Data\ProductInterface as Product;

/**
* Interface for processing parent items of complex product types
*/
interface ParentItemProcessorInterface
{
/**
* Process stock for parent items
*
* @param Product $product
* @return void
*/
public function process(Product $product);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use Magento\CatalogInventory\Api\StockRegistryInterface;
use Magento\CatalogInventory\Model\StockItemValidator;
use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;

/**
* Saves stock data from a product to the Stock Item
Expand All @@ -39,6 +41,11 @@ class SaveInventoryDataObserver implements ObserverInterface
*/
private $stockItemValidator;

/**
* @var ParentItemProcessorInterface[]
*/
private $parentItemProcessorPool;

/**
* @var array
*/
Expand Down Expand Up @@ -77,15 +84,18 @@ class SaveInventoryDataObserver implements ObserverInterface
* @param StockConfigurationInterface $stockConfiguration
* @param StockRegistryInterface $stockRegistry
* @param StockItemValidator $stockItemValidator
* @param ParentItemProcessorInterface[] $parentItemProcessorPool
*/
public function __construct(
StockConfigurationInterface $stockConfiguration,
StockRegistryInterface $stockRegistry,
StockItemValidator $stockItemValidator = null
StockItemValidator $stockItemValidator = null,
array $parentItemProcessorPool = []
) {
$this->stockConfiguration = $stockConfiguration;
$this->stockRegistry = $stockRegistry;
$this->stockItemValidator = $stockItemValidator ?: ObjectManager::getInstance()->get(StockItemValidator::class);
$this->parentItemProcessorPool = $parentItemProcessorPool;
}

/**
Expand All @@ -96,10 +106,15 @@ public function __construct(
*
* @param EventObserver $observer
* @return void
* @throws LocalizedException
* @throws NoSuchEntityException
*/
public function execute(EventObserver $observer)
{
/** @var Product $product */
$product = $observer->getEvent()->getProduct();

/** @var Item $stockItem */
$stockItem = $this->getStockItemToBeUpdated($product);

if ($product->getStockData() !== null) {
Expand All @@ -108,6 +123,7 @@ public function execute(EventObserver $observer)
}
$this->stockItemValidator->validate($product, $stockItem);
$this->stockRegistry->updateStockItemBySku($product->getSku(), $stockItem);
$this->processParents($product);
}

/**
Expand Down Expand Up @@ -156,4 +172,17 @@ private function getStockData(Product $product)
}
return $stockData;
}

/**
* Process stock data for parent products
*
* @param Product $product
* @return void
*/
private function processParents(Product $product)
{
foreach ($this->parentItemProcessorPool as $processor) {
$processor->process($product);
}
}
}
Loading

0 comments on commit 1e58f70

Please sign in to comment.