Guest User

Untitled

a guest
Jul 15th, 2018
465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.64 KB | None | 0 0
  1. class Order < ActiveRecord::Base
  2. include ActiveMerchant::Billing
  3.  
  4.  
  5. def self.validates_presence_of(fields)
  6. if $already_included
  7. raise "I was already included, this is the second time, the first was #{$already_included.inspect}"
  8. end
  9. begin
  10. raise "This is the first time"
  11. rescue Exception => e
  12. $already_included = e.backtrace
  13. end
  14. end
  15.  
  16. has_many :line_items
  17. has_many :line_item_options
  18.  
  19. CARD_TYPES = [
  20. # Displayed stored in db
  21. [ "Visa", "visa" ],
  22. [ "Mastercard", "mastercard" ],
  23. ]
  24.  
  25. CARD_EXPIRATION_MONTHS = [
  26. # Displayed stored in db
  27. [ "January", "1" ],
  28. [ "February", "2" ],
  29. [ "March", "3" ],
  30. [ "April", "4" ],
  31. [ "May", "5" ],
  32. [ "June", "6" ],
  33. [ "July", "7" ],
  34. [ "August", "8" ],
  35. [ "September", "9" ],
  36. [ "October", "10" ],
  37. [ "November", "11" ],
  38. [ "December", "12" ],
  39. ]
  40.  
  41. CARD_EXPIRATION_YEARS = [
  42. # Displayed stored in db
  43. [ Time.now.year, Time.now.year ],
  44. [ 1.year.from_now.year, "#{1.year.from_now.year}" ],
  45. [ 2.years.from_now.year, "#{2.years.from_now.year}" ],
  46. [ 3.years.from_now.year, "#{3.years.from_now.year}" ],
  47. [ 4.years.from_now.year, "#{4.years.from_now.year}" ],
  48. [ 5.years.from_now.year, "#{5.years.from_now.year}" ],
  49. [ 6.years.from_now.year, "#{6.years.from_now.year}" ],
  50. ]
  51.  
  52. validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :if=> Proc.new{|record| record.step == "checkout"}
  53.  
  54. validates_presence_of :billing_addr1, #:billing_address,
  55. :first_name,
  56. :last_name,
  57. :email,
  58. :billing_city,
  59. :billing_state,
  60. :billing_zip,
  61. :customer_ip,
  62. :card_type,
  63. :card_verification_value,
  64. :card_number,
  65. :card_expiration_month,
  66. :card_expiration_year,
  67. :if=> Proc.new{|record| record.step == "checkout"}
  68.  
  69. # cc fields as accessors so we don't store anything bad.
  70. attr_accessor :card_type, :card_expiration_month, :card_expiration_year, :card_number, :card_verification_value, :step
  71.  
  72. def add_line_items_from_cart(cart)
  73. cart.image_items.each do |item|
  74. li = LineItem.from_cart_item(item)
  75. line_items << li
  76. end
  77. end
  78.  
  79.  
  80. # Process the payment
  81. def process
  82. puts "doing it"
  83. self.processed_at = Time.now
  84. begin
  85. process_payment
  86. rescue => e
  87. logger.error("reservation #{self.id} failed with error message #{e} ")
  88. self.error_message = "#{e}"
  89. self.status = 7 #failed
  90. return false
  91. end
  92. save!
  93. end
  94.  
  95. def subtotal_price
  96.  
  97. self.line_items.sum(:total_price)
  98. end
  99.  
  100.  
  101.  
  102. def tax
  103. self.line_items.first.subject.job.tax_rate * self.subtotal_price
  104. end
  105.  
  106. def shipping
  107. self.line_items.first.subject.job.shipping_handling
  108. end
  109.  
  110. def total_price
  111. self.subtotal_price + self.tax + self.shipping
  112. end
  113.  
  114.  
  115. protected #--------------------------------------------------------------------------------------------------------------------------------
  116.  
  117. def process_payment
  118.  
  119. # this forces the system to use the testing server, which is what all dev accounts use.
  120. ActiveMerchant::Billing::Base.mode = :test if RAILS_ENV != "production"
  121.  
  122. # read api key and transaction # from config file
  123. c = YAML::load(File.open("#{RAILS_ROOT}/config/config.yml"))
  124. user = c[RAILS_ENV]['auth_net_user']
  125. pass = c[RAILS_ENV]['auth_net_pass']
  126.  
  127. creditcard = ActiveMerchant::Billing::CreditCard.new(
  128. :first_name => self.first_name,
  129. :last_name => self.last_name,
  130. :number=> self.card_number,
  131. :verification_value =>card_verification_value,
  132. :type => self.card_type,
  133. :month => self.card_expiration_month,
  134. :year => self.card_expiration_year
  135. )
  136.  
  137. if creditcard.valid?
  138. options = {:name => self.first_name,
  139. :email => self.email,
  140. :phone => self.phone_number,
  141. :ip => self.customer_ip,
  142. :card_code => self.card_verification_value,
  143. :order_id => self.id,
  144. :description => "Portrait Package",
  145. :address1 => self.billing_addr1,
  146. :city => self.billing_city,
  147. :state => self.billing_state,
  148. :zip => self.billing_zip,
  149. :country => "US"# }
  150. }
  151.  
  152. gateway = AuthorizeNetGateway.new({:login => user, :password=>pass})
  153. response = gateway.purchase(self.total_cost, creditcard, options)
  154.  
  155. if response.success?
  156. self.status = 1
  157. puts "success"
  158. self.error_message = nil
  159. self.trans_number = response.authorization
  160. else
  161. self.status = 7
  162. self.error_message = response.message
  163. end
  164. else
  165. self.status = 7
  166. self.error_message = response.message
  167. end
  168. end
  169.  
  170.  
  171.  
  172. end
Add Comment
Please, Sign In to add comment