visa credit card application
Hack to Learn: Validating Credit Card Numbers With Ruby (Part 2)
never fear
- Validating Credit Card Numbers With Ruby (Part 2)
As promised here is the second article about validating credit cards with Ruby.
This edition wraps a module and class around the code in preparation for future enhancement.
Feel free to change whatever you want. If you do please send me an update and at least give me credit for the original.
To use it just do something like this in your controller:
ifparams[:cc]begincc=Payment::CreditCard.new(params[:cc])cc.valid?@user.update_attribute(:verified_at,Time.now)rescueException=>elogger.debuge.inspectflash[:notice]="Your profile has been updated. However, #{e}."returnendendThis assumes you have a form like this somewhere in your view:
<p><label>Name On Card</label><%= text_field :cc, :name, :class => "text-n" %></p> <p><label>Card Type</label><%= select :cc, :card_type, FundingSource.get_card_types %></p> <p><label>Card Number</label><%= text_field :cc, :number, {:size => 16, :maxlength => 16, :class => "text-n"} %><br /> <label></label><small>(15-16 digits)</small></p> <p><label>Expiration Date</label><%= date_select :cc, :expiration, :start_year => Time.now.year, :end_year => Time.now.year+10, :use_month_numbers => true, :discard_day => true, :include_blank => true, :order => [:month, :year] %></p> <p><label>Security Code</label><%= text_field :cc, :security_code, :size => 4, :class => "text-s" %><br /> <label></label><small>(3 Digit Code on back of credit card)</small></p>Obviously, this code comes with no warranty of any kind and could hurt your application, your data, your home, your feelings, etc. Don’t sue me. Other than that, use it as you see fit. Just please give me some credit.
module Paymentclass CreditCardCARD_TYPE={:master_card=>0,:visa=>1,:american_express=>2,:diners_club=>3,:discover=>4}#################################################################### Construct the object and do minimal validation# Params:# :name - cardholder's name (optional, for future use)# :card_type - type of card (required)# :number - 15-16 digit card number (required)# :security_code - 3-4 digit security code (required, only# needs to exist and be the right length)# 'expiration(1i)' - expiration year (required).# 'expiration(2i)' - expiration month (required).###################################################################def initialize(attributes={})# Cardholders Nameif(@name=attributes[:name]).nil?or@name.empty?raise"Cardholder Name is required"returnend# Card Type@card_type=attributes[:card_type]@card_type=convert_cc_type(@card_type)if@card_type.nil?or!CARD_TYPE.has_value?(@card_type)raise"A valid Card Type is required"returnend# Card Numberif(@number=attributes[:number]).nil?or@number.to_i.nil?or@number.length<15raise"A valid Card Number is required"returnend# Security Codeif((@security_code=attributes[:security_code]).nil?or@security_code.to_i.nil?or(@security_code.length!=3and@card_type!=CARD_TYPE[:american_express])or(@security_code.length!=4and@c