Permalink
Browse files

Moved capture method into PaypalAccount

  • Loading branch information...
1 parent fb0ac7b commit 472321b53da2280786222d6235cecfcd36ad80ae @BDQ BDQ committed Feb 25, 2010
Showing with 15 additions and 56 deletions.
  1. +13 −15 app/models/paypal_account.rb
  2. +2 −36 lib/spree/paypal_express.rb
  3. +0 −5 paypal_express_extension.rb
@@ -2,12 +2,10 @@ class PaypalAccount < ActiveRecord::Base
has_many :payments, :as => :source
def finalize!(payment)
- authorization = find_authorization
+ authorization = find_authorization(payment)
- ppx_response = p.payment_method.capture((100 * payment.amount).to_i, authorization.transaction_id)
+ ppx_response = payment.payment_method.provider.capture((100 * payment.amount).to_i, authorization.transaction_id)
if ppx_response.success?
- payment = authorization.paypal_payment
-
transaction = PaypalTxn.new(:payment => payment,
:amount => ppx_response.params["gross_amount"].to_f,
:message => ppx_response.params["message"],
@@ -16,38 +14,38 @@ def finalize!(payment)
:transaction_id => ppx_response.params["transaction_id"],
:transaction_type => ppx_response.params["transaction_type"],
:payment_type => ppx_response.params["payment_type"],
- :ack => ppx_response.params["ack"],
+ :response_code => ppx_response.params["ack"],
:token => ppx_response.params["token"],
:avs_response => ppx_response.avs_result["code"],
:cvv_response => ppx_response.cvv_result["code"])
-
- payment.paypal_txns << transaction
-
- payment.save
else
gateway_error(ppx_response)
end
-
end
-
private
def find_authorization(payment)
#find the transaction associated with the original authorization/capture
- txns.find(:first,
- :conditions => {:pending_reason => "authorization", :payment_status => "Pending"},
+ payment.txns.find(:first,
+ :conditions => {:pending_reason => "authorization", :payment_status => "Pending", :txn_type => PaypalTxn::TxnType::AUTHORIZE.to_s},
:order => 'created_at DESC')
end
def find_capture(payment)
#find the transaction associated with the original authorization/capture
- txns.find(:first,
- :conditions => {:payment_status => "Completed"},
+ payment.txns.find(:first,
+ :conditions => {:payment_status => "Completed", :txn_type => PaypalTxn::TxnType::CAPTURE.to_s},
:order => 'created_at DESC')
end
def can_capture?(payment)
find_capture.nil?
end
+
+ def gateway_error(text)
+ msg = "#{I18n.t('gateway_error')} ... #{text}"
+ logger.error(msg)
+ raise Spree::GatewayError.new(msg)
+ end
end
@@ -123,50 +123,16 @@ def paypal_finish
@order.save!
@checkout.reload
+ #need to force checkout to complete state
until @checkout.state == "complete"
@checkout.next!
end
-
- # todo - share code
- flash[:notice] = t('order_processed_successfully')
- order_params = {:checkout_complete => true}
- order_params[:order_token] = @order.token unless @order.user
- session[:order_id] = nil if @order.checkout.completed_at
+ complete_checkout
else
order_params = {}
gateway_error(ppx_auth_response)
end
-
- redirect_to order_url(@order, order_params)
- end
-
- def paypal_capture(authorization)
- ppx_response = paypal_gateway.capture((100 * authorization.gross_amount).to_i, authorization.transaction_id)
-
- if ppx_response.success?
- payment = authorization.paypal_payment
-
- transaction = PaypalTxn.new(:payment => payment,
- :txn_type => PaypalTxn::TxnType::CAPTURE,
- :amount => ppx_response.params["gross_amount"].to_f,
- :message => ppx_response.params["message"],
- :payment_status => ppx_response.params["payment_status"],
- :pending_reason => ppx_response.params["pending_reason"],
- :transaction_id => ppx_response.params["transaction_id"],
- :transaction_type => ppx_response.params["transaction_type"],
- :payment_type => ppx_response.params["payment_type"],
- :response_code => ppx_response.params["ack"],
- :token => ppx_response.params["token"],
- :avs_response => ppx_response.avs_result["code"],
- :cvv_response => ppx_response.cvv_result["code"])
-
- payment.paypal_txns << transaction
-
- payment.save
- else
- gateway_error(ppx_response)
- end
end
def paypal_refund(authorization, amount=nil)
@@ -21,10 +21,5 @@ def activate
CheckoutsController.class_eval do
include Spree::PaypalExpress
end
-
- # probably not needed once the payments mech is generalised
- Order.class_eval do
- has_many :paypal_payments
- end
end
end

0 comments on commit 472321b

Please sign in to comment.