Skip to content
This repository has been archived by the owner on Jun 25, 2021. It is now read-only.

Commit

Permalink
fix issue #43
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas committed Jan 17, 2021
1 parent 4aa3fa1 commit 1f2f0d6
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 2 deletions.
36 changes: 36 additions & 0 deletions app/code/community/ProxiBlue/ReCaptcha/Model/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,5 +192,41 @@ public function newsletterSubscriber($observer)
return $this;
}

/**
* Check Captcha On Checkout Register Page
*
* @param Varien_Event_Observer $observer
* @return $this
*/
public function checkCheckout($observer)
{
$checkoutMethod = Mage::getSingleton('checkout/type_onepage')->getQuote()->getCheckoutMethod();
if ($checkoutMethod == Mage_Checkout_Model_Type_Onepage::METHOD_REGISTER) {
$formId = 'register_during_checkout';
$captchaModel = Mage::helper('captcha')->getCaptcha($formId);
if ($captchaModel->isRequired()) {
$controller = $observer->getControllerAction();
if (!$captchaModel->isCorrect($this->_getCaptchaString($controller->getRequest(), $formId))) {
$controller->setFlag('', Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true);
$result = array('error' => 1, 'message' => Mage::helper('captcha')->__('Incorrect CAPTCHA.'));
$controller->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
}
}
}
if ($checkoutMethod == Mage_Checkout_Model_Type_Onepage::METHOD_GUEST) {
$formId = 'guest_checkout';
$captchaModel = Mage::helper('captcha')->getCaptcha($formId);
if ($captchaModel->isRequired()) {
$controller = $observer->getControllerAction();
if (!$captchaModel->isCorrect($this->_getCaptchaString($controller->getRequest(), $formId))) {
$controller->setFlag('', Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true);
$result = array('error' => 1, 'message' => Mage::helper('captcha')->__('Incorrect CAPTCHA.'));
$controller->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
}
}
}
return $this;
}


}
34 changes: 34 additions & 0 deletions app/code/community/ProxiBlue/ReCaptcha/Model/Recaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ public function getElementId($type = 'input')
public function isRequired($login = null)
{
if(!$this->_isEnabled() || !in_array($this->_formId, $this->_getTargetForms())){
if($this->_formId == 'recapctha_checkout') {
return true;
}
return false;
}

Expand All @@ -208,4 +211,35 @@ private function _debug($message) {
Mage::log($message, null, 'recapctha.log');
}
}

/**
* Retrieve list of forms where captcha must be shown
*
* For frontend this list is based on current website
*
* @return array
*/
protected function _getTargetForms()
{
$formsString = (string) $this->_getHelper()->getConfigNode('forms');
$forms = explode(',', $formsString);
// remove either checkout as guest or register at checkout, and if either is there, replace with a generic checkout
if (($key = array_search('register_during_checkout', $forms)) !== false) {
unset($forms[$key]);
if (Mage::registry('has_recapctha') == false && $this->_formId == 'register_during_checkout') {
$forms[] = 'recapctha_checkout';
$this->_formId = 'recapctha_checkout';
Mage::register('has_recapctha', true, true);
}
}
if (($key = array_search('guest_checkout', $forms)) !== false) {
unset($forms[$key]);
if (Mage::registry('has_recapctha') == false && $this->_formId == 'guest_checkout') {
$forms[] = 'recapctha_checkout';
$this->_formId = 'recapctha_checkout';
Mage::register('has_recapctha', true, true);
}
}
return array_unique($forms);
}
}
10 changes: 9 additions & 1 deletion app/code/community/ProxiBlue/ReCaptcha/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<config>
<modules>
<ProxiBlue_ReCaptcha>
<version>2.3.4</version>
<version>2.3.7</version>
<depends>
<Mage_Captcha/>
</depends>
Expand Down Expand Up @@ -54,6 +54,14 @@
</captcha>
</observers>
</controller_action_predispatch_newsletter_subscriber_new>
<controller_action_predispatch_checkout_onepage_savebilling>
<observers>
<captcha_guest>
<class>proxiblue_recaptcha/observer</class>
<method>checkCheckout</method>
</captcha_guest>
</observers>
</controller_action_predispatch_checkout_onepage_savebilling>
</events>
</global>
<adminhtml>
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"license": "OSL-3.0",
"homepage": "https://github.com/ProxiBlue/reCaptcha",
"description": "Clean integration of Google reCaptcha to OpenMage",
"version": "2.3.6",
"version": "2.3.7",
"authors": [
{
"name": "Lucas van Staden",
Expand Down

0 comments on commit 1f2f0d6

Please sign in to comment.