Skip to content

Commit

Permalink
Merge pull request #102 from magento-cia/AC-7836
Browse files Browse the repository at this point in the history
Google ReCaptcha Fixes
  • Loading branch information
admanesachin authored Jan 31, 2023
2 parents c3bd993 + fd7ba29 commit 63c3728
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,15 @@ define([
});
//Trigger ReCaptcha validation
recaptchaRegistry.triggers['recaptcha-checkout-place-order']();
//remove listener so that place order action is only triggered by the 'Place Order' button
recaptchaRegistry.removeListener('recaptcha-checkout-place-order');

if (
!recaptchaRegistry._isInvisibleType.hasOwnProperty('recaptcha-checkout-place-order') ||
recaptchaRegistry._isInvisibleType['recaptcha-checkout-place-order'] === false
) {
//remove listener so that place order action is only triggered by the 'Place Order' button
recaptchaRegistry.removeListener('recaptcha-checkout-place-order');
}

return recaptchaDeferred;
}

Expand Down
23 changes: 23 additions & 0 deletions ReCaptchaValidation/Model/ReCaptcha.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\ReCaptchaValidation\Model;

use ReCaptcha\ReCaptcha as GoogleReCaptcha;

/**
* Wrapper Class for Google Recaptcha
* Used to fix dynamic property deprecation error
*/
class ReCaptcha extends GoogleReCaptcha
{

/**
* @var float
*/
protected float $threshold = 0.0;
}
4 changes: 2 additions & 2 deletions ReCaptchaValidation/Model/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Magento\ReCaptchaValidationApi\Api\ValidatorInterface;
use Magento\ReCaptchaValidationApi\Model\ErrorMessagesProvider;
use ReCaptcha\ReCaptcha;
use ReCaptcha\ReCaptchaFactory;
use Magento\ReCaptchaValidation\Model\ReCaptchaFactory;

/**
* @inheritdoc
Expand All @@ -31,7 +31,7 @@ class Validator implements ValidatorInterface
private $errorMessagesProvider;

/**
* @var ReCaptchaFactory
* @var ReCaptchaFactory\
*/
private $reCaptchaFactory;

Expand Down
10 changes: 6 additions & 4 deletions ReCaptchaWebapiUi/view/frontend/web/js/webapiReCaptcha.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,16 @@ define(
var self = this,
trigger;

trigger = function () {
self.reCaptchaCallback(grecaptcha.getResponse(widgetId));
};
registry._isInvisibleType[this.getReCaptchaId()] = false;

if (this.getIsInvisibleRecaptcha()) {
trigger = function () {
grecaptcha.execute(widgetId);
};
} else {
trigger = function () {
self.reCaptchaCallback(grecaptcha.getResponse(widgetId));
};
registry._isInvisibleType[this.getReCaptchaId()] = true;
}

if (this.autoTrigger) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ define([], function () {
*/
_listeners: {},

/**
* recaptchaId: bool map
*/
_isInvisibleType: {},

/**
* Add a listener to when the ReCaptcha finishes verification
* @param {String} id - ReCaptchaId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,38 +41,40 @@ define(['squire'

expect(placeOrderMixins['Magento_ReCaptchaCheckout/js/model/place-order-mixin']).toBe(true);
});
});

describe('Magento_Checkout/js/action/redirect-on-success is called', function () {
var recaptchaId = 'recaptcha-checkout-place-order',
messageContainer = jasmine.createSpy('messageContainer'),
payload = {},
serviceUrl = 'test',

it('Magento_Checkout/js/action/redirect-on-success is called', function () {
let recaptchaId = 'recaptcha-checkout-place-order',
messageContainer = jasmine.createSpy('messageContainer'),
payload = {},
serviceUrl = 'test',

/**
* Order place action mock
*
* @returns {{fail: fail, done: (function(Function): *)}}
*/
action = function () {
return {
/**
* Success result for request
*
* @param {Function} handler
* @returns {*}
*/
done: function (handler) {
handler();
return this;
},

/**
* Fail result for request
*/
fail: function () {}
};
/**
* Order place action mock
*
* @returns {{fail: fail, done: (function(Function): *)}}
*/
action = function () {
return {
/**
* Success result for request
*
* @param {Function} handler
* @returns {*}
*/
done: function (handler) {
handler();
return this;
},

/**
* Fail result for request
*/
fail: function () {}
};
};

it('Only PlaceOrder button triggers place order action', function () {
/**
* Triggers declared listener
*
Expand All @@ -93,9 +95,39 @@ define(['squire'
registry.addListener = function (id, func) {
registry._listeners[id] = func;
};

registry.removeListener = jasmine.createSpy();
mixin()(action, serviceUrl, payload, messageContainer);
expect(registry.removeListener).toHaveBeenCalledWith(recaptchaId);
});

it('PlaceOrder Listener is called for invisible google recaptcha', function () {
/**
* Triggers declared listener
*
* @returns {*}
*/
registry.triggers[recaptchaId] = function () {
if (registry._listeners[recaptchaId] !== undefined) {
return registry._listeners[recaptchaId]('token');
}
};

/**
* Registers a listener
*
* @param id
* @param func
*/
registry.addListener = function (id, func) {
registry._listeners[id] = func;
};

registry._isInvisibleType[recaptchaId] = true;
registry.removeListener = jasmine.createSpy();
mixin()(action, serviceUrl, payload, messageContainer);

expect(registry.removeListener).not.toHaveBeenCalled();
});
});
});

0 comments on commit 63c3728

Please sign in to comment.