Skip to content

Commit

Permalink
MAGETWO-47615: Display Vault Tokens in Account on Storefront
Browse files Browse the repository at this point in the history
- Displayed tokens for BraintreeTwo
  • Loading branch information
dkvashninbay committed Jan 18, 2016
1 parent a7e1b50 commit e511f1b
Show file tree
Hide file tree
Showing 21 changed files with 732 additions and 122 deletions.
96 changes: 96 additions & 0 deletions app/code/Magento/BraintreeTwo/Block/Customer/CardRenderer.php
@@ -0,0 +1,96 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\BraintreeTwo\Block\Customer;

use Magento\BraintreeTwo\Model\Ui\ConfigProvider;
use Magento\Framework\View\Element\Template;
use Magento\Payment\Model\CcConfigProvider;
use Magento\Vault\Api\Data\PaymentTokenInterface;
use Magento\Vault\Block\TokenRendererInterface;

class CardRenderer extends Template implements TokenRendererInterface
{
/**
* @var PaymentTokenInterface|null
*/
private $token;

/**
* @var CcConfigProvider
*/
private $iconsProvider;

/**
* Constructor
*
* @param Template\Context $context
* @param CcConfigProvider $iconsProvider
* @param array $data
*/
public function __construct(
Template\Context $context,
CcConfigProvider $iconsProvider,
array $data = []
) {
parent::__construct($context, $data);
$this->iconsProvider = $iconsProvider;
}

/**
* Can render specified token
*
* @param PaymentTokenInterface $token
* @return boolean
*/
public function canRender(PaymentTokenInterface $token)
{
return $token->getPaymentMethodCode() === ConfigProvider::CODE;
}

/**
* Renders specified token
*
* @param PaymentTokenInterface $token
* @return string
*/
public function render(PaymentTokenInterface $token)
{
$this->token = $token;
$result = $this->toHtml();
$this->token = null;

return $result;
}

/**
* @return PaymentTokenInterface|null
*/
public function getToken()
{
return $this->token;
}

/**
* @return mixed
*/
public function getTokenDetails()
{
return json_decode($this->getToken()->getTokenDetails() ?: '{}', true);
}

/**
* @param string $type
* @return array|null
*/
public function getIconForType($type)
{
if (isset($this->iconsProvider->getIcons()[$type])) {
return $this->iconsProvider->getIcons()[$type];
}

return null;
}
}
@@ -0,0 +1,16 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<referenceBlock name="vault.cards.list">
<block class="Magento\BraintreeTwo\Block\Customer\CardRenderer" name="braintree.card.renderer" template="customer_account/credit_card.phtml"/>
</referenceBlock>
</referenceContainer>
</body>
</page>
@@ -0,0 +1,44 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
use Magento\BraintreeTwo\Block\Customer\CardRenderer;
use Magento\Vault\Api\Data\PaymentTokenInterface;

/** @var CardRenderer $block */
$details = $block->getTokenDetails();
$icon = $block->getIconForType($details['type']);
?>
<div class="payment-method">
<form class="form" action="<?php /* @noEscape */ echo $block->getUrl('vault/cards/deleteaction') ?>" method="post">
<?php echo $block->getBlockHtml('formkey')?>
<input
name="<?php echo /* @noEscape */ PaymentTokenInterface::PUBLIC_HASH?>"
value="<?php echo /* @noEscape */ $block->getToken()->getPublicHash()?>"
type="hidden"/>
<div class="payment-method-title field">
<label class="label">
<img src="<?php echo $block->escapeUrl($icon['url']); ?>"
width="<?php echo $block->escapeHtml($icon['width']); ?>"
height="<?php echo $block->escapeHtml($icon['height']); ?>"
>
<span><?php echo $block->escapeHtml(__('ending'));?></span>
<span><?php echo $block->escapeHtml($details['maskedCC']); ?></span>
<span><?php echo $block->escapeHtml(__('expires'));?></span>:
<span><?php echo $block->escapeHtml($details['expirationDate']); ?></span>
</label>
</div>
<div class="actions-toolbar">
<div class="primary">
<button
type="submit"
class="action delete primary"
title="<?php /* @noEscape */ echo __('Delete') ?>">
<span><?php /* @noEscape */ echo __('Delete') ?></span>
</button>
</div>
</div>
</form>
</div>

23 changes: 18 additions & 5 deletions app/code/Magento/Payment/Model/CcConfigProvider.php
Expand Up @@ -8,8 +8,17 @@
use Magento\Checkout\Model\ConfigProviderInterface;
use Magento\Framework\View\Asset\Source;

/**
* Class CcConfigProvider
* @api
*/
class CcConfigProvider implements ConfigProviderInterface
{
/**
* @var array
*/
private $icons = [];

/**
* @var CcConfig
*/
Expand Down Expand Up @@ -51,24 +60,28 @@ public function getConfig()
*
* @return array
*/
protected function getIcons()
public function getIcons()
{
$icons = [];
if (!empty($this->icons)) {
return $this->icons;
}

$types = $this->ccConfig->getCcAvailableTypes();
foreach (array_keys($types) as $code) {
if (!array_key_exists($code, $icons)) {
if (!array_key_exists($code, $this->icons)) {
$asset = $this->ccConfig->createAsset('Magento_Payment::images/cc/' . strtolower($code) . '.png');
$placeholder = $this->assetSource->findSource($asset);
if ($placeholder) {
list($width, $height) = getimagesize($asset->getSourceFile());
$icons[$code] = [
$this->icons[$code] = [
'url' => $asset->getUrl(),
'width' => $width,
'height' => $height
];
}
}
}
return $icons;

return $this->icons;
}
}
Expand Up @@ -115,7 +115,7 @@ public function testConstructMethod()
->method('set')
->willReturnSelf();

$this->model = new \Magento\Sales\Block\Order\History(
$this->model = new \Magento\Sales\Block\Order\CreditCards(
$this->context,
$this->orderCollectionFactory,
$this->customerSession,
Expand Down
59 changes: 59 additions & 0 deletions app/code/Magento/Vault/Block/CreditCards.php
@@ -0,0 +1,59 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Vault\Block;

use Magento\Customer\Model\Session;
use Magento\Framework\View\Element\Template;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Vault\Api\Data\PaymentTokenInterface;
use Magento\Vault\Model\CustomerTokenManagement;

class CreditCards extends Template
{
/**
* @var CustomerTokenManagement
*/
private $customerTokenManagement;

/**
* CreditCards constructor.
* @param Template\Context $context
* @param CustomerTokenManagement $customerTokenManagement
* @param array $data
*/
public function __construct(
Template\Context $context,
CustomerTokenManagement $customerTokenManagement,
array $data = []
) {
parent::__construct($context, $data);
$this->customerTokenManagement = $customerTokenManagement;
}

/**
* @return PaymentTokenInterface[]
*/
public function getPaymentTokens()
{
return $this->customerTokenManagement->getCustomerSessionTokens();
}

/**
* @param PaymentTokenInterface $token
* @return string
*/
public function renderTokenHtml(PaymentTokenInterface $token)
{
foreach ($this->getChildNames() as $childName) {
$childBlock = $this->getChildBlock($childName);
if ($childBlock instanceof TokenRendererInterface && $childBlock->canRender($token)) {
return $childBlock->render($token);
}
}

return '';
}
}
16 changes: 10 additions & 6 deletions app/code/Magento/Vault/Block/Form.php
Expand Up @@ -58,13 +58,17 @@ protected function _prepareLayout()
*/
protected function createVaultBlocks()
{
$icons = $this->cardConfigProvider->getConfig()['payment']['ccform']['icons'];
$payments = $this->tokensProvider->getConfig();
$icons = $this->cardConfigProvider->getIcons();
$payments = $this->tokensProvider->getTokensComponents();
foreach ($payments as $key => $payment) {
$data = $payment['config'];
$data['id'] = $key;
$data['icons'] = $icons;
$this->addChild($key, $payment['component'], $data);
$this->addChild(
$key,
$payment->getName(),
array_merge(
['id' => $key, 'icons' => $icons],
$payment->getConfig()
)
);
}
}
}
31 changes: 31 additions & 0 deletions app/code/Magento/Vault/Block/TokenRendererInterface.php
@@ -0,0 +1,31 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Vault\Block;

use Magento\Vault\Api\Data\PaymentTokenInterface;

/**
* Interface TokenRendererInterface
* @api
*/
interface TokenRendererInterface
{
/**
* Can render specified token
*
* @param PaymentTokenInterface $token
* @return boolean
*/
public function canRender(PaymentTokenInterface $token);

/**
* Renders specified token
*
* @param PaymentTokenInterface $token
* @return string
*/
public function render(PaymentTokenInterface $token);
}

0 comments on commit e511f1b

Please sign in to comment.