ActiveMerchant says invalid credit card for a vali

2019-09-14 23:24发布

问题:

Im running through a weird issue with ActiveMerchant. I'm using activemerchant to validate a credit card number and its been working fine. However, I found that it somehow doesn't seem to validate this card number 3088023605344101, also most of the validation issues arise when I input a card number of the type JCB. Here is what my code looks like

cc = CreditCard.new(
                    :first_name => client_details[:firstname],
                    :last_name  => client_details[:lastname],
                    :month      => client_details[:month],
                    :year       => client_details[:year],
                    :number     => client_details[:cardnum],
                    :verification_value => client_details[:cvv]
                    )

Here is an example from my console which correctly validates the card.

2.1.1 :052 > cc = CreditCard.new(:first_name => 'Steve',:last_name => 'Smith', :month   => '9',:year => '2015',:number => '5201457519355638', :verification_value => '123')
=> #<ActiveMerchant::Billing::CreditCard:0x00000109d3acc0 @first_name="Steve", @last_name="Smith", @month="9", @year="2015", @number="5201457519355638", @verification_value="123"> 
2.1.1 :053 > cc.valid?
=> true 
2.1.1 :054 > cc.brand
=> "master" 

while this seems to be working fine, here is an example which that throws brand error. At first, I don't feed in the brand and leave it to active merchant to find it.

2.1.1 :056 > cc = CreditCard.new(:first_name => 'Steve',:last_name => 'Smith', :month => '9',:year => '2015',:number => '3088023605344101', :verification_value => '123')
=> #<ActiveMerchant::Billing::CreditCard:0x0000010a0d91d8 @first_name="Steve", @last_name="Smith", @month="9", @year="2015", @number="3088023605344101", @verification_value="123"> 
2.1.1 :057 > cc.valid?
=> false 
2.1.1 :058 > cc.errors
=> {"brand"=>["is required"], "number"=>[]}     

So I feed brand

2.1.1 :059 > cc = CreditCard.new(:first_name => 'Steve',:last_name => 'Smith', :month => '9',:year => '2015',:number => '3088023605344101', :verification_value => '123', :brand => 'jcb')
=> #<ActiveMerchant::Billing::CreditCard:0x00000109d886c8 @first_name="Steve", @last_name="Smith", @month="9", @year="2015", @number="3088023605344101", @verification_value="123", @brand="jcb"> 
2.1.1 :060 > cc.valid?
=> false 
2.1.1 :061 > cc.errors
=> {"number"=>[], "brand"=>["does not match the card number"]} 

I have validated the card numbers from different site and they seem to be just fine. The sites I validated the card from are freeformatter and igo

Im not sure what the problem is, but if anyone knows why this is happening then do let me know.

回答1:

The issue I raised on active_merchant github suggested that they use this regex /^35(28|29|[3-8]\d)\d{12}$/ to validate if the card is of type JCB. So I just changed the regex accordingly but the question is why are those sites I mentioned have 30 series cards while the IIN is 35. So I need to clarification on this, a question for which Im going to raise on SO now.

Here is the changed regex

def type
  if @card =~ /^5[1-5][0-9]{14}$/
    return SUPPORTED_CARD_BRANDS[:MASTERCARD]
  elsif @card.match(/^4[0-9]{12}([0-9]{3})?$/)
    return SUPPORTED_CARD_BRANDS[:VISA]
  elsif @card.match(/^3[47][0-9]{13}$/)
    return SUPPORTED_CARD_BRANDS[:AMEX]
  elsif @card =~ /^3(0[0-5]|[68][0-9])[0-9]{11}$/
    return SUPPORTED_CARD_BRANDS[:DINNERS]
  elsif @card =~ /^6011[0-9]{12}$/
    return SUPPORTED_CARD_BRANDS[:DISCOVER]
  elsif @card =~ /^(3[0-9]{4}|2131|1800)[0-9]{11}$/
    return SUPPORTED_CARD_BRANDS[:JCB]
  elsif @card =~ /^(5[06-8]|6)[0-9]{10,17}$/
    return SUPPORTED_CARD_BRANDS[:MAESTRO]
  else
    return nil
  end
end