class FlexCommerce::PaypalExpress::GenerateSummary
@class GenerateSummary
This class is used while setting up the paypal for FE It deals with line items total, sub total, tax calculations and Also deals with discounted line items and discounts inorder to send to paypal
Attributes
Public Class Methods
# File lib/paypal_express/generate_summary.rb, line 15 def initialize(cart: , use_tax: false) self.cart = cart self.use_tax = use_tax raise "use_tax is not yet supported. FlexCommerce::PaypalExpress::GenerateSummary should support it in the future" if use_tax end
Public Instance Methods
@method call
@returns an object with subtotal, tax, handling, shipping and items keys
# File lib/paypal_express/generate_summary.rb, line 24 def call { subtotal: subtotal, tax: tax, handling: handling, shipping: shipping, items: items } end
Private Instance Methods
@method discounts
@returns [] if there are no discounts on cart @returns Array containing about the total discount amount, if any applied.
# File lib/paypal_express/generate_summary.rb, line 84 def discount_items return [] if cart.total_discount == 0 [ { name: "Total discounts", number: "NA", quantity: 1, amount: convert_amount(BigDecimal(0) - cart.total_discount), description: "Total discounts", tax: 0 } ] end
@method handling
@returns Payment handling charges, which is 0
# File lib/paypal_express/generate_summary.rb, line 60 def handling 0 end
@mthod items
@returns both line items and discounts
# File lib/paypal_express/generate_summary.rb, line 76 def items normal_items + discount_items end
@method normal_items
@returns Object, the normal line items added to cart @note these line items unit prices will be without any discounts
# File lib/paypal_express/generate_summary.rb, line 102 def normal_items @normal_items ||= cart.line_items.map do |li| { name: li.title, number: li.item.sku, quantity: li.unit_quantity, amount: convert_amount(li.unit_price), description: li.title, tax: 0 } end end
@method shipping
@returns 0 if cart is eligible for free shipping @returns cart.shipping_total, if cart is not eligibl for free shipping
# File lib/paypal_express/generate_summary.rb, line 68 def shipping return 0 if cart.free_shipping convert_amount(cart.shipping_total) end
@method subtotal
@returns the sum of line items total. This doesnt include any promotions
# File lib/paypal_express/generate_summary.rb, line 39 def subtotal items.sum {|i| i[:quantity] * (i[:amount])} end
@method tax
@returns the sum of total line items tax
# File lib/paypal_express/generate_summary.rb, line 53 def tax items.sum {|i| i[:tax] * i[:quantity]} end
@method total
@return amount after converting cart total from pence to pounds
# File lib/paypal_express/generate_summary.rb, line 46 def total convert_amount(cart.total) end