Sha256: 0f598b8fcd0b0174efa6b2d151921379fdfb3da806e7c0525d94993ca9512cf7

Contents?: true

Size: 1.97 KB

Versions: 22

Compression:

Stored size: 1.97 KB

Contents

module ForestLiana
  class StripePaymentsGetter
    attr_accessor :records

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

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

    def perform
      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['source'] = { object: :card }
      query['include[]'] = 'total_count'

      @charges = fetch_charges(query)
      if @charges.blank?
        @records = []
        return
      end

      @records = @charges.data.map do |d|
        d.created = Time.at(d.created).to_datetime
        d.amount /= 100.00

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

        d
      end
    end

    def fetch_charges(params)
      return if @params[:id] && params[:customer].blank?
      Stripe::Charge.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

    def limit
      return 10 unless pagination?

      if @params[:page][:size]
        @params[:page][:size].to_i
      else
        10
      end
    end

    def pagination?
      @params[:page]
    end

    def collection
      @params[:collection].singularize.camelize.constantize
    end

    def field
      ForestLiana.integrations[:stripe][:mapping].select { |value|
        value.split('.')[0] == ForestLiana::SchemaUtils
          .find_model_from_table_name(@params[:collection]).try(:name)
      }.first.split('.')[1]
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
forest_liana-1.6.5 app/services/forest_liana/stripe_payments_getter.rb
forest_liana-1.6.4 app/services/forest_liana/stripe_payments_getter.rb
forest_liana-1.6.3 app/services/forest_liana/stripe_payments_getter.rb
forest_liana-1.6.2 app/services/forest_liana/stripe_payments_getter.rb
forest_liana-1.6.1 app/services/forest_liana/stripe_payments_getter.rb
forest_liana-1.6.0 app/services/forest_liana/stripe_payments_getter.rb
forest_liana-1.5.26 app/services/forest_liana/stripe_payments_getter.rb
forest_liana-1.5.25 app/services/forest_liana/stripe_payments_getter.rb
forest_liana-1.5.24 app/services/forest_liana/stripe_payments_getter.rb
forest_liana-1.5.23 app/services/forest_liana/stripe_payments_getter.rb
forest_liana-1.5.22 app/services/forest_liana/stripe_payments_getter.rb
forest_liana-1.5.21 app/services/forest_liana/stripe_payments_getter.rb
forest_liana-1.5.20 app/services/forest_liana/stripe_payments_getter.rb
forest_liana-1.5.19 app/services/forest_liana/stripe_payments_getter.rb
forest_liana-1.5.18 app/services/forest_liana/stripe_payments_getter.rb
forest_liana-1.5.17 app/services/forest_liana/stripe_payments_getter.rb
forest_liana-1.5.16 app/services/forest_liana/stripe_payments_getter.rb
forest_liana-1.5.15 app/services/forest_liana/stripe_payments_getter.rb
forest_liana-1.5.14 app/services/forest_liana/stripe_payments_getter.rb
forest_liana-1.5.13 app/services/forest_liana/stripe_payments_getter.rb