Class: Spree::Payment

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Processing
Defined in:
app/models/spree/payment.rb,
app/models/spree/payment/processing.rb

Defined Under Namespace

Modules: Processing

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Processing

#authorize!, #capture!, #process!, #purchase!, #void_transaction!

Instance Attribute Details

#source_attributesObject

Returns the value of attribute source_attributes.



16
17
18
# File 'app/models/spree/payment.rb', line 16

def source_attributes
  @source_attributes
end

Instance Method Details

#actionsObject



70
71
72
73
# File 'app/models/spree/payment.rb', line 70

def actions
  return [] unless payment_source and payment_source.respond_to? :actions
  payment_source.actions.select { |action| !payment_source.respond_to?("can_#{action}?") or payment_source.send("can_#{action}?", self) }
end

#build_sourceObject



63
64
65
66
67
68
# File 'app/models/spree/payment.rb', line 63

def build_source
  return if source_attributes.nil?
  if payment_method and payment_method.payment_source_class
    self.source = payment_method.payment_source_class.new(source_attributes)
  end
end

#can_credit?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'app/models/spree/payment.rb', line 58

def can_credit?
  credit_allowed > 0
end

#credit!(credit_amount = nil) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/models/spree/payment/processing.rb', line 73

def credit!(credit_amount=nil)
  protect_from_connection_error do
    check_environment

    credit_amount ||= credit_allowed >= order.outstanding_balance.abs ? order.outstanding_balance.abs : credit_allowed.abs
    credit_amount = credit_amount.to_f

    if payment_method.payment_profiles_supported?
      response = payment_method.credit((credit_amount * 100).round, source, response_code, gateway_options)
    else
      response = payment_method.credit((credit_amount * 100).round, response_code, gateway_options)
    end

    record_response(response)

    if response.success?
      self.class.create({ :order => order,
                          :source => self,
                          :payment_method => payment_method,
                          :amount => credit_amount.abs * -1,
                          :response_code => response.authorization,
                          :state => 'completed' }, :without_protection => true)
    else
      gateway_error(response)
    end
  end
end

#credit_allowedObject



54
55
56
# File 'app/models/spree/payment.rb', line 54

def credit_allowed
  amount - offsets_total
end

#gateway_error(error) ⇒ Object

This needs to be public so that gateway subclasses can use this to report errors

Raises:



109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/models/spree/payment/processing.rb', line 109

def gateway_error(error)
  if error.is_a? ActiveMerchant::Billing::Response
    text = error.params['message'] || error.params['response_reason_text'] || error.message
  elsif error.is_a? ActiveMerchant::ConnectionError
    text = I18n.t(:unable_to_connect_to_gateway)
  else
    text = error.to_s
  end
  logger.error(I18n.t(:gateway_error))
  logger.error("  #{error.to_yaml}")
  raise Core::GatewayError.new(text)
end

#offsets_totalObject



50
51
52
# File 'app/models/spree/payment.rb', line 50

def offsets_total
  offsets.map(&:amount).sum
end

#partial_credit(amount) ⇒ Object



101
102
103
104
105
# File 'app/models/spree/payment/processing.rb', line 101

def partial_credit(amount)
  return if amount > credit_allowed
  started_processing!
  credit!(amount)
end

#payment_sourceObject



75
76
77
78
# File 'app/models/spree/payment.rb', line 75

def payment_source
  res = source.is_a?(Payment) ? source.source : source
  res || payment_method
end