2011-12-07 22:00:27 +00:00
|
|
|
class Spree::PaypalAccount < ActiveRecord::Base
|
2012-05-01 16:47:04 +00:00
|
|
|
attr_accessible :email, :payer_id, :payer_country, :payer_status
|
2010-02-24 20:43:18 +00:00
|
|
|
has_many :payments, :as => :source
|
|
|
|
|
2010-02-25 16:20:15 +00:00
|
|
|
def actions
|
|
|
|
%w{capture credit}
|
|
|
|
end
|
|
|
|
|
|
|
|
def capture(payment)
|
2010-02-25 11:12:15 +00:00
|
|
|
authorization = find_authorization(payment)
|
2010-02-24 20:43:18 +00:00
|
|
|
|
2011-12-16 23:01:28 +00:00
|
|
|
ppx_response = payment.payment_method.provider.capture(amount_in_cents(payment.amount), authorization.params["transaction_id"], :currency => payment.payment_method.preferred_currency)
|
2010-02-24 20:43:18 +00:00
|
|
|
if ppx_response.success?
|
2010-11-28 14:31:31 +00:00
|
|
|
record_log payment, ppx_response
|
|
|
|
payment.complete
|
2010-02-24 20:43:18 +00:00
|
|
|
else
|
2010-02-25 16:20:15 +00:00
|
|
|
gateway_error(ppx_response.message)
|
2010-02-24 20:43:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2010-02-25 16:20:15 +00:00
|
|
|
def can_capture?(payment)
|
2010-11-28 14:31:31 +00:00
|
|
|
!echeck?(payment) && payment.state == "pending"
|
2010-02-25 16:20:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def credit(payment, amount=nil)
|
|
|
|
authorization = find_capture(payment)
|
|
|
|
|
2010-11-28 14:31:31 +00:00
|
|
|
amount = payment.credit_allowed >= payment.order.outstanding_balance.abs ? payment.order.outstanding_balance : payment.credit_allowed
|
2012-04-13 01:21:55 +00:00
|
|
|
amount=amount.abs if amount
|
2011-01-18 16:11:33 +00:00
|
|
|
|
2012-04-13 01:21:55 +00:00
|
|
|
ppx_response = payment.payment_method.provider.credit(amount.nil? ? amount_in_cents(amount) : amount_in_cents(amount), authorization.params['transaction_id'], :currency => payment.payment_method.preferred_currency)
|
2010-02-25 16:20:15 +00:00
|
|
|
|
2010-11-28 14:31:31 +00:00
|
|
|
if ppx_response.success?
|
|
|
|
record_log payment, ppx_response
|
2010-05-12 16:06:24 +00:00
|
|
|
payment.update_attribute(:amount, payment.amount - amount)
|
2010-11-28 14:31:31 +00:00
|
|
|
payment.complete
|
|
|
|
payment.order.update!
|
2010-02-25 16:20:15 +00:00
|
|
|
else
|
|
|
|
gateway_error(ppx_response.message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_credit?(payment)
|
2010-11-28 14:31:31 +00:00
|
|
|
return false unless payment.state == "completed"
|
|
|
|
return false unless payment.order.payment_state == "credit_owed"
|
|
|
|
payment.credit_allowed > 0
|
2010-02-25 16:20:15 +00:00
|
|
|
!find_capture(payment).nil?
|
|
|
|
end
|
2010-05-12 16:06:24 +00:00
|
|
|
|
2010-03-05 11:59:20 +00:00
|
|
|
# fix for Payment#payment_profiles_supported?
|
|
|
|
def payment_gateway
|
|
|
|
false
|
|
|
|
end
|
2010-02-25 16:20:15 +00:00
|
|
|
|
2010-12-23 13:46:57 +00:00
|
|
|
def echeck?(payment)
|
2011-01-18 16:11:33 +00:00
|
|
|
logs = payment.log_entries.all(:order => 'created_at DESC')
|
|
|
|
logs.each do |log|
|
|
|
|
details = YAML.load(log.details) # return the transaction details
|
|
|
|
if details.params['payment_type'] == 'echeck'
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2010-11-28 14:31:31 +00:00
|
|
|
def record_log(payment, response)
|
|
|
|
payment.log_entries.create(:details => response.to_yaml)
|
2010-12-23 13:46:57 +00:00
|
|
|
end
|
|
|
|
|
2010-02-24 20:43:18 +00:00
|
|
|
private
|
|
|
|
def find_authorization(payment)
|
2010-11-28 14:31:31 +00:00
|
|
|
logs = payment.log_entries.all(:order => 'created_at DESC')
|
|
|
|
logs.each do |log|
|
|
|
|
details = YAML.load(log.details) # return the transaction details
|
|
|
|
if (details.params['payment_status'] == 'Pending' && details.params['pending_reason'] == 'authorization')
|
|
|
|
return details
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return nil
|
2010-02-24 20:43:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def find_capture(payment)
|
|
|
|
#find the transaction associated with the original authorization/capture
|
2010-11-28 14:31:31 +00:00
|
|
|
logs = payment.log_entries.all(:order => 'created_at DESC')
|
|
|
|
logs.each do |log|
|
|
|
|
details = YAML.load(log.details) # return the transaction details
|
|
|
|
if details.params['payment_status'] == 'Completed'
|
|
|
|
return details
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return nil
|
2010-02-24 20:43:18 +00:00
|
|
|
end
|
|
|
|
|
2010-02-25 11:12:15 +00:00
|
|
|
def gateway_error(text)
|
|
|
|
msg = "#{I18n.t('gateway_error')} ... #{text}"
|
|
|
|
logger.error(msg)
|
2012-02-16 07:20:38 +00:00
|
|
|
raise Spree::Core::GatewayError.new(msg)
|
2010-02-25 11:12:15 +00:00
|
|
|
end
|
2011-12-16 23:01:28 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def amount_in_cents(amount)
|
|
|
|
(100 * amount).to_i
|
|
|
|
end
|
2010-02-24 20:43:18 +00:00
|
|
|
end
|