Skip to content

Commit

Permalink
Merge pull request #356 from magento-mpi/MPI-PR-2.1.3
Browse files Browse the repository at this point in the history
Fixed issues:
- MAGETWO-56910: Braintree doesn't work when using table prefixing
- MAGETWO-57426: [Github] Braintree Vault payments causing GET order API to throw error
- MAGETWO-56932: Checkout page freezes when ordering Virtual Gift Card with Authorize.net set to Authorize and Capture
- MAGETWO-57037: UPS not providing shipping rates for Puerto Rico
- MAGETWO-57086: [Github] #5910 Braintree sandbox errors when using alternative Merchant Account ID
  • Loading branch information
MomotenkoNatalia authored Sep 9, 2016
2 parents 68112cc + cbb2688 commit e5cf9f4
Show file tree
Hide file tree
Showing 16 changed files with 187 additions and 37 deletions.
10 changes: 10 additions & 0 deletions app/code/Magento/Braintree/Gateway/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,14 @@ public function isActive()
{
return (bool) $this->getValue(self::KEY_ACTIVE);
}

/**
* Get Merchant account ID
*
* @return string
*/
public function getMerchantAccountId()
{
return $this->getValue(self::KEY_MERCHANT_ACCOUNT_ID);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function build(array $buildSubject)
self::ORDER_ID => $order->getOrderIncrementId()
];

$merchantAccountId = $this->config->getValue(Config::KEY_MERCHANT_ACCOUNT_ID);
$merchantAccountId = $this->config->getMerchantAccountId();
if (!empty($merchantAccountId)) {
$result[self::MERCHANT_ACCOUNT_ID] = $merchantAccountId;
}
Expand Down
10 changes: 9 additions & 1 deletion app/code/Magento/Braintree/Model/Ui/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
namespace Magento\Braintree\Model\Ui;

use Magento\Braintree\Gateway\Request\PaymentDataBuilder;
use Magento\Checkout\Model\ConfigProviderInterface;
use Magento\Braintree\Gateway\Config\Config;
use Magento\Braintree\Gateway\Config\PayPal\Config as PayPalConfig;
Expand Down Expand Up @@ -116,7 +117,14 @@ public function getConfig()
public function getClientToken()
{
if (empty($this->clientToken)) {
$this->clientToken = $this->adapter->generate();
$params = [];

$merchantAccountId = $this->config->getMerchantAccountId();
if (!empty($merchantAccountId)) {
$params[PaymentDataBuilder::MERCHANT_ACCOUNT_ID] = $merchantAccountId;
}

$this->clientToken = $this->adapter->generate($params);
}

return $this->clientToken;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ public function testBuild()
->willReturnMap($additionalData);

$this->configMock->expects(static::once())
->method('getValue')
->with(Config::KEY_MERCHANT_ACCOUNT_ID)
->method('getMerchantAccountId')
->willReturn(self::MERCHANT_ACCOUNT_ID);

$this->paymentDO->expects(static::once())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
class ConfigProviderTest extends \PHPUnit_Framework_TestCase
{
const SDK_URL = 'https://js.braintreegateway.com/v2/braintree.js';

const CLIENT_TOKEN = 'token';
const MERCHANT_ACCOUNT_ID = '245345';

/**
* @var Config|\PHPUnit_Framework_MockObject_MockObject
Expand Down Expand Up @@ -115,11 +115,17 @@ public function testGetConfig($config, $expected)

/**
* @covers \Magento\Braintree\Model\Ui\ConfigProvider::getClientToken
* @dataProvider getClientTokenDataProvider
*/
public function testGetClientToken()
public function testGetClientToken($merchantAccountId, $params)
{
$this->config->expects(static::once())
->method('getMerchantAccountId')
->willReturn($merchantAccountId);

$this->braintreeAdapter->expects(static::once())
->method('generate')
->with($params)
->willReturn(self::CLIENT_TOKEN);

static::assertEquals(self::CLIENT_TOKEN, $this->configProvider->getClientToken());
Expand Down Expand Up @@ -188,4 +194,21 @@ public function getConfigDataProvider()
]
];
}

