Skip to content

Commit

Permalink
Merge remote-tracking branch 'mainline/develop' into MAGETWO-44074
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrii Lugovyi committed Dec 9, 2015
2 parents 12f8060 + 2f74e09 commit 8235d5b
Show file tree
Hide file tree
Showing 299 changed files with 15,508 additions and 3,481 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\BraintreeTwo\Block\Adminhtml\Form\Field;

use Magento\Framework\View\Element\Context;
use Magento\Framework\View\Element\Html\Select;
use Magento\BraintreeTwo\Helper\CcType;

/**
* Class Cctypes
* @package Magento\BraintreeTwo\Block\Adminhtml\Form\Field
*/
class Cctypes extends Select
{
/**
* @var \
*/
private $ccTypeHelper;

/**
* Constructor
*
* @param \Magento\Framework\View\Element\Context $context
* @param \Magento\BraintreeTwo\Helper\CcType $ccTypeHelper
* @param array $data
*/
public function __construct(
Context $context,
CcType $ccTypeHelper,
array $data = []
) {
parent::__construct($context, $data);
$this->ccTypeHelper = $ccTypeHelper;
}

/**
* Render block HTML
*
* @return string
*/
protected function _toHtml()
{
if (!$this->getOptions()) {
$this->setOptions($this->ccTypeHelper->getCcTypes());
}
$this->setClass('cc-type-select');
$this->setExtraParams('multiple="multiple"');
return parent::_toHtml();
}

/**
* Sets name for input element
*
* @param string $value
* @return $this
*/
public function setInputName($value)
{
return $this->setName($value . '[]');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\BraintreeTwo\Block\Adminhtml\Form\Field;

use Magento\BraintreeTwo\Helper\Country;
use Magento\Framework\View\Element\Context;
use Magento\Framework\View\Element\Html\Select;

/**
* Class Countries
* @package Magento\BraintreeTwo\Block\Adminhtml\Form\Field
*/
class Countries extends Select
{
/**
* @var \Magento\BraintreeTwo\Helper\Country
*/
private $countryHelper;

/**
* Constructor
*
* @param \Magento\Framework\View\Element\Context $context
* @param \Magento\BraintreeTwo\Helper\Country $countryHelper
* @param array $data
*/
public function __construct(Context $context, Country $countryHelper, array $data = [])
{
parent::__construct($context, $data);
$this->countryHelper = $countryHelper;
}

/**
* Render block HTML
*
* @return string
*/
protected function _toHtml()
{
if (!$this->getOptions()) {
$this->setOptions($this->countryHelper->getCountries());
}
return parent::_toHtml();
}

/**
* Sets name for input element
*
* @param string $value
* @return $this
*/
public function setInputName($value)
{
return $this->setName($value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\BraintreeTwo\Block\Adminhtml\Form\Field;

use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;
use Magento\Framework\DataObject;

/**
* Class CountryCreditCard
* @package Magento\BraintreeTwo\Block\Adminhtml\Form\Field
*/
class CountryCreditCard extends AbstractFieldArray
{
/**
* @var \Magento\BraintreeTwo\Block\Adminhtml\Form\Field\Countries
*/
protected $countryRenderer = null;

/**
* @var \Magento\BraintreeTwo\Block\Adminhtml\Form\Field\CcTypes
*/
protected $ccTypesRenderer = null;

/**
* Returns renderer for country element
*
* @return \Magento\BraintreeTwo\Block\Adminhtml\Form\Field\Countries
*/
protected function getCountryRenderer()
{
if (!$this->countryRenderer) {
$this->countryRenderer = $this->getLayout()->createBlock(
Countries::class,
'',
['data' => ['is_render_to_js_template' => true]]
);
}
return $this->countryRenderer;
}

/**
* Returns renderer for country element
*
* @return \Magento\BraintreeTwo\Block\Adminhtml\Form\Field\Cctypes
*/
protected function getCcTypesRenderer()
{
if (!$this->ccTypesRenderer) {
$this->ccTypesRenderer = $this->getLayout()->createBlock(
Cctypes::class,
'',
['data' => ['is_render_to_js_template' => true]]
);
}
return $this->ccTypesRenderer;
}

/**
* Prepare to render
* @return void
*/
protected function _prepareToRender()
{
$this->addColumn(
'country_id',
[
'label' => __('Country'),
'renderer' => $this->getCountryRenderer(),
]
);
$this->addColumn(
'cc_types',
[
'label' => __('Allowed Credit Card Types'),
'renderer' => $this->getCcTypesRenderer(),
]
);
$this->_addAfter = false;
$this->_addButtonLabel = __('Add Rule');
}

/**
* Prepare existing row data object
*
* @param \Magento\Framework\DataObject $row
* @return void
*/
protected function _prepareArrayRow(DataObject $row)
{
$country = $row->getCountryId();
$options = [];
if ($country) {
$options['option_' . $this->getCountryRenderer()->calcOptionHash($country)]
= 'selected="selected"';

$ccTypes = $row->getCcTypes();
foreach ($ccTypes as $cardType) {
$options['option_' . $this->getCcTypesRenderer()->calcOptionHash($cardType)]
= 'selected="selected"';
}
}
$row->setData('option_extra_attrs', $options);
return;
}
}
108 changes: 108 additions & 0 deletions app/code/Magento/BraintreeTwo/Block/Form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\BraintreeTwo\Block;

use Magento\Backend\Model\Session\Quote;
use Magento\Framework\View\Element\Template\Context;
use Magento\Payment\Block\Form\Cc;
use Magento\Payment\Model\Config;
use Magento\BraintreeTwo\Gateway\Config\Config as GatewayConfig;
use Magento\BraintreeTwo\Model\Adminhtml\Source\CcType;

/**
* Class Form
* @package Magento\BraintreeTwo\Block
*/
class Form extends Cc
{

/**
* @var \Magento\Backend\Model\Session\Quote
*/
protected $sessionQuote;

/**
* @var \Magento\BraintreeTwo\Gateway\Config\Config
*/
protected $gatewayConfig;

/**
* @var \Magento\BraintreeTwo\Model\Adminhtml\Source\CcType
*/
protected $ccType;

/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Payment\Model\Config $paymentConfig
* @param \Magento\Backend\Model\Session\Quote $sessionQuote
* @param \Magento\BraintreeTwo\Gateway\Config\Config $gatewayConfig
* @param \Magento\BraintreeTwo\Model\Adminhtml\Source\CcType $ccType
* @param array $data
*/
public function __construct(
Context $context,
Config $paymentConfig,
Quote $sessionQuote,
GatewayConfig $gatewayConfig,
CcType $ccType,
array $data = []
) {
parent::__construct($context, $paymentConfig, $data);
$this->sessionQuote = $sessionQuote;
$this->gatewayConfig = $gatewayConfig;
$this->ccType = $ccType;
}

/**
* Get list of available card types of order billing address country
* @return array
*/
public function getCcAvailableTypes()
{
$configuredCardTypes = $this->getConfiguredCardTypes();
$countryId = $this->sessionQuote->getQuote()->getBillingAddress()->getCountryId();
return $this->filterCardTypesForCountry($configuredCardTypes, $countryId);
}

/**
* Check if cvv validation is available
* @return boolean
*/
public function useCvv()
{
return $this->gatewayConfig->isCvvEnabled();
}

/**
* Get card types available for Braintree
* @return array
*/
private function getConfiguredCardTypes()
{
$types = $this->ccType->getCcTypeLabelMap();
$configCardTypes = array_fill_keys($this->gatewayConfig->getAvailableCardTypes(), '');

return array_intersect_key($types, $configCardTypes);
}

/**
* Filter card types for specific country
* @param array $configCardTypes
* @param string $countryId
* @return array
*/
private function filterCardTypesForCountry(array $configCardTypes, $countryId)
{
$filtered = $configCardTypes;
$countryCardTypes = $this->gatewayConfig->getCountryAvailableCardTypes($countryId);
// filter card types only if specific card types are set for country
if (!empty($countryCardTypes)) {
$availableTypes = array_fill_keys($countryCardTypes, '');
$filtered = array_intersect_key($filtered, $availableTypes);
}
return $filtered;
}
}
27 changes: 27 additions & 0 deletions app/code/Magento/BraintreeTwo/Block/Info.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\BraintreeTwo\Block;

use Magento\Framework\Phrase;
use Magento\Payment\Block\ConfigurableInfo;

/**
* Class Info
* @package Magento\BraintreeTwo\Block
*/
class Info extends ConfigurableInfo
{
/**
* Returns label
*
* @param string $field
* @return Phrase
*/
protected function getLabel($field)
{
return __($field);
}
}
Loading

0 comments on commit 8235d5b

Please sign in to comment.