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.



20
21
22
# File 'app/models/spree/payment.rb', line 20

def source_attributes
  @source_attributes
end

Instance Method Details

#actionsObject



96
97
98
99
# File 'app/models/spree/payment.rb', line 96

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



89
90
91
92
93
94
# File 'app/models/spree/payment.rb', line 89

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)


84
85
86
# File 'app/models/spree/payment.rb', line 84

def can_credit?
  credit_allowed > 0
end

#credit!(credit_amount = nil) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/models/spree/payment/processing.rb', line 81

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



80
81
82
# File 'app/models/spree/payment.rb', line 80

def credit_allowed
  amount - offsets_total
end

#currencyObject



67
68
69
# File 'app/models/spree/payment.rb', line 67

def currency
  order.currency
end

#gateway_optionsObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'app/models/spree/payment/processing.rb', line 115

def gateway_options
  options = { :email    => order.email,
              :customer => order.email,
              :ip       => order.last_ip_address,
              # Need to pass in a unique identifier here to make some
              # payment gateways happy.
              #
              # For more information, please see Spree::Payment#set_unique_identifier
              :order_id => gateway_order_id }

  options.merge!({ :shipping => order.ship_total * 100,
                   :tax      => order.tax_total * 100,
                   :subtotal => order.item_total * 100,
                   :discount => order.promo_total * 100,
                   :currency => currency })

  options.merge!({ :billing_address  => order.bill_address.try(:active_merchant_hash),
                  :shipping_address => order.ship_address.try(:active_merchant_hash) })

  options
end

#moneyObject Also known as: display_amount



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

def money
  Spree::Money.new(amount, { currency: currency })
end

#offsets_totalObject



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

def offsets_total
  offsets.pluck(:amount).sum
end

#partial_credit(amount) ⇒ Object



109
110
111
112
113
# File 'app/models/spree/payment/processing.rb', line 109

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

#payment_sourceObject



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

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

#persist_invalidObject



34
35
36
37
38
# File 'app/models/spree/payment.rb', line 34

def persist_invalid
  return unless ['failed', 'invalid'].include?(state)
  state_will_change!
  save 
end