Skip to content

Commit

Permalink
Merge pull request #19 from Foxy/feat/add-customer-name-to-transactio…
Browse files Browse the repository at this point in the history
…n-block-function-matchlist

Add customer name matching to the transaction blocking function
  • Loading branch information
rijarobinson authored Feb 24, 2023
2 parents 643edba + f5e44ca commit 19f3df2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Forking the repository will create your own copy of this Webhook, allowing you t

### Customize

Modify the matchlist.json file to include the email addresses/ip addresses that you want to block. If you don't want to block based on one of those, you can leave the array empty. Note that the examples provided are for illustrative purposes only. It'll work as is, but blocking based on IP or email isn't necessarily a recommended approach.
Modify the matchlist.json file to include the email addresses/ip addresses/customer names that you want to block. If you don't want to block based on one of those, you can leave the array empty. Note that the examples provided are for illustrative purposes only. It'll work as is, but blocking based on IP, email or name isn't necessarily a recommended approach.

You can also modify the error message that's displayed to customers when the pre-payment webhook validation fails. The default message is "Sorry, the transaction cannot be completed." Do not remove the quotes enclosing the message.

Expand Down
36 changes: 34 additions & 2 deletions src/functions/block-transactions-on-customer-criteria/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const MatchList = require("./matchlist.json");

/**
* Receives the request, processes it and sends the response.
*
*
* @param {Object} requestEvent the request event built by Netlify Functions
* @returns {Promise<{statusCode: number, body: string}>} the response object
*/
Expand All @@ -12,7 +12,7 @@ async function handler(requestEvent) {
const customerEmail = extractCustomerEmail(requestEvent.body);
const customerIP = extractCustomerIP(requestEvent.body);

if (getEmailList().includes(customerEmail) || getIPAddressList().includes(customerIP)) {
if (getEmailList().includes(customerEmail) || getIPAddressList().includes(customerIP) || checkCustomerNames(requestEvent.body)) {
return {
body: JSON.stringify({ details: "Sorry, the transaction cannot be completed.", ok: false }),
statusCode: 200,
Expand Down Expand Up @@ -52,6 +52,25 @@ function extractCustomerEmail(body) {
return "";
}

/**
* Extract Customer Name from payload received from FoxyCart
*
* @param {string} body of the data received from payload
* @param {string} address_object the embedded object to get the name from
* @returns {string} full customer name
*/
function extractCustomerName(body, address_object = "fx:customer") {
const objBody = JSON.parse(body);
let customer_name = "";
if (objBody?._embedded?.[address_object]?.['first_name']) {
customer_name += objBody._embedded[address_object]['first_name'];
}
if (objBody?._embedded?.[address_object]?.['last_name']) {
customer_name += " " + objBody._embedded[address_object]['last_name'];
}
return customer_name.trim().toLowerCase();
}

/**
* Opens file and returns list of emails
*
Expand Down Expand Up @@ -82,6 +101,19 @@ function getEmailList() {
return [];
}

/**
* Check if any customer names from payload received from FoxyCart are in the block list
*
* @param {string} body of the data received from payload
* @returns {boolean} true if a customer name from the match list is found in the transaction
*/
function checkCustomerNames(body) {
const namesToReject = MatchList["customer_names"] || [];
const customer_names = [extractCustomerName(body, 'fx:customer'), extractCustomerName(body, 'fx:shipment')];

return namesToReject.some((name) => { return (name != "" && customer_names.includes(name.toLowerCase())); });
}

module.exports = {
handler
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
{ "_comment": "Leave any arrays empty ([]) that you don't want to track.",
{ "_comment": "Leave any arrays empty ([]) that you don't want to block.",
"email_addresses": [
"[email protected]",
"[email protected]"
],
"ip_addresses": [
"192.169.1.1"
],
"customer_names": [
"John Doe",
"Jane Doe"
]
}

0 comments on commit 19f3df2

Please sign in to comment.