forked from magento/magento2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'mpi/MAGETWO-48372-2' into pr-mpi-200416
- Loading branch information
Showing
9 changed files
with
285 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
app/code/Magento/Paypal/CustomerData/BillingAgreement.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 []; | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
app/code/Magento/Paypal/Test/Unit/CustomerData/BillingAgreementTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.