Module: Vpago::OrderDecorator

Extended by:
Spree::DisplayMoney
Defined in:
app/models/vpago/order_decorator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'app/models/vpago/order_decorator.rb', line 6

def self.prepended(base)
  base.has_many :payouts, class_name: 'Spree::Payout', through: :payments
  base.has_many :vendor_payment_methods, -> { unscope(:order).distinct },
                class_name: 'Spree::PaymentMethod',
                through: :line_items

  base.state_machine.before_transition from: :cart, do: :ensure_valid_vendor_payment_methods
  base.state_machine.after_transition to: :complete, do: :generate_line_items_total_metadata
end

Instance Method Details

#available_payment_methods(store = nil) ⇒ Object

override



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/models/vpago/order_decorator.rb', line 50

def available_payment_methods(store = nil)
  payment_methods = if respond_to?(:tenant) && tenant.present?
                      tenant_payment_methods
                    elsif vendor_payment_methods.any?
                      vendor_payment_methods
                    else
                      collect_payment_methods(store)
                    end

  @available_payment_methods ||= if required_payway_payout?
                                   payment_methods.select(&:support_payout?)
                                 else
                                   payment_methods
                                 end
end

#ensure_valid_vendor_payment_methodsObject



32
33
34
35
36
37
38
39
# File 'app/models/vpago/order_decorator.rb', line 32

def ensure_valid_vendor_payment_methods
  return if line_items_from_same_vendor?
  return unless vendor_payment_methods.any?

  errors.add(:line_items, 'some_payment_methods_cant_be_used_across_vendors')

  false
end

#generate_line_items_total_metadataObject



74
75
76
# File 'app/models/vpago/order_decorator.rb', line 74

def 
  line_items.each(&:update_total_metadata)
end

#insufficient_processed_payment?Boolean



24
25
26
# File 'app/models/vpago/order_decorator.rb', line 24

def insufficient_processed_payment?
  processed_payment_total < total
end

#line_items_countObject



70
71
72
# File 'app/models/vpago/order_decorator.rb', line 70

def line_items_count
  line_items.size
end

#line_items_from_same_vendor?Boolean



41
42
43
# File 'app/models/vpago/order_decorator.rb', line 41

def line_items_from_same_vendor?
  line_items.joins(:variant).pluck('spree_variants.vendor_id').uniq.size == 1
end

#order_adjustment_totalObject



78
79
80
# File 'app/models/vpago/order_decorator.rb', line 78

def order_adjustment_total
  adjustments.eligible.sum(:amount)
end

#payment_hostObject

override this method if you want flexibility for example, host per payment method or tenant



84
85
86
# File 'app/models/vpago/order_decorator.rb', line 84

def payment_host
  ENV.fetch('DEFAULT_URL_HOST')
end

#process_payments!Object

override



17
18
19
20
21
22
# File 'app/models/vpago/order_decorator.rb', line 17

def process_payments!
  super if insufficient_processed_payment?

  # Prevent from reaching :complete state if payment is insufficient
  errors.add(:base, Spree.t(:insufficient_payment_amount_to_cover_the_total)) if insufficient_processed_payment?
end

#processed_payment_totalObject



28
29
30
# File 'app/models/vpago/order_decorator.rb', line 28

def processed_payment_total
  payment_total + pending_payments.sum(:amount)
end

#required_payway_payout?Boolean



45
46
47
# File 'app/models/vpago/order_decorator.rb', line 45

def required_payway_payout?
  line_items.any?(&:required_payway_payout?) || shipments.any?(&:required_payway_payout?)
end

#tenant_payment_methodsObject



66
67
68
# File 'app/models/vpago/order_decorator.rb', line 66

def tenant_payment_methods
  tenant.tenant_payment_methods
end