Skip to content
This repository has been archived by the owner on Aug 6, 2021. It is now read-only.

Commit

Permalink
PLGMAGTWOS-539: Prevent selection of billing address for Pay After De…
Browse files Browse the repository at this point in the history
…livery
  • Loading branch information
vinodsowdagar authored and mikededecker1 committed Dec 11, 2019
1 parent 453306f commit ce1790f
Show file tree
Hide file tree
Showing 13 changed files with 342 additions and 8 deletions.
29 changes: 29 additions & 0 deletions Helper/AddressHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace MultiSafepay\Connect\Helper;

class AddressHelper
{
/**
* @param $address
* @return bool|string
*/
public function serializeAddress($address)
{
return serialize([
'street' => $address->getStreet(),
'city' => $address->getCity(),
'postcode' => $address->getPostcode()
]);
}

/**
* @param $shippingAddress
* @param $billingAddress
* @return bool
*/
public function isSameAddress($shippingAddress, $billingAddress): bool
{
return $this->serializeAddress($shippingAddress) === $this->serializeAddress($billingAddress);
}
}
8 changes: 8 additions & 0 deletions Model/Connect.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
use Magento\Sales\Model\OrderNotifier;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;
use MultiSafepay\Connect\Helper\AddressHelper;
use MultiSafepay\Connect\Helper\Data as HelperData;
use MultiSafepay\Connect\Helper\RefundHelper;
use MultiSafepay\Connect\Model\Api\MspClient;
Expand Down Expand Up @@ -223,6 +224,10 @@ class Connect extends \Magento\Payment\Model\Method\AbstractMethod
protected $restrictions;

protected $dataObjectFactory;
/**
* @var AddressHelper
*/
protected $addressHelper;

/**
* Connect constructor.
Expand Down Expand Up @@ -255,6 +260,7 @@ class Connect extends \Magento\Payment\Model\Method\AbstractMethod
* @param \MultiSafepay\Connect\Helper\Data $helperData
* @param \MultiSafepay\Connect\Model\Config\Source\Creditcards $creditcards
* @param \Magento\Customer\Model\Session $customerSession
* @param \MultiSafepay\Connect\Helper\AddressHelper $addressHelper
* @param \MultiSafepay\Connect\Helper\RefundHelper $refundHelper
* @param \MultiSafepay\Connect\Model\GatewayRestrictions $restrictions
* @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource
Expand Down Expand Up @@ -292,6 +298,7 @@ public function __construct(
Creditcards $creditcards,
\Magento\Customer\Model\Session $customerSession,
RefundHelper $refundHelper,
AddressHelper $addressHelper,
GatewayRestrictions $restrictions,
DataObjectFactory $dataObjectFactory,
AbstractResource $resource = null,
Expand Down Expand Up @@ -319,6 +326,7 @@ public function __construct(
$this->_urlBuilder = $urlBuilder;
$this->_requestHttp = $requestHttp;
$this->_currencyFactory = $currencyFactory;
$this->addressHelper = $addressHelper;
$this->refundHelper = $refundHelper;
$this->restrictions = $restrictions;

Expand Down
13 changes: 12 additions & 1 deletion Model/Gateways/Betaalnaontvangst.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,19 @@

class Betaalnaontvangst extends \MultiSafepay\Connect\Model\Connect
{

protected $_code = 'betaalnaontvangst';
public $_gatewayCode = 'PAYAFTER';
protected $_canUseInternal = false;

public function validate()
{
$quote = $this->_checkoutSession->getQuote();

if (!$this->addressHelper->isSameAddress($quote->getShippingAddress(), $quote->getBillingAddress())) {
throw new \Magento\Framework\Exception\LocalizedException(
__('This gateway does not allow a different billing and shipping address')
);
}
return parent::validate();
}
}
121 changes: 121 additions & 0 deletions Test/Integration/Helper/AddressHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

namespace MultiSafepay\Connect\Test\Integration\Helper;

use Magento\Sales\Model\Order\Address;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\ObjectManager;
use MultiSafepay\Connect\Helper\AddressHelper;
use PHPUnit\Framework\TestCase;

class AddressHelperTest extends TestCase
{
/**
* @var \Magento\Framework\App\ObjectManager
*/
private $objectManager;

/**
* @var AddressHelper
*/
private $addressHelperInstance;

/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();

$this->objectManager = ObjectManager::getInstance();
$this->addressHelperInstance = $this->objectManager->get(AddressHelper::class);
}

