spree_paypal_express/app/models/paypal_account.rb

99 lines
2.8 KiB
Ruby
Raw Normal View History

class PaypalAccount < ActiveRecord::Base
has_many :payments, :as => :source
2010-02-25 16:20:15 +00:00
def actions
%w{capture credit}
end
def capture(payment)
authorization = find_authorization(payment)
2010-11-28 14:31:31 +00:00
ppx_response = payment.payment_method.provider.capture((100 * payment.amount).to_i, authorization.params["transaction_id"])
if ppx_response.success?
record_log payment, ppx_response
payment.complete
else
2010-02-25 16:20:15 +00:00
gateway_error(ppx_response.message)
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
ppx_response = payment.payment_method.provider.credit(amount.nil? ? (100 * amount).to_i : (100 * amount).to_i, authorization.params['transaction_id'])
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
# fix for Payment#payment_profiles_supported?
def payment_gateway
false
end
2010-11-28 14:31:31 +00:00
def record_log(payment, response)
payment.log_entries.create(:details => response.to_yaml)
end
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
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
end
2010-11-28 14:31:31 +00:00
2010-11-24 09:31:31 +00:00
def echeck?(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_type'] == 'echeck'
return true
end
end
return false
2010-11-24 09:31:31 +00:00
end
def gateway_error(text)
msg = "#{I18n.t('gateway_error')} ... #{text}"
logger.error(msg)
raise Spree::GatewayError.new(msg)
end
end