No commit activity in last 3 years
No release in over 3 years
Detects issuer from a number, has ActiveModel validations and translations support.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
>= 0

Runtime

 Project Readme

CreditCardSupport

Intro

  • Internationalization and localization support for validations
  • At the moment knows AMEX, DinersClub, Discover, Enroute, Maestro, MasterCard, JCB, Solo, VISA (if you need more, create an issue and request for it :)

Installation

Into your Gemfile add:

gem 'credit_card_support'

Or in shell:

gem install credit_card_support

Basic usage

# for a String object
credit_card_number = "5500005555555559"
credit_card_number.extend(CreditCardSupport::CreditCardNumber)

# or for all the Strings
class String
  include CreditCardSupport::CreditCardNumber
end

# Usage
credit_card_number.luhn?   # true
credit_card_number.issuer  # :visa
credit_card_number.test?   # true

For better understanding read the source and RSpec.

Validations support

Define

ActiveRecord or Mongoid

class CreditCard < ActiveRecord::Base

  attr_accessible :number

  validates :number, presence: true,
                      length: {
                        minimum: 13,
                        maximum: 19
                        },
                      credit_card_number: {
                        allow_testcards: true,
                        allow_issuers: [:visa, :master_card]
                      }

end

Only ActiveModel

Rails 3
class CreditCard
  extend  ActiveModel::Naming
  extend  ActiveModel::Translation
  include ActiveModel::Validations
  include ActiveModel::Conversion

  attr_accessor :number, :expiry_year, :expiry_month, :errors

  def initialize(opts={})
    @errors = ActiveModel::Errors.new(self)
    opts.each {|key, value| send("#{key}=", value) }
  end

  validates :number, presence: true,
                      length: {
                        minimum: 13,
                        maximum: 19
                        },
                      credit_card_number: {
                        allow_testcards: true,
                        allow_issuers: [:visa, :master_card]
                      }

end
Rails 4
class CreditCard
  include ActiveModel::Model

  attr_accessor :number, :expiry_year, :expiry_month

  validates :number, presence: true,
                      length: {
                        minimum: 13,
                        maximum: 19
                        },
                      credit_card_number: {
                        allow_testcards: true,
                        allow_issuers: [:visa, :master_card]
                      }

end

Use

After you have defined your CreditCard

today      = Date.today
creditcard = CreditCard.new(number: '4000111111111115')
creditcard.valid?             # true

# in case of allow_testcards is set to false
creditcard.valid?             # false
creditcard.errors             # <ActiveModel::Errors: ... @messages={:number=>["testcards not supported"]}>

Problems?

Create an issue or submit a patch.

Inspired by

Author

Margus Pärt