Skip to content

Instantly share code, notes, and snippets.

@jefflunt
Created May 15, 2013 18:24
Show Gist options
  • Save jefflunt/5586136 to your computer and use it in GitHub Desktop.
Save jefflunt/5586136 to your computer and use it in GitHub Desktop.
Order model - before
class Order < ActiveRecord::Base
...
after_initialize :init
before_validation :can_be_placed?
before_save :process_transaction, :on => :create
validates :customer_id,
:company_id,
:credit_card_id,
:tip,
:transaction_number,
:delivery_location,
presence: true
scope :latest, order('created_at DESC')
def init
self.transaction_number = 'pending'
# Temporarily setting created_at attribute (when its a new record), otherwise json_to_twisted will fail (nil object)
if self.new_record?
self.created_at = Time.now
end
end
...
def can_be_placed?
return false if order_items.empty?
return terminals_involved_including_items.all? {|t| t.online? }
end
def process_transaction
result = Braintree::Transaction.sale({
amount: format_total,
payment_method_token: credit_card.bt_token,
options: {
submit_for_settlement: true
}
})
Rails.logger.info "Order#process_transaction: Order total is #{total}"
# Inside this if statement, we need to add an error to the :base
# if the result is not success, telling the user that the transaction
# was not able to be saved
if result.success?
Rails.logger.info "Order#process_transaction: Transaction ID: #{result.transaction.id}"
# status will be authorized or submitted_for_settlement
Rails.logger.info "Order#process_transaction: Transaction Status: #{result.transaction.status}"
self.transaction_number = result.transaction.id
send_to_printers
Rails.logger.info "Order#process_transaction: Order#send_to_printers is called"
else
Rails.logger.info "Order#process_transaction: Message: #{result.message}"
if result.transaction.nil?
# validation errors prevented transaction from being created
Rails.logger.info "Order#process_transaction: result.errors: #{result.errors}"
# ADD ERRORS HERE
# self.errors[:base] << 'Unable to process the transaction'
return false
else
Rails.logger.info "Order#process_transaction: Transaction ID: #{result.transaction.id}"
# status will be processor_declined, gateway_rejected, or failed
Rails.logger.info "Order#process_transaction: Transaction Status: #{result.transaction.status}"
processor_response_text = result.transaction.processor_response_text
if result.transaction.status == 'processor_declined'
Rails.logger.info "Order#process_transaction: Processor declined: " + processor_response_text
elsif result.transaction.status == 'gateway_rejected'
Rails.logger.info "Order#process_transaction: Gateway rejected: " + processor_response_text
elsif result.transaction.status == 'failed'
Rails.logger.info "Order#process_transaction: Failed: " + processor_response_text
end
# ADD ERRORS HERE
# self.errors[:base] << processor_response_text
return false
end
end
end
def terminals_involved_including_items
Rails.logger.info 'Figuring out which printers to use to print this order...'
terminals = self.order_items.collect {|oi| oi.menu_item.terminal}.uniq
terminals.each do |t|
t.items = []
self.order_items.each{|oi| t.items << oi if oi.terminal == t}
end
terminals
end
def send_to_printers
Rails.logger.info 'Order#send_to_printers...'
terminals_involved_including_items.each {|t| post_to_twisted(t.id) }
end
def post_to_twisted(terminal_number)
Rails.logger.info 'Posting json to twisted...'
::RestClient.post(TWISTED_DOMAIN_ADDRESS, json_for_twisted(terminal_number))
end
...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment