Skip to content

Commit

Permalink
Merge remote-tracking branch 'mpi/MAGETWO-48372-2' into pr-mpi-200416
Browse files Browse the repository at this point in the history
  • Loading branch information
oshmyheliuk committed Apr 21, 2016
2 parents 0ead714 + e1d81b8 commit 9d70111
Show file tree
Hide file tree
Showing 9 changed files with 285 additions and 48 deletions.
32 changes: 0 additions & 32 deletions app/code/Magento/Paypal/Block/Express/Shortcut.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,6 @@ class Shortcut extends \Magento\Framework\View\Element\Template implements Catal
*/
protected $_alias = '';

/**
* Paypal data
*
* @var \Magento\Paypal\Helper\Data
*/
protected $_paypalData;

/**
* @var \Magento\Paypal\Model\ConfigFactory
*/
Expand All @@ -78,11 +71,6 @@ class Shortcut extends \Magento\Framework\View\Element\Template implements Catal
*/
protected $_mathRandom;

/**
* @var \Magento\Customer\Helper\Session\CurrentCustomer
*/
protected $currentCustomer;

/**
* @var \Magento\Framework\Locale\ResolverInterface
*/
Expand All @@ -100,11 +88,9 @@ class Shortcut extends \Magento\Framework\View\Element\Template implements Catal

/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Paypal\Helper\Data $paypalData
* @param \Magento\Paypal\Model\ConfigFactory $paypalConfigFactory
* @param \Magento\Paypal\Model\Express\Checkout\Factory $checkoutFactory
* @param \Magento\Framework\Math\Random $mathRandom
* @param \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer
* @param \Magento\Framework\Locale\ResolverInterface $localeResolver
* @param ValidatorInterface $shortcutValidator
* @param string $paymentMethodCode
Expand All @@ -118,11 +104,9 @@ class Shortcut extends \Magento\Framework\View\Element\Template implements Catal
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Paypal\Helper\Data $paypalData,
\Magento\Paypal\Model\ConfigFactory $paypalConfigFactory,
\Magento\Paypal\Model\Express\Checkout\Factory $checkoutFactory,
\Magento\Framework\Math\Random $mathRandom,
\Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer,
\Magento\Framework\Locale\ResolverInterface $localeResolver,
ValidatorInterface $shortcutValidator,
$paymentMethodCode,
Expand All @@ -133,7 +117,6 @@ public function __construct(
\Magento\Checkout\Model\Session $checkoutSession = null,
array $data = []
) {
$this->_paypalData = $paypalData;
$this->_paypalConfigFactory = $paypalConfigFactory;
$this->_checkoutSession = $checkoutSession;
$this->_checkoutFactory = $checkoutFactory;
Expand All @@ -151,7 +134,6 @@ public function __construct(

$this->config = $this->_paypalConfigFactory->create();
$this->config->setMethod($this->_paymentMethodCode);
$this->currentCustomer = $currentCustomer;
}

/**
Expand Down Expand Up @@ -187,20 +169,6 @@ protected function _beforeToHtml()
$this->setImageUrl($checkoutModel->getCheckoutShortcutImageUrl());
}

// ask whether to create a billing agreement
$customerId = $this->currentCustomer->getCustomerId(); // potential issue for caching
if ($this->_paypalData->shouldAskToCreateBillingAgreement($this->config, $customerId)) {
$this->setConfirmationUrl(
$this->getUrl(
$this->_startAction,
[\Magento\Paypal\Model\Express\Checkout::PAYMENT_INFO_TRANSPORT_BILLING_AGREEMENT => 1]
)
);
$this->setConfirmationMessage(
__('Would you like to sign a billing agreement to streamline further purchases with PayPal?')
);
}

return $result;
}

Expand Down
104 changes: 104 additions & 0 deletions app/code/Magento/Paypal/CustomerData/BillingAgreement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Paypal\CustomerData;

use Magento\Customer\CustomerData\SectionSourceInterface;
use Magento\Customer\Helper\Session\CurrentCustomer;
use Magento\Framework\Escaper;
use Magento\Framework\UrlInterface;
use Magento\Paypal\Helper\Data;
use Magento\Paypal\Model\Config;
use Magento\Paypal\Model\ConfigFactory;

/**
* BillingAgreement section
*/
class BillingAgreement implements SectionSourceInterface
{
/**
* @var CurrentCustomer
*/
private $currentCustomer;

/**
* Paypal data
*
* @var Data
*/
private $paypalData;

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

/**
* Url Builder
*
* @var UrlInterface
*/
private $urlBuilder;

/**
* Escaper
*
* @var Escaper
*/
private $escaper;

/**
* Start express action
*
* @var string
*/
private $startAction = 'paypal/express/start/button/1';

/**
* @param CurrentCustomer $currentCustomer
* @param Data $paypalData
* @param ConfigFactory $paypalConfigFactory
* @param UrlInterface $urlBuilder
* @param Escaper $escaper
*/
public function __construct(
CurrentCustomer $currentCustomer,
Data $paypalData,
ConfigFactory $paypalConfigFactory,
UrlInterface $urlBuilder,
Escaper $escaper
) {
$this->currentCustomer = $currentCustomer;
$this->paypalData = $paypalData;
$this->urlBuilder = $urlBuilder;
$this->escaper = $escaper;
$this->config = $paypalConfigFactory->create();
$this->config->setMethod(Config::METHOD_EXPRESS);
}

/**
* {@inheritdoc}
*/
public function getSectionData()
{
$customerId = $this->currentCustomer->getCustomerId();
if ($this->paypalData->shouldAskToCreateBillingAgreement($this->config, $customerId)) {
return [
'askToCreate' => true,
'confirmUrl' => $this->escaper->escapeUrl(
$this->urlBuilder->getUrl(
$this->startAction,
[\Magento\Paypal\Model\Express\Checkout::PAYMENT_INFO_TRANSPORT_BILLING_AGREEMENT => 1]
)
),
'confirmMessage' => $this->escaper->escapeJsQuote(
__('Would you like to sign a billing agreement to streamline further purchases with PayPal?')
)
];
}

return [];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Paypal\Test\Unit\CustomerData;

use Magento\Customer\Helper\Session\CurrentCustomer;
use Magento\Paypal\CustomerData\BillingAgreement;
use Magento\Paypal\Helper\Data;
use Magento\Paypal\Model\Config;
use Magento\Paypal\Model\ConfigFactory;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;

class BillingAgreementTest extends \PHPUnit_Framework_TestCase
{

/**
* @var CurrentCustomer | \PHPUnit_Framework_MockObject_MockObject
*/
private $currentCustomer;

/**
* @var Data | \PHPUnit_Framework_MockObject_MockObject
*/
private $paypalData;

/**
* @var Config|\PHPUnit_Framework_MockObject_MockObject
*/
private $paypalConfig;

/**
* @var BillingAgreement
*/
private $billingAgreement;

protected function setUp()
{
$this->paypalConfig = $this->getMock(Config::class, [], [], '', false);
$this->paypalConfig
->expects($this->once())
->method('setMethod')
->will($this->returnSelf());

$this->paypalConfig->expects($this->once())
->method('setMethod')
->with(Config::METHOD_EXPRESS);

$paypalConfigFactory = $this->getMock(ConfigFactory::class, ['create'], [], '', false);
$paypalConfigFactory->expects($this->once())
->method('create')
->will($this->returnValue($this->paypalConfig));

$customerId = 20;
$this->currentCustomer = $this->getMock(CurrentCustomer::class, [], [], '', false);
$this->currentCustomer->expects($this->any())
->method('getCustomerId')
->willReturn($customerId);

$this->paypalData = $this->getMock(Data::class, [], [], '', false);

$helper = new ObjectManager($this);
$this->billingAgreement = $helper->getObject(
BillingAgreement::class,
[
'paypalConfigFactory' => $paypalConfigFactory,
'paypalData' => $this->paypalData,
'currentCustomer' => $this->currentCustomer
]
);
}

public function testGetSectionData()
{
$this->paypalData->expects($this->once())
->method('shouldAskToCreateBillingAgreement')
->with($this->paypalConfig, $this->currentCustomer->getCustomerId())
->willReturn(true);

$result = $this->billingAgreement->getSectionData();

$this->assertArrayHasKey('askToCreate', $result);
$this->assertArrayHasKey('confirmUrl', $result);
$this->assertArrayHasKey('confirmMessage', $result);
$this->assertTrue($result['askToCreate']);
}

public function testGetSectionDataNotNeedToCreateBillingAgreement()
{
$this->paypalData->expects($this->once())
->method('shouldAskToCreateBillingAgreement')
->with($this->paypalConfig, $this->currentCustomer->getCustomerId())
->willReturn(false);

$result = $this->billingAgreement->getSectionData();

$this->assertEmpty($result);
}
}
7 changes: 7 additions & 0 deletions app/code/Magento/Paypal/etc/frontend/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,11 @@
</argument>
</arguments>
</type>
<type name="Magento\Customer\CustomerData\SectionPool">
<arguments>
<argument name="sectionSourceMap" xsi:type="array">
<item name="paypal-billing-agreement" xsi:type="string">Magento\Paypal\CustomerData\BillingAgreement</item>
</argument>
</arguments>
</type>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ $relatedOrders = $block->getRelatedOrders();
<?php echo $block->escapeHtml($block->getReferenceId()); ?>
</strong>
<?php if ($block->getCanCancel()): ?>
<button type="button" title="<?php echo $block->escapeHtml(__('Cancel')); ?>"
class="secondary action cancel" onclick="if( confirm('<?php
echo $block->escapeHtml(__('Are you sure you want to do this?')); ?>') ) { window.location.href = '<?php
echo $block->escapeUrl($block->getCancelUrl()) ?>'; } return false;">
<button data-mage-init='{"Magento_Paypal/js/in-context/billing-agreement": {
"cancelMessage" : "<?php echo $block->escapeHtml(__('Are you sure you want to do this?')); ?>",
"cancelUrl" : "<?php echo $block->escapeUrl($block->getCancelUrl()) ?>"
}}'
type="button" title="<?php echo $block->escapeHtml(__('Cancel')); ?>"
class="secondary action cancel" />
<span><?php echo $block->escapeHtml(__('Cancel')); ?></span>
</button>
<?php endif; ?>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @var \Magento\Paypal\Block\Checkout\Onepage\Success\BillingAgreement $block
*/
?>
<p>
<p data-mage-init='{"Magento_Paypal/js/in-context/billing-agreement": {"invalidateOnLoad" : true}}'>
<?php echo $block->escapeHtml(__('Your billing agreement # is: ')); ?>
<a href="<?php echo $block->escapeUrl($block->getAgreementUrl()); ?>">
<?php echo $block->escapeHtml($block->getAgreementRefId()); ?>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ if ($block->isOrPositionBefore()) {
$labelPosition = ' after';
}
$shortcutHtmlId = $block->escapeHtml($block->getShortcutHtmlId());
$confirmationUrl = '';
$isInCatalogProduct = false;
if ($block->getConfirmationUrl() || $block->getIsInCatalogProduct()) {
$confirmationUrl = $block->escapeUrl($block->getConfirmationUrl());
if ($block->getIsInCatalogProduct()) {
$isInCatalogProduct = $block->getIsInCatalogProduct();
}
?>
Expand All @@ -29,8 +27,6 @@ if ($block->getConfirmationUrl() || $block->getIsInCatalogProduct()) {
<?php /* @noEscape */ echo $shortcutHtmlId; ?>"
data-mage-init='{
"paypalCheckout": {
"confirmMessage": "<?php /* @noEscape */ echo $block->escapeJsQuote($block->getConfirmationMessage()); ?>",
"confirmUrl": "<?php /* @noEscape */ echo !empty($confirmationUrl) ? $confirmationUrl : false; ?>",
"isCatalogProduct": "<?php /* @noEscape */ echo !empty($isInCatalogProduct) ? (bool)$isInCatalogProduct : false;?>",
"shortcutContainerClass": "<?php /* @noEscape */ echo "." . $shortcutHtmlId; ?>"
}
Expand Down
Loading

0 comments on commit 9d70111

Please sign in to comment.