Class: Order

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
AASM
Defined in:
app/models/order.rb

Constant Summary collapse

PAYPAL_CERT_PEM =
File.read("#{Rails.root}/certs/#{Rails.env.to_s}/paypal_cert.pem")
APP_CERT_PEM =
File.read("#{Rails.root}/certs/#{Rails.env.to_s}/app_cert.pem")
APP_KEY_PEM =
File.read("#{Rails.root}/certs/#{Rails.env.to_s}/app_key.pem")
@@per_page =
10

Instance Method Summary collapse

Instance Method Details

#addressObject



32
33
34
35
36
# File 'app/models/order.rb', line 32

def address
  return "Unknown" unless payment_notification
  notification = payment_notification.params
  [notification[:address_street], notification[:address_city], notification[:address_state], notification[:address_zip], notification[:address_country]].join(" ") rescue "Unknown"
end

#empty?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'app/models/order.rb', line 22

def empty?
  order_items.empty?
end

#encrypt_for_paypal(values) ⇒ Object



65
66
67
68
# File 'app/models/order.rb', line 65

def encrypt_for_paypal(values)
  signed = OpenSSL::PKCS7::sign(OpenSSL::X509::Certificate.new(APP_CERT_PEM), OpenSSL::PKey::RSA.new(APP_KEY_PEM, ''), values.map { |k, v| "#{k}=#{v}" }.join("\n"), [], OpenSSL::PKCS7::BINARY)
  OpenSSL::PKCS7::encrypt([OpenSSL::X509::Certificate.new(PAYPAL_CERT_PEM)], signed.to_der, OpenSSL::Cipher::Cipher::new("DES3"), OpenSSL::PKCS7::BINARY).to_s.gsub("\n", "")
end

#nameObject



26
27
28
29
30
# File 'app/models/order.rb', line 26

def name
  return "Unknown" unless payment_notification
  notification = payment_notification.params
  notification[:first_name] + " " + notification[:last_name] rescue "Unknown"
end

#payment_notificationObject



18
19
20
# File 'app/models/order.rb', line 18

def payment_notification
  payment_notifications.last
end

#paypal_url(return_url, notify_url) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/models/order.rb', line 38

def paypal_url(return_url, notify_url)
  values = {
    :business => APP_CONFIG[:paypal_email],
    :cmd => "_cart",
    :upload => 1,
    :return => return_url,
    :invoice => id,
    :notify => notify_url,
    :cert_id => APP_CONFIG[:paypal_cert_id],
    :no_shipping => 2
  }  
  order_items.each_with_index do |order_item, index|
    values.merge!({
      "amount_#{index + 1}" => order_item.product.price,
      "item_name_#{index + 1}" => order_item.product.name,
      "item_number_#{index + 1}" => order_item.product.id,
      "quantity_#{index + 1}" => order_item.quantity
    })  
  end
  logger.info values.inspect
  APP_CONFIG[:paypal_url] + {:encrypted => encrypt_for_paypal(values), :cmd => "_s-xclick"}.to_query
end

#send_paidObject



98
99
100
# File 'app/models/order.rb', line 98

def send_paid
  CartMailer.paid(self).deliver
end

#send_shippedObject



102
103
104
# File 'app/models/order.rb', line 102

def send_shipped
  CartMailer.shipped(self).deliver
end

#timestampObject



94
95
96
# File 'app/models/order.rb', line 94

def timestamp
  update_attribute(:purchased_at, Time.now)
end

#totalObject



14
15
16
# File 'app/models/order.rb', line 14

def total
  order_items.map(&:sub_total).inject(:+)
end