class Paya::ProductService < Paya::ApplicationService
  def call
    response = RestClient.get(endpoint, headers)
    JSON.parse(response)
  rescue RestClient::Exception => e
    JSON.parse(e.response.body)
  end

  def find_product(method, type)
    products = call['locationinfo']['product_transactions']

    products.each do |product|
      next unless method == product['payment_method'].to_sym
      return product if method == :cc
      return product if "#{type.capitalize} Initiated".include?(product['title'])
    end

    {}
  end

  private
    def endpoint
      "#{base_uri}/locationinfos/#{location_id}"
    end
end
