lib/workarea/klarna/gateway/order.rb in workarea-klarna-1.0.0.beta1 vs lib/workarea/klarna/gateway/order.rb in workarea-klarna-1.0.0.beta2

- old
+ new

@@ -87,19 +87,28 @@ end def format_item(item) view_model = Storefront::OrderItemViewModel.new(item) breadcrumbs = Navigation::Breadcrumbs.from_global_id(item.via) if item.via.present? + tax_amount, tax_rate = item_tax_data(item) + unit_price = item.current_unit_price.cents + total_amount = item.total_price.cents + if include_tax_in_totals? + tax_amount_per_item = tax_amount / item.quantity + unit_price += tax_amount_per_item + total_amount += tax_amount + end + { name: view_model.product_name, type: item.shipping? ? 'physical' : 'digital', reference: item.sku, quantity: item.quantity, - unit_price: item.current_unit_price.cents, - total_amount: item.total_price.cents, + unit_price: unit_price, + total_amount: total_amount, total_tax_amount: tax_amount, tax_rate: tax_rate, product_url: Workarea::Storefront::Engine.routes.url_helpers.product_url( host: Workarea.config.host, id: view_model.product.slug @@ -130,18 +139,21 @@ .adjusting('tax') .select { |pa| !!pa.data['shipping_service_tax'] } .sum(&:amount) .cents + tax_rate = ((shipping_tax * 1.0 / shipping_total).round(2) * 10000).to_i + shipping_total += shipping_tax if include_tax_in_totals? + { name: 'Shipping', type: 'shipping_fee', quantity: 1, unit_price: shipping_total, total_amount: shipping_total, total_tax_amount: shipping_tax, - tax_rate: ((shipping_tax * 1.0 / shipping_total).round(2) * 10000).to_i + tax_rate: tax_rate } end def discount_line order_discount = price_adjustments.adjusting('order').discounts.sum.cents @@ -223,10 +235,14 @@ .checkout_confirmation_url(host: Workarea.config.host) } end def include_tax_line? - payment.address.country.alpha2 == 'US' + payment.address.country.continent == 'North America' + end + + def include_tax_in_totals? + !include_tax_line? end def remove_inline_tax(hash) hash[:order_lines].each do |line| line.delete(:total_tax_amount)