Skip to content

Commit

Permalink
Merge pull request #1687 from magento-mpi/MPI-PR-2.2.2
Browse files Browse the repository at this point in the history
[MPI][2.2.2] Bugfixes
  • Loading branch information
Oleksii Korshenko authored Nov 10, 2017
2 parents 688cd2b + 47aae5f commit 758ba75
Show file tree
Hide file tree
Showing 92 changed files with 1,564 additions and 1,130 deletions.
36 changes: 15 additions & 21 deletions app/code/Magento/Braintree/Block/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Magento\Braintree\Gateway\Config\Config as GatewayConfig;
use Magento\Braintree\Model\Adminhtml\Source\CcType;
use Magento\Braintree\Model\Ui\ConfigProvider;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\View\Element\Template\Context;
use Magento\Payment\Block\Form\Cc;
use Magento\Payment\Helper\Data;
Expand All @@ -21,7 +20,6 @@
*/
class Form extends Cc
{

/**
* @var Quote
*/
Expand All @@ -48,6 +46,7 @@ class Form extends Cc
* @param Quote $sessionQuote
* @param GatewayConfig $gatewayConfig
* @param CcType $ccType
* @param Data $paymentDataHelper
* @param array $data
*/
public function __construct(
Expand All @@ -56,12 +55,14 @@ public function __construct(
Quote $sessionQuote,
GatewayConfig $gatewayConfig,
CcType $ccType,
Data $paymentDataHelper,
array $data = []
) {
parent::__construct($context, $paymentConfig, $data);
$this->sessionQuote = $sessionQuote;
$this->gatewayConfig = $gatewayConfig;
$this->ccType = $ccType;
$this->paymentDataHelper = $paymentDataHelper;
}

/**
Expand All @@ -81,7 +82,7 @@ public function getCcAvailableTypes()
*/
public function useCvv()
{
return $this->gatewayConfig->isCvvEnabled();
return $this->gatewayConfig->isCvvEnabled($this->sessionQuote->getStoreId());
}

/**
Expand All @@ -90,9 +91,8 @@ public function useCvv()
*/
public function isVaultEnabled()
{
$storeId = $this->_storeManager->getStore()->getId();
$vaultPayment = $this->getVaultPayment();
return $vaultPayment->isActive($storeId);
return $vaultPayment->isActive($this->sessionQuote->getStoreId());
}

/**
Expand All @@ -102,7 +102,10 @@ public function isVaultEnabled()
private function getConfiguredCardTypes()
{
$types = $this->ccType->getCcTypeLabelMap();
$configCardTypes = array_fill_keys($this->gatewayConfig->getAvailableCardTypes(), '');
$configCardTypes = array_fill_keys(
$this->gatewayConfig->getAvailableCardTypes($this->sessionQuote->getStoreId()),
''
);

return array_intersect_key($types, $configCardTypes);
}
Expand All @@ -116,7 +119,11 @@ private function getConfiguredCardTypes()
private function filterCardTypesForCountry(array $configCardTypes, $countryId)
{
$filtered = $configCardTypes;
$countryCardTypes = $this->gatewayConfig->getCountryAvailableCardTypes($countryId);
$countryCardTypes = $this->gatewayConfig->getCountryAvailableCardTypes(
$countryId,
$this->sessionQuote->getStoreId()
);

// filter card types only if specific card types are set for country
if (!empty($countryCardTypes)) {
$availableTypes = array_fill_keys($countryCardTypes, '');
Expand All @@ -131,19 +138,6 @@ private function filterCardTypesForCountry(array $configCardTypes, $countryId)
*/
private function getVaultPayment()
{
return $this->getPaymentDataHelper()->getMethodInstance(ConfigProvider::CC_VAULT_CODE);
}

/**
* Get payment data helper instance
* @return Data
* @deprecated 100.1.0
*/
private function getPaymentDataHelper()
{
if ($this->paymentDataHelper === null) {
$this->paymentDataHelper = ObjectManager::getInstance()->get(Data::class);
}
return $this->paymentDataHelper;
return $this->paymentDataHelper->getMethodInstance(ConfigProvider::CC_VAULT_CODE);
}
}
4 changes: 4 additions & 0 deletions app/code/Magento/Braintree/Block/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ public function getPaymentConfig()
$payment = $this->config->getConfig()['payment'];
$config = $payment[$this->getCode()];
$config['code'] = $this->getCode();
$config['clientTokenUrl'] = $this->_urlBuilder->getUrl(
'braintree/payment/getClientToken',
['_secure' => true]
);
return json_encode($config, JSON_UNESCAPED_SLASHES);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Braintree\Controller\Adminhtml\Payment;

use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Backend\Model\Session\Quote;
use Magento\Braintree\Gateway\Config\Config;
use Magento\Braintree\Gateway\Request\PaymentDataBuilder;
use Magento\Braintree\Model\Adapter\BraintreeAdapterFactory;
use Magento\Framework\Controller\ResultFactory;

class GetClientToken extends Action
{
const ADMIN_RESOURCE = 'Magento_Braintree::get_client_token';

/**
* @var Config
*/
private $config;

/**
* @var BraintreeAdapterFactory
*/
private $adapterFactory;

/**
* @var Quote
*/
private $quoteSession;

/**
* @param Context $context
* @param Config $config
* @param BraintreeAdapterFactory $adapterFactory
* @param Quote $quoteSession
*/
public function __construct(
Context $context,
Config $config,
BraintreeAdapterFactory $adapterFactory,
Quote $quoteSession
) {
parent::__construct($context);
$this->config = $config;
$this->adapterFactory = $adapterFactory;
$this->quoteSession = $quoteSession;
}

/**
* @inheritdoc
*/
public function execute()
{
$params = [];
$response = $this->resultFactory->create(ResultFactory::TYPE_JSON);

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

$clientToken = $this->adapterFactory->create($storeId)
->generate($params);
$response->setData(['clientToken' => $clientToken]);

return $response;
}
}
5 changes: 4 additions & 1 deletion app/code/Magento/Braintree/Controller/Payment/GetNonce.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ public function execute()
try {
$publicHash = $this->getRequest()->getParam('public_hash');
$customerId = $this->session->getCustomerId();
$result = $this->command->execute(['public_hash' => $publicHash, 'customer_id' => $customerId])->get();
$result = $this->command->execute(
['public_hash' => $publicHash, 'customer_id' => $customerId, 'store_id' => $this->session->getStoreId()]
)
->get();
$response->setData(['paymentMethodNonce' => $result['paymentMethodNonce']]);
} catch (\Exception $e) {
$this->logger->critical($e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@
namespace Magento\Braintree\Gateway\Command;

use Braintree\Transaction;
use Magento\Braintree\Model\Adapter\BraintreeAdapter;
use Magento\Braintree\Gateway\SubjectReader;
use Magento\Braintree\Model\Adapter\BraintreeAdapterFactory;
use Magento\Braintree\Model\Adapter\BraintreeSearchAdapter;
use Magento\Framework\Api\FilterBuilder;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Payment\Gateway\Command;
use Magento\Payment\Gateway\Command\CommandPoolInterface;
use Magento\Payment\Gateway\CommandInterface;
use Magento\Payment\Gateway\Data\OrderAdapterInterface;
use Magento\Payment\Gateway\Helper\ContextHelper;
use Magento\Braintree\Gateway\Helper\SubjectReader;
use Magento\Sales\Api\Data\OrderPaymentInterface;
use Magento\Sales\Api\TransactionRepositoryInterface;
use Magento\Sales\Api\Data\TransactionInterface;
use Magento\Sales\Api\TransactionRepositoryInterface;
use Magento\Payment\Gateway\Data\PaymentDataObjectInterface;

/**
* Class CaptureStrategyCommand
Expand Down Expand Up @@ -66,9 +67,9 @@ class CaptureStrategyCommand implements CommandInterface
private $subjectReader;

/**
* @var BraintreeAdapter
* @var BraintreeAdapterFactory
*/
private $braintreeAdapter;
private $braintreeAdapterFactory;

/**
* @var BraintreeSearchAdapter
Expand All @@ -83,7 +84,7 @@ class CaptureStrategyCommand implements CommandInterface
* @param FilterBuilder $filterBuilder
* @param SearchCriteriaBuilder $searchCriteriaBuilder
* @param SubjectReader $subjectReader
* @param BraintreeAdapter $braintreeAdapter
* @param BraintreeAdapterFactory $braintreeAdapterFactory,
* @param BraintreeSearchAdapter $braintreeSearchAdapter
*/
public function __construct(
Expand All @@ -92,15 +93,15 @@ public function __construct(
FilterBuilder $filterBuilder,
SearchCriteriaBuilder $searchCriteriaBuilder,
SubjectReader $subjectReader,
BraintreeAdapter $braintreeAdapter,
BraintreeAdapterFactory $braintreeAdapterFactory,
BraintreeSearchAdapter $braintreeSearchAdapter
) {
$this->commandPool = $commandPool;
$this->transactionRepository = $repository;
$this->filterBuilder = $filterBuilder;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->subjectReader = $subjectReader;
$this->braintreeAdapter = $braintreeAdapter;
$this->braintreeAdapterFactory = $braintreeAdapterFactory;
$this->braintreeSearchAdapter = $braintreeSearchAdapter;
}

Expand All @@ -112,29 +113,29 @@ public function execute(array $commandSubject)
/** @var \Magento\Payment\Gateway\Data\PaymentDataObjectInterface $paymentDO */
$paymentDO = $this->subjectReader->readPayment($commandSubject);

/** @var \Magento\Sales\Api\Data\OrderPaymentInterface $paymentInfo */
$paymentInfo = $paymentDO->getPayment();
ContextHelper::assertOrderPayment($paymentInfo);

$command = $this->getCommand($paymentInfo);
$command = $this->getCommand($paymentDO);
$this->commandPool->get($command)->execute($commandSubject);
}

/**
* Get execution command name
* @param OrderPaymentInterface $payment
* Gets command name.
*
* @param PaymentDataObjectInterface $paymentDO
* @return string
*/
private function getCommand(OrderPaymentInterface $payment)
private function getCommand(PaymentDataObjectInterface $paymentDO)
{
// if auth transaction is not exists execute authorize&capture command
$payment = $paymentDO->getPayment();
ContextHelper::assertOrderPayment($payment);

// if auth transaction does not exist then execute authorize&capture command
$existsCapture = $this->isExistsCaptureTransaction($payment);
if (!$payment->getAuthorizationTransaction() && !$existsCapture) {
return self::SALE;
}

// do capture for authorization transaction
if (!$existsCapture && !$this->isExpiredAuthorization($payment)) {
if (!$existsCapture && !$this->isExpiredAuthorization($payment, $paymentDO->getOrder())) {
return self::CAPTURE;
}

Expand All @@ -143,12 +144,16 @@ private function getCommand(OrderPaymentInterface $payment)
}

/**
* Checks if authorization transaction does not expired yet.
*
* @param OrderPaymentInterface $payment
* @return boolean
* @param OrderAdapterInterface $orderAdapter
* @return bool
*/
private function isExpiredAuthorization(OrderPaymentInterface $payment)
private function isExpiredAuthorization(OrderPaymentInterface $payment, OrderAdapterInterface $orderAdapter)
{
$collection = $this->braintreeAdapter->search(
$adapter = $this->braintreeAdapterFactory->create($orderAdapter->getStoreId());
$collection = $adapter->search(
[
$this->braintreeSearchAdapter->id()->is($payment->getLastTransId()),
$this->braintreeSearchAdapter->status()->is(Transaction::AUTHORIZATION_EXPIRED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
namespace Magento\Braintree\Gateway\Command;

use Exception;
use Magento\Braintree\Gateway\Helper\SubjectReader;
use Magento\Braintree\Gateway\SubjectReader;
use Magento\Braintree\Gateway\Validator\PaymentNonceResponseValidator;
use Magento\Braintree\Model\Adapter\BraintreeAdapter;
use Magento\Payment\Gateway\Command;
use Magento\Braintree\Model\Adapter\BraintreeAdapterFactory;
use Magento\Payment\Gateway\Command\Result\ArrayResultFactory;
use Magento\Payment\Gateway\CommandInterface;
use Magento\Vault\Api\PaymentTokenManagementInterface;
Expand All @@ -27,9 +26,9 @@ class GetPaymentNonceCommand implements CommandInterface
private $tokenManagement;

/**
* @var BraintreeAdapter
* @var BraintreeAdapterFactory
*/
private $adapter;
private $adapterFactory;

/**
* @var ArrayResultFactory
Expand All @@ -48,20 +47,20 @@ class GetPaymentNonceCommand implements CommandInterface

/**
* @param PaymentTokenManagementInterface $tokenManagement
* @param BraintreeAdapter $adapter
* @param BraintreeAdapterFactory $adapterFactory
* @param ArrayResultFactory $resultFactory
* @param SubjectReader $subjectReader
* @param PaymentNonceResponseValidator $responseValidator
*/
public function __construct(
PaymentTokenManagementInterface $tokenManagement,
BraintreeAdapter $adapter,
BraintreeAdapterFactory $adapterFactory,
ArrayResultFactory $resultFactory,
SubjectReader $subjectReader,
PaymentNonceResponseValidator $responseValidator
) {
$this->tokenManagement = $tokenManagement;
$this->adapter = $adapter;
$this->adapterFactory = $adapterFactory;
$this->resultFactory = $resultFactory;
$this->subjectReader = $subjectReader;
$this->responseValidator = $responseValidator;
Expand All @@ -80,7 +79,9 @@ public function execute(array $commandSubject)
throw new Exception('No available payment tokens');
}

$data = $this->adapter->createNonce($paymentToken->getGatewayToken());
$storeId = $this->subjectReader->readStoreId($commandSubject);
$data = $this->adapterFactory->create($storeId)
->createNonce($paymentToken->getGatewayToken());
$result = $this->responseValidator->validate(['response' => ['object' => $data]]);

if (!$result->isValid()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
namespace Magento\Braintree\Gateway\Config;

use Magento\Braintree\Gateway\Helper\SubjectReader;
use Magento\Braintree\Gateway\SubjectReader;
use Magento\Payment\Gateway\Config\ValueHandlerInterface;
use Magento\Sales\Model\Order\Payment;

Expand Down
Loading

0 comments on commit 758ba75

Please sign in to comment.