diff --git a/app/controllers/admin/paypal_payments_controller.rb b/app/controllers/admin/paypal_payments_controller.rb deleted file mode 100644 index 6ae41d0..0000000 --- a/app/controllers/admin/paypal_payments_controller.rb +++ /dev/null @@ -1,43 +0,0 @@ -class Admin::PaypalPaymentsController < Admin::BaseController - resource_controller - belongs_to :order - ssl_required - - # to allow capture (NB also included in checkout controller...) - include Spree::PaypalExpress - - def capture - load_object - if !@order.paypal_payments.empty? && (payment = @order.paypal_payments.last).can_capture? - - paypal_capture(payment.find_authorization) - - flash[:notice] = t("paypal_capture_complete") - else - flash[:error] = t("unable_to_capture_paypal") - end - redirect_to edit_admin_order_payment_url(@order, @paypal_payment) - end - - - def refund - load_object - if params.has_key? :amount - - if !@order.paypal_payments.empty? - payment = @order.paypal_payments.first - - paypal_refund(payment.find_capture, params[:amount].to_f) - - flash[:notice] = t("paypal_refund_complete") - else - flash[:error] = t("unable_to_refund_paypal") - end - redirect_to edit_admin_order_payment_url(@order, @paypal_payment) - - - end - end - - -end diff --git a/app/models/paypal_account.rb b/app/models/paypal_account.rb index 7efac9f..7927bed 100644 --- a/app/models/paypal_account.rb +++ b/app/models/paypal_account.rb @@ -1,29 +1,72 @@ class PaypalAccount < ActiveRecord::Base has_many :payments, :as => :source - def finalize!(payment) + def actions + %w{capture credit} + end + + def capture(payment) authorization = find_authorization(payment) ppx_response = payment.payment_method.provider.capture((100 * payment.amount).to_i, authorization.transaction_id) if ppx_response.success? - transaction = PaypalTxn.new(:payment => payment, - :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"]) + PaypalTxn.create(: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.finalize! else - gateway_error(ppx_response) + gateway_error(ppx_response.message) end end + def can_capture?(payment) + find_capture(payment).nil? + end + + def credit(payment, amount=nil) + authorization = find_capture(payment) + + ppx_response = payment.payment_method.provider.credit(amount.nil? ? (100 * amount).to_i : (100 * amount).to_i, authorization.transaction_id) + + if ppx_response.success? + payment = authorization.paypal_payment + + PaypalTxn.new(:paypal_payment => payment, + :txn_type => PaypalTxn::TxnType::CREDIT, + :gross_amount => ppx_response.params["gross_refund_amount"].to_f, + :message => ppx_response.params["message"], + :payment_status => "Refunded", + :pending_reason => ppx_response.params["pending_reason"], + :transaction_id => ppx_response.params["refund_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"]) + + + else + gateway_error(ppx_response.message) + end + end + + def can_credit?(payment) + !find_capture(payment).nil? + end + private def find_authorization(payment) #find the transaction associated with the original authorization/capture @@ -39,9 +82,7 @@ class PaypalAccount < ActiveRecord::Base :order => 'created_at DESC') end - def can_capture?(payment) - find_capture.nil? - end + def gateway_error(text) msg = "#{I18n.t('gateway_error')} ... #{text}" diff --git a/app/views/admin/payments/edit.html.erb b/app/views/admin/payments/edit.html.erb deleted file mode 100644 index 8c0b7cc..0000000 --- a/app/views/admin/payments/edit.html.erb +++ /dev/null @@ -1,121 +0,0 @@ -<%= render :partial => 'admin/shared/order_tabs', :locals => {:current => "Payments"} %> - -

<%= t("activerecord.models.#{@object.class.to_s.underscore}.one") %>

- -<%=error_messages_for :creditcard_payment %> -<% form_for(object, :url => object_url, :html => { :method => :put}) do |payment_form| %> -<%= hidden_field_tag :payment_type, object.class.to_s.underscore %> - -

- - <%= object.amount %> -

- - <% if object.class == CreditcardPayment %> -
- <%= t('creditcard') %> - - - - - - - - - - - - - - - - - - - -
<%= t('card_details') %>
- XXXX-XXXX-XXXX-<%= object.creditcard.last_digits %> - - <%= object.creditcard.month %>/<%= object.creditcard.year %> - - <%= object.creditcard.verification_value %> -
- <%= object.creditcard.issue_number %> - - <%= object.creditcard.start_month %>/<%= object.creditcard.start_year %> -
- - <% payment_form.fields_for :order do |order_form| %> - <% order_form.fields_for :checkout do |checkout_form| %> - - <% checkout_form.fields_for :bill_address do |ba_form| %> - <%= render :partial => "admin/checkouts/address_form", :locals => {:f => ba_form, :name => t('billing_address')} %> - <% end %> - - <% end %> - <% end %> - -
- <% end %> - - <% if object.class == PaypalPayment %> -
- <%= t('paypal_payment') %> - - <% object.txns.reverse.each do |txn| %> - - - - - - - - - - - - - - - - - - - - <% if txn.payment_status == "Pending" %> - - - - - <% end %> -
<%= t('transaction') %> <%= txn.transaction_id %> - <%= txn.created_at.to_s(:date_time24) %>
- <%= txn.payment_status %> - - <%= txn.ack %> - - <%= number_to_currency txn.gross_amount %> - - <% if object.can_capture? %> - <%= link_to t("capture").titleize, capture_admin_order_paypal_payment_url(@order, object) %> - <% end %> - <%= link_to t("refund"), refund_admin_order_paypal_payment_url(@order, object) %> -
- <%= txn.message %> - - <%= txn.paypal_payment_id %> -
- <%= txn.pending_reason %> -
- <% end %> - -
- <% end %> - -

