Sha256: ae4a40cd0ef9375f660d6602e7b59a6836ee0e87fbe487906b08e97b8f72f28f

Contents?: true

Size: 1.81 KB

Versions: 83

Compression:

Stored size: 1.81 KB

Contents

module ForestLiana
  class StripeInvoicesGetter < StripeBaseGetter
    attr_accessor :records

    def initialize(params, secret_key, reference)
      @params = params
      Stripe.api_key = ForestLiana.integrations[:stripe][:api_key]
    end

    def count
      @invoices.try(:total_count) || 0
    end

    def perform
      begin
        query = {
          limit: limit,
          starting_after: starting_after,
          ending_before: ending_before
        }

        if @params[:id] && collection && field
          resource = collection.find(@params[:id])
          query[:customer] = resource[field]
        end

        query['include[]'] = 'total_count'
        @invoices = fetch_invoices(query)
        if @invoices.blank?
          @records = []
          return
        end

        @records = @invoices.data.map do |d|
          d.date = Time.at(d.date).to_datetime
          d.period_start = Time.at(d.period_start).to_datetime
          d.period_end = Time.at(d.period_end).to_datetime
          d.subtotal /= 100.00
          d.total /= 100.00
          d.amount_due /= 100.00

          query = {}
          query[field] = d.customer
          if collection
            d.customer = collection.find_by(query)
          else
            d.customer = nil
          end

          d
        end
      rescue ::Stripe::InvalidRequestError => error
        FOREST_LOGGER.error "Stripe error: #{error.message}"
        @records = []
      end
    end

    def fetch_invoices(params)
      return if @params[:id] && params[:customer].blank?
      ::Stripe::Invoice.all(params)
    end

    def starting_after
      if pagination? && @params[:starting_after]
        @params[:starting_after]
      end
    end

    def ending_before
      if pagination? && @params[:ending_before]
        @params[:ending_before]
      end
    end
  end
end

Version data entries

83 entries across 83 versions & 1 rubygems

Version Path
forest_liana-4.1.2 app/services/forest_liana/stripe_invoices_getter.rb
forest_liana-4.1.1 app/services/forest_liana/stripe_invoices_getter.rb
forest_liana-4.1.0 app/services/forest_liana/stripe_invoices_getter.rb
forest_liana-4.0.2 app/services/forest_liana/stripe_invoices_getter.rb
forest_liana-4.0.1 app/services/forest_liana/stripe_invoices_getter.rb
forest_liana-4.0.0 app/services/forest_liana/stripe_invoices_getter.rb
forest_liana-4.0.0.pre.beta.1 app/services/forest_liana/stripe_invoices_getter.rb
forest_liana-3.3.0 app/services/forest_liana/stripe_invoices_getter.rb
forest_liana-4.0.0.pre.beta.0 app/services/forest_liana/stripe_invoices_getter.rb
forest_liana-3.2.0 app/services/forest_liana/stripe_invoices_getter.rb
forest_liana-3.1.1 app/services/forest_liana/stripe_invoices_getter.rb
forest_liana-3.1.0 app/services/forest_liana/stripe_invoices_getter.rb
forest_liana-3.0.7 app/services/forest_liana/stripe_invoices_getter.rb
forest_liana-3.0.6 app/services/forest_liana/stripe_invoices_getter.rb
forest_liana-3.0.5 app/services/forest_liana/stripe_invoices_getter.rb
forest_liana-3.0.4 app/services/forest_liana/stripe_invoices_getter.rb
forest_liana-3.0.3 app/services/forest_liana/stripe_invoices_getter.rb
forest_liana-3.0.2 app/services/forest_liana/stripe_invoices_getter.rb
forest_liana-3.0.1 app/services/forest_liana/stripe_invoices_getter.rb
forest_liana-3.0.0 app/services/forest_liana/stripe_invoices_getter.rb