Skip to content

Commit 9d70111

Browse files
committed
Merge remote-tracking branch 'mpi/MAGETWO-48372-2' into pr-mpi-200416
2 parents 0ead714 + e1d81b8 commit 9d70111

File tree

9 files changed

+285
-48
lines changed

9 files changed

+285
-48
lines changed

app/code/Magento/Paypal/Block/Express/Shortcut.php

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,6 @@ class Shortcut extends \Magento\Framework\View\Element\Template implements Catal
5151
*/
5252
protected $_alias = '';
5353

54-
/**
55-
* Paypal data
56-
*
57-
* @var \Magento\Paypal\Helper\Data
58-
*/
59-
protected $_paypalData;
60-
6154
/**
6255
* @var \Magento\Paypal\Model\ConfigFactory
6356
*/
@@ -78,11 +71,6 @@ class Shortcut extends \Magento\Framework\View\Element\Template implements Catal
7871
*/
7972
protected $_mathRandom;
8073

81-
/**
82-
* @var \Magento\Customer\Helper\Session\CurrentCustomer
83-
*/
84-
protected $currentCustomer;
85-
8674
/**
8775
* @var \Magento\Framework\Locale\ResolverInterface
8876
*/
@@ -100,11 +88,9 @@ class Shortcut extends \Magento\Framework\View\Element\Template implements Catal
10088

10189
/**
10290
* @param \Magento\Framework\View\Element\Template\Context $context
103-
* @param \Magento\Paypal\Helper\Data $paypalData
10491
* @param \Magento\Paypal\Model\ConfigFactory $paypalConfigFactory
10592
* @param \Magento\Paypal\Model\Express\Checkout\Factory $checkoutFactory
10693
* @param \Magento\Framework\Math\Random $mathRandom
107-
* @param \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer
10894
* @param \Magento\Framework\Locale\ResolverInterface $localeResolver
10995
* @param ValidatorInterface $shortcutValidator
11096
* @param string $paymentMethodCode
@@ -118,11 +104,9 @@ class Shortcut extends \Magento\Framework\View\Element\Template implements Catal
118104
*/
119105
public function __construct(
120106
\Magento\Framework\View\Element\Template\Context $context,
121-
\Magento\Paypal\Helper\Data $paypalData,
122107
\Magento\Paypal\Model\ConfigFactory $paypalConfigFactory,
123108
\Magento\Paypal\Model\Express\Checkout\Factory $checkoutFactory,
124109
\Magento\Framework\Math\Random $mathRandom,
125-
\Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer,
126110
\Magento\Framework\Locale\ResolverInterface $localeResolver,
127111
ValidatorInterface $shortcutValidator,
128112
$paymentMethodCode,
@@ -133,7 +117,6 @@ public function __construct(
133117
\Magento\Checkout\Model\Session $checkoutSession = null,
134118
array $data = []
135119
) {
136-
$this->_paypalData = $paypalData;
137120
$this->_paypalConfigFactory = $paypalConfigFactory;
138121
$this->_checkoutSession = $checkoutSession;
139122
$this->_checkoutFactory = $checkoutFactory;
@@ -151,7 +134,6 @@ public function __construct(
151134

152135
$this->config = $this->_paypalConfigFactory->create();
153136
$this->config->setMethod($this->_paymentMethodCode);
154-
$this->currentCustomer = $currentCustomer;
155137
}
156138

157139
/**
@@ -187,20 +169,6 @@ protected function _beforeToHtml()
187169
$this->setImageUrl($checkoutModel->getCheckoutShortcutImageUrl());
188170
}
189171

190-
// ask whether to create a billing agreement
191-
$customerId = $this->currentCustomer->getCustomerId(); // potential issue for caching
192-
if ($this->_paypalData->shouldAskToCreateBillingAgreement($this->config, $customerId)) {
193-
$this->setConfirmationUrl(
194-
$this->getUrl(
195-
$this->_startAction,
196-
[\Magento\Paypal\Model\Express\Checkout::PAYMENT_INFO_TRANSPORT_BILLING_AGREEMENT => 1]
197-
)
198-
);
199-
$this->setConfirmationMessage(
200-
__('Would you like to sign a billing agreement to streamline further purchases with PayPal?')
201-
);
202-
}
203-
204172
return $result;
205173
}
206174

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Paypal\CustomerData;
7+
8+
use Magento\Customer\CustomerData\SectionSourceInterface;
9+
use Magento\Customer\Helper\Session\CurrentCustomer;
10+
use Magento\Framework\Escaper;
11+
use Magento\Framework\UrlInterface;
12+
use Magento\Paypal\Helper\Data;
13+
use Magento\Paypal\Model\Config;
14+
use Magento\Paypal\Model\ConfigFactory;
15+
16+
/**
17+
* BillingAgreement section
18+
*/
19+
class BillingAgreement implements SectionSourceInterface
20+
{
21+
/**
22+
* @var CurrentCustomer
23+
*/
24+
private $currentCustomer;
25+
26+
/**
27+
* Paypal data
28+
*
29+
* @var Data
30+
*/
31+
private $paypalData;
32+
33+
/**
34+
* @var Config
35+
*/
36+
private $config;
37+
38+
/**
39+
* Url Builder
40+
*
41+
* @var UrlInterface
42+
*/
43+
private $urlBuilder;
44+
45+
/**
46+
* Escaper
47+
*
48+
* @var Escaper
49+
*/
50+
private $escaper;
51+
52+
/**
53+
* Start express action
54+
*
55+
* @var string
56+
*/
57+
private $startAction = 'paypal/express/start/button/1';
58+
59+
/**
60+
* @param CurrentCustomer $currentCustomer
61+
* @param Data $paypalData
62+
* @param ConfigFactory $paypalConfigFactory
63+
* @param UrlInterface $urlBuilder
64+
* @param Escaper $escaper
65+
*/
66+
public function __construct(
67+
CurrentCustomer $currentCustomer,
68+
Data $paypalData,
69+
ConfigFactory $paypalConfigFactory,
70+
UrlInterface $urlBuilder,
71+
Escaper $escaper
72+
) {
73+
$this->currentCustomer = $currentCustomer;
74+
$this->paypalData = $paypalData;
75+
$this->urlBuilder = $urlBuilder;
76+
$this->escaper = $escaper;
77+
$this->config = $paypalConfigFactory->create();
78+
$this->config->setMethod(Config::METHOD_EXPRESS);
79+
}
80+
81+
/**
82+
* {@inheritdoc}
83+
*/
84+
public function getSectionData()
85+
{
86+
$customerId = $this->currentCustomer->getCustomerId();
87+
if ($this->paypalData->shouldAskToCreateBillingAgreement($this->config, $customerId)) {
88+
return [
89+
'askToCreate' => true,
90+
'confirmUrl' => $this->escaper->escapeUrl(
91+
$this->urlBuilder->getUrl(
92+
$this->startAction,
93+
[\Magento\Paypal\Model\Express\Checkout::PAYMENT_INFO_TRANSPORT_BILLING_AGREEMENT => 1]
94+
)
95+
),
96+
'confirmMessage' => $this->escaper->escapeJsQuote(
97+
__('Would you like to sign a billing agreement to streamline further purchases with PayPal?')
98+
)
99+
];
100+
}
101+
102+
return [];
103+
}
104+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Paypal\Test\Unit\CustomerData;
7+
8+
use Magento\Customer\Helper\Session\CurrentCustomer;
9+
use Magento\Paypal\CustomerData\BillingAgreement;
10+
use Magento\Paypal\Helper\Data;
11+
use Magento\Paypal\Model\Config;
12+
use Magento\Paypal\Model\ConfigFactory;
13+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
14+
15+
class BillingAgreementTest extends \PHPUnit_Framework_TestCase
16+
{
17+
18+
/**
19+
* @var CurrentCustomer | \PHPUnit_Framework_MockObject_MockObject
20+
*/
21+
private $currentCustomer;
22+
23+
/**
24+
* @var Data | \PHPUnit_Framework_MockObject_MockObject
25+
*/
26+
private $paypalData;
27+
28+
/**
29+
* @var Config|\PHPUnit_Framework_MockObject_MockObject
30+
*/
31+
private $paypalConfig;
32+
33+
/**
34+
* @var BillingAgreement
35+
*/
36+
private $billingAgreement;
37+
38+
protected function setUp()
39+
{
40+
$this->paypalConfig = $this->getMock(Config::class, [], [], '', false);
41+
$this->paypalConfig
42+
->expects($this->once())
43+
->method('setMethod')
44+
->will($this->returnSelf());
45+
46+
$this->paypalConfig->expects($this->once())
47+
->method('setMethod')
48+
->with(Config::METHOD_EXPRESS);
49+
50+
$paypalConfigFactory = $this->getMock(ConfigFactory::class, ['create'], [], '', false);
51+
$paypalConfigFactory->expects($this->once())
52+
->method('create')
53+
->will($this->returnValue($this->paypalConfig));
54+
55+
$customerId = 20;
56+
$this->currentCustomer = $this->getMock(CurrentCustomer::class, [], [], '', false);
57+
$this->currentCustomer->expects($this->any())
58+
->method('getCustomerId')
59+
->willReturn($customerId);
60+
61+
$this->paypalData = $this->getMock(Data::class, [], [], '', false);
62+
63+
$helper = new ObjectManager($this);
64+
$this->billingAgreement = $helper->getObject(
65+
BillingAgreement::class,
66+
[
67+
'paypalConfigFactory' => $paypalConfigFactory,
68+
'paypalData' => $this->paypalData,
69+
'currentCustomer' => $this->currentCustomer
70+
]
71+
);
72+
}
73+
74+
public function testGetSectionData()
75+
{
76+
$this->paypalData->expects($this->once())
77+
->method('shouldAskToCreateBillingAgreement')
78+
->with($this->paypalConfig, $this->currentCustomer->getCustomerId())
79+
->willReturn(true);
80+
81+
$result = $this->billingAgreement->getSectionData();
82+
83+
$this->assertArrayHasKey('askToCreate', $result);
84+
$this->assertArrayHasKey('confirmUrl', $result);
85+
$this->assertArrayHasKey('confirmMessage', $result);
86+
$this->assertTrue($result['askToCreate']);
87+
}
88+
89+
public function testGetSectionDataNotNeedToCreateBillingAgreement()
90+
{
91+
$this->paypalData->expects($this->once())
92+
->method('shouldAskToCreateBillingAgreement')
93+
->with($this->paypalConfig, $this->currentCustomer->getCustomerId())
94+
->willReturn(false);
95+
96+
$result = $this->billingAgreement->getSectionData();
97+
98+
$this->assertEmpty($result);
99+
}
100+
}

app/code/Magento/Paypal/etc/frontend/di.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,11 @@
105105
</argument>
106106
</arguments>
107107
</type>
108+
<type name="Magento\Customer\CustomerData\SectionPool">
109+
<arguments>
110+
<argument name="sectionSourceMap" xsi:type="array">
111+
<item name="paypal-billing-agreement" xsi:type="string">Magento\Paypal\CustomerData\BillingAgreement</item>
112+
</argument>
113+
</arguments>
114+
</type>
108115
</config>

app/code/Magento/Paypal/view/frontend/templates/billing/agreement/view.phtml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ $relatedOrders = $block->getRelatedOrders();
1717
<?php echo $block->escapeHtml($block->getReferenceId()); ?>
1818
</strong>
1919
<?php if ($block->getCanCancel()): ?>
20-
<button type="button" title="<?php echo $block->escapeHtml(__('Cancel')); ?>"
21-
class="secondary action cancel" onclick="if( confirm('<?php
22-
echo $block->escapeHtml(__('Are you sure you want to do this?')); ?>') ) { window.location.href = '<?php
23-
echo $block->escapeUrl($block->getCancelUrl()) ?>'; } return false;">
20+
<button data-mage-init='{"Magento_Paypal/js/in-context/billing-agreement": {
21+
"cancelMessage" : "<?php echo $block->escapeHtml(__('Are you sure you want to do this?')); ?>",
22+
"cancelUrl" : "<?php echo $block->escapeUrl($block->getCancelUrl()) ?>"
23+
}}'
24+
type="button" title="<?php echo $block->escapeHtml(__('Cancel')); ?>"
25+
class="secondary action cancel" />
2426
<span><?php echo $block->escapeHtml(__('Cancel')); ?></span>
2527
</button>
2628
<?php endif; ?>

app/code/Magento/Paypal/view/frontend/templates/checkout/onepage/success/billing_agreement.phtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* @var \Magento\Paypal\Block\Checkout\Onepage\Success\BillingAgreement $block
1010
*/
1111
?>
12-
<p>
12+
<p data-mage-init='{"Magento_Paypal/js/in-context/billing-agreement": {"invalidateOnLoad" : true}}'>
1313
<?php echo $block->escapeHtml(__('Your billing agreement # is: ')); ?>
1414
<a href="<?php echo $block->escapeUrl($block->getAgreementUrl()); ?>">
1515
<?php echo $block->escapeHtml($block->getAgreementRefId()); ?>

app/code/Magento/Paypal/view/frontend/templates/express/shortcut.phtml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@ if ($block->isOrPositionBefore()) {
1717
$labelPosition = ' after';
1818
}
1919
$shortcutHtmlId = $block->escapeHtml($block->getShortcutHtmlId());
20-
$confirmationUrl = '';
2120
$isInCatalogProduct = false;
22-
if ($block->getConfirmationUrl() || $block->getIsInCatalogProduct()) {
23-
$confirmationUrl = $block->escapeUrl($block->getConfirmationUrl());
21+
if ($block->getIsInCatalogProduct()) {
2422
$isInCatalogProduct = $block->getIsInCatalogProduct();
2523
}
2624
?>
@@ -29,8 +27,6 @@ if ($block->getConfirmationUrl() || $block->getIsInCatalogProduct()) {
2927
<?php /* @noEscape */ echo $shortcutHtmlId; ?>"
3028
data-mage-init='{
3129
"paypalCheckout": {
32-
"confirmMessage": "<?php /* @noEscape */ echo $block->escapeJsQuote($block->getConfirmationMessage()); ?>",
33-
"confirmUrl": "<?php /* @noEscape */ echo !empty($confirmationUrl) ? $confirmationUrl : false; ?>",
3430
"isCatalogProduct": "<?php /* @noEscape */ echo !empty($isInCatalogProduct) ? (bool)$isInCatalogProduct : false;?>",
3531
"shortcutContainerClass": "<?php /* @noEscape */ echo "." . $shortcutHtmlId; ?>"
3632
}

0 commit comments

Comments
 (0)