diff --git a/app/models/paypal_account.rb b/app/models/paypal_account.rb index 5b6ffd4..7efac9f 100644 --- a/app/models/paypal_account.rb +++ b/app/models/paypal_account.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 @@ class PaypalAccount < ActiveRecord::Base :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 diff --git a/lib/spree/paypal_express.rb b/lib/spree/paypal_express.rb index b88a07d..bbeddeb 100644 --- a/lib/spree/paypal_express.rb +++ b/lib/spree/paypal_express.rb @@ -123,50 +123,16 @@ module Spree::PaypalExpress @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) diff --git a/paypal_express_extension.rb b/paypal_express_extension.rb index 62f4a28..23009e1 100644 --- a/paypal_express_extension.rb +++ b/paypal_express_extension.rb @@ -21,10 +21,5 @@ class PaypalExpressExtension < Spree::Extension 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