+ Start a Discussion
PammyPammy 

How to mask credit card number using apex

Hi All,

I want to mask credit card number in Case Description field. Whenever case get created in sfdc if case description field contains any credit card number, I just wanted to mask credit card number. For example if anyone enter credit card number like 4346-7810-4356-6678 in case description, After saving the case Credit card number should be look like as xxxx-xxxx-xxxx-xxxx. I have also tried to write trigger on Case object. But it is not working properly. It is making all number as cross(x) in case description field. Please find below code:
Trigger:
trigger testMaskCC on Case(before insert){
 if(trigger.isBefore && trigger.isInsert){ 
        CreditCard_No_Masking_OnCase NewCase= new CreditCard_No_Masking_OnCase();
        NewCase.newCaseInsert(Trigger.new);
    }      
}


Trigger Class
public class CreditCard_No_Masking_OnCase{

    public void newCaseInsert(list<Case> newList){
    
    String reGexVisa='4[0-9]{12}(?:[0-9]{3})';
    Pattern regexPattern = Pattern.compile(reGexVisa);
        for(case cs : newList){
           String withOutSpec=(cs.description).replaceAll('[-, +]','');
           Matcher regexMatcher = regexPattern.matcher(withOutSpec);
            system.debug('-------------group count----'+regexMatcher.groupCount());
           while(regexMatcher.find()){
              Integer countGroup=0;
              String  matchValue=regexMatcher.group(countGroup);
              cs.description= String.valueOf(cs.description).replaceAll('[0-9]', 'X');
              countGroup++;
            }
        }
    }
}


And also I am using regex for idetifying different companies's credit card number like Visa Card: ^4[0-9]{12}(?:[0-9]{3})?$  Mastercard: ^5[1-5][0-9]{14}$.

Please help me to generalized Regex(I wanted to use one Regex to identify different comany's credit card instead of using different company's regex) and Mask the Credit Card number only.
Any Help in this regard would be much appriciated.

 
Sean FieldingSean Fielding
We built an application for this
https://appexchange.salesforce.com/listingDetail?listingId=a0N3A00000ErAUSUA3
Matt Parker 1Matt Parker 1
@Pammy 
We built a (slightly cheaper) application for this called PCIFY:
https://appexchange.salesforce.com/appxListingDetail?listingId=a0N3A00000EcvUUUAZ

It has a 14-day free trial so give it a shot and see if it works for you
Sean FieldingSean Fielding
For anyone who doesn't want to worry about custom triggers & REGEX, we recently released DataMask 2.2, which also includes Einstein Bot support and advanced masking logic (including look behind inclusions).  Check us out at: https://appexchange.salesforce.com/appxListingDetail?listingId=a0N3A00000ErAUSUA3
Mallik1221Mallik1221
@Pammy
Hi Pammy, Did you find solution for this issue, please let me know.

Thanks
Malli