From 3ff2cdbfd47098b50ea2e280eb7d336fa2de7b0c Mon Sep 17 00:00:00 2001 From: Isaac Vallee Date: Wed, 22 Dec 2021 09:45:09 -0800 Subject: [PATCH] Initial recaptcha example cartridge --- int_recaptcha/int_recaptcha/.project | 17 +++ int_recaptcha/int_recaptcha/.tern-project | 11 ++ .../cartridge/app_custom_core.properties | 4 + .../cartridge/controllers/ContactUs.js | 45 +++++++ .../cartridge/scripts/services/recaptcha.js | 32 +++++ .../default/contactUs/contactUs.isml | 114 ++++++++++++++++++ 6 files changed, 223 insertions(+) create mode 100644 int_recaptcha/int_recaptcha/.project create mode 100644 int_recaptcha/int_recaptcha/.tern-project create mode 100644 int_recaptcha/int_recaptcha/cartridge/app_custom_core.properties create mode 100644 int_recaptcha/int_recaptcha/cartridge/controllers/ContactUs.js create mode 100644 int_recaptcha/int_recaptcha/cartridge/scripts/services/recaptcha.js create mode 100644 int_recaptcha/int_recaptcha/cartridge/templates/default/contactUs/contactUs.isml diff --git a/int_recaptcha/int_recaptcha/.project b/int_recaptcha/int_recaptcha/.project new file mode 100644 index 0000000..81802af --- /dev/null +++ b/int_recaptcha/int_recaptcha/.project @@ -0,0 +1,17 @@ + + + app_custom_core + + + + + + com.demandware.studio.core.beehiveElementBuilder + + + + + + com.demandware.studio.core.beehiveNature + + diff --git a/int_recaptcha/int_recaptcha/.tern-project b/int_recaptcha/int_recaptcha/.tern-project new file mode 100644 index 0000000..3e94bc2 --- /dev/null +++ b/int_recaptcha/int_recaptcha/.tern-project @@ -0,0 +1,11 @@ +{ + 'ecmaVersion': 5, + 'plugins': { + 'guess-types': { + }, + 'outline': { + }, + 'demandware': { + } + } +}' diff --git a/int_recaptcha/int_recaptcha/cartridge/app_custom_core.properties b/int_recaptcha/int_recaptcha/cartridge/app_custom_core.properties new file mode 100644 index 0000000..fb2e6f0 --- /dev/null +++ b/int_recaptcha/int_recaptcha/cartridge/app_custom_core.properties @@ -0,0 +1,4 @@ +## cartridge.properties for cartridge app_custom_core +#Tue Nov 21 11:43:49 CEST 2021 +demandware.cartridges.app_custom_core.multipleLanguageStorefront=true +demandware.cartridges.app_custom_core.id=app_custom_core \ No newline at end of file diff --git a/int_recaptcha/int_recaptcha/cartridge/controllers/ContactUs.js b/int_recaptcha/int_recaptcha/cartridge/controllers/ContactUs.js new file mode 100644 index 0000000..911dab4 --- /dev/null +++ b/int_recaptcha/int_recaptcha/cartridge/controllers/ContactUs.js @@ -0,0 +1,45 @@ +'use strict'; + +var server = require('server'); + +server.append('Landing', function(req, res, next) { + res.setViewData({ + recaptchaUrl: 'https://www.google.com/recaptcha/api.js', + siteKey: '6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI' // Test Site Key from Google + }); + + next(); +}); + +server.append('Subscribe', function(req, res, next) { + // Token automatically added to request by recaptcha + var token = req.form['g-recaptcha-response']; + + // Add required parameters for validation call + var params = { + // This is a test secret from Google + // In practice, do not hardcode this. It's better to store + // In a custom preference or service credential Configuration + secret: '6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe', // Test secret key from Google + response: token + }; + + // Calls recaptcha service + // Returns a Service class instance + var validationResult = recaptchaService.call(params); + + // Actual response from Google + var response = verificationResult.object; + + if (!response.success) { + // Handle failure by returning error to the client + res.json({ + success: false, + msg: 'Recaptcha Validation Failure' + }); + } + + next(); +}); + +module.exports = server.exports(); \ No newline at end of file diff --git a/int_recaptcha/int_recaptcha/cartridge/scripts/services/recaptcha.js b/int_recaptcha/int_recaptcha/cartridge/scripts/services/recaptcha.js new file mode 100644 index 0000000..54edb7e --- /dev/null +++ b/int_recaptcha/int_recaptcha/cartridge/scripts/services/recaptcha.js @@ -0,0 +1,32 @@ +'use strict'; +var LocalServiceRegistry = require('dw/svc/LocalServiceRegistry'); + +var recaptchaService = LocalServiceRegistry.createService('recaptcha.http.post', { + createRequest: function(svc, params) { + svc.setRequestMethod('POST'); + // You can perform other operations, such as adding request headers + svc.addHeader('Content-Type', 'application/json'); + // Add properties to the request body + svc.addParam('secret', params.secret); + svc.addParam('response', params.response); + }, + parseResponse: function(svc, responseObject) { + return JSON.parse(response.text); + }, + // mockCall: function(svc, requestObject) { + // // Return a mocked response + // // If configuration set to MOCK mode + // return { + // success: true, + // 'challenge_ts': Date.now(), + // hostname: 'MOCK EXAMPLE' + // }; + // } + // If dealing with any sensitive information + // You should filter it out before it hits the logs + // filterLogMessage: function() {} +}); + +module.exports = { + recaptchaService: recaptchaService +}; diff --git a/int_recaptcha/int_recaptcha/cartridge/templates/default/contactUs/contactUs.isml b/int_recaptcha/int_recaptcha/cartridge/templates/default/contactUs/contactUs.isml new file mode 100644 index 0000000..b5b06e4 --- /dev/null +++ b/int_recaptcha/int_recaptcha/cartridge/templates/default/contactUs/contactUs.isml @@ -0,0 +1,114 @@ + + + var assets = require('*/cartridge/scripts/assets.js'); + assets.addJs('/js/contactUs.js'); + assets.addCss('/css/contactUs.css'); + + // Loads recaptcha API + assets.addJs(pdict.recaptchaUrl); + + +
+

${Resource.msg('title.contact.us.page', 'contactUs', null)}

+
+ +
+
+
+
+
+
+ Add recaptcha container +
+ + +
+
+
+ + +
+
+
+
+
+ + +
+
+
+
+ + +
+
+
+ + +
+
+
+
+ + +
+
+
+ + +
+
+
+ + +
+
+
+ + +
+
+
+ + +
+
+
+
+
+ +
+
+
+
+
+
+
+
+
+