- <%= button t('continue') %> -

- -<% end %> - -<%#= link_to t("capture").titleize, capture_admin_order_payment_url(@order, @creditcard_payment), :confirm => t('are_you_sure_you_want_to_capture') if object.can_capture? %>   - diff --git a/app/views/admin/payments/source_views/_paypalexpress.erb b/app/views/admin/payments/source_views/_paypalexpress.erb new file mode 100644 index 0000000..a5b79a0 --- /dev/null +++ b/app/views/admin/payments/source_views/_paypalexpress.erb @@ -0,0 +1,74 @@ +
+ <%= t('paypal_account') %> + + + + + + + + + + + + + + + + + +
<%= t('account_details') %>
+ <%= payment.source.email %> + + <%= payment.source.payer_id %> + + <%= payment.source.payer_country %> +
+ <%= payment.source.payer_status %> +
+
+ +
+ <%= t('transactions') %> + + <% payment.txns.reverse.each do |txn| %> + + + + + + + + + + + + + + + + + + + <% if txn.payment_status == "Pending" %> + + + + + <% end %> +
<%= t('transaction') %> <%= txn.transaction_id %> - <%= txn.created_at.to_s(:date_time24) %>
+ <%= txn.txn_type_name %> + + <%= txn.response_code %> + + <%= number_to_currency txn.amount %> +
+ <%= txn.message %> + + <%= txn.payment_status %> +
+ <%= txn.pending_reason %> +
+ <% end %> + +
\ No newline at end of file diff --git a/config/locales/en-GB.yml b/config/locales/en-GB.yml index 6a26e1d..c7eb886 100644 --- a/config/locales/en-GB.yml +++ b/config/locales/en-GB.yml @@ -10,6 +10,8 @@ en-GB: paypal_payment_id: PayPal Payment ID pending_reason: Pending Reason result: Result + finalize: + paypalexpress: Capture Payment activerecord: attributes: paypal_payment: diff --git a/config/locales/en-US.yml b/config/locales/en-US.yml index 5186ff3..c3f59be 100644 --- a/config/locales/en-US.yml +++ b/config/locales/en-US.yml @@ -10,6 +10,15 @@ en-US: paypal_payment_id: PayPal Payment ID pending_reason: Pending Reason result: Result + review: Review + paypal_account: PayPal Account + payer_id: Payer ID + payer_country: Country + payer_status: Status + comment: Comment + account_details: Account Details + finalize: + paypalexpress: Capture Payment activerecord: attributes: paypal_payment: diff --git a/lib/spree/paypal_express.rb b/lib/spree/paypal_express.rb index e5ce7c2..aa57a49 100644 --- a/lib/spree/paypal_express.rb +++ b/lib/spree/paypal_express.rb @@ -95,8 +95,10 @@ module Spree::PaypalExpress if Spree::Config[:auto_capture] ppx_auth_response = gateway.purchase((@order.total*100).to_i, opts) + txn_type = PaypalTxn::TxnType::CAPTURE else ppx_auth_response = gateway.authorize((@order.total*100).to_i, opts) + txn_type = PaypalTxn::TxnType::AUTHORIZE end if ppx_auth_response.success? @@ -107,7 +109,7 @@ module Spree::PaypalExpress :payment_method_id => params[:payment_method_id]) PaypalTxn.create(:payment => payment, - :txn_type => PaypalTxn::TxnType::AUTHORIZE, + :txn_type => txn_type, :amount => ppx_auth_response.params["gross_amount"].to_f, :message => ppx_auth_response.params["message"], :payment_status => ppx_auth_response.params["payment_status"], @@ -135,33 +137,6 @@ module Spree::PaypalExpress end end - def paypal_refund(authorization, amount=nil) - ppx_response = paypal_gateway.credit(amount.nil? ? (100 * authorization.gross_amount).to_i : (100 * amount).to_i, authorization.transaction_id) - - if ppx_response.success? - payment = authorization.paypal_payment - - transaction = PaypalTxn.new(:paypal_payment => payment, - :gross_amount => ppx_response.params["gross_refund_amount"].to_f, - :message => ppx_response.params["message"], - :payment_status => "Refunded", - :pending_reason => ppx_response.params["pending_reason"], - :transaction_id => ppx_response.params["refund_transaction_id"], - :transaction_type => ppx_response.params["transaction_type"], - :payment_type => ppx_response.params["payment_type"], - :ack => 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 fixed_opts if Spree::Config[:paypal_express_local_confirm].nil?