Skip to content

Commit

Permalink
Added Payment model
Browse files Browse the repository at this point in the history
  • Loading branch information
anathematic committed Mar 24, 2010
1 parent 5bb8315 commit 1189120
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/controllers/orders_controller.rb
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/models/order.rb
Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions 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
3 changes: 2 additions & 1 deletion app/views/admin/orders/show.html.erb
@@ -1,4 +1,5 @@
<h1>Showing Order</h1>

<% if @order.paid? %><p>This order is currently pending delivery, <%= button_to "mark as delivered", admin_order_path(@order, :order => { :status => "paid" }), :method => :put %></p><% end %>
<% if @order.paid? %>
<p>This order is currently pending delivery, <%= button_to "mark as delivered", admin_order_path(@order), :method => :put %></p><% end %>
<% if @order.sent? %><p>This order has been delivered</p><% end %>
2 changes: 1 addition & 1 deletion app/views/orders/pay.html.erb
Expand Up @@ -12,6 +12,6 @@
<%= f.input :number, :label => "Credit Card Number" %>
<%= f.input :month %>
<%= f.input :year %>
<%= f.input %>
<%= f.input :verification_value %>
<p><%= f.input "Confirm", :disable_with => "Processing" %> or <%= link_to "Back to Order", order_path(@order) %></p>
<% end %>
1 change: 1 addition & 0 deletions config/environment.rb
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions 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
10 changes: 9 additions & 1 deletion db/schema.rb
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
9 changes: 9 additions & 0 deletions 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
28 changes: 28 additions & 0 deletions 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

0 comments on commit 1189120

Please sign in to comment.