Google Invisible reCaptcha Magento

This tutorial shows how to add Google Invisible reCaptcha Magento 1.

google-invisible-recaptcha-magento

Google Invisible reCaptcha Magento

<?php
class Addpeople_Validatecaptcha_Model_Observer
{

    public function loginValidateCaptcha(Varien_Event_Observer $observer)
    {
        try {

            $action = $observer->getControllerAction();
            $request = $action->getRequest();
            if( ($request->getActionName() == 'loginPost') && ( Mage::getStoreConfig('validate_captcha/server_side_validation/enabled_login') ) ) {
                $dataPost = $action->getRequest()->getPost();
                $this->validateCaptcha( $action, $dataPost );
            }

        }
        catch (Exception $ex) {
            Mage::log('Captcha Validation Error Login Page', null, "order.log");
        }

    }

    public function validateCaptcha( $action, $postedData ) {

        if ( $postedData['g-recaptcha-response'] == '' ) {

            $action->setFlag('', Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true);
            Mage::getSingleton('customer/session')->addError(Mage::helper('captcha')->__('Please check the reCAPTCHA field to continue.'));
            $action->getResponse()->setRedirect(Mage::getUrl('*/*/'));
        }
        else
        {
            $params = array();
            $params['secret'] = Mage::getStoreConfig('validate_captcha/server_side_validation/secret_key');
            $params['response'] = $postedData['g-recaptcha-response'];
            $params['remoteip'] = $_SERVER['REMOTE_ADDR'];

            $params_string = http_build_query($params);
            $url = 'https://www.google.com/recaptcha/api/siteverify?' . $params_string;

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $response = curl_exec($ch);

            $result = json_decode($response, true);

            if ( trim( $result['success'] ) != true ) {

                $action->setFlag('', Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true);
                Mage::getSingleton('customer/session')->addError(Mage::helper('captcha')->__('reCAPTCHA unable to verify.'));
                $action->getResponse()->setRedirect(Mage::getUrl('*/*/'));

            }
        }
    }

}

recaptcha_observer_code

Share this Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *