Module: ActiveMerchant::Billing::CreditCardMethods

Included in:
CreditCard
Defined in:
lib/active_merchant/billing/credit_card_methods.rb

Overview

Convenience methods that can be included into a custom Credit Card object, such as an ActiveRecord based Credit Card object.

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

CARD_COMPANY_DETECTORS =
{
  'visa'               => ->(num) { num =~ /^4\d{12}(\d{3})?(\d{3})?$/ },
  'master'             => ->(num) { num&.size == 16 && in_bin_range?(num.slice(0, 6), MASTERCARD_RANGES) },
  'discover'           => ->(num) { num =~ /^(6011|65\d{2}|64[4-9]\d)\d{12}|(62\d{14})$/ },
  'american_express'   => ->(num) { num =~ /^3[47]\d{13}$/ },
  'diners_club'        => ->(num) { num =~ /^3(0[0-5]|[68]\d)\d{11}$/ },
  'jcb'                => ->(num) { num =~ /^35(28|29|[3-8]\d)\d{12}$/ },
  'dankort'            => ->(num) { num =~ /^5019\d{12}$/ },
  'maestro'            => ->(num) { (12..19).include?(num&.size) && in_bin_range?(num.slice(0, 6), MAESTRO_RANGES) },
  'forbrugsforeningen' => ->(num) { num =~ /^600722\d{10}$/ },
  'sodexo'             => ->(num) { num =~ /^(606071|603389|606070|606069|606068|600818)\d{8}$/ },
  'vr'                 => ->(num) { num =~ /^(627416|637036)\d{8}$/ },
  'carnet'             => lambda { |num|
    num&.size == 16 && (
      in_bin_range?(num.slice(0, 6), CARNET_RANGES) ||
      CARNET_BINS.any? { |bin| num.slice(0, bin.size) == bin }
    )
  }
}
ELECTRON_RANGES =
[
  [400115],
  (400837..400839),
  (412921..412923),
  [417935],
  (419740..419741),
  (419773..419775),
  [424519],
  (424962..424963),
  [437860],
  [444000],
  [459472],
  (484406..484411),
  (484413..484414),
  (484418..484418),
  (484428..484455),
  (491730..491759),
]
CARNET_RANGES =
[
  (506199..506499),
]
CARNET_BINS =
Set.new(
  [
    '286900', '502275', '606333', '627535', '636318', '636379', '639388',
    '639484', '639559', '50633601', '50633606', '58877274', '62753500',
    '60462203', '60462204', '588772'
  ]
)
MASTERCARD_RANGES =
[
  (222100..272099),
  (510000..559999),
]
MAESTRO_RANGES =
[
  (639000..639099),
  (670000..679999),
]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.in_bin_range?(number, ranges) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
77
78
# File 'lib/active_merchant/billing/credit_card_methods.rb', line 73

def self.in_bin_range?(number, ranges)
  bin = number.to_i
  ranges.any? do |range|
    range.cover?(bin)
  end
end

.included(base) ⇒ Object



69
70
71
# File 'lib/active_merchant/billing/credit_card_methods.rb', line 69

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#card_verification_value_length(brand) ⇒ Object



113
114
115
116
117
118
119
120
121
122
# File 'lib/active_merchant/billing/credit_card_methods.rb', line 113

def card_verification_value_length(brand)
  case brand
  when 'american_express'
    4
  when 'maestro'
    0
  else
    3
  end
end

#credit_card?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/active_merchant/billing/credit_card_methods.rb', line 84

def credit_card?
  true
end

#electron?Boolean

Returns if the card matches known Electron BINs

Returns:

  • (Boolean)


129
130
131
# File 'lib/active_merchant/billing/credit_card_methods.rb', line 129

def electron?
  self.class.electron?(number)
end

#valid_card_verification_value?(cvv, brand) ⇒ Boolean

Credit card providers have 3 digit verification values This isn’t standardised, these are called various names such as CVC, CVV, CID, CSC and more See: en.wikipedia.org/wiki/Card_security_code American Express is the exception with 4 digits

Below are links from the card providers with their requirements visa: usa.visa.com/personal/security/3-digit-security-code.jsp master: www.mastercard.com/ca/merchant/en/getstarted/Anatomy_MasterCard.html jcb: www.jcbcard.com/security/info.html diners_club: www.dinersclub.com/assets/DinersClub_card_ID_features.pdf discover: www.discover.com/credit-cards/help-center/glossary.html american_express: online.americanexpress.com/myca/fuidfyp/us/action?request_type=un_fuid&Face=en_US

Returns:

  • (Boolean)


109
110
111
# File 'lib/active_merchant/billing/credit_card_methods.rb', line 109

def valid_card_verification_value?(cvv, brand)
  cvv.to_s =~ /^\d{#{card_verification_value_length(brand)}}$/
end

#valid_expiry_year?(year) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/active_merchant/billing/credit_card_methods.rb', line 88

def valid_expiry_year?(year)
  (Time.now.year..Time.now.year + 20).include?(year.to_i)
end

#valid_issue_number?(number) ⇒ Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/active_merchant/billing/credit_card_methods.rb', line 124

def valid_issue_number?(number)
  (number.to_s =~ /^\d{1,2}$/)
end

#valid_month?(month) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/active_merchant/billing/credit_card_methods.rb', line 80

def valid_month?(month)
  (1..12).cover?(month.to_i)
end

#valid_start_year?(year) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/active_merchant/billing/credit_card_methods.rb', line 92

def valid_start_year?(year)
  ((year.to_s =~ /^\d{4}$/) && (year.to_i > 1987))
end