Created
August 25, 2016 09:18
-
-
Save jrgcubano/6e2c8d0eedc86c9e4959d3d0601869fb to your computer and use it in GitHub Desktop.
Faker::CreditCard creates fake credit card numbers and PhoneNumber::ddd creates fake ddds (brazilian) for testing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Complement to Faker gem to create credit card number | |
# Usage: | |
# > Faker::CreditCard.visa | |
# => "4916287009378994" | |
# | |
# Card support: | |
# visa, master, amex, discover, diners, en_route, jcb, voyager | |
# Inspired by: http://www.darkcoding.net/credit-card/luhn-formula/ | |
module Faker | |
class InexistentCardTypeError < ArgumentError; end | |
class CreditCard | |
class << self | |
def number(type, size = DEFAULT_SIZE) | |
raise InexistentCardTypeError unless PREFIXES.include? type | |
prefix = PREFIXES[type].sample | |
number = prefix.to_s.split(//).map(&:to_i) | |
number << rand(0..9) while number.size < size - 1 | |
number << check_digit(number) | |
number.join | |
end | |
def method_missing(m) | |
begin | |
number(m) | |
rescue InexistentCardTypeError | |
super | |
end | |
end | |
def cvv | |
rand(100..999) | |
end | |
def expire_date | |
(Time.now + rand(80000000..200000000)).strftime('%m/%y') | |
end | |
def type | |
PREFIXES.keys.sample | |
end | |
def sample | |
type = self.type | |
{ | |
type: type, | |
number: send(type), | |
cvv: cvv, | |
expire_date: expire_date, | |
} | |
end | |
private | |
DEFAULT_SIZE = 16 | |
def check_digit(number) | |
sum = 0 | |
number.reverse.each_with_index do |digit, index| | |
# remeber indexes starts with 0!!! | |
if index.even? | |
digit *= 2 | |
digit -= 9 if digit > 9 | |
end | |
sum += digit | |
end | |
((sum / 10 + 1) * 10 - sum) % 10 | |
end | |
end | |
PREFIXES = { | |
visa: [4539, 4556, 4916, 4532, 4929, 40240071, 4485, 4716, 4], | |
mastercard: [51, 52, 53, 54, 55], | |
amex: [34, 37], | |
discover: [6011], | |
diners: [300, 301, 302, 303, 36, 38], | |
en_route: [2014, 2149], | |
jcb: [3088, 3096, 3112, 3158, 3337, 3528], | |
voyager: [8699], | |
} | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Faker | |
class PhoneNumber | |
class << self | |
def ddd | |
DDD.sample | |
end | |
end | |
DDD = [11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 24, 27, 28, 31, 32, 33, | |
34, 35, 37, 38, 41, 42, 43, 44, 45, 46, 47, 48, 49, 51, 53, 54, 55, | |
61, 62, 63, 64, 65, 66, 67, 68, 69, 71, 73, 74, 75, 77, 79, 81, 82, | |
83, 84, 85, 86, 87, 88, 89, 91, 92, 93, 94, 95, 96, 97, 98] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://groups.google.com/g/fake-credit-card can i share this at here ?