Class: Effective::Order
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Effective::Order
- Defined in:
- app/models/effective/order.rb
Instance Attribute Summary collapse
-
#save_billing_address ⇒ Object
save these addresses to the user if selected.
-
#save_shipping_address ⇒ Object
save these addresses to the user if selected.
-
#shipping_address_same_as_billing ⇒ Object
save these addresses to the user if selected.
Instance Method Summary collapse
- #add(item, quantity = 1) ⇒ Object (also: #add_to_order)
- #billing_name ⇒ Object
- #decline!(payment_details = nil) ⇒ Object
- #declined? ⇒ Boolean
-
#initialize(cart = {}, user = nil) ⇒ Order
constructor
A new instance of Order.
- #num_items ⇒ Object
-
#order_items_attributes=(order_item_attributes) ⇒ Object
This is used for updating Subscription codes.
-
#purchase!(payment_details = nil, opts = {}) ⇒ Object
:validate => false, :email => false.
- #purchased?(provider = nil) ⇒ Boolean
- #save_billing_address? ⇒ Boolean
- #save_shipping_address? ⇒ Boolean
- #shipping_address_same_as_billing? ⇒ Boolean
- #subtotal ⇒ Object
- #tax ⇒ Object
- #total ⇒ Object
- #user=(user) ⇒ Object
Constructor Details
#initialize(cart = {}, user = nil) ⇒ Order
Returns a new instance of Order.
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'app/models/effective/order.rb', line 50 def initialize(cart = {}, user = nil) super() # Call super with no arguments # Set up defaults self.save_billing_address = true self.save_shipping_address = true self.shipping_address_same_as_billing = true add_to_order(cart) if cart.present? self.user = user if user.present? end |
Instance Attribute Details
#save_billing_address ⇒ Object
save these addresses to the user if selected
10 11 12 |
# File 'app/models/effective/order.rb', line 10 def save_billing_address @save_billing_address end |
#save_shipping_address ⇒ Object
save these addresses to the user if selected
10 11 12 |
# File 'app/models/effective/order.rb', line 10 def save_shipping_address @save_shipping_address end |
#shipping_address_same_as_billing ⇒ Object
save these addresses to the user if selected
10 11 12 |
# File 'app/models/effective/order.rb', line 10 def shipping_address_same_as_billing @shipping_address_same_as_billing end |
Instance Method Details
#add(item, quantity = 1) ⇒ Object Also known as: add_to_order
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'app/models/effective/order.rb', line 63 def add(item, quantity = 1) raise 'unable to alter a purchased order' if purchased? raise 'unable to alter a declined order' if declined? if item.kind_of?(Effective::Cart) cart_items = item.cart_items else purchasables = [item].flatten if purchasables.any? { |p| !p.respond_to?(:is_effectively_purchasable?) } raise ArgumentError.new('Effective::Order.add() expects a single acts_as_purchasable item, or an array of acts_as_purchasable items') end cart_items = purchasables.map do |purchasable| CartItem.new(:quantity => quantity).tap { |cart_item| cart_item.purchasable = purchasable } end end retval = cart_items.map do |item| order_items.build( :title => item.title, :quantity => item.quantity, :price => item.price, :tax_exempt => item.tax_exempt, :tax_rate => item.tax_rate, :seller_id => (item.purchasable.try(:seller).try(:id) rescue nil) ).tap { |order_item| order_item.purchasable = item.purchasable } end retval.size == 1 ? retval.first : retval end |
#billing_name ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'app/models/effective/order.rb', line 167 def billing_name if billing_address.try(:full_name).present? billing_address.full_name elsif user.respond_to?(:full_name) user.full_name.to_s elsif user.respond_to?(:first_name) && user.respond_to?(:last_name) user.first_name.to_s + ' ' + user.last_name.to_s elsif user.to_s.start_with?('#<User:') == false user.to_s else '' end end |
#decline!(payment_details = nil) ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'app/models/effective/order.rb', line 229 def decline!(payment_details = nil) raise EffectiveOrders::AlreadyPurchasedException.new('order already purchased') if self.purchased? raise EffectiveOrders::AlreadyDeclinedException.new('order already declined') if self.declined? Order.transaction do self.purchase_state = EffectiveOrders::DECLINED self.payment = payment_details.kind_of?(Hash) ? payment_details : {:details => (payment_details || 'none').to_s} order_items.each { |item| item.purchasable.declined!(self, item) } self.save! end end |
#declined? ⇒ Boolean
264 265 266 |
# File 'app/models/effective/order.rb', line 264 def declined? purchase_state == EffectiveOrders::DECLINED end |
#num_items ⇒ Object
147 148 149 |
# File 'app/models/effective/order.rb', line 147 def num_items order_items.map(&:quantity).sum end |
#order_items_attributes=(order_item_attributes) ⇒ Object
This is used for updating Subscription codes. We want to update the underlying purchasable object of an OrderItem Passing the order_item_attributes using rails default acts_as_nested creates a new object instead of updating the temporary one. So we override this method to do the updates on the non-persisted OrderItem objects Right now strong_paramaters only lets through stripe_coupon_id “stripe_coupon_id”=>“50OFF”, “id”=>“2”}}
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'app/models/effective/order.rb', line 116 def order_items_attributes=(order_item_attributes) if self.persisted? == false (order_item_attributes || {}).each do |_, atts| order_item = self.order_items.find { |oi| oi.purchasable.class.name == atts[:class] && oi.purchasable.id == atts[:id].to_i } if order_item order_item.purchasable.attributes = atts.except(:id, :class) # Recalculate the OrderItem based on the updated purchasable object order_item.title = order_item.purchasable.title order_item.price = order_item.purchasable.price order_item.tax_exempt = order_item.purchasable.tax_exempt order_item.tax_rate = order_item.purchasable.tax_rate order_item.seller_id = (order_item.purchasable.try(:seller).try(:id) rescue nil) end end end end |
#purchase!(payment_details = nil, opts = {}) ⇒ Object
:validate => false, :email => false
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'app/models/effective/order.rb', line 182 def purchase!(payment_details = nil, opts = {}) opts = {:validate => true, :email => true}.merge(opts) raise EffectiveOrders::AlreadyPurchasedException.new('order already purchased') if self.purchased? raise EffectiveOrders::AlreadyDeclinedException.new('order already declined') if (self.declined? && opts[:validate]) Order.transaction do self.purchase_state = EffectiveOrders::PURCHASED self.purchased_at ||= Time.zone.now self.payment = payment_details.kind_of?(Hash) ? payment_details : {:details => (payment_details || 'none').to_s} order_items.each { |item| item.purchasable.purchased!(self, item) } self.save!(:validate => opts[:validate]) if EffectiveOrders.mailer[:send_order_receipt_to_admin] && opts[:email] if Rails.env.production? (OrdersMailer.order_receipt_to_admin(self).deliver rescue false) else OrdersMailer.order_receipt_to_admin(self).deliver end end if EffectiveOrders.mailer[:send_order_receipt_to_buyer] && opts[:email] if Rails.env.production? (OrdersMailer.order_receipt_to_buyer(self).deliver rescue false) else OrdersMailer.order_receipt_to_buyer(self).deliver end end if EffectiveOrders.mailer[:send_order_receipt_to_seller] && self.purchased?(:stripe_connect) && opts[:email] self.order_items.group_by(&:seller).each do |seller, order_items| if Rails.env.production? (OrdersMailer.order_receipt_to_seller(self, seller, order_items).deliver rescue false) else OrdersMailer.order_receipt_to_seller(self, seller, order_items).deliver end end end return true end false end |
#purchased?(provider = nil) ⇒ Boolean
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'app/models/effective/order.rb', line 243 def purchased?(provider = nil) return false if (purchase_state != EffectiveOrders::PURCHASED) return true if provider == nil begin case provider when :stripe_connect payment.keys.first.kind_of?(Numeric) && payment[payment.keys.first].key?('object') && payment[payment.keys.first]['object'] == 'charge' when :stripe payment.key?('object') && payment['object'] == 'charge' when :moneris payment.key?('response_code') && payment.key?('transactionKey') when :paypal else false end rescue => e false end end |
#save_billing_address? ⇒ Boolean
151 152 153 |
# File 'app/models/effective/order.rb', line 151 def save_billing_address? ::ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES.include?(self.save_billing_address) end |
#save_shipping_address? ⇒ Boolean
155 156 157 |
# File 'app/models/effective/order.rb', line 155 def save_shipping_address? ::ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES.include?(self.save_shipping_address) end |
#shipping_address_same_as_billing? ⇒ Boolean
159 160 161 162 163 164 165 |
# File 'app/models/effective/order.rb', line 159 def shipping_address_same_as_billing? if self.shipping_address_same_as_billing.nil? true # Default value else ::ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES.include?(self.shipping_address_same_as_billing) end end |
#subtotal ⇒ Object
139 140 141 |
# File 'app/models/effective/order.rb', line 139 def subtotal order_items.map(&:subtotal).sum end |
#tax ⇒ Object
143 144 145 |
# File 'app/models/effective/order.rb', line 143 def tax [order_items.map(&:tax).sum, 0].max end |
#total ⇒ Object
135 136 137 |
# File 'app/models/effective/order.rb', line 135 def total [order_items.map(&:total).sum, 0].max end |
#user=(user) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'app/models/effective/order.rb', line 96 def user=(user) super if user.respond_to?(:billing_address) && EffectiveOrders.require_billing_address self.billing_address = user.billing_address self.billing_address.full_name = billing_name if self.billing_address.present? && self.billing_address.full_name.blank? end if user.respond_to?(:shipping_address) && EffectiveOrders.require_shipping_address self.shipping_address = user.shipping_address self.shipping_address.full_name = billing_name if self.shipping_address.present? && self.shipping_address.full_name.blank? end end |