Skip to content

Instantly share code, notes, and snippets.

@safarista
Created October 10, 2011 20:54
Show Gist options
  • Save safarista/1276507 to your computer and use it in GitHub Desktop.
Save safarista/1276507 to your computer and use it in GitHub Desktop.
am trying to do the buying, and sending email to user and notifications to admin
class Order < ActiveRecord::Base
has_many :line_items, :dependent => :destroy
belongs_to :user
attr_accessor :card_number, :card_type, :card_verification, :card_expires_on, :first_name, :last_name, :name, :address1,:city, :county, :country, :post_code
#VALIDATIONS
validate :validate_card, :on => :create
validate :card_verification, :presence => true
#SCOPES
scope :in_progress, where("orders.checked_out_at IS NULL")
scope :complete, where("orders.checked_out_at IS NOT NULL")
COMPLETE = "complete"
IN_PROGRESS = "in_progress"
def self.find_with_recipe(recipe)
return [] unless recipe
complete.includes(:line_items).
where(["line_items.recipe_id = ?", recipe.id]).
order("orders.checked_out_at DESC")
end
# def gateway
# ActiveMerchant::Billing::PaypalGateway.new(
# :login => "xxxxxxxxxxxxxxxxxxxxx",
# :password => "xxxxxxxxxxxxxxxxxxxxxxxx",
# :signature => "xxxxxxxxxxxxxxxxxxxxx"
# )
# end
def purchase
gateway.authorize(price_in_cents, credit_card, buyer_options)
end
def price_in_cents
(self.total_price * 100).round
end
def buyer_options
{
:ip => ip_address,
:billing_address => {
:name => name,
:address1 => address1,
:city => city,
:state => county,
:country => country,
:post_code => post_code
}
}
end
def checkout!
self.checked_out_at = Time.now
self.save
end
def recalculate_price!
self.total_price = line_items.inject(0.0) do |sum, line_item|
sum += (line_item.price || 0)
end
save!
end
def state
checked_out_at.nil? ? IN_PROGRESS : COMPLETE
end
def display_name
ActionController::Base.helpers.number_to_currency(total_price) +
" - Order ##{id} (#{user.username})"
end
private
def validate_card
unless credit_card.valid?
credit_card.errors.full_messages.each do |message|
credit_card.errors[:base] << message
end
end
end
def credit_card
@credit_card ||= ActiveMerchant::Billing::CreditCard.new(
:type => card_type,
:number => card_number,
:verification_value => card_verification,
:month => card_expires_on.try(:month),
:year => card_expires_on.try(:year),
:first_name => first_name,
:last_name => last_name
)
end
end
class OrdersController < ApplicationController
before_filter :find_cart
def show
@order = current_user.orders.all("LIMIT 10")
end
def new
@order = Order.new
end
def create
@order = current_user.orders.build(params[:order])
@order.ip_address = request.remote_ip
if @order.save
if @order.purchase
session[:cart_id] = nil
# TODO: Set Order.state = "complete"
# TODO: Email User the "receipt" and notify Admin of a new sale
# TODO: Create a downloadable Receipt link in completed recipes
render :action => "success"
else
render :action => "failure"
end
else
render :action => "new"
end
end
def success
end
def failure
end
private
def find_cart
@cart = session[:cart_id] ? Order.find(session[:cart_id]) : current_user.orders.build
end
end
# Am thinking of scraping the methods in order.rb to use this
class Payment < ActiveRecord::Base
 belongs_to :order
 validates_presence_of :order, :amount, :card_info
 validates_presence_of :credit_card, :on => :create
 validate :validate_credit_card
 validate :validate_transaction
 attr_accessor   :credit_card, :customer_info, :payment_result
 attr_accessible :amount, :credit_card, :customer_info
 before_validation :store_card_info
 before_validation :process_transaction
 def success?
   persisted?
 end
 
 private
 def store_card_info
   self.card_info = [:display_number, :type, :month, :year, :first_name, :last_name].map do |key|
     "#{key}: #{credit_card.try(key)}"
   end.join("\n")
 end
 
 def process_transaction
   if amount && credit_card && customer_info
     # store our result
     self.payment_result = gateway.purchase(amount, credit_card, customer_info)
   end
 end
 
 def gateway
   # your gateway code here e.g:
   ActiveMerchant::Billing::PaypalGateway.new(
     :login => "xxxxxxxxxxx",
     :password => "xxxxxxxxxx",
     :signature => "xxxxxxxxxxxxx"
   )
 end
 
 def validate_credit_card
   unless credit_card && credit_card.valid?
     self.errors.add(:credit_card, credit_card.errors.full_messages.join(", "))
   end
 end
 
 def validate_transaction
   if self.payment_result
     unless self.payment_result.success?
       self.errors.add(:base, "Payment failed: #{self.payment_result.message}")
     end
   end
 end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment