Move payment object logic out of order serializer and delete code

This commit is contained in:
Matt-Yorkley
2018-09-19 10:21:01 +01:00
parent ba254802f8
commit 3cbb576b4f
3 changed files with 20 additions and 27 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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'}