Skip to content

Commit a3c627f

Browse files
committed
Merge remote-tracking branch 'l3/MC-41001' into PR-L3-20210405
2 parents 3eea437 + 9db3298 commit a3c627f

File tree

8 files changed

+140
-3
lines changed

8 files changed

+140
-3
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Captcha\Plugin;
7+
8+
use Magento\Captcha\Helper\Data as HelperCaptcha;
9+
use Magento\Captcha\Model\ResourceModel\LogFactory;
10+
use Magento\Sales\Api\Data\OrderInterface;
11+
use Magento\Sales\Api\OrderManagementInterface;
12+
13+
/**
14+
* Reset attempts for frontend checkout
15+
*/
16+
class ResetPaymentAttemptsAfterOrderIsPlacedPlugin
17+
{
18+
/**
19+
* Form ID
20+
*/
21+
private const FORM_ID = 'payment_processing_request';
22+
23+
/**
24+
* @var HelperCaptcha
25+
*/
26+
private $helper;
27+
28+
/**
29+
* @var LogFactory
30+
*/
31+
private $resLogFactory;
32+
33+
/**
34+
* ResetPaymentAttemptsAfterOrderIsPlacedPlugin constructor
35+
*
36+
* @param HelperCaptcha $helper
37+
* @param LogFactory $resLogFactory
38+
*/
39+
public function __construct(
40+
HelperCaptcha $helper,
41+
LogFactory $resLogFactory
42+
) {
43+
$this->helper = $helper;
44+
$this->resLogFactory = $resLogFactory;
45+
}
46+
47+
/**
48+
* Reset attempts for frontend checkout
49+
*
50+
* @param OrderManagementInterface $subject
51+
* @param OrderInterface $result
52+
* @param OrderInterface $order
53+
* @return OrderInterface
54+
*/
55+
public function afterPlace(
56+
OrderManagementInterface $subject,
57+
OrderInterface $result,
58+
OrderInterface $order
59+
): OrderInterface {
60+
$captchaModel = $this->helper->getCaptcha(self::FORM_ID);
61+
$captchaModel->setShowCaptchaInSession(false);
62+
$this->resLogFactory->create()->deleteUserAttempts($order->getCustomerEmail());
63+
return $result;
64+
}
65+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Captcha\Test\Unit\Plugin;
10+
11+
use Magento\Captcha\Model\ResourceModel\Log;
12+
use Magento\Captcha\Model\ResourceModel\LogFactory;
13+
use Magento\Captcha\Plugin\ResetPaymentAttemptsAfterOrderIsPlacedPlugin;
14+
use Magento\Captcha\Helper\Data as HelperCaptcha;
15+
use Magento\Captcha\Model\DefaultModel;
16+
use Magento\Sales\Api\Data\OrderInterface;
17+
use Magento\Sales\Api\OrderManagementInterface;
18+
use PHPUnit\Framework\TestCase;
19+
20+
/**
21+
* Unit test for ResetPaymentAttemptsAfterOrderIsPlacedPluginTest
22+
*/
23+
class ResetPaymentAttemptsAfterOrderIsPlacedPluginTest extends TestCase
24+
{
25+
/**
26+
* Test that the method resets attempts for frontend checkout
27+
*/
28+
public function testExecuteExpectsDeleteUserAttemptsCalled()
29+
{
30+
$orderManagementInterfaceMock = $this->getMockForAbstractClass(OrderManagementInterface::class);
31+
$resultOrderMock = $this->createMock(OrderInterface::class);
32+
$orderMock = $this->createMock(OrderInterface::class);
33+
$orderMock->expects($this->once())->method('getCustomerEmail')->willReturn('email@example.com');
34+
$captchaModelMock = $this->createMock(DefaultModel::class);
35+
$captchaModelMock->expects($this->once())->method('setShowCaptchaInSession')->with(false)->willReturnSelf();
36+
$helperCaptchaMock = $this->createMock(HelperCaptcha::class);
37+
$helperCaptchaMock->expects($this->once())->method('getCaptcha')->willReturn($captchaModelMock);
38+
$logMock = $this->createMock(Log::class);
39+
$logMock->expects($this->once())->method('deleteUserAttempts')->willReturnSelf();
40+
$resLogFactoryMock = $this->createMock(LogFactory::class);
41+
$resLogFactoryMock->expects($this->once())->method('create')->willReturn($logMock);
42+
$observer = new ResetPaymentAttemptsAfterOrderIsPlacedPlugin($helperCaptchaMock, $resLogFactoryMock);
43+
$observer->afterPlace($orderManagementInterfaceMock, $resultOrderMock, $orderMock);
44+
}
45+
}

app/code/Magento/Captcha/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"magento/module-backend": "*",
1111
"magento/module-checkout": "*",
1212
"magento/module-customer": "*",
13+
"magento/module-sales": "*",
1314
"magento/module-store": "*",
1415
"magento/module-authorization": "*",
1516
"laminas/laminas-captcha": "^2.7.1",

app/code/Magento/Captcha/etc/frontend/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,7 @@
3434
</argument>
3535
</arguments>
3636
</type>
37+
<type name="Magento\Sales\Api\OrderManagementInterface">
38+
<plugin name="reset_payment_attempts_after_order_is_placed_plugin" type="Magento\Captcha\Plugin\ResetPaymentAttemptsAfterOrderIsPlacedPlugin"/>
39+
</type>
3740
</config>

app/code/Magento/Captcha/etc/frontend/sections.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,10 @@
1010
<action name="customer/ajax/login">
1111
<section name="captcha"/>
1212
</action>
13+
<action name="rest/*/V1/carts/*/payment-information">
14+
<section name="captcha"/>
15+
</action>
16+
<action name="rest/*/V1/guest-carts/*/payment-information">
17+
<section name="captcha"/>
18+
</action>
1319
</config>

app/code/Magento/Captcha/etc/module.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<sequence>
1111
<module name="Magento_Customer"/>
1212
<module name="Magento_Checkout"/>
13+
<module name="Magento_Sales"/>
1314
</sequence>
1415
</module>
1516
</config>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
9+
<type name="Magento\Sales\Api\OrderManagementInterface">
10+
<plugin name="reset_payment_attempts_after_order_is_placed_plugin" type="Magento\Captcha\Plugin\ResetPaymentAttemptsAfterOrderIsPlacedPlugin"/>
11+
</type>
12+
</config>

app/code/Magento/Captcha/view/frontend/web/js/view/checkout/defaultCaptcha.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ define([
2121
},
2222
dataScope: 'global',
2323
currentCaptcha: null,
24+
subscribedFormIds: [],
2425

2526
/**
2627
* @return {*}
@@ -74,9 +75,12 @@ define([
7475
* @param {Object} captcha
7576
*/
7677
subscribeCustomerData: function (formId, captcha) {
77-
customerData.get('captcha').subscribe(function (captchaData) {
78-
this.checkCustomerData(formId, captchaData, captcha);
79-
}.bind(this));
78+
if (this.subscribedFormIds.includes(formId) === false) {
79+
this.subscribedFormIds.push(formId);
80+
customerData.get('captcha').subscribe(function (captchaData) {
81+
this.checkCustomerData(formId, captchaData, captcha);
82+
}.bind(this));
83+
}
8084
},
8185

8286
/**

0 commit comments

Comments
 (0)