Sha256: 1a6cdbabbf62cb24ebf2600feb07a3cf5262121be03bc7bfd6a1e5e2e986bea6

Contents?: true

Size: 1.82 KB

Versions: 15

Compression:

Stored size: 1.82 KB

Contents

module Spree
  class Gateway < PaymentMethod
    delegate :authorize, :purchase, :capture, :void, :credit, to: :provider

    validates :name, :type, presence: true

    preference :server, :string, default: 'test'
    preference :test_mode, :boolean, default: true

    def payment_source_class
      CreditCard
    end

    # instantiates the selected gateway and configures with the options stored in the database
    def self.current
      super
    end

    def provider
      gateway_options = options
      gateway_options.delete :login if gateway_options.has_key?(:login) and gateway_options[:login].nil?
      if gateway_options[:server]
        ActiveMerchant::Billing::Base.gateway_mode = gateway_options[:server].to_sym
      end
      @provider ||= provider_class.new(gateway_options)
    end

    def options
      preferences.to_hash
    end

    def payment_profiles_supported?
      false
    end

    def method_type
      'gateway'
    end

    def supports?(source)
      return true unless provider_class.respond_to? :supports?
      return false unless source.brand
      provider_class.supports?(source.brand)
    end

    def disable_customer_profile(source)
      if source.is_a? CreditCard
        source.update_column :gateway_customer_profile_id, nil
      else
        raise 'You must implement disable_customer_profile method for this gateway.'
      end
    end

    def sources_by_order(order)
      source_ids = order.payments.where(payment_method_id: self.id).pluck(:source_id).uniq
      payment_source_class.where(id: source_ids).with_payment_profile
    end

    def reusable_sources(order)
      if order.completed?
        sources_by_order order
      else
        if order.user_id
          self.credit_cards.where(user_id: order.user_id).with_payment_profile
        else
          []
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
solidus_core-1.2.3 app/models/spree/gateway.rb
solidus_core-1.1.4 app/models/spree/gateway.rb
solidus_core-1.2.2 app/models/spree/gateway.rb
solidus_core-1.2.1 app/models/spree/gateway.rb
solidus_core-1.1.3 app/models/spree/gateway.rb
solidus_core-1.2.0 app/models/spree/gateway.rb
solidus_core-1.2.0.rc2 app/models/spree/gateway.rb
solidus_core-1.1.2 app/models/spree/gateway.rb
solidus_core-1.2.0.rc1 app/models/spree/gateway.rb
solidus_core-1.2.0.beta1 app/models/spree/gateway.rb
solidus_core-1.1.1 app/models/spree/gateway.rb
solidus_core-1.1.0 app/models/spree/gateway.rb
solidus_core-1.1.0.pre2 app/models/spree/gateway.rb
solidus_core-1.1.0.pre1 app/models/spree/gateway.rb
solidus_core-1.1.0.beta1 app/models/spree/gateway.rb