From 3cbb576b4f8e0ff150b9e30f076ab39f3d2ca9d1 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Wed, 19 Sep 2018 10:21:01 +0100 Subject: [PATCH] Move payment object logic out of order serializer and delete code --- app/serializers/api/admin/order_serializer.rb | 30 +++---------------- app/services/pending_payments.rb | 15 ++++++++++ app/views/spree/admin/orders/index.html.haml | 2 +- 3 files changed, 20 insertions(+), 27 deletions(-) create mode 100644 app/services/pending_payments.rb diff --git a/app/serializers/api/admin/order_serializer.rb b/app/serializers/api/admin/order_serializer.rb index 065025d22f..7b7c5018c1 100644 --- a/app/serializers/api/admin/order_serializer.rb +++ b/app/serializers/api/admin/order_serializer.rb @@ -2,7 +2,7 @@ class Api::Admin::OrderSerializer < ActiveModel::Serializer attributes :id, :number, :full_name, :email, :phone, :completed_at, :display_total attributes :show_path, :edit_path, :state, :payment_state, :shipment_state attributes :payments_path, :shipments_path, :ship_path, :ready_to_ship, :created_at - attributes :distributor_name, :special_instructions, :pending_payments, :capture_path + attributes :distributor_name, :special_instructions, :capture_path has_one :distributor, serializer: Api::Admin::IdSerializer has_one :order_cycle, serializer: Api::Admin::IdSerializer @@ -40,9 +40,9 @@ class Api::Admin::OrderSerializer < ActiveModel::Serializer end def capture_path - return '' unless ready_for_payment? - return unless payment_to_capture - Spree::Core::Engine.routes_url_helpers.fire_admin_order_payment_path(object, payment_to_capture.id, e: 'capture') + payment_due = PendingPayments.new(object) + return '' unless object.payment_required? && payment_due.payment_object + Spree::Core::Engine.routes_url_helpers.fire_admin_order_payment_path(object, payment_due.payment_object.id, e: 'capture') end def ready_to_ship @@ -68,26 +68,4 @@ class Api::Admin::OrderSerializer < ActiveModel::Serializer def completed_at object.completed_at.blank? ? "" : I18n.l(object.completed_at, format: '%B %d, %Y') end - - def pending_payments - return if object.payments.blank? - payment = object.payments.select{ |p| p if p.state == 'checkout' }.first - return unless can_be_captured? payment - - payment.id - end - - private - - def ready_for_payment? - object.payment_required? && object.payments.present? - end - - def payment_to_capture - object.payments.select{ |p| p if p.state == 'checkout' }.first - end - - def can_be_captured?(payment) - payment && payment.actions.include?('capture') - end end diff --git a/app/services/pending_payments.rb b/app/services/pending_payments.rb new file mode 100644 index 0000000000..729af49e47 --- /dev/null +++ b/app/services/pending_payments.rb @@ -0,0 +1,15 @@ +# Returns the capturable payment object for an order with balance due + +class PendingPayments + def initialize(order) + @order = order + end + + def payment_object + @order.payments.select{ |p| p if p.state == 'checkout' }.first + end + + def can_be_captured? + payment_object && payment_object.actions.include?('capture') + end +end diff --git a/app/views/spree/admin/orders/index.html.haml b/app/views/spree/admin/orders/index.html.haml index 750a7d7958..ed94800e02 100644 --- a/app/views/spree/admin/orders/index.html.haml +++ b/app/views/spree/admin/orders/index.html.haml @@ -98,7 +98,7 @@ %a.icon_link.with-tip.icon-edit.no-text{'ng-href' => '{{::order.edit_path}}', 'data-action' => 'edit', 'ofn-with-tip' => t(:edit)} %div{'ng-if' => 'order.ready_to_ship'} %a.icon-road.icon_link.with-tip.no-text{'ng-href' => '{{::order.ship_path}}', 'data-action' => 'ship', 'data-confirm' => t(:are_you_sure), 'data-method' => 'put', rel: 'nofollow', 'ofn-with-tip' => t(:ship)} - %div{'ng-if' => 'order.pending_payments'} + %div{'ng-if' => 'order.capture_path'} %a.icon-capture.icon_link.no-text{'ng-href' => '{{::order.capture_path}}', 'data-action' => 'capture', 'data-method' => 'put', rel: 'nofollow', 'ofn-with-tip' => t(:capture)} .orders-loading{'ng-show' => 'RequestMonitor.loading'}