Module: Klarna::Checkout::Validations::OrderValidations
- Included in:
- Order
- Defined in:
- lib/klarna/checkout/validations/order_validations.rb
Constant Summary collapse
- REQUIRED_ORDER_KEYS =
%i[purchase_country purchase_currency locale order_amount order_tax_amount].freeze
- REQUIRED_ITEM_KEYS =
%i[type name quantity unit_price tax_rate total_amount total_tax_amount].freeze
Instance Method Summary collapse
-
#amount_validation ⇒ Object
1.
- #header_keys_existance ⇒ Object
- #item_keys_existance ⇒ Object
-
#tax_amount_validation ⇒ Object
2.
-
#total_amount_validation ⇒ Object
3.
-
#total_tax_amount_validation ⇒ Object
4.
Instance Method Details
#amount_validation ⇒ Object
-
order_amount == sum of all items total_amounts
30 31 32 33 34 35 36 |
# File 'lib/klarna/checkout/validations/order_validations.rb', line 30 def amount_validation return if @items.map { |i| i[:total_amount].to_i }.sum == @header[:order_amount].to_i raise Klarna::Checkout::Errors::OrderValidationError.new( 'inconsistent_values', 'order_amount_not_matching_total_amounts' ) end |
#header_keys_existance ⇒ Object
10 11 12 13 14 15 |
# File 'lib/klarna/checkout/validations/order_validations.rb', line 10 def header_keys_existance missing_keys = REQUIRED_ORDER_KEYS - @header.keys return true if missing_keys.empty? raise Klarna::Checkout::Errors::OrderValidationError.new(missing_keys, 'missing_order_keys') end |
#item_keys_existance ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/klarna/checkout/validations/order_validations.rb', line 17 def item_keys_existance @items.map(&:keys).each do |item_keys| missing_keys = REQUIRED_ITEM_KEYS - item_keys unless missing_keys.empty? raise Klarna::Checkout::Errors::OrderValidationError.new(missing_keys, 'missing_item_keys') end end true end |
#tax_amount_validation ⇒ Object
-
order_tax_amount == sum of all items total_tax_amounts
39 40 41 42 43 44 45 |
# File 'lib/klarna/checkout/validations/order_validations.rb', line 39 def tax_amount_validation return if @items.map { |i| i[:total_tax_amount].to_i }.sum == @header[:order_tax_amount].to_i raise Klarna::Checkout::Errors::OrderValidationError.new( 'inconsistent_values', 'total_tax_amount_not_matching_total_tax_amounts' ) end |
#total_amount_validation ⇒ Object
- loop through all items
-
-> unit_price * quantity == total_amount
48 49 50 51 52 53 54 55 56 |
# File 'lib/klarna/checkout/validations/order_validations.rb', line 48 def total_amount_validation @items.each do |i| next if i[:quantity].to_i * (i[:unit_price].to_i - i[:total_discount_amount].to_i) == i[:total_amount].to_i raise Klarna::Checkout::Errors::OrderValidationError.new( 'inconsistent_order_line_totals', 'total_line_amount_not_matching_qty_times_unit_price' ) end end |
#total_tax_amount_validation ⇒ Object
- loop through all items
-
-> total_tax_amount / (total_amount - total_tax_amount) == tax_rate * 10_000
59 60 61 62 63 64 65 66 67 |
# File 'lib/klarna/checkout/validations/order_validations.rb', line 59 def total_tax_amount_validation @items.each do |i| next if i[:total_tax_amount].to_i * 100 / (i[:total_amount].to_i - i[:total_tax_amount].to_i) == i[:tax_rate].to_i / 100 raise Klarna::Checkout::Errors::OrderValidationError.new( 'inconsistent_order_line_total_tax', 'line_total_tax_amount_not_matching_tax_rate' ) end end |