/**
* @return Address
*/
private function getShippingAddress(): Address
{
/** @var $address Address */
$address = Bootstrap::getObjectManager()->create(Address::class);

$address->setRegion('NL')
->setPostcode('1033SC')
->setFirstname('MultiSafepayFirstName')
->setLastname('MultiSafepayLastName')
->setStreet('Kraanspoor 39')
->setCity('Amsterdam')
->setEmail('test@example.com')
->setTelephone('0208500500')
->setCountryId('NL')
->setAddressType('shipping')
->save();

return $address;
}

/**
* @return Address
*/
private function getBillingAddress(): Address
{
/** @var $address Address */
$address = Bootstrap::getObjectManager()->create(Address::class);

$address->setRegion('CA')
->setPostcode('90210')
->setFirstname('TestFirstName')
->setLastname('TestLastName')
->setStreet('teststreet 50')
->setCity('Beverly Hills')
->setEmail('admin@example.com')
->setTelephone('1111111111')
->setCountryId('US')
->setAddressType('billing')
->save();

return $address;
}

/**
* @param $string
* @return bool
*/
private function isSerialized($string): bool
{
return unserialize($string) !== false;
}

public function testSerializeAddressShouldReturnSerializedAddress()
{
$address = $this->getShippingAddress();

$result = $this->addressHelperInstance->serializeAddress($address);

$this->assertInternalType('string', $result);
$this->assertTrue($this->isSerialized($result));
}

public function testIsSameAddressShouldReturnFalseIfDifferentBillingAndShipping()
{
$shippingAddress = $this->getShippingAddress();
$billingAddress = $this->getBillingAddress();

$result = $this->addressHelperInstance->isSameAddress($shippingAddress, $billingAddress);

$this->assertFalse($result);
}

public function testIsSameAddressShouldReturnTrueIfSameBillingAndShipping()
{
$shippingAddress = $this->getShippingAddress();
$billingAddress = $this->getBillingAddress()
->setStreet('Kraanspoor 39')
->setPostcode('1033SC')
->setCity('Amsterdam');

$result = $this->addressHelperInstance->isSameAddress($shippingAddress, $billingAddress);

$this->assertTrue($result);
}
}
151 changes: 151 additions & 0 deletions Test/Integration/Model/Gateways/BetaalnaontvangstTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php


namespace MultiSafepay\Connect\Test\Integration\Model\Gateways;

use Magento\CatalogInventory\Api\StockRegistryInterface;
use Magento\Checkout\Model\Session;
use Magento\Directory\Model\CurrencyFactory;
use Magento\Framework\Api\AttributeValueFactory;
use Magento\Framework\Api\ExtensionAttributesFactory;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\ProductMetadataInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Data\Collection\AbstractDb;
use Magento\Framework\DataObjectFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Locale\Resolver;
use Magento\Framework\Model\Context;
use Magento\Framework\Model\ResourceModel\AbstractResource;
use Magento\Framework\Module\ModuleListInterface;
use Magento\Framework\Registry;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
use Magento\Framework\UrlInterface;
use Magento\Payment\Helper\Data;
use Magento\Payment\Model\Method\Logger;
use Magento\Quote\Model\Quote;
use Magento\Quote\Model\Quote\Address;
use Magento\Sales\Api\InvoiceRepositoryInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Api\TransactionRepositoryInterface;
use Magento\Sales\Model\Order\Email\Sender\InvoiceSender;
use Magento\Sales\Model\Order\StatusResolver;
use Magento\Sales\Model\OrderNotifier;
use Magento\Store\Model\StoreManagerInterface;
use Magento\TestFramework\ObjectManager;
use MultiSafepay\Connect\Helper\AddressHelper;
use MultiSafepay\Connect\Helper\Data as HelperData;
use MultiSafepay\Connect\Helper\RefundHelper;
use MultiSafepay\Connect\Model\Api\MspClient;
use MultiSafepay\Connect\Model\Config\Source\Creditcards;
use MultiSafepay\Connect\Model\GatewayRestrictions;
use MultiSafepay\Connect\Model\Gateways\Betaalnaontvangst;
use MultiSafepay\Connect\Model\MultisafepayTokenizationFactory;
use PHPUnit\Framework\TestCase;

