Class: Levelup::Utils::PaymentCalculator

Inherits:
Object
  • Object
show all
Defined in:
lib/levelup/utils/payment_calculator.rb

Overview

This is a tool to allow integrators to calculate various values to discount and tender

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(discount_applied:, gift_card_credit_available:, spend_amount_returned_from_levelup:, tip_returned_from_levelup:) ⇒ PaymentCalculator

Returns a new instance of PaymentCalculator.

Parameters:

  • discount_applied (Integer)

    The total LevelUp discount applied to the check.

  • gift_card_credit_available (Integer)

    The total available amount of LevelUp gift card credit.

  • spend_amount_returned_from_levelup (Integer)

    The amount of spend returned from the LevelUp API.

  • tip_returned_from_levelup (Integer)

    The tip returned from the LevelUp API.



11
12
13
14
15
16
17
# File 'lib/levelup/utils/payment_calculator.rb', line 11

def initialize(discount_applied:, gift_card_credit_available:,
    spend_amount_returned_from_levelup:, tip_returned_from_levelup:)
  @discount_applied = discount_applied
  @gift_card_credit_available = gift_card_credit_available
  @spend_amount_returned_from_levelup = spend_amount_returned_from_levelup
  @tip_returned_from_levelup = tip_returned_from_levelup
end

Class Method Details

.levelup_discount_to_apply(check_total_due_including_tax:, exempted_item_total:, merchant_funded_credit_available:, payment_amount_requested:, tax_amount_due:) ⇒ Object

Calculates the LevelUp discount to apply based on the arguments passed

Parameters:

  • check_total_due_including_tax (Integer)

    Grand total of check balance in cents

  • exempted_item_total (Integer)

    Combined total of exempted items in cents

  • merchant_funded_credit_available (Integer)

    Total amount of merchant funded credit available in cents

  • payment_amount_requested (Integer)

    Spend amount requested from LevelUp API order endpoint in cents

  • tax_amount_due (Integer)

    Total amount of tax due on the check



27
28
29
30
31
32
33
# File 'lib/levelup/utils/payment_calculator.rb', line 27

def self.levelup_discount_to_apply(check_total_due_including_tax:, exempted_item_total:,
    merchant_funded_credit_available:, payment_amount_requested:, tax_amount_due:)
  total_due_without_tax = check_total_due_including_tax - tax_amount_due
  max_discount = [payment_amount_requested, total_due_without_tax].min
  max_discount_less_exemptions = [0,  max_discount - exempted_item_total].max
  [merchant_funded_credit_available, max_discount_less_exemptions].min
end

Instance Method Details

#gift_card_payment_to_applyObject

Cent amount of LevelUp gift card payment to apply NOT including tip



36
37
38
# File 'lib/levelup/utils/payment_calculator.rb', line 36

def gift_card_payment_to_apply
  [spend_amount_after_discount, @gift_card_credit_available].min
end

#gift_card_remaining_balance_after_paymentObject

Remaining balance in cents on LevelUp gift card after gift card payment has been applied



41
42
43
# File 'lib/levelup/utils/payment_calculator.rb', line 41

def gift_card_remaining_balance_after_payment
  [0, @gift_card_credit_available - total_gift_card_payment_to_apply_including_tip].max
end

#gift_card_tip_to_applyObject

Cent amount of LevelUp gift card tip to apply NOT including LevelUp gift card payment



46
47
48
# File 'lib/levelup/utils/payment_calculator.rb', line 46

def gift_card_tip_to_apply
  total_gift_card_payment_to_apply_including_tip - gift_card_payment_to_apply
end

#levelup_payment_to_applyObject

Cent amount of LevelUp payment to apply NOT including the tip amount



51
52
53
# File 'lib/levelup/utils/payment_calculator.rb', line 51

def levelup_payment_to_apply
  [0, spend_amount_after_discount - @gift_card_credit_available].max
end

#levelup_tip_to_applyObject

Cent amount of LevelUp tip to apply NOT including payment amount



56
57
58
# File 'lib/levelup/utils/payment_calculator.rb', line 56

def levelup_tip_to_apply
  total_levelup_payment_to_apply_including_tip - levelup_payment_to_apply
end

#total_levelup_payment_to_apply_including_tipObject

Total amount in cents of LevelUp payment to apply including both tip AND payment amounts



61
62
63
# File 'lib/levelup/utils/payment_calculator.rb', line 61

def total_levelup_payment_to_apply_including_tip
  [0, total_amount_after_discount - @gift_card_credit_available].max
end