├── .gitignore ├── .travis.yml ├── README.md ├── build.xml ├── composer.json ├── phpunit.xml.dist ├── src ├── Model │ ├── Entity │ │ └── Attribute │ │ │ └── Source │ │ │ └── PaymentMethods.php │ ├── Filter │ │ ├── Customer.php │ │ ├── Guest.php │ │ └── QuoteContent.php │ └── FilterInterface.php ├── Observer │ └── ExcludeDisallowedPaymentMethod.php ├── Setup │ └── InstallData.php ├── etc │ ├── adminhtml │ │ └── system.xml │ ├── catalog_attributes.xml │ ├── di.xml │ ├── events.xml │ └── module.xml └── registration.php ├── tests └── Unit │ ├── Model │ ├── Entity │ │ └── Attribute │ │ │ └── Source │ │ │ └── PaymentMethodsTest.php │ └── Filter │ │ ├── CustomerTest.php │ │ ├── GuestTest.php │ │ └── QuoteContentTest.php │ ├── Observer │ └── ExcludeDisallowedPaymentMethodTest.php │ └── bootstrap.php └── var └── generation └── Magento ├── Catalog └── Api │ └── Data │ └── ProductExtensionInterface.php ├── Customer └── Api │ └── Data │ └── CustomerExtensionInterface.php └── Quote └── Api └── Data ├── CartExtensionInterface.php └── CartItemExtensionInterface.php /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | 3 | build 4 | composer.lock 5 | vendor -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | php: 3 | - 5.6 4 | - 7.0 5 | before_script: 6 | - echo \{\"http-basic\":\{\"repo.magento.com\":\{\"username\":\"${REPO_MAGENTO_PUBLIC}\",\"password\":\"${REPO_MAGENTO_PRIVATE}\"\}\},\"github-oauth\":\{\"github.com\":\"${GITHUB_TOKEN}\"\}\} > ~/.composer/auth.json 7 | - composer install 8 | script: 9 | - vendor/bin/phing 10 | after_script: 11 | - vendor/bin/coveralls -v -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/daniel-rose/magento2-payment-method-filter.svg?branch=master)](https://travis-ci.org/daniel-rose/magento2-payment-method-filter) [![Coverage Status](https://coveralls.io/repos/github/daniel-rose/magento2-payment-method-filter/badge.svg?branch=master)](https://coveralls.io/github/daniel-rose/magento2-payment-method-filter?branch=master) 2 | 3 | # DR_PaymentMethodFilter 4 | This module excludes payment methods from checkout by using filters. 5 | 6 | ## Description 7 | This module allows to exclude active payment methods from checkout. 8 | 9 | The following table shows the predefined filters which control the state of payment methods: 10 | 11 | |Name|Description| 12 | |---|---| 13 | |GuestFilter|This filter is only for guest checkout. All disallowed payment methods for guests can be set at the system configuration.| 14 | |CustomerFilter|Excludes active payment by the customer attribute "disallowed_payment_methods". Only available for customer checkout.| 15 | |QuoteContentFilter|The product attribute "disallowed_payment_methods" controls which payment method can be removed from the active list. Available for both checkout types.| 16 | 17 | ## Installation 18 | 19 | ### Via composer 20 | Open the command line and run the following commands 21 | ``` 22 | cd PATH_TO_MAGENTO_2_ROOT 23 | composer require dr/payment-method-filter 24 | ``` 25 | 26 | ### Via archive 27 | * Download the ZIP-Archive 28 | * Extract files 29 | * Copy the extracted Files to PATH_TO_MAGENTO_2_ROOT/app/code/DR/PaymentMethodFilter 30 | * Run the following Commands: 31 | ``` 32 | php bin/magento setup:upgrade 33 | php bin/magento setup:di:compile 34 | php bin/magento setup:static-content:deploy 35 | ``` 36 | 37 | ## Support 38 | If you have any issues with this extension, open an issue on GitHub (see URL above). 39 | 40 | ## Contribution 41 | Any contributions are highly appreciated. The best way to contribute code is to open a [pull request on GitHub](https://help.github.com/articles/using-pull-requests/). 42 | 43 | ## Developer 44 | Daniel Rose 45 | 46 | * Xing: https://www.xing.com/profile/Daniel_Rose16 47 | 48 | ## Licence 49 | [MIT License](https://opensource.org/licenses/MIT) 50 | 51 | ## Copyright 52 | (c) 2016 Daniel Rose -------------------------------------------------------------------------------- /build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 18 | 19 | 22 | 23 | 26 | 27 | 30 | 31 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dr/payment-method-filter", 3 | "description": "This module excludes payment methods from checkout by using filters.", 4 | "authors": [ 5 | { 6 | "name": "Daniel Rose", 7 | "email": "daniel-rose@gmx.de" 8 | } 9 | ], 10 | "type": "magento2-module", 11 | "repositories": [ 12 | { 13 | "type": "composer", 14 | "url": "https://repo.magento.com/" 15 | } 16 | ], 17 | "require": { 18 | "magento/module-payment": "100.3.*|100.2.*" 19 | }, 20 | "require-dev": { 21 | "phpunit/phpunit": "^6.2.4", 22 | "squizlabs/php_codesniffer": "^3.0.1", 23 | "phpmd/phpmd": "@stable", 24 | "pdepend/pdepend": "^2.5.0", 25 | "sebastian/phpcpd": "^2.0.0", 26 | "zendframework/zend-http": "~2.7.0", 27 | "phing/phing": "@stable", 28 | "satooshi/php-coveralls": "@stable", 29 | "magento/module-offline-payments": "100.2.*" 30 | }, 31 | "license": "MIT", 32 | "keywords": [ 33 | "Magento 2", 34 | "Payment method filter" 35 | ], 36 | "autoload": { 37 | "files": [ 38 | "src/registration.php" 39 | ], 40 | "psr-4": { 41 | "DR\\PaymentMethodFilter\\": "src" 42 | } 43 | }, 44 | "autoload-dev": { 45 | "files": [ 46 | "var/generation/Magento/Quote/Api/Data/CartExtensionInterface.php", 47 | "var/generation/Magento/Quote/Api/Data/CartItemExtensionInterface.php", 48 | "var/generation/Magento/Catalog/Api/Data/ProductExtensionInterface.php", 49 | "var/generation/Magento/Customer/Api/Data/CustomerExtensionInterface.php" 50 | ], 51 | "psr-4": { 52 | "DR\\PaymentMethodFilter\\Test\\": "tests" 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 11 | tests/Unit/ 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | src 25 | 26 | src/registration.php 27 | test 28 | vendor 29 | var 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /src/Model/Entity/Attribute/Source/PaymentMethods.php: -------------------------------------------------------------------------------- 1 | paymentData = $paymentData; 25 | } 26 | 27 | 28 | /** 29 | * Retrieve All options 30 | * 31 | * @return array 32 | */ 33 | public function getAllOptions() 34 | { 35 | if ($this->_options === null) { 36 | $this->_options = []; 37 | $paymentMethods = $this->paymentData->getPaymentMethods(); 38 | 39 | foreach ($paymentMethods as $code => $paymentMethod) { 40 | if (isset($paymentMethod['title'])) { 41 | $label = $paymentMethod['title']; 42 | } else { 43 | $label = $this->paymentData->getMethodInstance($code)->getConfigData('title', null); 44 | } 45 | 46 | if ($label) { 47 | $this->_options[$code] = [ 48 | 'label' => $code . ' - ' . $label, 49 | 'value' => $code 50 | ]; 51 | } 52 | } 53 | 54 | usort($this->_options, function($a, $b) { 55 | return strcmp($a['value'], $b['value']); 56 | }); 57 | } 58 | 59 | return $this->_options; 60 | } 61 | } -------------------------------------------------------------------------------- /src/Model/Filter/Customer.php: -------------------------------------------------------------------------------- 1 | getCustomer(); 25 | 26 | if (!$customer || !($customer instanceof CustomerInterface) || !$customer->getId()) { 27 | return; 28 | } 29 | 30 | $customAttribute = $customer->getCustomAttribute('disallowed_payment_methods'); 31 | 32 | if ($customAttribute === null) { 33 | return; 34 | } 35 | 36 | $disallowedPaymentMethods = $customAttribute->getValue(); 37 | 38 | if ($disallowedPaymentMethods == '') { 39 | return; 40 | } 41 | 42 | $disallowedPaymentMethods = explode(',', $disallowedPaymentMethods); 43 | $result->setData('is_available', !in_array($paymentMethod->getCode(), $disallowedPaymentMethods)); 44 | } 45 | } -------------------------------------------------------------------------------- /src/Model/Filter/Guest.php: -------------------------------------------------------------------------------- 1 | scopeConfig = $scopeConfig; 31 | } 32 | 33 | 34 | /** 35 | * Execute 36 | * 37 | * @param MethodInterface $paymentMethod 38 | * @param CartInterface $quote 39 | * @param DataObject $result 40 | */ 41 | public function execute(MethodInterface $paymentMethod, CartInterface $quote, DataObject $result) 42 | { 43 | $customer = $quote->getCustomer(); 44 | 45 | if ($customer && ($customer instanceof CustomerInterface) && $customer->getId()) { 46 | return; 47 | } 48 | 49 | $disallowedPaymentMethods = $this->scopeConfig->getValue(self::XML_PATH_DISALLOWED_PAYMENT_METHODS_FOR_GUEST); 50 | 51 | if ($disallowedPaymentMethods === null || $disallowedPaymentMethods === '') { 52 | return; 53 | } 54 | 55 | $disallowedPaymentMethods = explode(',', $disallowedPaymentMethods); 56 | $result->setData('is_available', !in_array($paymentMethod->getCode(), $disallowedPaymentMethods)); 57 | } 58 | } -------------------------------------------------------------------------------- /src/Model/Filter/QuoteContent.php: -------------------------------------------------------------------------------- 1 | getItems(); 24 | 25 | foreach ($visibleItems as $visibleItem) { 26 | $product = $visibleItem->getProduct(); 27 | 28 | $customAttribute = $product->getCustomAttribute('disallowed_payment_methods'); 29 | 30 | 31 | if ($customAttribute === null) { 32 | continue; 33 | } 34 | 35 | $disallowedPaymentMethods = $customAttribute->getValue(); 36 | 37 | if ($disallowedPaymentMethods == '') { 38 | continue; 39 | } 40 | 41 | $disallowedPaymentMethods = explode(',', $disallowedPaymentMethods); 42 | 43 | if (in_array($paymentMethod->getCode(), $disallowedPaymentMethods)) { 44 | $result->setData('is_available', false); 45 | return; 46 | } 47 | } 48 | } 49 | } -------------------------------------------------------------------------------- /src/Model/FilterInterface.php: -------------------------------------------------------------------------------- 1 | filterList = $filterList; 25 | } 26 | 27 | /** 28 | * @param Observer $observer 29 | * @return void 30 | */ 31 | public function execute(Observer $observer) 32 | { 33 | $event = $observer->getEvent(); 34 | 35 | if (!$event || !($event instanceof Event)) { 36 | return; 37 | } 38 | 39 | $result = $event->getResult(); 40 | 41 | if (!$result || !($result instanceof DataObject) || !$result->getIsAvailable()) { 42 | return; 43 | } 44 | 45 | $paymentMethod = $event->getMethodInstance(); 46 | 47 | if (!$paymentMethod || !($paymentMethod instanceof MethodInterface)) { 48 | return; 49 | } 50 | 51 | $quote = $event->getQuote(); 52 | 53 | if (!$quote || !($quote instanceof Quote)) { 54 | return; 55 | } 56 | 57 | foreach ($this->filterList as $filter) { 58 | $filter->execute($paymentMethod, $quote, $result); 59 | 60 | if (!$result->getData('is_available')) { 61 | return; 62 | } 63 | } 64 | } 65 | } -------------------------------------------------------------------------------- /src/Setup/InstallData.php: -------------------------------------------------------------------------------- 1 | customerSetupFactory = $customerSetupFactory; 50 | $this->eavSetupFactory = $eavSetupFactory; 51 | } 52 | 53 | /** 54 | * Installs data for a module 55 | * 56 | * @param ModuleDataSetupInterface $setup 57 | * @param ModuleContextInterface $context 58 | * @return void 59 | */ 60 | public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) 61 | { 62 | /** @var CustomerSetup $customerSetup */ 63 | $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]); 64 | 65 | /** @var EavSetup $eavSetup */ 66 | $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]); 67 | 68 | $setup->startSetup(); 69 | 70 | $customerSetup->addAttribute(Customer::ENTITY, 'disallowed_payment_methods', [ 71 | 'type' => 'text', 72 | 'label' => 'Disallowed Payment Methods', 73 | 'input' => 'multiselect', 74 | 'source' => PaymentMethods::class, 75 | 'backend' => ArrayBackend::class, 76 | 'system' => false, 77 | 'required' => false, 78 | 'position' => 100 79 | ]); 80 | 81 | $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'disallowed_payment_methods') 82 | ->setData('used_in_forms', ['adminhtml_customer']) 83 | ->save(); 84 | 85 | $eavSetup->addAttribute(Product::ENTITY, 'disallowed_payment_methods', [ 86 | 'type' => 'text', 87 | 'label' => 'Disallowed Payment Methods', 88 | 'input' => 'multiselect', 89 | 'source' => PaymentMethods::class, 90 | 'backend' => ArrayBackend::class, 91 | 'required' => false, 92 | 'global' => Attribute::SCOPE_STORE, 93 | 'user_defined' => false, 94 | 'apply_to' => '' 95 | ]); 96 | 97 | $setup->endSetup(); 98 | } 99 | } -------------------------------------------------------------------------------- /src/etc/adminhtml/system.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |
5 | 6 | 7 | 8 | Magento\Payment\Model\Config\Source\Allmethods 9 | 10 | 1 11 | 12 | 13 | 14 |
15 |
16 |
17 | -------------------------------------------------------------------------------- /src/etc/catalog_attributes.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /src/etc/di.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | DR\PaymentMethodFilter\Model\Filter\Guest 7 | DR\PaymentMethodFilter\Model\Filter\Customer 8 | DR\PaymentMethodFilter\Model\Filter\QuoteContent 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /src/etc/events.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /src/etc/module.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/registration.php: -------------------------------------------------------------------------------- 1 | paymentHelperMock = $this->getMockBuilder(Data::class) 40 | ->disableOriginalConstructor() 41 | ->getMock(); 42 | 43 | $this->methodInstanceMock = $this->getMockBuilder(MethodInterface::class) 44 | ->disableOriginalConstructor() 45 | ->getMock(); 46 | 47 | $this->attributeSourceModel = $objectManager->getObject(PaymentMethods::class, [ 48 | 'paymentData' => $this->paymentHelperMock 49 | ]); 50 | } 51 | 52 | /** 53 | * @test 54 | */ 55 | public function testGeTAllOptions() 56 | { 57 | $paymentMethods = [ 58 | 'free' => [ 59 | 'title' => 'No Payment Information Required', 60 | 'group' => 'offline' 61 | ], 62 | 'cashondelivery' => [ 63 | 'group' => 'offline' 64 | ] 65 | ]; 66 | 67 | $this->methodInstanceMock 68 | ->expects($this->atLeastOnce()) 69 | ->method('getConfigData') 70 | ->with('title', null) 71 | ->willReturn('Cash On Delivery'); 72 | 73 | $this->paymentHelperMock 74 | ->expects($this->atLeastOnce()) 75 | ->method('getMethodInstance') 76 | ->with('cashondelivery') 77 | ->willReturn($this->methodInstanceMock); 78 | 79 | $allOptions = [ 80 | [ 81 | 'label' => 'cashondelivery - Cash On Delivery', 82 | 'value' => 'cashondelivery' 83 | ], 84 | [ 85 | 'label' => 'free - No Payment Information Required', 86 | 'value' => 'free' 87 | ] 88 | ]; 89 | 90 | $this->paymentHelperMock 91 | ->expects($this->atLeastOnce()) 92 | ->method('getPaymentMethods') 93 | ->willReturn($paymentMethods); 94 | 95 | $this->assertEquals($allOptions, $this->attributeSourceModel->getAllOptions()); 96 | } 97 | } -------------------------------------------------------------------------------- /tests/Unit/Model/Filter/CustomerTest.php: -------------------------------------------------------------------------------- 1 | paymentMethodMock = $this->getMockBuilder(Cashondelivery::class) 54 | ->disableOriginalConstructor() 55 | ->getMock(); 56 | 57 | $this->quoteMock = $this->getMockBuilder(CartInterface::class) 58 | ->disableOriginalConstructor() 59 | ->getMock(); 60 | 61 | $this->customerMock = $this->getMockBuilder(CustomerInterface::class) 62 | ->disableOriginalConstructor() 63 | ->getMock(); 64 | 65 | $this->attributeMock = $this->getMockBuilder(AttributeInterface::class) 66 | ->getMock(); 67 | 68 | $this->customerFilter = $objectManager->getObject(CustomerFilter::class, []); 69 | 70 | $this->result = $objectManager->getObject(DataObject::class, []); 71 | } 72 | 73 | /** 74 | * @test 75 | */ 76 | public function testExecuteWithCustomerThatIsNotAllowedToPayByCashOnDelivery() 77 | { 78 | $this->quoteMock 79 | ->expects($this->atLeastOnce()) 80 | ->method('getCustomer') 81 | ->willReturn($this->customerMock); 82 | 83 | $this->attributeMock 84 | ->expects($this->atLeastOnce()) 85 | ->method('getValue') 86 | ->willReturn('cashondelivery'); 87 | 88 | $this->customerMock 89 | ->expects($this->atLeastOnce()) 90 | ->method('getId') 91 | ->willReturn(1); 92 | 93 | $this->customerMock 94 | ->expects($this->atLeastOnce()) 95 | ->method('getCustomAttribute') 96 | ->with('disallowed_payment_methods') 97 | ->willReturn($this->attributeMock); 98 | 99 | $this->paymentMethodMock 100 | ->expects($this->atLeastOnce()) 101 | ->method('getCode') 102 | ->willReturn(Cashondelivery::PAYMENT_METHOD_CASHONDELIVERY_CODE); 103 | 104 | $this->result->setData('is_available', true); 105 | 106 | $this->customerFilter->execute($this->paymentMethodMock, $this->quoteMock, $this->result); 107 | 108 | $this->assertFalse($this->result->getData('is_available')); 109 | } 110 | 111 | /** 112 | * @test 113 | */ 114 | public function testExecuteWithCustomerThatIsAllowedToPayByCashOnDelivery() 115 | { 116 | $this->attributeMock 117 | ->expects($this->atLeastOnce()) 118 | ->method('getValue') 119 | ->willReturn(''); 120 | 121 | $this->customerMock 122 | ->expects($this->atLeastOnce()) 123 | ->method('getCustomAttribute') 124 | ->with('disallowed_payment_methods') 125 | ->willReturn($this->attributeMock); 126 | 127 | $this->customerMock 128 | ->expects($this->atLeastOnce()) 129 | ->method('getId') 130 | ->willReturn(1); 131 | 132 | $this->quoteMock 133 | ->expects($this->atLeastOnce()) 134 | ->method('getCustomer') 135 | ->willReturn($this->customerMock); 136 | 137 | $this->paymentMethodMock 138 | ->expects($this->never()) 139 | ->method('getCode'); 140 | 141 | $this->result->setData('is_available', true); 142 | 143 | $this->customerFilter->execute($this->paymentMethodMock, $this->quoteMock, $this->result); 144 | 145 | $this->assertTrue($this->result->getData('is_available')); 146 | } 147 | 148 | /** 149 | * @test 150 | */ 151 | public function testExecuteWithGuest() 152 | { 153 | $this->attributeMock 154 | ->expects($this->never()) 155 | ->method('getValue') 156 | ->willReturn(''); 157 | 158 | $this->customerMock 159 | ->expects($this->never()) 160 | ->method('getCustomAttribute') 161 | ->with('disallowed_payment_methods') 162 | ->willReturn($this->attributeMock); 163 | 164 | $this->customerMock 165 | ->expects($this->atLeastOnce()) 166 | ->method('getId') 167 | ->willReturn(null); 168 | 169 | $this->quoteMock 170 | ->expects($this->atLeastOnce()) 171 | ->method('getCustomer') 172 | ->willReturn($this->customerMock); 173 | 174 | $this->paymentMethodMock 175 | ->expects($this->never()) 176 | ->method('getCode'); 177 | 178 | $this->result->setData('is_available', true); 179 | 180 | $this->customerFilter->execute($this->paymentMethodMock, $this->quoteMock, $this->result); 181 | 182 | $this->assertTrue($this->result->getData('is_available')); 183 | } 184 | 185 | /** 186 | * @test 187 | */ 188 | public function testExecuteWithCustomAttributeEqualsNull() 189 | { 190 | $this->customerMock 191 | ->expects($this->atLeastOnce()) 192 | ->method('getCustomAttribute') 193 | ->with('disallowed_payment_methods') 194 | ->willReturn(null); 195 | 196 | $this->customerMock 197 | ->expects($this->atLeastOnce()) 198 | ->method('getId') 199 | ->willReturn(1); 200 | 201 | $this->quoteMock 202 | ->expects($this->atLeastOnce()) 203 | ->method('getCustomer') 204 | ->willReturn($this->customerMock); 205 | 206 | $this->paymentMethodMock 207 | ->expects($this->never()) 208 | ->method('getCode'); 209 | 210 | $this->result->setData('is_available', true); 211 | 212 | $this->customerFilter->execute($this->paymentMethodMock, $this->quoteMock, $this->result); 213 | 214 | $this->assertTrue($this->result->getData('is_available')); 215 | } 216 | } -------------------------------------------------------------------------------- /tests/Unit/Model/Filter/GuestTest.php: -------------------------------------------------------------------------------- 1 | paymentMethodMock = $this->getMockBuilder(Cashondelivery::class) 54 | ->disableOriginalConstructor() 55 | ->getMock(); 56 | 57 | $this->quoteMock = $this->getMockBuilder(CartInterface::class) 58 | ->disableOriginalConstructor() 59 | ->getMock(); 60 | 61 | $this->customerMock = $this->getMockBuilder(CustomerInterface::class) 62 | ->disableOriginalConstructor() 63 | ->getMock(); 64 | 65 | $this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class) 66 | ->disableOriginalConstructor() 67 | ->getMock(); 68 | 69 | $this->guestFilter = $objectManager->getObject(GuestFilter::class, [ 70 | 'scopeConfig' => $this->scopeConfigMock 71 | ]); 72 | 73 | $this->result = $objectManager->getObject(DataObject::class, []); 74 | } 75 | 76 | public function testExecuteWithGuestThatIsNotAllowedToPayByCashOnDelivery() 77 | { 78 | $this->customerMock 79 | ->expects($this->atLeastOnce()) 80 | ->method('getId') 81 | ->willReturn(null); 82 | 83 | $this->quoteMock 84 | ->expects($this->atLeastOnce()) 85 | ->method('getCustomer') 86 | ->willReturn($this->customerMock); 87 | 88 | $this->scopeConfigMock 89 | ->expects($this->atLeastOnce()) 90 | ->method('getValue') 91 | ->with(GuestFilter::XML_PATH_DISALLOWED_PAYMENT_METHODS_FOR_GUEST) 92 | ->willReturn(Cashondelivery::PAYMENT_METHOD_CASHONDELIVERY_CODE . ',free'); 93 | 94 | $this->paymentMethodMock 95 | ->expects($this->atLeastOnce()) 96 | ->method('getCode') 97 | ->willReturn(Cashondelivery::PAYMENT_METHOD_CASHONDELIVERY_CODE); 98 | 99 | $this->result->setData('is_available', true); 100 | 101 | $this->guestFilter->execute($this->paymentMethodMock, $this->quoteMock, $this->result); 102 | 103 | $this->assertFalse($this->result->getData('is_available')); 104 | } 105 | 106 | /** 107 | * @test 108 | */ 109 | public function testExecuteWithCustomer() 110 | { 111 | $this->customerMock 112 | ->expects($this->atLeastOnce()) 113 | ->method('getId') 114 | ->willReturn(1); 115 | 116 | $this->quoteMock 117 | ->expects($this->atLeastOnce()) 118 | ->method('getCustomer') 119 | ->willReturn($this->customerMock); 120 | 121 | $this->scopeConfigMock 122 | ->expects($this->never()) 123 | ->method('getValue') 124 | ->with(GuestFilter::XML_PATH_DISALLOWED_PAYMENT_METHODS_FOR_GUEST) 125 | ->willReturn(Cashondelivery::PAYMENT_METHOD_CASHONDELIVERY_CODE); 126 | 127 | $this->paymentMethodMock 128 | ->expects($this->never()) 129 | ->method('getCode') 130 | ->willReturn(Cashondelivery::PAYMENT_METHOD_CASHONDELIVERY_CODE); 131 | 132 | $this->result->setData('is_available', true); 133 | 134 | $this->guestFilter->execute($this->paymentMethodMock, $this->quoteMock, $this->result); 135 | 136 | $this->assertTrue($this->result->getData('is_available')); 137 | } 138 | 139 | public function testExecuteWithGuestThatIsAllowedToPayByCashOnDelivery() 140 | { 141 | $this->customerMock 142 | ->expects($this->atLeastOnce()) 143 | ->method('getId') 144 | ->willReturn(null); 145 | 146 | $this->quoteMock 147 | ->expects($this->atLeastOnce()) 148 | ->method('getCustomer') 149 | ->willReturn($this->customerMock); 150 | 151 | $this->scopeConfigMock 152 | ->expects($this->atLeastOnce()) 153 | ->method('getValue') 154 | ->with(GuestFilter::XML_PATH_DISALLOWED_PAYMENT_METHODS_FOR_GUEST) 155 | ->willReturn('free'); 156 | 157 | $this->paymentMethodMock 158 | ->expects($this->atLeastOnce()) 159 | ->method('getCode') 160 | ->willReturn(Cashondelivery::PAYMENT_METHOD_CASHONDELIVERY_CODE); 161 | 162 | $this->result->setData('is_available', true); 163 | 164 | $this->guestFilter->execute($this->paymentMethodMock, $this->quoteMock, $this->result); 165 | 166 | $this->assertTrue($this->result->getData('is_available')); 167 | } 168 | 169 | public function testExecuteWithoutConfig() 170 | { 171 | $this->customerMock 172 | ->expects($this->atLeastOnce()) 173 | ->method('getId') 174 | ->willReturn(null); 175 | 176 | $this->quoteMock 177 | ->expects($this->atLeastOnce()) 178 | ->method('getCustomer') 179 | ->willReturn($this->customerMock); 180 | 181 | $this->scopeConfigMock 182 | ->expects($this->atLeastOnce()) 183 | ->method('getValue') 184 | ->with(GuestFilter::XML_PATH_DISALLOWED_PAYMENT_METHODS_FOR_GUEST) 185 | ->willReturn(null); 186 | 187 | $this->paymentMethodMock 188 | ->expects($this->never()) 189 | ->method('getCode'); 190 | 191 | $this->result->setData('is_available', true); 192 | 193 | $this->guestFilter->execute($this->paymentMethodMock, $this->quoteMock, $this->result); 194 | 195 | $this->assertTrue($this->result->getData('is_available')); 196 | } 197 | } -------------------------------------------------------------------------------- /tests/Unit/Model/Filter/QuoteContentTest.php: -------------------------------------------------------------------------------- 1 | paymentMethodMock = $this->getMockBuilder(Cashondelivery::class) 75 | ->disableOriginalConstructor() 76 | ->getMock(); 77 | 78 | $this->quoteMock = $this->getMockBuilder(CartInterface::class) 79 | ->disableOriginalConstructor() 80 | ->getMock(); 81 | 82 | $this->firstQuoteItemMock = $this->getMockBuilder(Item::class) 83 | ->disableOriginalConstructor() 84 | ->getMock(); 85 | 86 | $this->secondQuoteItemMock = $this->getMockBuilder(Item::class) 87 | ->disableOriginalConstructor() 88 | ->getMock(); 89 | 90 | $this->firstProductMock = $this->getMockBuilder(ProductInterface::class) 91 | ->disableOriginalConstructor() 92 | ->getMock(); 93 | 94 | $this->secondProductMock = $this->getMockBuilder(ProductInterface::class) 95 | ->disableOriginalConstructor() 96 | ->getMock(); 97 | 98 | $this->attributeMockForFirstProduct = $this->getMockBuilder(AttributeInterface::class) 99 | ->disableOriginalConstructor() 100 | ->getMock(); 101 | 102 | $this->attributeMockForSecondProduct = $this->getMockBuilder(AttributeInterface::class) 103 | ->disableOriginalConstructor() 104 | ->getMock(); 105 | 106 | $this->quoteContentFilter = $objectManager->getObject(QuoteContent::class, []); 107 | 108 | $this->result = $objectManager->getObject(DataObject::class, []); 109 | } 110 | 111 | public function testExecuteWithQuoteItemThatDisallowCashOnDelivery() 112 | { 113 | $this->attributeMockForFirstProduct 114 | ->expects($this->atLeastOnce()) 115 | ->method('getValue') 116 | ->willReturn(''); 117 | 118 | $this->firstProductMock 119 | ->expects($this->atLeastOnce()) 120 | ->method('getCustomAttribute') 121 | ->with('disallowed_payment_methods') 122 | ->willReturn($this->attributeMockForFirstProduct); 123 | 124 | $this->firstQuoteItemMock 125 | ->expects($this->atLeastOnce()) 126 | ->method('getProduct') 127 | ->willReturn($this->firstProductMock); 128 | 129 | $this->attributeMockForSecondProduct 130 | ->expects($this->atLeastOnce()) 131 | ->method('getValue') 132 | ->willReturn('cashondelivery,free'); 133 | 134 | $this->secondProductMock 135 | ->expects($this->atLeastOnce()) 136 | ->method('getCustomAttribute') 137 | ->with('disallowed_payment_methods') 138 | ->willReturn($this->attributeMockForSecondProduct); 139 | 140 | $this->secondQuoteItemMock 141 | ->expects($this->atLeastOnce()) 142 | ->method('getProduct') 143 | ->willReturn($this->secondProductMock); 144 | 145 | $this->quoteMock 146 | ->expects($this->atLeastOnce()) 147 | ->method('getItems') 148 | ->willReturn([$this->firstQuoteItemMock, $this->secondQuoteItemMock]); 149 | 150 | $this->paymentMethodMock 151 | ->expects($this->atLeastOnce()) 152 | ->method('getCode') 153 | ->willReturn(Cashondelivery::PAYMENT_METHOD_CASHONDELIVERY_CODE); 154 | 155 | $this->result->setData('is_available', true); 156 | 157 | $this->quoteContentFilter->execute($this->paymentMethodMock, $this->quoteMock, $this->result); 158 | 159 | $this->assertFalse($this->result->getData('is_available')); 160 | } 161 | 162 | public function testExecuteWithQuoteItemsThatAllowAllPaymentMethods() 163 | { 164 | $this->attributeMockForFirstProduct 165 | ->expects($this->atLeastOnce()) 166 | ->method('getValue') 167 | ->willReturn(''); 168 | 169 | $this->firstProductMock 170 | ->expects($this->atLeastOnce()) 171 | ->method('getCustomAttribute') 172 | ->with('disallowed_payment_methods') 173 | ->willReturn($this->attributeMockForFirstProduct); 174 | 175 | $this->firstQuoteItemMock 176 | ->expects($this->atLeastOnce()) 177 | ->method('getProduct') 178 | ->willReturn($this->firstProductMock); 179 | 180 | $this->attributeMockForSecondProduct 181 | ->expects($this->atLeastOnce()) 182 | ->method('getValue') 183 | ->willReturn(''); 184 | 185 | $this->secondProductMock 186 | ->expects($this->atLeastOnce()) 187 | ->method('getCustomAttribute') 188 | ->with('disallowed_payment_methods') 189 | ->willReturn($this->attributeMockForSecondProduct); 190 | 191 | $this->secondQuoteItemMock 192 | ->expects($this->atLeastOnce()) 193 | ->method('getProduct') 194 | ->willReturn($this->secondProductMock); 195 | 196 | $this->quoteMock 197 | ->expects($this->atLeastOnce()) 198 | ->method('getItems') 199 | ->willReturn([$this->firstQuoteItemMock, $this->secondQuoteItemMock]); 200 | 201 | $this->paymentMethodMock 202 | ->expects($this->never()) 203 | ->method('getCode') 204 | ->willReturn(Cashondelivery::PAYMENT_METHOD_CASHONDELIVERY_CODE); 205 | 206 | $this->result->setData('is_available', true); 207 | 208 | $this->quoteContentFilter->execute($this->paymentMethodMock, $this->quoteMock, $this->result); 209 | 210 | $this->assertTrue($this->result->getData('is_available')); 211 | } 212 | 213 | public function testExecuteWithCustomAttributeEqualsNull() 214 | { 215 | $this->attributeMockForFirstProduct 216 | ->expects($this->never()) 217 | ->method('getValue'); 218 | 219 | $this->firstProductMock 220 | ->expects($this->atLeastOnce()) 221 | ->method('getCustomAttribute') 222 | ->with('disallowed_payment_methods') 223 | ->willReturn(null); 224 | 225 | $this->firstQuoteItemMock 226 | ->expects($this->atLeastOnce()) 227 | ->method('getProduct') 228 | ->willReturn($this->firstProductMock); 229 | 230 | $this->attributeMockForSecondProduct 231 | ->expects($this->atLeastOnce()) 232 | ->method('getValue') 233 | ->willReturn(''); 234 | 235 | $this->secondProductMock 236 | ->expects($this->atLeastOnce()) 237 | ->method('getCustomAttribute') 238 | ->with('disallowed_payment_methods') 239 | ->willReturn($this->attributeMockForSecondProduct); 240 | 241 | $this->secondQuoteItemMock 242 | ->expects($this->atLeastOnce()) 243 | ->method('getProduct') 244 | ->willReturn($this->secondProductMock); 245 | 246 | $this->quoteMock 247 | ->expects($this->atLeastOnce()) 248 | ->method('getItems') 249 | ->willReturn([$this->firstQuoteItemMock, $this->secondQuoteItemMock]); 250 | 251 | $this->paymentMethodMock 252 | ->expects($this->never()) 253 | ->method('getCode') 254 | ->willReturn(Cashondelivery::PAYMENT_METHOD_CASHONDELIVERY_CODE); 255 | 256 | $this->result->setData('is_available', true); 257 | 258 | $this->quoteContentFilter->execute($this->paymentMethodMock, $this->quoteMock, $this->result); 259 | 260 | $this->assertTrue($this->result->getData('is_available')); 261 | } 262 | } -------------------------------------------------------------------------------- /tests/Unit/Observer/ExcludeDisallowedPaymentMethodTest.php: -------------------------------------------------------------------------------- 1 | result = $objectManager->getObject(DataObject::class); 61 | 62 | $this->filterList[] = $this->getMockBuilder(Guest::class) 63 | ->disableOriginalConstructor() 64 | ->getMock(); 65 | 66 | $this->filterList[] = $this->getMockBuilder(Customer::class) 67 | ->disableOriginalConstructor() 68 | ->getMock(); 69 | 70 | $this->filterList[] = $this->getMockBuilder(QuoteContent::class) 71 | ->disableOriginalConstructor() 72 | ->getMock(); 73 | 74 | $this->paymentMethodMock = $this->getMockBuilder(Cashondelivery::class) 75 | ->disableOriginalConstructor() 76 | ->getMock(); 77 | 78 | $this->quoteMock = $this->getMockBuilder(Quote::class) 79 | ->disableOriginalConstructor() 80 | ->getMock(); 81 | 82 | $this->eventMock = $this->getMockBuilder(Event::class) 83 | ->disableOriginalConstructor() 84 | ->setMethods(['getMethodInstance', 'getQuote', 'getResult']) 85 | ->getMock(); 86 | 87 | $this->observerMock = $this->getMockBuilder(Observer::class) 88 | ->disableOriginalConstructor() 89 | ->getMock(); 90 | 91 | $this->excludeDisallowedPaymentMethodObserver = $objectManager->getObject(ExcludeDisallowedPaymentMethod::class, [ 92 | 'filterList' => $this->filterList 93 | ]); 94 | } 95 | 96 | /** 97 | * @test 98 | */ 99 | public function testExecuteWithNotAvailablePaymentMethod() 100 | { 101 | $this->result->setData('is_available', false); 102 | 103 | $this->eventMock 104 | ->expects($this->never()) 105 | ->method('getQuote'); 106 | 107 | $this->eventMock 108 | ->expects($this->never()) 109 | ->method('getMethodInstance'); 110 | 111 | $this->eventMock 112 | ->expects($this->atLeastOnce()) 113 | ->method('getResult') 114 | ->willReturn($this->result); 115 | 116 | $this->observerMock 117 | ->expects($this->atLeastOnce()) 118 | ->method('getEvent') 119 | ->willReturn($this->eventMock); 120 | 121 | $this->excludeDisallowedPaymentMethodObserver->execute($this->observerMock); 122 | 123 | $this->assertFalse($this->result->getData('is_available')); 124 | } 125 | 126 | /** 127 | * @test 128 | */ 129 | public function testExecuteWithAllowedPaymentMethod() 130 | { 131 | $this->result->setData('is_available', true); 132 | 133 | $this->eventMock 134 | ->expects($this->atLeastOnce()) 135 | ->method('getQuote') 136 | ->willReturn($this->quoteMock); 137 | 138 | $this->eventMock 139 | ->expects($this->atLeastOnce()) 140 | ->method('getMethodInstance') 141 | ->willReturn($this->paymentMethodMock); 142 | 143 | $this->eventMock 144 | ->expects($this->atLeastOnce()) 145 | ->method('getResult') 146 | ->willReturn($this->result); 147 | 148 | $this->observerMock 149 | ->expects($this->atLeastOnce()) 150 | ->method('getEvent') 151 | ->willReturn($this->eventMock); 152 | 153 | foreach ($this->filterList as $filter) { 154 | $filter->expects($this->atLeastOnce()) 155 | ->method('execute'); 156 | } 157 | 158 | $this->excludeDisallowedPaymentMethodObserver->execute($this->observerMock); 159 | 160 | $this->assertTrue($this->result->getData('is_available')); 161 | } 162 | 163 | /** 164 | * @test 165 | */ 166 | public function testExecuteWithPaymentMethodThatWillBeDisallowedByCustomer() 167 | { 168 | $this->result->setData('is_available', true); 169 | 170 | $this->eventMock 171 | ->expects($this->atLeastOnce()) 172 | ->method('getQuote') 173 | ->willReturn($this->quoteMock); 174 | 175 | $this->eventMock 176 | ->expects($this->atLeastOnce()) 177 | ->method('getMethodInstance') 178 | ->willReturn($this->paymentMethodMock); 179 | 180 | $this->eventMock 181 | ->expects($this->atLeastOnce()) 182 | ->method('getResult') 183 | ->willReturn($this->result); 184 | 185 | $this->observerMock 186 | ->expects($this->atLeastOnce()) 187 | ->method('getEvent') 188 | ->willReturn($this->eventMock); 189 | 190 | $this->filterList[0]->expects($this->atLeastOnce()) 191 | ->method('execute'); 192 | 193 | $this->filterList[1]->expects($this->atLeastOnce()) 194 | ->method('execute') 195 | ->willReturnCallback(function ($paymentMethod, $quote, $result) { 196 | $result->setData('is_available', false); 197 | }); 198 | 199 | $this->filterList[2]->expects($this->never()) 200 | ->method('execute'); 201 | 202 | $this->excludeDisallowedPaymentMethodObserver->execute($this->observerMock); 203 | 204 | $this->assertFalse($this->result->getData('is_available')); 205 | } 206 | 207 | /** 208 | * @test 209 | */ 210 | public function testExecuteWithAllowedPaymentMethodWithoutQuote() 211 | { 212 | $this->result->setData('is_available', true); 213 | 214 | $this->eventMock 215 | ->expects($this->atLeastOnce()) 216 | ->method('getQuote') 217 | ->willReturn(null); 218 | 219 | $this->eventMock 220 | ->expects($this->atLeastOnce()) 221 | ->method('getMethodInstance') 222 | ->willReturn($this->paymentMethodMock); 223 | 224 | $this->eventMock 225 | ->expects($this->atLeastOnce()) 226 | ->method('getResult') 227 | ->willReturn($this->result); 228 | 229 | $this->observerMock 230 | ->expects($this->atLeastOnce()) 231 | ->method('getEvent') 232 | ->willReturn($this->eventMock); 233 | 234 | foreach ($this->filterList as $filter) { 235 | $filter->expects($this->never()) 236 | ->method('execute'); 237 | } 238 | 239 | $this->excludeDisallowedPaymentMethodObserver->execute($this->observerMock); 240 | 241 | $this->assertTrue($this->result->getData('is_available')); 242 | } 243 | 244 | /** 245 | * @test 246 | */ 247 | public function testExecuteWithAllowedPaymentMethodWithoutPaymentMethodInstance() 248 | { 249 | $this->result->setData('is_available', true); 250 | 251 | $this->eventMock 252 | ->expects($this->never()) 253 | ->method('getQuote'); 254 | 255 | $this->eventMock 256 | ->expects($this->atLeastOnce()) 257 | ->method('getMethodInstance') 258 | ->willReturn(null); 259 | 260 | $this->eventMock 261 | ->expects($this->atLeastOnce()) 262 | ->method('getResult') 263 | ->willReturn($this->result); 264 | 265 | $this->observerMock 266 | ->expects($this->atLeastOnce()) 267 | ->method('getEvent') 268 | ->willReturn($this->eventMock); 269 | 270 | foreach ($this->filterList as $filter) { 271 | $filter->expects($this->never()) 272 | ->method('execute'); 273 | } 274 | 275 | $this->excludeDisallowedPaymentMethodObserver->execute($this->observerMock); 276 | 277 | $this->assertTrue($this->result->getData('is_available')); 278 | } 279 | 280 | /** 281 | * @test 282 | */ 283 | public function testExecuteWithAllowedPaymentMethodWithoutEvent() 284 | { 285 | $this->result->setData('is_available', true); 286 | 287 | $this->eventMock 288 | ->expects($this->never()) 289 | ->method('getQuote'); 290 | 291 | $this->eventMock 292 | ->expects($this->never()) 293 | ->method('getMethodInstance'); 294 | 295 | $this->eventMock 296 | ->expects($this->never()) 297 | ->method('getResult'); 298 | 299 | $this->observerMock 300 | ->expects($this->atLeastOnce()) 301 | ->method('getEvent') 302 | ->willReturn(null); 303 | 304 | foreach ($this->filterList as $filter) { 305 | $filter->expects($this->never()) 306 | ->method('execute'); 307 | } 308 | 309 | $this->excludeDisallowedPaymentMethodObserver->execute($this->observerMock); 310 | 311 | $this->assertTrue($this->result->getData('is_available')); 312 | } 313 | } -------------------------------------------------------------------------------- /tests/Unit/bootstrap.php: -------------------------------------------------------------------------------- 1 |