What is a good way to create a random number for a valid credit card?

I am developing a Java toolkit for checking and working with credit cards. So far, I support:

  • LUHN Confirmation.

  • Checking the date (simple completion).

  • Checking the code code length (CVV, CVC, CID) based on the brand (Visa, MasterCard, etc.).

  • Check credit card length (based on brand).

  • Check BIN / IIN (in relation to the database of valid numbers).

  • Hiding numbers (425010 * * * * * * 1234)

To make the tool a little more complete, I would like to create a random number generator for credit cards based on different brands of cards. This functionality (hopefully) will make my test cases more reliable.

Basically, I would like to be able to generate numbers that:

  • LUHN valid

  • Based on brand prefixes

  • Valid based on BIN / IIN prefix numbers

For valid BIN / IIN card numbers, I am going to find a random BIN / IIN number from the database (based on the brand, of course), and then add the remaining digits using Random . Obviously, this would be invalid most of the time, and I would have to increase one of the digits until it passes the LUHN check.

I can't seem to think of a better way. Maybe someone can suggest something a little smarter ...?

Looking forward to your suggestions! Thanks in advance!:)

+7
source share
2 answers

This functionality (hopefully) will make my test cases more reliable.

I'm not sure. In my experience, it is not practical to use random data in unit tests because you never know if you covered all the important cases ... and errors.

I would recommend creating your test credit card numbers manually and ensuring that they cover all cases that need to be verified.

+2
source

Not so long ago I created a library called MockNeat . One possibility is to allow the developer to generate different valid credit card numbers.

Check out this method: creditCards () .

A short example for writing 1000 AMEX and Mastercard credit cards in a file for later use:

  MockNeat m = MockNeat.threadLocal(); final Path path = Paths.get("cc.txt"); // Write in a file 1000 credit cards AMEX and Mastercard: m.creditCards() .types(MASTERCARD, AMERICAN_EXPRESS) .list(1000) .consume(list -> { try { Files.write(path, list, CREATE, WRITE); } catch (IOException e) { e.printStackTrace(); } }); 
+2
source

All Articles