Skip to content

Commit 63c3728

Browse files
authored
Merge pull request #102 from magento-cia/AC-7836
Google ReCaptcha Fixes
2 parents c3bd993 + fd7ba29 commit 63c3728

File tree

6 files changed

+106
-37
lines changed

6 files changed

+106
-37
lines changed

ReCaptchaCheckout/view/frontend/web/js/model/place-order-mixin.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,15 @@ define([
3030
});
3131
//Trigger ReCaptcha validation
3232
recaptchaRegistry.triggers['recaptcha-checkout-place-order']();
33-
//remove listener so that place order action is only triggered by the 'Place Order' button
34-
recaptchaRegistry.removeListener('recaptcha-checkout-place-order');
33+
34+
if (
35+
!recaptchaRegistry._isInvisibleType.hasOwnProperty('recaptcha-checkout-place-order') ||
36+
recaptchaRegistry._isInvisibleType['recaptcha-checkout-place-order'] === false
37+
) {
38+
//remove listener so that place order action is only triggered by the 'Place Order' button
39+
recaptchaRegistry.removeListener('recaptcha-checkout-place-order');
40+
}
41+
3542
return recaptchaDeferred;
3643
}
3744

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\ReCaptchaValidation\Model;
9+
10+
use ReCaptcha\ReCaptcha as GoogleReCaptcha;
11+
12+
/**
13+
* Wrapper Class for Google Recaptcha
14+
* Used to fix dynamic property deprecation error
15+
*/
16+
class ReCaptcha extends GoogleReCaptcha
17+
{
18+
19+
/**
20+
* @var float
21+
*/
22+
protected float $threshold = 0.0;
23+
}

ReCaptchaValidation/Model/Validator.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use Magento\ReCaptchaValidationApi\Api\ValidatorInterface;
1414
use Magento\ReCaptchaValidationApi\Model\ErrorMessagesProvider;
1515
use ReCaptcha\ReCaptcha;
16-
use ReCaptcha\ReCaptchaFactory;
16+
use Magento\ReCaptchaValidation\Model\ReCaptchaFactory;
1717

1818
/**
1919
* @inheritdoc
@@ -31,7 +31,7 @@ class Validator implements ValidatorInterface
3131
private $errorMessagesProvider;
3232

3333
/**
34-
* @var ReCaptchaFactory
34+
* @var ReCaptchaFactory\
3535
*/
3636
private $reCaptchaFactory;
3737

ReCaptchaWebapiUi/view/frontend/web/js/webapiReCaptcha.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,16 @@ define(
4343
var self = this,
4444
trigger;
4545

46+
trigger = function () {
47+
self.reCaptchaCallback(grecaptcha.getResponse(widgetId));
48+
};
49+
registry._isInvisibleType[this.getReCaptchaId()] = false;
50+
4651
if (this.getIsInvisibleRecaptcha()) {
4752
trigger = function () {
4853
grecaptcha.execute(widgetId);
4954
};
50-
} else {
51-
trigger = function () {
52-
self.reCaptchaCallback(grecaptcha.getResponse(widgetId));
53-
};
55+
registry._isInvisibleType[this.getReCaptchaId()] = true;
5456
}
5557

5658
if (this.autoTrigger) {

ReCaptchaWebapiUi/view/frontend/web/js/webapiReCaptchaRegistry.js

+5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ define([], function () {
2626
*/
2727
_listeners: {},
2828

29+
/**
30+
* recaptchaId: bool map
31+
*/
32+
_isInvisibleType: {},
33+
2934
/**
3035
* Add a listener to when the ReCaptcha finishes verification
3136
* @param {String} id - ReCaptchaId

dev/tests/js/jasmine/tests/app/code/Magento/ReCaptchaCheckout/frontend/js/model/place-order-mixin.test.js

+61-29
Original file line numberDiff line numberDiff line change
@@ -41,38 +41,40 @@ define(['squire'
4141

4242
expect(placeOrderMixins['Magento_ReCaptchaCheckout/js/model/place-order-mixin']).toBe(true);
4343
});
44+
});
45+
46+
describe('Magento_Checkout/js/action/redirect-on-success is called', function () {
47+
var recaptchaId = 'recaptcha-checkout-place-order',
48+
messageContainer = jasmine.createSpy('messageContainer'),
49+
payload = {},
50+
serviceUrl = 'test',
4451

45-
it('Magento_Checkout/js/action/redirect-on-success is called', function () {
46-
let recaptchaId = 'recaptcha-checkout-place-order',
47-
messageContainer = jasmine.createSpy('messageContainer'),
48-
payload = {},
49-
serviceUrl = 'test',
50-
51-
/**
52-
* Order place action mock
53-
*
54-
* @returns {{fail: fail, done: (function(Function): *)}}
55-
*/
56-
action = function () {
57-
return {
58-
/**
59-
* Success result for request
60-
*
61-
* @param {Function} handler
62-
* @returns {*}
63-
*/
64-
done: function (handler) {
65-
handler();
66-
return this;
67-
},
68-
69-
/**
70-
* Fail result for request
71-
*/
72-
fail: function () {}
73-
};
52+
/**
53+
* Order place action mock
54+
*
55+
* @returns {{fail: fail, done: (function(Function): *)}}
56+
*/
57+
action = function () {
58+
return {
59+
/**
60+
* Success result for request
61+
*
62+
* @param {Function} handler
63+
* @returns {*}
64+
*/
65+
done: function (handler) {
66+
handler();
67+
return this;
68+
},
69+
70+
/**
71+
* Fail result for request
72+
*/
73+
fail: function () {}
7474
};
75+
};
7576

77+
it('Only PlaceOrder button triggers place order action', function () {
7678
/**
7779
* Triggers declared listener
7880
*
@@ -93,9 +95,39 @@ define(['squire'
9395
registry.addListener = function (id, func) {
9496
registry._listeners[id] = func;
9597
};
98+
9699
registry.removeListener = jasmine.createSpy();
97100
mixin()(action, serviceUrl, payload, messageContainer);
98101
expect(registry.removeListener).toHaveBeenCalledWith(recaptchaId);
99102
});
103+
104+
it('PlaceOrder Listener is called for invisible google recaptcha', function () {
105+
/**
106+
* Triggers declared listener
107+
*
108+
* @returns {*}
109+
*/
110+
registry.triggers[recaptchaId] = function () {
111+
if (registry._listeners[recaptchaId] !== undefined) {
112+
return registry._listeners[recaptchaId]('token');
113+
}
114+
};
115+
116+
/**
117+
* Registers a listener
118+
*
119+
* @param id
120+
* @param func
121+
*/
122+
registry.addListener = function (id, func) {
123+
registry._listeners[id] = func;
124+
};
125+
126+
registry._isInvisibleType[recaptchaId] = true;
127+
registry.removeListener = jasmine.createSpy();
128+
mixin()(action, serviceUrl, payload, messageContainer);
129+
130+
expect(registry.removeListener).not.toHaveBeenCalled();
131+
});
100132
});
101133
});

0 commit comments

Comments
 (0)