Sha256: 137b07319d78809fb1f1c64cca0822c38f0995dcd3e4febe8e3a3b5c7312c32a

Contents?: true

Size: 1.59 KB

Versions: 68

Compression:

Stored size: 1.59 KB

Contents

module ForestLiana
  class StripeSubscriptionsGetter < StripeBaseGetter
    attr_accessor :records

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

    def count
      @subscriptions.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'
        @subscriptions = fetch_subscriptions(query)
        if @subscriptions.blank?
          @records = []
          return
        end

        @records = @subscriptions.data.map do |d|
          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_subscriptions(params)
      return if @params[:id] && params[:customer].blank?
      ::Stripe::Subscription.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

68 entries across 68 versions & 1 rubygems

Version Path
forest_liana-7.3.0 app/services/forest_liana/stripe_subscriptions_getter.rb
forest_liana-6.6.3 app/services/forest_liana/stripe_subscriptions_getter.rb
forest_liana-7.2.2 app/services/forest_liana/stripe_subscriptions_getter.rb
forest_liana-7.2.1 app/services/forest_liana/stripe_subscriptions_getter.rb
forest_liana-7.2.0 app/services/forest_liana/stripe_subscriptions_getter.rb
forest_liana-7.1.0 app/services/forest_liana/stripe_subscriptions_getter.rb
forest_liana-7.0.2 app/services/forest_liana/stripe_subscriptions_getter.rb
forest_liana-7.0.1 app/services/forest_liana/stripe_subscriptions_getter.rb
forest_liana-7.0.0 app/services/forest_liana/stripe_subscriptions_getter.rb
forest_liana-7.0.0.beta.6 app/services/forest_liana/stripe_subscriptions_getter.rb
forest_liana-7.0.0.beta.5 app/services/forest_liana/stripe_subscriptions_getter.rb
forest_liana-7.0.0.beta.4 app/services/forest_liana/stripe_subscriptions_getter.rb
forest_liana-7.0.0.beta.3 app/services/forest_liana/stripe_subscriptions_getter.rb
forest_liana-6.6.2 app/services/forest_liana/stripe_subscriptions_getter.rb
forest_liana-6.6.1 app/services/forest_liana/stripe_subscriptions_getter.rb
forest_liana-6.6.0 app/services/forest_liana/stripe_subscriptions_getter.rb
forest_liana-6.5.1 app/services/forest_liana/stripe_subscriptions_getter.rb
forest_liana-7.0.0.beta.2 app/services/forest_liana/stripe_subscriptions_getter.rb
forest_liana-6.5.0 app/services/forest_liana/stripe_subscriptions_getter.rb
forest_liana-6.4.1 app/services/forest_liana/stripe_subscriptions_getter.rb