class BetaalnaontvangstTest extends TestCase
{
/**
* @var \Magento\Framework\App\ObjectManager
*/
private $objectManager;

/**
* @var Betaalnaontvangst
*/
private $payAfterInstance;

/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();

$this->objectManager = ObjectManager::getInstance();

$checkoutSession = $this->getMockBuilder(Session::class)->disableOriginalConstructor()->getMock();
$checkoutSession->method('getQuote')->willReturn($this->createQuote());

$this->payAfterInstance = $this->getMockBuilder(Betaalnaontvangst::class)->setConstructorArgs(
[
$this->objectManager->create(Context::class),
$this->objectManager->create(Registry::class),
$this->objectManager->create(ExtensionAttributesFactory::class),
$this->objectManager->create(AttributeValueFactory::class),
$this->objectManager->create(Data::class),
$this->objectManager->create(ScopeConfigInterface::class),
$this->objectManager->create(Logger::class),
$this->objectManager->create(ModuleListInterface::class),
$this->objectManager->create(TimezoneInterface::class),
$this->objectManager->create(StoreManagerInterface::class),
$checkoutSession,
$this->objectManager->create(UrlInterface::class),
$this->objectManager->create(RequestInterface::class),
$this->objectManager->create(StockRegistryInterface::class),
$this->objectManager->create(InvoiceSender::class),
$this->objectManager->create(ProductMetadataInterface::class),
$this->objectManager->create(InvoiceRepositoryInterface::class),
$this->objectManager->create(TransactionRepositoryInterface::class),
$this->objectManager->create(Resolver::class),
$this->objectManager->create(OrderRepositoryInterface::class),
$this->objectManager->create(OrderNotifier::class),
$this->objectManager->create(StatusResolver::class),
$this->objectManager->create(CurrencyFactory::class),
$this->objectManager->create(MultisafepayTokenizationFactory::class),
$this->objectManager->create(MspClient::class),
$this->objectManager->create(HelperData::class),
$this->objectManager->create(Creditcards::class),
$this->objectManager->create(\Magento\Customer\Model\Session::class),
$this->objectManager->create(RefundHelper::class),
$this->objectManager->create(AddressHelper::class),
$this->objectManager->create(GatewayRestrictions::class),
$this->objectManager->create(DataObjectFactory::class),
$this->getMockBuilder(AbstractResource::class)->disableOriginalConstructor()->getMock(),
$this->getMockBuilder(AbstractDb::class)->disableOriginalConstructor()->getMock(),
]
)->setMethodsExcept(['validate'])->getMock();
}

/**
* @return Quote
*/
protected function createQuote(): Quote
{
/** @var Address $quoteShippingAddress */
$quoteShippingAddress = $this->objectManager->create(Address::class);
$quoteShippingAddress->setStreet('Kraanspoor 39')
->setPostcode('1033SC')
->setCity('Amsterdam');

/** @var Address $quoteBillingAddress */
$quoteBillingAddress = $this->objectManager->create(Address::class);
$quoteBillingAddress->setStreet('teststreet 50')
->setPostcode('90210')
->setCity('Beverly Hills');

/** @var Quote $quote */
$quote = $this->objectManager->create(Quote::class);
$quote->setStoreId(1)
->setIsActive(true)
->setIsMultiShipping(false)
->setShippingAddress($quoteShippingAddress)
->setBillingAddress($quoteBillingAddress)
->setCheckoutMethod('customer')
->setReservedOrderId('test_order_1')
->setCustomerEmail('test@example.com')
->setQuoteCurrencyCode('EUR');

return $quote;
}

/**
* @throws LocalizedException
*/
public function testValidateShouldThrowErrorIfDifferentShippingAndBilling()
{
$this->expectException(LocalizedException::class);
$this->expectExceptionMessage('This gateway does not allow a different billing and shipping address');
$this->payAfterInstance->validate();
}
}
3 changes: 2 additions & 1 deletion i18n/de_DE.csv
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,5 @@ Newsletter,Newsletter
"Order", "Bestellen"
"@","@"
"Notice: Captured amount %1 differs from MultiSafepay amount %2", "Notice: Captured amount %1 differs from MultiSafepay amount %2"
"Payment methods specification","Payment methods specification"
"Payment methods specification","Payment methods specification"
"This gateway does not allow a different billing and shipping address","Dieses Zahlungsmethode erlaubt keine abweichende Rechnungs- und Lieferadresse"
3 changes: 2 additions & 1 deletion i18n/en_US.csv
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,5 @@ Newsletter,Newsletter
"Order", "Order"
"@","@"
"Notice: Captured amount %1 differs from MultiSafepay amount %2", "Notice: Captured amount %1 differs from MultiSafepay amount %2"
"Payment methods specification","Payment methods specification"
"Payment methods specification","Payment methods specification"
"This gateway does not allow a different billing and shipping address","This gateway does not allow a different billing and shipping address"
3 changes: 2 additions & 1 deletion i18n/es_ES.csv
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,5 @@ Newsletter,Newsletter
"Order", "Orden"
"@","@"
"Notice: Captured amount %1 differs from MultiSafepay amount %2", "Notice: Captured amount %1 differs from MultiSafepay amount %2"
"Payment methods specification","Payment methods specification"
"Payment methods specification","Payment methods specification"
"This gateway does not allow a different billing and shipping address","This gateway does not allow a different billing and shipping address"
Loading

0 comments on commit ce1790f

Please sign in to comment.