/**
* @return array
*/
public function getClientTokenDataProvider()
{
return [
[
'merchantAccountId' => '',
'params' => []
],
[
'merchantAccountId' => self::MERCHANT_ACCOUNT_ID,
'params' => ['merchantAccountId' => self::MERCHANT_ACCOUNT_ID]
]
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ define(
*/
clearTimeout: function () {
clearTimeout(this.timeoutId);
this.fail();

return this;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
namespace Magento\Sales\Model\ResourceModel\Order\Payment;

use Magento\Sales\Api\Data\OrderPaymentInterface;
use Magento\Sales\Api\Data\OrderPaymentSearchResultInterface;
use Magento\Sales\Model\ResourceModel\Order\Collection\AbstractCollection;

Expand Down Expand Up @@ -75,7 +76,34 @@ protected function _afterLoad()
{
foreach ($this->_items as $item) {
$this->getResource()->unserializeFields($item);
if (!empty($item->getData(OrderPaymentInterface::ADDITIONAL_INFORMATION))) {
$additionalInfo = $this->convertAdditionalInfo(
$item->getData(OrderPaymentInterface::ADDITIONAL_INFORMATION)
);
$item->setData(OrderPaymentInterface::ADDITIONAL_INFORMATION, $additionalInfo);
}
}
return parent::_afterLoad();
}

/**
* Convert multidimensional additional information array to single
*
* @param array $info
* @return array
*/
private function convertAdditionalInfo($info)
{
$result = [];
foreach ($info as $key => $item) {
if (is_array($item)) {
$result += $this->convertAdditionalInfo($item);
unset($info[$key]);
} else {
$result[$key] = $item;
}
}

return $result;
}
}
4 changes: 2 additions & 2 deletions app/code/Magento/Ups/Model/Carrier.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,13 +325,13 @@ public function setRequest(RateRequest $request)
$destCountry = self::GUAM_COUNTRY_ID;
}

$rowRequest->setDestCountry($this->_countryFactory->create()->load($destCountry)->getData('iso2_code'));
$country = $this->_countryFactory->create()->load($destCountry);
$rowRequest->setDestCountry($country->getData('iso2_code') ?: $destCountry);

$rowRequest->setDestRegionCode($request->getDestRegionCode());

if ($request->getDestPostcode()) {
$rowRequest->setDestPostal($request->getDestPostcode());
} else {
}

$weight = $this->getTotalNumOfBoxes($request->getPackageWeight());
Expand Down
47 changes: 46 additions & 1 deletion app/code/Magento/Ups/Test/Unit/Model/CarrierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

use Magento\Quote\Model\Quote\Address\RateRequest;
use Magento\Ups\Model\Carrier;
use Magento\Directory\Model\Country;
use PHPUnit_Framework_MockObject_MockObject as MockObject;

class CarrierTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -54,7 +56,7 @@ class CarrierTest extends \PHPUnit_Framework_TestCase
protected $countryFactory;

/**
* @var \Magento\Directory\Model\Country
* @var Country|MockObject
*/
protected $country;

Expand Down Expand Up @@ -306,4 +308,47 @@ public function logDataProvider()
]
];
}

/**
* @covers \Magento\Ups\Model\Carrier::setRequest
* @param string $countryCode
* @param string $foundCountryCode
* @dataProvider countryDataProvider
*/
public function testSetRequest($countryCode, $foundCountryCode)
{
/** @var RateRequest $request */
$request = $this->helper->getObject(RateRequest::class);
$request->setData([
'orig_country' => 'USA',
'orig_region_code' => 'CA',
'orig_post_code' => 90230,
'orig_city' => 'Culver City',
'dest_country_id' => $countryCode,
]);

$this->country->expects(static::at(1))
->method('load')
->with($countryCode)
->willReturnSelf();

$this->country->expects(static::any())
->method('getData')
->with('iso2_code')
->willReturn($foundCountryCode);

$this->model->setRequest($request);
}

