0

I am trying to display Google ReCaptcha in a custom phtml form which is calling in CMS page. Please note this page does not have any xml handler. After some struggles I was able to display Google ReCAPTCHA in the form but everytime the validation is failing and showing this message "You cannot proceed with such operation, your reCaptcha reputation is too low.". Please note that the google recaptcha configurations are fine because the same recaptcha I am using in other forms like login, user creation and other custom forms. All other places this ReCaptcha is working fine except this custom form. Can you Suggest me where exactly I should focus to fix this issue ?

Please find the Steps I followed to display ReCaptcha:
Step-1: I called this snippet in my form phtml file

<?php
        echo $recaptchaBlock = $this->getLayout()
            ->createBlock(Vendor\Module\Block\Frontend\ReCaptcha::class)
            ->setTemplate("MSP_ReCaptcha::msp_recaptcha.phtml")
            ->setJsLayout(['components' => ['msp-recaptcha' => ['component' => 'MSP_ReCaptcha/js/reCaptcha', 'zone' => 'custom_contact_form']]])
            ->toHtml();
        ?>

Step-2: This is my ReCaptcha.php block file code

<?php
namespace Vendor\Module\Block\Frontend;

use Magento\Framework\View\Element\Template;
use MSP\ReCaptcha\Model\Config;
use MSP\ReCaptcha\Model\LayoutSettings;

class ReCaptcha extends \MSP\ReCaptcha\Block\Frontend\ReCaptcha
{
    public function __construct(Template\Context $context, LayoutSettings $layoutSettings, array $data = [], Config $config = null, $decoder = null, $encoder = null)
    {
        parent::__construct($context, $decoder, $encoder, $layoutSettings, $data, $config);
    }

    public function setJsLayout(array $jsLayout)
    {
        $this->jsLayout = $jsLayout;
        return $this;
    }
}

Step-3: this is my di.xml file code

<type name="MSP\ReCaptcha\Model\LayoutSettings">
        <plugin name="vendor_module_contact_form_recaptcha_zone"
                type="Vendor\Module\Plugin\ReCaptcha\Model\ContactLayoutSettings"
                sortOrder="20" />
    </type>
    <virtualType name="Vendor\Module\Model\Provider\IsCheckRequired\Frontend\CustomContactForm"
                 type="MSP\ReCaptcha\Model\IsCheckRequired">
        <arguments>
            <argument name="enableConfigFlag"
                      xsi:type="string">msp_securitysuite_recaptcha/frontend/enabled_custom_contact_form</argument>
            <argument name="area" xsi:type="string">frontend</argument>
        </arguments>
    </virtualType>
    <virtualType name="Vendor\Module\Model\Provider\Failure\CustomContactFormObserver"
                 type="MSP\ReCaptcha\Model\Provider\Failure\AjaxResponseFailure">
        <arguments>
            <argument name="redirectUrlProvider"
                      xsi:type="object">MSP\ReCaptcha\Model\Provider\Failure\RedirectUrl\ReferrerUrlProvider</argument>
        </arguments>
    </virtualType>
    <virtualType name="Vendor\Module\Observer\Frontend\CustomContactFormObserver"
                 type="MSP\ReCaptcha\Observer\ReCaptchaObserver">
        <arguments>
            <argument name="isCheckRequired"
                      xsi:type="object">Vendor\Module\Model\Provider\IsCheckRequired\Frontend\CustomContactForm</argument>
            <argument name="responseProvider"
                      xsi:type="object">Vendor\Module\Model\Provider\Response\AjaxResponseProvider</argument>
            <argument name="failureProvider"
                      xsi:type="object">Vendor\Module\Model\Provider\Failure\CustomContactFormObserver</argument>
        </arguments>
    </virtualType>

Step-4: this is my plugin model ContactLayoutSettings.php file code

<?php

namespace Vendor\Module\Plugin\ReCaptcha\Model;

use MSP\ReCaptcha\Model\LayoutSettings as Subject;
use Vendor\Module\Helper\Data as Config;

/**
 * Class LayoutSettings
 * The class responsible for adding custom_contact_form zone to MSP_ReCaptcha Layout setting
 */
class ContactLayoutSettings
{
    /**
     * @var Config
     */
    private $config;

    /**
     * LayoutSettings constructor.
     * @param Config $config
     */
    public function __construct(Config $config)
    {
        $this->config = $config;
    }

    /**
     * @param Subject $subject
     * @param array $result
     * @return array
     */
    public function afterGetCaptchaSettings(Subject $subject, array $result)
    {

        if (isset($result['enabled'])) {
            $result['enabled']['custom_contact_form'] = $this->config->isEnabledFrontendCustomContactForm();
        }

        return $result;
    }
}

Step-5: this is my events.xml file

<event name="controller_action_predispatch_bookappointment_contact_save">
        <observer name="vendor_module_custom_contact_form_recaptcha" instance=
        "Vendor\Module\Observer\Frontend\CustomContactFormObserver" />
    </event>
</config>

step-6: this is my help data function

/**
     * Return true if enabled on frontend BookAppointment custom contact form
     * @return boolean
     */
    public function isEnabledFrontendCustomContactForm(): bool
    {
        if (!$this->recaptchaConfig->isEnabledFrontend()) {
            return false;
        }

        return (bool) $this->scopeConfig->getValue(
            self::XML_PATH_ENABLED_FRONTEND_CUSTOM_CONTACT_FORM,
            ScopeInterface::SCOPE_WEBSITE
        );
    }
2
  • Can you please share the code that you are doing for entering Recaptcha? Commented Apr 4, 2022 at 11:29
  • Hi @RutvikMonpara I updated the code in the post above Commented Apr 4, 2022 at 11:52

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.