Extract order capturing to OrderCaptureService

This commit is contained in:
Matt-Yorkley
2023-05-07 19:57:06 +01:00
parent 73b41154cc
commit 61849d84e7
2 changed files with 26 additions and 6 deletions

View File

@@ -47,17 +47,15 @@ module Api
def capture
authorize! :admin, order
pending_payment = order.pending_payments.first
payment_capture = OrderCaptureService.new(order)
return payment_capture_failed unless order.payment_required? && pending_payment
if pending_payment.capture!
if payment_capture.call
render json: order.reload, serializer: Api::Admin::OrderSerializer, status: :ok
elsif payment_capture.gateway_error.present?
error_during_processing(payment_capture.gateway_error)
else
payment_capture_failed
end
rescue Spree::Core::GatewayError => e
error_during_processing(e)
end
private

View File

@@ -0,0 +1,22 @@
# frozen_string_literal: true
# Use `authorize! :admin order` before calling this service
class OrderCaptureService
attr_reader :gateway_error
def initialize(order)
@order = order
@gateway_error = nil
end
def call
return false unless @order.payment_required?
return false unless (pending_payment = @order.pending_payments.first)
pending_payment.capture!
rescue Spree::Core::GatewayError => e
@gateway_error = e
false
end
end