Mask 16 digit credit card but not 20 digit numbers

deepaltrivedi
Mega Contributor

Hi All - We have a change in requirement where we want to mask 16 digit credit card information in inbound email body but not 20 digit numbers as they are not credit card. I've below script that works perfectly but it masks any number over 12 digits -- i don't want to mask 20 digits number.

 

Thoughts on how i need to modify the bolded portion??

 

(function executeRule(current, previous /*null when async*/) {
var emailVars = [current.body,current.subject,current.body_text];
var arr = [];
for(e = 0; e < emailVars.length; e++) {


var wt = emailVars[e].match(/(https:\/\/.*wetransfer\.com)(.*)[^\s]/g);
var matches = emailVars[e].match(/(https:\/\/.*wetransfer\.com)(.*)[^\s]|([\d]){4}([ -])?([\d]){4}([ -])?([\d]){4}([ -])?([\d]){4}/g);
if(matches){
for(var i = 0; i < 16; i++) {
if(!wt || matches[i] != wt[0]) {
var numCount = 0;
var maskedCredCard = '';
for(var j = 0; j < matches[i].length; j++) {
if(matches[i][j] == '-' ||matches[i][j] == ' '|| numCount >= 12) {
maskedCredCard += matches[i][j];
}
else if(numCount < 12) {
maskedCredCard += '*';
numCount++;
}
}
if(e == 0) {
current.body = current.body.replace(matches[i] ,maskedCredCard);
}
else if(e == 1) {
current.subject = current.subject.replace(matches[i] ,maskedCredCard);
}
else if(e == 2) {
current.body_text = current.body_text.replace(matches[i], maskedCredCard);
}
} //closes WT if statement
}//closes matches for loop


} //closes if there are matches
} //closes loop through email variables
})(current, previous);

1 ACCEPTED SOLUTION

Hi,

 

Replace this below line and check

var numbers = email_body.match(/\d+/g).map(Number);

WITH

var numbers = email_body.match(/\s\d+/g).map(Number);

View solution in original post

26 REPLIES 26

Also, you can have a condition like this, but then i am not sure how you are checking with 20 digit

if(matches && matches[0].length <=19){ //19 here stands for 16 digit number and 3 hyphens. 

 

the url is something like https://wetransfer.com/4567891234152637

 

As to your second question, that is what i'm trying to figure out. Currently, the code isn't checking how to handle 20 digits differently.

Any update?

Hi,

Sorry, did not see your earlier message. I have changed your code as per given Sample URL, try with this.

Tested with sample data in my dev instance and its working fine.

var emailVars = [current.body,current.subject,current.body_text];
var arr = [];
for(e = 0; e < emailVars.length; e++) {
    var numbers = emailVars[e].match(/\d+/g).map(Number);
    for(j=0;j<numbers.length;j++) {
      //gs.print("Number is "+numbers[j]+"---length is "+numbers[j].toString().length);
      if(numbers[j].toString().length == 16) {
        var cc_number = numbers[j].toString();
        break;
      } 
    }
    var credit_card_found=0;
    if (j < numbers[j].toString().length) {
       credit_card_found=1;
      //mask the credit card number
      var numCount=0;
      var maskedCredCard = '';
      for (i=0;i<16;i++) {
         if(numCount <12) {
           maskedCredCard += '*';
         } 
         else {
           maskedCredCard += cc_number[i];
         }
         numCount++;
      }
   // gs.print("CC number in "+emailVars[e]+" is "+maskedCredCard+"---"+cc_number);
  } //if j matches

  if(e == 0 && credit_card_found == 1) {
    current.body = current.body.replace(cc_number ,maskedCredCard);
  }
  else if(e == 1 && credit_card_found == 1) {
    current.subject = current.subject.replace(cc_number ,maskedCredCard);
  }
  else if(e == 2 && credit_card_found == 1) {
    current.body_text = current.body_text.replace(cc_number, maskedCredCard);
  }
} //closes loop through email variables

Mark the comment as a correct answer and also helpful once worked.

Thank you, it works partially.  The thing is we want do not want to block 16 digits if they exist in the URL. the sample URL i provided is one of the types of wetransfer URL, there could be other ones where there's phrases in front of it.