diff --git a/app/controllers/orders_controller.rb b/app/controllers/orders_controller.rb index fc8248e..16e883a 100644 --- a/app/controllers/orders_controller.rb +++ b/app/controllers/orders_controller.rb @@ -47,7 +47,7 @@ def order def check_if_paid redirect_to order_path(@order) unless @order.pending_payment? - @payment = @order.payment.new(:name => @order.name) + @payment = @order.build_payment(:name => @order.name) end def load_cart_into_line_items diff --git a/app/models/order.rb b/app/models/order.rb index ed42c2f..9276333 100644 --- a/app/models/order.rb +++ b/app/models/order.rb @@ -6,6 +6,7 @@ class Order < ActiveRecord::Base belongs_to :shipping, :class_name => "Address" belongs_to :billing, :class_name => "Address" + has_one :payment has_many :line_items validates_presence_of :user, :shipping, :billing diff --git a/app/models/payment.rb b/app/models/payment.rb new file mode 100644 index 0000000..52dd6b4 --- /dev/null +++ b/app/models/payment.rb @@ -0,0 +1,34 @@ +class Payment < ActiveRecord::Base + + belongs_to :order + + attr_accessor :number, :month, :year, :verification_value, :name + + validate :credit_card + + def first_name + order.name.split(" ")[0] + end + + def last_name + order.name.split(" ")[1] + end + + def credit_card + credit_card = ActiveMerchant::Billing::CreditCard.new( + :number => number, + :month => month, + :year => year, + :first_name => first_name, + :last_name => last_name, + :verification_value => verification_value + ) + + unless credit_card.valid? + credit_card.errors.full_messages.each do |message| + errors.add_to_base message + end + end + end + +end diff --git a/app/views/admin/orders/show.html.erb b/app/views/admin/orders/show.html.erb index 09c9d29..81f8486 100644 --- a/app/views/admin/orders/show.html.erb +++ b/app/views/admin/orders/show.html.erb @@ -1,4 +1,5 @@

Showing Order

-<% if @order.paid? %>

This order is currently pending delivery, <%= button_to "mark as delivered", admin_order_path(@order, :order => { :status => "paid" }), :method => :put %>

<% end %> +<% if @order.paid? %> +

This order is currently pending delivery, <%= button_to "mark as delivered", admin_order_path(@order), :method => :put %>

<% end %> <% if @order.sent? %>

This order has been delivered

<% end %> \ No newline at end of file diff --git a/app/views/orders/pay.html.erb b/app/views/orders/pay.html.erb index 8da0a4c..f59d1fb 100644 --- a/app/views/orders/pay.html.erb +++ b/app/views/orders/pay.html.erb @@ -12,6 +12,6 @@ <%= f.input :number, :label => "Credit Card Number" %> <%= f.input :month %> <%= f.input :year %> - <%= f.input %> + <%= f.input :verification_value %>

<%= f.input "Confirm", :disable_with => "Processing" %> or <%= link_to "Back to Order", order_path(@order) %>

<% end %> \ No newline at end of file diff --git a/config/environment.rb b/config/environment.rb index 6291722..d6d16bc 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -19,6 +19,7 @@ # config.gem "hpricot", :version => '0.6', :source => "http://code.whytheluckystiff.net" # config.gem "sqlite3-ruby", :lib => "sqlite3" # config.gem "aws-s3", :lib => "aws/s3" + config.gem "activemerchant", :lib => "active_merchant" config.gem "formtastic" config.gem "authlogic" config.gem "rubyist-aasm", :source => "http://gemcutter.org", :lib => 'aasm' diff --git a/config/routes.rb b/config/routes.rb index ca5ab5c..ea5fbc0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -31,7 +31,7 @@ # end map.admin '/admin', :controller => "admin", :action => "index" - # map.resources :admin + map.namespace :admin do |admin_index| admin_index.resources :orders admin_index.resources :products diff --git a/db/migrate/20100208012410_create_payments.rb b/db/migrate/20100208012410_create_payments.rb new file mode 100644 index 0000000..abd003b --- /dev/null +++ b/db/migrate/20100208012410_create_payments.rb @@ -0,0 +1,14 @@ +class CreatePayments < ActiveRecord::Migration + def self.up + create_table :payments do |t| + t.text :return_params + t.boolean :success + t.integer :order_id + t.timestamps + end + end + + def self.down + drop_table :payments + end +end diff --git a/db/schema.rb b/db/schema.rb index 15d6142..851cead 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -9,7 +9,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20100207092114) do +ActiveRecord::Schema.define(:version => 20100208012410) do create_table "addresses", :force => true do |t| t.string "name" @@ -42,6 +42,14 @@ t.datetime "updated_at" end + create_table "payments", :force => true do |t| + t.text "return_params" + t.boolean "success" + t.integer "order_id" + t.datetime "created_at" + t.datetime "updated_at" + end + create_table "photos", :force => true do |t| t.string "name" t.string "photo_file_name" diff --git a/spec/fixtures/payments.yml b/spec/fixtures/payments.yml new file mode 100644 index 0000000..a3566b8 --- /dev/null +++ b/spec/fixtures/payments.yml @@ -0,0 +1,9 @@ +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html + +one: + return_params: MyText + success: false + +two: + return_params: MyText + success: false diff --git a/spec/models/payment_spec.rb b/spec/models/payment_spec.rb new file mode 100644 index 0000000..c0a046c --- /dev/null +++ b/spec/models/payment_spec.rb @@ -0,0 +1,28 @@ +require 'spec_helper' + +describe Payment do + before(:each) do + @order = Order.make + @valid_attributes = { + :success => false, + :order => @order, + :name => @order.name, + :number => "0000 0000 0000 0000", + :month => "05", + :year => "2011", + :verification_value => "123" + } + @payment = Payment.new(@valid_attributes) + end + + it "should create a new instance given valid attributes" do + Payment.create!(@valid_attributes) + end + + it "should validate that the credit card is validated" do + @payment.number = nil + @payment.save + @payment.should have(1).error + end + +end