Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
destruk
Binge Watcher

Luhn Credit Card Validation (code included)

I've been working on this a bit lately and thought I'd share a simplistic routine to verify credit card numbers.
This takes input as a numeric string and tells you if the card should be valid or not - for American Express, Discover, Visa, mastercard, Diner's Club, Maestro, and anything else out there (any number of digits)
Of course there can still be counterfeit cards, or invalid cvc codes or billing problems, but the payment processor takes care of that. This is simply another short check you can do before sending the number to your server.


Function Luhn(ccnumber As String) As String
N=[]
Tot=0
For x=LEN(ccnumber) To 0 Step -1
N.Push(ccnumber.mid(X,1).ToInt())
Next
For x=1 to LEN(ccnumber) Step 2
Tot=Tot+N[X]
Next
For x=2 to LEN(ccnumber) Step 2
N[X]=N[X]*2
If N[X]>9
Tot=Tot+1+(N[X]-10)
Else
Tot=Tot+N[X]
End If
Next
Print "Total: ";Tot
If (Tot.ToStr()).Right(1)="0"
Return "VALID CARD"
Else
Return "INVALID CARD"
End If
End Function


It's short enough for me but I'm sure someone here could make it much more efficient. 🙂 Have fun.
0 Kudos
13 REPLIES 13
RokuMarkn
Visitor

Re: Luhn Credit Card Validation (code included)

Well, if you have to make a challenge out of it, ok. 😉
I prefer to return a boolean rather than a string since it's less likely that a spelling mistake would result in an invalid comparison.


function Luhn(cc as String) as Boolean
sum = 0
for i = 0 to cc.Len()-1
digit = cc.Mid(cc.Len()-i-1, 1).ToInt()
if (i MOD 2) <> 0 then digit = 2 * digit
digitsum = [ 0,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9 ]
sum = sum + digitsum[digit]
end for
return (sum MOD 10) = 0
end function
0 Kudos
destruk
Binge Watcher

Re: Luhn Credit Card Validation (code included)

Hey, very cool RokuMarkn!
0 Kudos
EnTerr
Roku Guru

Luhn Account Number Check

"Me too": :roll:
function isLuhnValid(cc as String) as Boolean:
digisum = [0,1,2,3,4,5,6,7,8,9,0,2,4,6,8,1,3,5,7,9]
sum = 0: ofs = 0
for i = len(cc) to 1 step -1:
sum = sum + digisum[strtoi(mid(cc,i,1)) + ofs]
ofs = 10 - ofs
end for
return sum mod 10 = 0
end function

Differences
  • is faster (in the unlikely event that matters; 1/3 less time)

  • is less readable (hey, i did not say it's an advantage)

  • can be turned into a one-liner since has no IFs (i.e. `function isLuhnValid(cc as String) as Boolean: digisum = [0,1,2,3,4,5,6,7,8,9,0,2,4,6,8,1,3,5,7,9]: sum = 0: ofs = 0: for i = len(cc) to 1 step -1: sum = sum + digisum[strtoi(mid(cc,i,1)) + ofs]: ofs = 10 - ofs: end for: return sum mod 10 = 0: end function`)

Following belltown's performance notes, i toyed with a roByteArray version:
function isLuhnValid2(cc as String) as Boolean:
if len(cc) mod 2 > 0 then ofs = 10 else ofs = 0
ba = createObject("roByteArray")
ba.fromAsciiString(cc)
digisum = [0,1,2,3,4,5,6,7,8,9,0,2,4,6,8,1,3,5,7,9]
sum = 0
for each x in ba:
sum = sum + digisum[x - 48 + ofs]
ofs = 10 - ofs
end for
return sum mod 10 = 0
end function

This one is even faster (0.6x of isLuhnValid time, >2x faster than destruk's) but even less readable and it gets ugly if there are non-digits in the string (not that the other functions return correct result in such case - they don't - but this one gives me the creeps) - so i consider it of cryptozoological interest; e.g. trying to understand how it does it.

RokuMarkn's version is probably as close to golden middle as it goes - with the only note to pull digitsum init outside the loop.
0 Kudos
RokuMarkn
Visitor

Re: Luhn Credit Card Validation (code included)

Hi EnTerr,
You've been talking about performance a lot lately, and while there's nothing wrong with that, I just want to remind everyone that "premature optimization is the root of all evil" (Knuth). Unless you have good evidence to believe that a piece of code is going to be a bottleneck, worrying about optimizing it on the first write is just going to result in less readable, less maintainable code for no real reason. In a piece of code like a credit card validator, unless you're running a backend payment processor, it's NEVER going to be a bottleneck in any real world situation.

--Mark
0 Kudos
EnTerr
Roku Guru

Re: Luhn Credit Card Validation (code included)

"RokuMarkn" wrote:
You've been talking about performance a lot lately, and while there's nothing wrong with that, I just want to remind everyone that "premature optimization is the root of all evil"

No argument there, Mark.
In previous threads i had reasons to look for performance - that roXML cannot parse properly on fw3 and everything followed from that (had to write parser etc). The re-work here I consider it an exercise, building intuition on the language. To quote in turn Patton/Suvorov/the marines, "the more you sweat in training, the less you bleed in battle".

PS. Note I said "is faster (in the unlikely event that matters... ) ... is less readable " and concluded with
RokuMarkn's version is probably as close to golden middle as it goes - with the only note to pull digitsum init outside the loop.
0 Kudos
RokuJoel
Binge Watcher

Re: Luhn Credit Card Validation (code included)

"RokuMarkn" wrote:
unless you're running a backend payment processor


Ok, who leaked my 100% pure Brightscript payment processing plans? With this super efficient code running secretly as a distributed processing screensaver on 7 million Rokus I can now take over the universe, the galaxy shall be mine as I grind my enemies into dust....uh, sorry got carried away there, what were you saying about the root of all evil?

- Joel
0 Kudos
EnTerr
Roku Guru

Re: Luhn Credit Card Validation (code included)

"RokuJoel" wrote:
Ok, who leaked my 100% pure Brightscript payment processing plans? With this super efficient code running secretly as a distributed processing screensaver on 7 million Rokus I can now take over the universe...

😄 HHOS: Hmmm, that's quite brilliant thought actually!
Have you considered a distributed computing project running as screen saver, akin to what SETI@home was?
0 Kudos
RokuJoel
Binge Watcher

Re: Luhn Credit Card Validation (code included)

Yes I have, its one of my frequent daydreams, but when I get home from work I'm too tired to actually work on the project.

- Joel
0 Kudos
EnTerr
Roku Guru

Re: Luhn Credit Card Validation (code included)

"RokuJoel" wrote:
Yes I have, its one of my frequent daydreams, but when I get home from work I'm too tired to actually work on the project.

We need to talk to your boss about overworking you - and about instituting 20% side-project program at RokuCo, like the one in Google which brought us Gmail/Maps/AdSense/Google Now.

PS. that part about taking over the universe and pulverizing enemies though... i wouldn't mention it. :mrgreen:
0 Kudos