Sha256: e5ff23256d6f3ca31adf36df0aa9556d3f1fa94fe7468f6776fb49c92c961b9a

Contents?: true

Size: 1.66 KB

Versions: 5

Compression:

Stored size: 1.66 KB

Contents

module MercadoPago
  class OrderPreferencesBuilder
    include ActionView::Helpers::TextHelper
    include ActionView::Helpers::SanitizeHelper
    include Spree::ProductsHelper

    def initialize(order, payment, callback_urls, payer_data = nil)
      @order         = order
      @payment       = payment
      @callback_urls = callback_urls
      @payer_data    = payer_data
    end

    def preferences_hash
      {
        external_reference: @payment.identifier,
        back_urls: @callback_urls,
        payer: @payer_data,
        items: generate_items
      }
    end

    private

    def generate_items
      items = []

      items += generate_items_from_line_items
      items += generate_items_from_adjustments
      items += generate_items_from_shipments

      items
    end

    def generate_items_from_shipments
      @order.shipments.collect do |shipment|
        {
          :title => shipment.shipping_method.name,
          :unit_price => shipment.cost.to_f,
          :quantity => 1,
          :currency_id => 'ARS'
        }
      end
    end

    def generate_items_from_line_items
      @order.line_items.collect do |line_item|
        {
          :title => line_item_description_text(line_item.variant.product.name),
          :unit_price => line_item.price.to_f,
          :quantity => line_item.quantity,
          :currency_id => 'ARS'
        }
      end
    end

    def generate_items_from_adjustments
      @order.adjustments.eligible.collect do |adjustment|
        {
          title: line_item_description_text(adjustment.label),
          unit_price: adjustment.amount.to_f,
          quantity: 1,
          currency_id: "ARS"
        }
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
spree_mercado_pago-0.2.3 app/models/mercado_pago/order_preferences_builder.rb
spree_mercado_pago-0.2.2 app/models/mercado_pago/order_preferences_builder.rb
spree_mercado_pago-0.2.1 app/models/mercado_pago/order_preferences_builder.rb
spree_mercado_pago-0.2.0 app/models/mercado_pago/order_preferences_builder.rb
spree_mercado_pago_payment_method-0.2.0 app/models/mercado_pago/order_preferences_builder.rb