From 62e73f54f9127efb297973b3762279a99761a56b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Fri, 1 Mar 2024 18:11:02 +0100 Subject: [PATCH] MAG2-306 - Load Ratepay device fingerprint script only when needed --- Block/RatepayDeviceFingerprint.php | 111 ------------------ Helper/Ratepay.php | 2 + .../Block/RatepayDeviceFingerprintTest.php | 106 ----------------- view/frontend/layout/checkout_index_index.xml | 1 - .../ratepay_device_fingerprint.phtml | 37 ------ .../payment/method-renderer/ratepay_base.js | 25 +++- 6 files changed, 26 insertions(+), 256 deletions(-) delete mode 100644 Block/RatepayDeviceFingerprint.php delete mode 100644 Test/Unit/Block/RatepayDeviceFingerprintTest.php delete mode 100644 view/frontend/templates/ratepay_device_fingerprint.phtml diff --git a/Block/RatepayDeviceFingerprint.php b/Block/RatepayDeviceFingerprint.php deleted file mode 100644 index 821434c1..00000000 --- a/Block/RatepayDeviceFingerprint.php +++ /dev/null @@ -1,111 +0,0 @@ -. - * - * PHP version 5 - * - * @category Payone - * @package Payone_Magento2_Plugin - * @author FATCHIP GmbH - * @copyright 2003 - 2020 Payone GmbH - * @license GNU Lesser General Public License - * @link http://www.payone.de - */ - -namespace Payone\Core\Block; - -use Magento\Framework\View\Element\Template; -use Payone\Core\Model\PayoneConfig; - -class RatepayDeviceFingerprint extends Template -{ - /** - * @var \Payone\Core\Helper\Ratepay - */ - protected $ratepayHelper; - - /** - * @var \Payone\Core\Helper\Payment - */ - protected $paymentHelper; - - /** - * Constructor - * - * @param Template\Context $context - * @param \Payone\Core\Helper\Ratepay $ratepayHelper - * @param \Payone\Core\Helper\Payment $paymentHelper - * @param array $data - */ - public function __construct( - Template\Context $context, - \Payone\Core\Helper\Ratepay $ratepayHelper, - \Payone\Core\Helper\Payment $paymentHelper, - array $data = [] - ) { - parent::__construct($context, $data); - $this->ratepayHelper = $ratepayHelper; - $this->paymentHelper = $paymentHelper; - } - - /** - * Returns snippet id from config - * - * @return string - */ - public function getDevicefingerprintSnippetId() - { - return $this->ratepayHelper->getConfigParam('devicefingerprint_snippet_id', 'ratepay', 'payone_misc'); - } - - /** - * Returns token generated by Ratepay helper - * - * @return string - */ - public function getDevicefingerprintToken() - { - return $this->ratepayHelper->getRatepayDeviceFingerprintToken(); - } - - /** - * Determine if at least one of the three Ratepay methods is activated - * - * @return false - */ - protected function isRatepayMethodActive() - { - if ($this->paymentHelper->isPaymentMethodActive(PayoneConfig::METHOD_RATEPAY_INVOICE) === true || - $this->paymentHelper->isPaymentMethodActive(PayoneConfig::METHOD_RATEPAY_DEBIT) === true || - $this->paymentHelper->isPaymentMethodActive(PayoneConfig::METHOD_RATEPAY_INSTALLMENT) === true - ) { - return true; - } - return false; - } - - /** - * Render block HTML - * - * @return string - */ - protected function _toHtml() - { - if ($this->isRatepayMethodActive() === false) { - return ''; - } - return parent::_toHtml(); - } -} diff --git a/Helper/Ratepay.php b/Helper/Ratepay.php index a56b8dfd..edaf2146 100644 --- a/Helper/Ratepay.php +++ b/Helper/Ratepay.php @@ -365,6 +365,8 @@ public function getRatepayConfig() $aReturn[$sRatepayMethod] = $this->getRatepaySingleConfig($sRatepayMethod); } } + $aReturn['snippetId'] = $this->getConfigParam('devicefingerprint_snippet_id', 'ratepay', 'payone_misc'); + $aReturn['token'] = $this->getRatepayDeviceFingerprintToken(); return $aReturn; } diff --git a/Test/Unit/Block/RatepayDeviceFingerprintTest.php b/Test/Unit/Block/RatepayDeviceFingerprintTest.php deleted file mode 100644 index 2554b452..00000000 --- a/Test/Unit/Block/RatepayDeviceFingerprintTest.php +++ /dev/null @@ -1,106 +0,0 @@ -. - * - * PHP version 5 - * - * @category Payone - * @package Payone_Magento2_Plugin - * @author FATCHIP GmbH - * @copyright 2003 - 2020 Payone GmbH - * @license GNU Lesser General Public License - * @link http://www.payone.de - */ - -namespace Payone\Core\Test\Unit\Block; - -use Payone\Core\Block\RatepayDeviceFingerprint as ClassToTest; -use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; -use Magento\Checkout\Model\Session; -use Magento\Quote\Model\Quote; -use Magento\Quote\Model\Quote\Address; -use Magento\Quote\Model\Quote\Payment; -use Payone\Core\Helper\Ratepay; -use Payone\Core\Helper\Payment as PaymentHelper; -use Payone\Core\Test\Unit\BaseTestCase; -use Payone\Core\Test\Unit\PayoneObjectManager; - -class RatepayDeviceFingerprintTest extends BaseTestCase -{ - /** - * @var ClassToTest - */ - private $classToTest; - - /** - * @var ObjectManager|PayoneObjectManager - */ - private $objectManager; - - /** - * @var Ratepay|\PHPUnit_Framework_MockObject_MockObject - */ - private $ratepayHelper; - - /** - * @var PaymentHelper|\PHPUnit_Framework_MockObject_MockObject - */ - private $paymentHelper; - - protected function setUp(): void - { - $this->objectManager = $this->getObjectManager(); - - $this->ratepayHelper = $this->getMockBuilder(Ratepay::class)->disableOriginalConstructor()->getMock(); - $this->paymentHelper = $this->getMockBuilder(PaymentHelper::class)->disableOriginalConstructor()->getMock(); - - $this->classToTest = $this->objectManager->getObject(ClassToTest::class, [ - 'ratepayHelper' => $this->ratepayHelper, - 'paymentHelper' => $this->paymentHelper, - ]); - } - - public function testGetDevicefingerprintSnippetId() - { - $expected = "ratepay"; - $this->ratepayHelper->method("getConfigParam")->willReturn($expected); - - $result = $this->classToTest->getDevicefingerprintSnippetId(); - $this->assertEquals($expected, $result); - } - - public function testGetDevicefingerprintToken() - { - $expected = "12345"; - $this->ratepayHelper->method("getRatepayDeviceFingerprintToken")->willReturn($expected); - - $result = $this->classToTest->getDevicefingerprintToken(); - $this->assertEquals($expected, $result); - } - - public function testToHtml() - { - $result = $this->classToTest->toHtml(); - $this->assertTrue(true); - } - - public function testToHtmlEmpty() - { - $this->paymentHelper->method('isPaymentMethodActive')->willReturn(false); - - $result = $this->classToTest->toHtml(); - $this->assertEmpty($result); - } -} diff --git a/view/frontend/layout/checkout_index_index.xml b/view/frontend/layout/checkout_index_index.xml index fa56efdb..a21dcfe7 100644 --- a/view/frontend/layout/checkout_index_index.xml +++ b/view/frontend/layout/checkout_index_index.xml @@ -176,7 +176,6 @@ - diff --git a/view/frontend/templates/ratepay_device_fingerprint.phtml b/view/frontend/templates/ratepay_device_fingerprint.phtml deleted file mode 100644 index ac64b42e..00000000 --- a/view/frontend/templates/ratepay_device_fingerprint.phtml +++ /dev/null @@ -1,37 +0,0 @@ -. - * - * PHP version 5 - * - * @category Payone - * @package Payone_Magento2_Plugin - * @author FATCHIP GmbH - * @copyright 2003 - 2020 Payone GmbH - * @license GNU Lesser General Public License - * @link http://www.payone.de - */ -$deviceIdentToken = $block->getDevicefingerprintToken(); -$deviceIdentSId = $block->getDevicefingerprintSnippetId(); -?> - - - diff --git a/view/frontend/web/js/view/payment/method-renderer/ratepay_base.js b/view/frontend/web/js/view/payment/method-renderer/ratepay_base.js index a81e8427..a7e934e4 100644 --- a/view/frontend/web/js/view/payment/method-renderer/ratepay_base.js +++ b/view/frontend/web/js/view/payment/method-renderer/ratepay_base.js @@ -27,9 +27,10 @@ define( 'Magento_Checkout/js/model/quote', 'Magento_Customer/js/model/customer', 'Payone_Core/js/action/ratepayconfig', + 'Magento_Checkout/js/checkout-data', 'mage/translate' ], - function (Component, quote, customer, ratepayconfig, $t) { + function (Component, quote, customer, ratepayconfig, checkoutData, $t) { 'use strict'; return Component.extend({ initialize: function () { @@ -38,8 +39,15 @@ define( ratepayconfig(); window.checkoutConfig.payment.payone.ratepayRefreshed = true; } + if (checkoutData.getSelectedPaymentMethod() === this.getCode()) { + this.handleDeviceFingerprint(); + } return parentReturn; }, + selectPaymentMethod: function () { + this.handleDeviceFingerprint(); + return this._super(); + }, isPlaceOrderActionAllowedRatePay: function () { return this.isDifferentAddressNotAllowed() === false && this.isB2BNotAllowed() === false; }, @@ -154,6 +162,21 @@ define( return false; } return true; + }, + handleDeviceFingerprint: function () { + if (window.checkoutConfig.payment.payone.ratepay.token) { + var diSkriptVar = document.createElement('script'); + diSkriptVar.type = 'text/javascript'; + diSkriptVar.text = "var di = {t:'" + window.checkoutConfig.payment.payone.ratepay.token + "',v:'" + window.checkoutConfig.payment.payone.ratepay.snippetId + "',l:'checkout'};"; + document.getElementsByTagName('head')[0].appendChild(diSkriptVar); + + var diSkript = document.createElement('script'); + diSkript.type = 'text/javascript'; + diSkript.src = '//d.ratepay.com/' + window.checkoutConfig.payment.payone.ratepay.snippetId + '/di.js'; + document.getElementsByTagName('head')[0].appendChild(diSkript); + + window.checkoutConfig.payment.payone.ratepay.token = false; + } } }); }