Sha256: 8dd0bcc406ac1e72f5d580582e40379257f6ab08b3d4f8a4a79fc3328f00fd6b

Contents?: true

Size: 1.71 KB

Versions: 117

Compression:

Stored size: 1.71 KB

Contents

module ForestLiana
  class StripePaymentsGetter < StripeBaseGetter
    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
      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['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
      rescue ::Stripe::InvalidRequestError => error
        FOREST_REPORTER.report error
        FOREST_LOGGER.error "Stripe error: #{error.message}"
        @records = []
      end
    end

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

117 entries across 117 versions & 1 rubygems

Version Path
forest_liana-9.11.1 app/services/forest_liana/stripe_payments_getter.rb
forest_liana-9.11.0 app/services/forest_liana/stripe_payments_getter.rb
forest_liana-9.10.6 app/services/forest_liana/stripe_payments_getter.rb
forest_liana-9.10.5 app/services/forest_liana/stripe_payments_getter.rb
forest_liana-9.10.4 app/services/forest_liana/stripe_payments_getter.rb
forest_liana-9.10.3 app/services/forest_liana/stripe_payments_getter.rb
forest_liana-9.10.2 app/services/forest_liana/stripe_payments_getter.rb
forest_liana-9.10.1 app/services/forest_liana/stripe_payments_getter.rb
forest_liana-9.10.0 app/services/forest_liana/stripe_payments_getter.rb
forest_liana-9.9.1 app/services/forest_liana/stripe_payments_getter.rb
forest_liana-9.9.0 app/services/forest_liana/stripe_payments_getter.rb
forest_liana-9.8.0 app/services/forest_liana/stripe_payments_getter.rb
forest_liana-9.7.0 app/services/forest_liana/stripe_payments_getter.rb
forest_liana-9.6.4 app/services/forest_liana/stripe_payments_getter.rb
forest_liana-9.6.3 app/services/forest_liana/stripe_payments_getter.rb
forest_liana-9.6.0 app/services/forest_liana/stripe_payments_getter.rb
forest_liana-9.5.7 app/services/forest_liana/stripe_payments_getter.rb
forest_liana-9.5.6 app/services/forest_liana/stripe_payments_getter.rb
forest_liana-9.5.5 app/services/forest_liana/stripe_payments_getter.rb
forest_liana-9.5.4 app/services/forest_liana/stripe_payments_getter.rb