/**
* Get list of country variations
* @return array
*/
public function countryDataProvider()
{
return [
['countryCode' => 'PR', 'foundCountryCode' => null],
['countryCode' => 'US', 'foundCountryCode' => 'US'],
];
}
}
19 changes: 11 additions & 8 deletions app/code/Magento/Vault/Model/Method/Vault.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
*/
final class Vault implements VaultPaymentInterface
{
/**
* @deprecated
*/
const TOKEN_METADATA_KEY = 'token_metadata';

/**
Expand Down Expand Up @@ -111,6 +114,7 @@ final class Vault implements VaultPaymentInterface
* @param PaymentTokenManagementInterface $tokenManagement
* @param OrderPaymentExtensionInterfaceFactory $paymentExtensionFactory
* @param string $code
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
ConfigInterface $config,
Expand Down Expand Up @@ -448,21 +452,20 @@ public function capture(\Magento\Payment\Model\InfoInterface $payment, $amount)
/**
* @param OrderPaymentInterface $orderPayment
* @return void
* @throws \LogicException
*/
private function attachTokenExtensionAttribute(OrderPaymentInterface $orderPayment)
{
$additionalInformation = $orderPayment->getAdditionalInformation();

$tokenData = isset($additionalInformation[self::TOKEN_METADATA_KEY])
? $additionalInformation[self::TOKEN_METADATA_KEY]
: null;

if ($tokenData === null) {
throw new \LogicException("Token metadata should be defined");
if (empty($additionalInformation[PaymentTokenInterface::CUSTOMER_ID]) ||
empty($additionalInformation[PaymentTokenInterface::PUBLIC_HASH])
) {
throw new \LogicException('Customer and public hash should be defined');
}

$customerId = $tokenData[PaymentTokenInterface::CUSTOMER_ID];
$publicHash = $tokenData[PaymentTokenInterface::PUBLIC_HASH];
$customerId = $additionalInformation[PaymentTokenInterface::CUSTOMER_ID];
$publicHash = $additionalInformation[PaymentTokenInterface::PUBLIC_HASH];

$paymentToken = $this->tokenManagement->getByPublicHash($publicHash, $customerId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function addLinkToOrderPayment($paymentTokenId, $orderPaymentId)
$connection = $this->getConnection();

$select = $connection->select()
->from(InstallSchema::ORDER_PAYMENT_TO_PAYMENT_TOKEN_TABLE)
->from($this->getTable(InstallSchema::ORDER_PAYMENT_TO_PAYMENT_TOKEN_TABLE))
->where('order_payment_id = ?', (int) $orderPaymentId)
->where('payment_token_id =?', (int) $paymentTokenId);

Expand Down
1 change: 0 additions & 1 deletion app/code/Magento/Vault/Observer/PaymentTokenAssigner.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ public function execute(Observer $observer)
}

$paymentModel->setAdditionalInformation(
Vault::TOKEN_METADATA_KEY,
[
PaymentTokenInterface::CUSTOMER_ID => $customerId,
PaymentTokenInterface::PUBLIC_HASH => $tokenPublicHash
Expand Down
38 changes: 27 additions & 11 deletions app/code/Magento/Vault/Test/Unit/Model/Method/VaultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
use Magento\Vault\Model\Method\Vault;
use Magento\Vault\Model\VaultPaymentInterface;

/**
* Class VaultTest
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class VaultTest extends \PHPUnit_Framework_TestCase
{
/**
Expand Down Expand Up @@ -45,23 +49,39 @@ public function testAuthorizeNotOrderPayment()

/**
* @expectedException \LogicException
* @expectedExceptionMessage Token metadata should be defined
* @expectedExceptionMessage Customer and public hash should be defined
* @dataProvider tokenMetadataProvider
*/
public function testAuthorizeNoTokenMetadata()
public function testAuthorizeNoTokenMetadata($additionalInfo)
{
$paymentModel = $this->getMockBuilder(Payment::class)
->disableOriginalConstructor()
->getMock();

$paymentModel->expects(static::once())
->method('getAdditionalInformation')
->willReturn([]);
->willReturn($additionalInfo);

/** @var Vault $model */
$model = $this->objectManager->getObject(Vault::class);
$model->authorize($paymentModel, 0);
}

/**
* Get list of variations
* @return array
*/
public function tokenMetadataProvider()
{
return [
['additionalInfo' => []],
['additionalInfo' => ['public_hash' => null]],
['additionalInfo' => ['customer_id' => null]],
['additionalInfo' => ['public_hash' => '1ds23', 'customer_id' => null]],
['additionalInfo' => ['public_hash' => null, 'customer_id' => 1]],
];
}

/**
* @expectedException \LogicException
* @expectedExceptionMessage No token found
Expand All @@ -80,10 +100,8 @@ public function testAuthorizeNoToken()
->method('getAdditionalInformation')
->willReturn(
[
Vault::TOKEN_METADATA_KEY => [
PaymentTokenInterface::CUSTOMER_ID => $customerId,
PaymentTokenInterface::PUBLIC_HASH => $publicHash
]
PaymentTokenInterface::CUSTOMER_ID => $customerId,
PaymentTokenInterface::PUBLIC_HASH => $publicHash
]
);
$tokenManagement->expects(static::once())
Expand Down Expand Up @@ -127,10 +145,8 @@ public function testAuthorize()
->method('getAdditionalInformation')
->willReturn(
[
Vault::TOKEN_METADATA_KEY => [
PaymentTokenInterface::CUSTOMER_ID => $customerId,
PaymentTokenInterface::PUBLIC_HASH => $publicHash
]
PaymentTokenInterface::CUSTOMER_ID => $customerId,
PaymentTokenInterface::PUBLIC_HASH => $publicHash
]
);
$tokenManagement->expects(static::once())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ public function testExecuteSaveMetadata()
$paymentModel->expects(static::once())
->method('setAdditionalInformation')
->with(
Vault::TOKEN_METADATA_KEY,
[
PaymentTokenInterface::CUSTOMER_ID => $customerId,
PaymentTokenInterface::PUBLIC_HASH => $publicHash
Expand Down
Loading

0 comments on commit e5cf9f4

Please sign in to comment.