Skip to content

Commit e511f1b

Browse files
committed
MAGETWO-47615: Display Vault Tokens in Account on Storefront
- Displayed tokens for BraintreeTwo
1 parent a7e1b50 commit e511f1b

File tree

21 files changed

+732
-122
lines changed

21 files changed

+732
-122
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\BraintreeTwo\Block\Customer;
7+
8+
use Magento\BraintreeTwo\Model\Ui\ConfigProvider;
9+
use Magento\Framework\View\Element\Template;
10+
use Magento\Payment\Model\CcConfigProvider;
11+
use Magento\Vault\Api\Data\PaymentTokenInterface;
12+
use Magento\Vault\Block\TokenRendererInterface;
13+
14+
class CardRenderer extends Template implements TokenRendererInterface
15+
{
16+
/**
17+
* @var PaymentTokenInterface|null
18+
*/
19+
private $token;
20+
21+
/**
22+
* @var CcConfigProvider
23+
*/
24+
private $iconsProvider;
25+
26+
/**
27+
* Constructor
28+
*
29+
* @param Template\Context $context
30+
* @param CcConfigProvider $iconsProvider
31+
* @param array $data
32+
*/
33+
public function __construct(
34+
Template\Context $context,
35+
CcConfigProvider $iconsProvider,
36+
array $data = []
37+
) {
38+
parent::__construct($context, $data);
39+
$this->iconsProvider = $iconsProvider;
40+
}
41+
42+
/**
43+
* Can render specified token
44+
*
45+
* @param PaymentTokenInterface $token
46+
* @return boolean
47+
*/
48+
public function canRender(PaymentTokenInterface $token)
49+
{
50+
return $token->getPaymentMethodCode() === ConfigProvider::CODE;
51+
}
52+
53+
/**
54+
* Renders specified token
55+
*
56+
* @param PaymentTokenInterface $token
57+
* @return string
58+
*/
59+
public function render(PaymentTokenInterface $token)
60+
{
61+
$this->token = $token;
62+
$result = $this->toHtml();
63+
$this->token = null;
64+
65+
return $result;
66+
}
67+
68+
/**
69+
* @return PaymentTokenInterface|null
70+
*/
71+
public function getToken()
72+
{
73+
return $this->token;
74+
}
75+
76+
/**
77+
* @return mixed
78+
*/
79+
public function getTokenDetails()
80+
{
81+
return json_decode($this->getToken()->getTokenDetails() ?: '{}', true);
82+
}
83+
84+
/**
85+
* @param string $type
86+
* @return array|null
87+
*/
88+
public function getIconForType($type)
89+
{
90+
if (isset($this->iconsProvider->getIcons()[$type])) {
91+
return $this->iconsProvider->getIcons()[$type];
92+
}
93+
94+
return null;
95+
}
96+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © 2015 Magento. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
9+
<body>
10+
<referenceContainer name="content">
11+
<referenceBlock name="vault.cards.list">
12+
<block class="Magento\BraintreeTwo\Block\Customer\CardRenderer" name="braintree.card.renderer" template="customer_account/credit_card.phtml"/>
13+
</referenceBlock>
14+
</referenceContainer>
15+
</body>
16+
</page>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
use Magento\BraintreeTwo\Block\Customer\CardRenderer;
7+
use Magento\Vault\Api\Data\PaymentTokenInterface;
8+
9+
/** @var CardRenderer $block */
10+
$details = $block->getTokenDetails();
11+
$icon = $block->getIconForType($details['type']);
12+
?>
13+
<div class="payment-method">
14+
<form class="form" action="<?php /* @noEscape */ echo $block->getUrl('vault/cards/deleteaction') ?>" method="post">
15+
<?php echo $block->getBlockHtml('formkey')?>
16+
<input
17+
name="<?php echo /* @noEscape */ PaymentTokenInterface::PUBLIC_HASH?>"
18+
value="<?php echo /* @noEscape */ $block->getToken()->getPublicHash()?>"
19+
type="hidden"/>
20+
<div class="payment-method-title field">
21+
<label class="label">
22+
<img src="<?php echo $block->escapeUrl($icon['url']); ?>"
23+
width="<?php echo $block->escapeHtml($icon['width']); ?>"
24+
height="<?php echo $block->escapeHtml($icon['height']); ?>"
25+
>
26+
<span><?php echo $block->escapeHtml(__('ending'));?></span>
27+
<span><?php echo $block->escapeHtml($details['maskedCC']); ?></span>
28+
<span><?php echo $block->escapeHtml(__('expires'));?></span>:
29+
<span><?php echo $block->escapeHtml($details['expirationDate']); ?></span>
30+
</label>
31+
</div>
32+
<div class="actions-toolbar">
33+
<div class="primary">
34+
<button
35+
type="submit"
36+
class="action delete primary"
37+
title="<?php /* @noEscape */ echo __('Delete') ?>">
38+
<span><?php /* @noEscape */ echo __('Delete') ?></span>
39+
</button>
40+
</div>
41+
</div>
42+
</form>
43+
</div>
44+

app/code/Magento/Payment/Model/CcConfigProvider.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,17 @@
88
use Magento\Checkout\Model\ConfigProviderInterface;
99
use Magento\Framework\View\Asset\Source;
1010

11+
/**
12+
* Class CcConfigProvider
13+
* @api
14+
*/
1115
class CcConfigProvider implements ConfigProviderInterface
1216
{
17+
/**
18+
* @var array
19+
*/
20+
private $icons = [];
21+
1322
/**
1423
* @var CcConfig
1524
*/
@@ -51,24 +60,28 @@ public function getConfig()
5160
*
5261
* @return array
5362
*/
54-
protected function getIcons()
63+
public function getIcons()
5564
{
56-
$icons = [];
65+
if (!empty($this->icons)) {
66+
return $this->icons;
67+
}
68+
5769
$types = $this->ccConfig->getCcAvailableTypes();
5870
foreach (array_keys($types) as $code) {
59-
if (!array_key_exists($code, $icons)) {
71+
if (!array_key_exists($code, $this->icons)) {
6072
$asset = $this->ccConfig->createAsset('Magento_Payment::images/cc/' . strtolower($code) . '.png');
6173
$placeholder = $this->assetSource->findSource($asset);
6274
if ($placeholder) {
6375
list($width, $height) = getimagesize($asset->getSourceFile());
64-
$icons[$code] = [
76+
$this->icons[$code] = [
6577
'url' => $asset->getUrl(),
6678
'width' => $width,
6779
'height' => $height
6880
];
6981
}
7082
}
7183
}
72-
return $icons;
84+
85+
return $this->icons;
7386
}
7487
}

app/code/Magento/Sales/Test/Unit/Block/Order/HistoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public function testConstructMethod()
115115
->method('set')
116116
->willReturnSelf();
117117

118-
$this->model = new \Magento\Sales\Block\Order\History(
118+
$this->model = new \Magento\Sales\Block\Order\CreditCards(
119119
$this->context,
120120
$this->orderCollectionFactory,
121121
$this->customerSession,
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Vault\Block;
7+
8+
use Magento\Customer\Model\Session;
9+
use Magento\Framework\View\Element\Template;
10+
use Magento\Store\Model\StoreManagerInterface;
11+
use Magento\Vault\Api\Data\PaymentTokenInterface;
12+
use Magento\Vault\Model\CustomerTokenManagement;
13+
14+
class CreditCards extends Template
15+
{
16+
/**
17+
* @var CustomerTokenManagement
18+
*/
19+
private $customerTokenManagement;
20+
21+
/**
22+
* CreditCards constructor.
23+
* @param Template\Context $context
24+
* @param CustomerTokenManagement $customerTokenManagement
25+
* @param array $data
26+
*/
27+
public function __construct(
28+
Template\Context $context,
29+
CustomerTokenManagement $customerTokenManagement,
30+
array $data = []
31+
) {
32+
parent::__construct($context, $data);
33+
$this->customerTokenManagement = $customerTokenManagement;
34+
}
35+
36+
/**
37+
* @return PaymentTokenInterface[]
38+
*/
39+
public function getPaymentTokens()
40+
{
41+
return $this->customerTokenManagement->getCustomerSessionTokens();
42+
}
43+
44+
/**
45+
* @param PaymentTokenInterface $token
46+
* @return string
47+
*/
48+
public function renderTokenHtml(PaymentTokenInterface $token)
49+
{
50+
foreach ($this->getChildNames() as $childName) {
51+
$childBlock = $this->getChildBlock($childName);
52+
if ($childBlock instanceof TokenRendererInterface && $childBlock->canRender($token)) {
53+
return $childBlock->render($token);
54+
}
55+
}
56+
57+
return '';
58+
}
59+
}

app/code/Magento/Vault/Block/Form.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,17 @@ protected function _prepareLayout()
5858
*/
5959
protected function createVaultBlocks()
6060
{
61-
$icons = $this->cardConfigProvider->getConfig()['payment']['ccform']['icons'];
62-
$payments = $this->tokensProvider->getConfig();
61+
$icons = $this->cardConfigProvider->getIcons();
62+
$payments = $this->tokensProvider->getTokensComponents();
6363
foreach ($payments as $key => $payment) {
64-
$data = $payment['config'];
65-
$data['id'] = $key;
66-
$data['icons'] = $icons;
67-
$this->addChild($key, $payment['component'], $data);
64+
$this->addChild(
65+
$key,
66+
$payment->getName(),
67+
array_merge(
68+
['id' => $key, 'icons' => $icons],
69+
$payment->getConfig()
70+
)
71+
);
6872
}
6973
}
7074
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Vault\Block;
7+
8+
use Magento\Vault\Api\Data\PaymentTokenInterface;
9+
10+
/**
11+
* Interface TokenRendererInterface
12+
* @api
13+
*/
14+
interface TokenRendererInterface
15+
{
16+
/**
17+
* Can render specified token
18+
*
19+
* @param PaymentTokenInterface $token
20+
* @return boolean
21+
*/
22+
public function canRender(PaymentTokenInterface $token);
23+
24+
/**
25+
* Renders specified token
26+
*
27+
* @param PaymentTokenInterface $token
28+
* @return string
29+
*/
30+
public function render(PaymentTokenInterface $token);
31+
}

0 commit comments

Comments
 (0)