• JeremyTCline
  • NEWBIE
  • 5 Points
  • Member since 2009

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 3
    Replies
Here is a class for validating credit card formats for 4 major cards.
 
Method isValid returns boolean.   See test method for call examples.
 
public with sharing class CreditCardValidation {
public Boolean isValid(String CardType, String CardNumber)
{
CardNumber = CardNumber.replace(' ','');
Boolean ret = false;
if(CardType=='MasterCard')
{
ret = true;
//prefix: 51->55
ret = ret && (checkPrefix(CardNumber, '51') || checkPrefix(CardNumber, '55'));
//length: 16
ret = ret && CardNumber.length()==16;
//mod 10
ret = ret && checkMod10(CardNumber);
}
if(CardType=='American Express')
{
ret = true;
//prefix: 34 or 37
ret = ret && (checkPrefix(CardNumber, '34')
|| checkPrefix(CardNumber, '35')
|| checkPrefix(CardNumber, '36')
|| checkPrefix(CardNumber, '37'));
//length: 15
ret = ret && CardNumber.length()==15;
//mod 10
ret = ret && checkMod10(CardNumber);
}
if(CardType=='Discover')
{
ret = true;
//prefix: 6011
ret = ret && checkPrefix(CardNumber, '6011');
//length: 16
ret = ret && CardNumber.length()==16;
//mod 10
ret = ret && checkMod10(CardNumber);
}
if(CardType=='Visa')
{
ret = true;
//prefix: 4
ret = ret && checkPrefix(CardNumber, '4');
//length: 13 or 16
ret = ret && (CardNumber.length()==16 || CardNumber.length()==13);
//mod 10
ret = ret && checkMod10(CardNumber);
}
return ret;
}
private Boolean checkPrefix(String CardNumber, String Prefix)
{
return CardNumber.startsWith(Prefix);
}
private Boolean checkMod10(String CardNumber)
{
//first check if all digits are numeric
if(!pattern.matches('[0-9]+',CardNumber)) return false;
String s = '';
Integer digit = 0;
for(Integer d=CardNumber.length()-1; d>=0;d--)
{
digit++;
if(Mod(digit,2)==0)
{
Integer i = Integer.valueOf(CardNumber.substring(d,d+1))*2;
s+=i.format();
}
else
{
Integer i = Integer.valueOf(CardNumber.substring(d,d+1));
s+=i.format();
}
}
Integer sum = getSum(s);
system.debug('s:'+sum);
system.debug('sum:'+s);
return Mod(sum,10)==0;
}
private Integer getSum(String s)
{
Integer sum = 0;
for(Integer d=0; d<s.length();d++)
{
sum+=Integer.valueOf(s.substring(d,d+1));
}
return sum;
}
public Integer Mod(Integer n, Integer div)
{
while(n>=div)
n-=div;
return n;
}

    static testMethod void test() 
    {
CreditCardValidation cc = new CreditCardValidation();
system.debug('visa:'+cc.isValid('Visa','4111 1111 1111 1111'));
system.debug('MasterCard:'+cc.isValid('MasterCard','5500 0000 0000 0004'));
system.debug('American Express:'+cc.isValid('American Express','3400 0000 0000 009 '));
system.debug('Discover:'+cc.isValid('Discover','6011 0000 0000 0004'));
    }
Here is a class for validating credit card formats for 4 major cards.
 
Method isValid returns boolean.   See test method for call examples.
 
public with sharing class CreditCardValidation {
public Boolean isValid(String CardType, String CardNumber)
{
CardNumber = CardNumber.replace(' ','');
Boolean ret = false;
if(CardType=='MasterCard')
{
ret = true;
//prefix: 51->55
ret = ret && (checkPrefix(CardNumber, '51') || checkPrefix(CardNumber, '55'));
//length: 16
ret = ret && CardNumber.length()==16;
//mod 10
ret = ret && checkMod10(CardNumber);
}
if(CardType=='American Express')
{
ret = true;
//prefix: 34 or 37
ret = ret && (checkPrefix(CardNumber, '34')
|| checkPrefix(CardNumber, '35')
|| checkPrefix(CardNumber, '36')
|| checkPrefix(CardNumber, '37'));
//length: 15
ret = ret && CardNumber.length()==15;
//mod 10
ret = ret && checkMod10(CardNumber);
}
if(CardType=='Discover')
{
ret = true;
//prefix: 6011
ret = ret && checkPrefix(CardNumber, '6011');
//length: 16
ret = ret && CardNumber.length()==16;
//mod 10
ret = ret && checkMod10(CardNumber);
}
if(CardType=='Visa')
{
ret = true;
//prefix: 4
ret = ret && checkPrefix(CardNumber, '4');
//length: 13 or 16
ret = ret && (CardNumber.length()==16 || CardNumber.length()==13);
//mod 10
ret = ret && checkMod10(CardNumber);
}
return ret;
}
private Boolean checkPrefix(String CardNumber, String Prefix)
{
return CardNumber.startsWith(Prefix);
}
private Boolean checkMod10(String CardNumber)
{
//first check if all digits are numeric
if(!pattern.matches('[0-9]+',CardNumber)) return false;
String s = '';
Integer digit = 0;
for(Integer d=CardNumber.length()-1; d>=0;d--)
{
digit++;
if(Mod(digit,2)==0)
{
Integer i = Integer.valueOf(CardNumber.substring(d,d+1))*2;
s+=i.format();
}
else
{
Integer i = Integer.valueOf(CardNumber.substring(d,d+1));
s+=i.format();
}
}
Integer sum = getSum(s);
system.debug('s:'+sum);
system.debug('sum:'+s);
return Mod(sum,10)==0;
}
private Integer getSum(String s)
{
Integer sum = 0;
for(Integer d=0; d<s.length();d++)
{
sum+=Integer.valueOf(s.substring(d,d+1));
}
return sum;
}
public Integer Mod(Integer n, Integer div)
{
while(n>=div)
n-=div;
return n;
}

    static testMethod void test() 
    {
CreditCardValidation cc = new CreditCardValidation();
system.debug('visa:'+cc.isValid('Visa','4111 1111 1111 1111'));
system.debug('MasterCard:'+cc.isValid('MasterCard','5500 0000 0000 0004'));
system.debug('American Express:'+cc.isValid('American Express','3400 0000 0000 009 '));
system.debug('Discover:'+cc.isValid('Discover','6011 0000 0000 0004'));
    }

I'm looking for permanent Salesforce Developer to be based in the UK managing some administration but mainly development and implementation of new functionality, changes and customisation of a global CRM System.

 

You will be responsible for developing sandbox and production environments, producing innovative functional designs and driving adoption, data quality and best practice to meet all business requirementsI'm looking for someone with a proven track record in CRM development using Salesforce.com.

 

Competitive salary plus bonus.

 

Please let me know if your interested.

Hey Guys,

 

This one has just popped up on a clients org when running test classes, im frantically searching through the config to make sure no one has change something to unique but i havent found anything yet, any ideas?

 

System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, duplicate value found: <unknown> duplicates value on record with id: <unknown>