Class: Vpago::PayoutsGenerator

Inherits:
Object
  • Object
show all
Defined in:
app/models/vpago/payouts_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(payment) ⇒ PayoutsGenerator

Returns a new instance of PayoutsGenerator.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/models/vpago/payouts_generator.rb', line 9

def initialize(payment)
  @payment = payment

  @line_items = payment.order.line_items.includes(
    :active_payway_payout_profiles,
    :confirmed_payouts_for_vendor,
    :adjustments
  )

  @shipments = payment.order.shipments.includes(
    :confirmed_payouts_for_vendor,
    :adjustments,
    selected_shipping_rate: :active_payway_payout_profiles
  )

  self.remaining_amount = payment.amount
end

Instance Attribute Details

#line_itemsObject (readonly)

Returns the value of attribute line_items.



5
6
7
# File 'app/models/vpago/payouts_generator.rb', line 5

def line_items
  @line_items
end

#paymentObject (readonly)

Returns the value of attribute payment.



5
6
7
# File 'app/models/vpago/payouts_generator.rb', line 5

def payment
  @payment
end

#remaining_amountObject

Returns the value of attribute remaining_amount.



7
8
9
# File 'app/models/vpago/payouts_generator.rb', line 7

def remaining_amount
  @remaining_amount
end

#shipmentsObject (readonly)

Returns the value of attribute shipments.



5
6
7
# File 'app/models/vpago/payouts_generator.rb', line 5

def shipments
  @shipments
end

Instance Method Details

#callObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/vpago/payouts_generator.rb', line 27

def call
  payouts = vendor_payouts + vendor_shipment_payouts + store_payouts
  payouts = payouts.map do |payout|
    payout.amount = round_up(payout.amount)
    payout
  end

  return [] if payouts.all?(&:default?)
  return [] unless payouts.all? { |payout| validated?(payout) }

  ActiveRecord::Base.transaction do
    payouts.each(&:save!)

    # If any payouts were rounded, the total may increase slightly.
    # Update the payment amount to match the sum of payouts to avoid mismatch errors.
    payment.update(amount: payouts.map(&:amount).sum)

    payouts
  end
end

#round_up(amount) ⇒ Object

ABA does not support amounts with more than 2 decimal places. Therefore, we must round all 3-decimal payouts to 2 decimals beforehand.

Example: Total amount: $2.5

  • Vendor 1: $0.625 → $0.63

  • Vendor 2: $0.625 → $0.63

  • Platform: $1.25

Final amount: 2.51$



148
149
150
# File 'app/models/vpago/payouts_generator.rb', line 148

def round_up(amount)
  (amount * 100).ceil / 100.0
end

#store_payoutsObject

generate payout for all remaining amount to store.



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'app/models/vpago/payouts_generator.rb', line 123

def store_payouts
  return [] if self.remaining_amount <= 0

  [
    Spree::Payout.new(
      default: true,
      state: :created,
      payment: payment,
      payout_profile: Spree::PayoutProfiles::PaywayV2.default,
      amount: self.remaining_amount
    )
  ]
end

#validated?(payout) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
# File 'app/models/vpago/payouts_generator.rb', line 48

def validated?(payout)
  return false if payout.payout_profile.preferred_merchant_id != payment.payment_method.preferred_merchant_id

  true
end

#vendor_payoutsObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/models/vpago/payouts_generator.rb', line 54

def vendor_payouts
  line_items.each_with_object([]) do |line_item, payouts|
    payout_profile = line_item.active_payway_payout_profiles.first
    next unless payout_profile.present?

    total_confirmed_payouts = line_item.confirmed_payouts_for_vendor.sum(:amount)
    next if total_confirmed_payouts >= line_item.pre_commission_amount

    amount_owed_to_vendor = line_item.pre_commission_amount - total_confirmed_payouts
    payout_amount = [amount_owed_to_vendor, remaining_amount].min
    outstanding_amount = [amount_owed_to_vendor - payout_amount, 0].max

    payouts << Spree::Payout.new(
      default: false,
      state: :created,
      payoutable: line_item,
      payment: payment,
      payout_profile: payout_profile,
      amount: payout_amount,
      outstanding_amount: outstanding_amount,
      private_metadata: {
        total_confirmed_payouts: total_confirmed_payouts,
        amount_owed_to_vendor: amount_owed_to_vendor,
        payoutable: line_item.
      }
    )

    self.remaining_amount -= payout_amount
  end
end

#vendor_shipment_payoutsObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/models/vpago/payouts_generator.rb', line 85

def vendor_shipment_payouts
  shipments.each_with_object([]) do |shipment, payouts|
    next unless shipment.selected_shipping_rate.handle_by_vendor?

    payout_profile = shipment.selected_shipping_rate.active_payway_payout_profiles.first
    next unless payout_profile.present?

    total_confirmed_payouts = shipment.confirmed_payouts_for_vendor.sum(:amount)
    next if total_confirmed_payouts >= shipment.cost_with_vendor_adjustment_total

    amount_owed_to_shipping_vendor = shipment.cost_with_vendor_adjustment_total - total_confirmed_payouts
    payout_amount = [amount_owed_to_shipping_vendor, remaining_amount].min
    outstanding_amount = [amount_owed_to_shipping_vendor - payout_amount, 0].max

    payouts << Spree::Payout.new(
      default: false,
      state: :created,
      payoutable: shipment,
      payment: payment,
      payout_profile: payout_profile,
      amount: payout_amount,
      outstanding_amount: outstanding_amount,
      private_metadata: {
        total_confirmed_payouts: total_confirmed_payouts,
        amount_owed_to_vendor: amount_owed_to_shipping_vendor,
        payoutable: {
          cost: shipment.cost,
          vendor_adjustment_total: shipment.vendor_adjustment_total,
          cost_with_vendor_adjustment_total: shipment.cost_with_vendor_adjustment_total
        }
      }
    )

    self.remaining_amount -= payout_amount
  end
end