anyone who tried to add captcha for newsletter? or...
# suitecommerce
s
anyone who tried to add captcha for newsletter? or any guide?
g
I added one to the registration.
Client side:
LoginRegisterRegisterView.prototype.submitForm = function (e, model, props) { e.preventDefault(); var self = this; var data = jQuery(e.target).closest(‘form’).serializeObject(); grecaptcha.ready(function () { grecaptcha.execute(’ABCDEFGHIJ, { action: ‘submit’ }).then(function (token) { self.$el.find(“#token”).val(token); return self.cancelableTrigger(‘before:LoginRegister.register’, data) .then(function () { self.saveForm(e, model, props); }); }); }); }
Server Side:
define( ’Register.Captcha, [ ‘Application’ ], function ( Application ) { ‘use strict’; Application.on(‘before:Account.Register.ServiceController.post’, function () { var res; try { var e = arguments[0]; res = nlapiRequestURL(“https://www.google.com/recaptcha/api/siteverify”, { secret: “ABCDEFG, response: e.data.token }, null); } catch (err) { nlapiLogExecution(“debug”, “reCAPTCHA error”, err); } if (res) { var gRes = JSON.parse(res.getBody()); if (!gRes.success) { nlapiLogExecution(“debug”, “reCAPTCHA blocked”, “Request blocked: ” + res.getBody() + ” ” + JSON.stringify(e.data)); throw new Error(“reCAPTCHA blocked this request. (” + gRes.score + “)”); }else{ nlapiLogExecution(“debug”, “reCAPTCHA passed”, “Request passed: ” + res.getBody()); } } }); });
You also need to link to the script client side:
if (!document.getElementById(“recaptcha”)) { var script = document.createElement(‘script’); script.id = ‘recaptcha’; script.type = ‘text/javascript’; script.src = ’https://www.google.com/recaptcha/api.js?render=ABCDEFGHI; document.getElementsByTagName(‘head’)[0].appendChild(script); }
Oh and add an input to the template:
<input id=“token” name=“token” type=“hidden” />
It’s not simple. Hope that helps.
s
thank you @Gordon Truslove