mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-01 21:47:16 +00:00
refactor to remove boolean flag param
This commit is contained in:
@@ -62,7 +62,7 @@ class SubscriptionConfirmJob
|
||||
return unless order.payment_required?
|
||||
|
||||
prepare_for_payment!(order)
|
||||
order.process_payments!(offline: true)
|
||||
order.process_payments_offline!
|
||||
raise if order.errors.any?
|
||||
end
|
||||
|
||||
|
||||
@@ -499,13 +499,13 @@ module Spree
|
||||
# which gets rescued and converted to FALSE when
|
||||
# :allow_checkout_on_gateway_error is set to false
|
||||
#
|
||||
def process_payments!(offline: false)
|
||||
def process_payments!
|
||||
raise Core::GatewayError, Spree.t(:no_pending_payments) if pending_payments.empty?
|
||||
|
||||
pending_payments.each do |payment|
|
||||
break if payment_total >= total
|
||||
|
||||
payment.process!(offline)
|
||||
payment.process!
|
||||
|
||||
if payment.completed?
|
||||
self.payment_total += payment.amount
|
||||
@@ -516,6 +516,23 @@ module Spree
|
||||
errors.add(:base, e.message) && (return result)
|
||||
end
|
||||
|
||||
def process_payments_offline!
|
||||
raise Core::GatewayError, Spree.t(:no_pending_payments) if pending_payments.empty?
|
||||
|
||||
pending_payments.each do |payment|
|
||||
break if payment_total >= total
|
||||
|
||||
payment.process_offline!
|
||||
|
||||
if payment.completed?
|
||||
self.payment_total += payment.amount
|
||||
end
|
||||
end
|
||||
rescue Core::GatewayError => e
|
||||
errors.add(:base, e.message)
|
||||
false
|
||||
end
|
||||
|
||||
def billing_firstname
|
||||
bill_address.try(:firstname)
|
||||
end
|
||||
|
||||
@@ -3,20 +3,21 @@
|
||||
module Spree
|
||||
class Payment < ActiveRecord::Base
|
||||
module Processing
|
||||
def process!(offline = false)
|
||||
return unless payment_method&.source_required?
|
||||
|
||||
raise Core::GatewayError, Spree.t(:payment_processing_failed) unless source
|
||||
|
||||
return if processing?
|
||||
|
||||
unless payment_method.supports?(source)
|
||||
invalidate!
|
||||
raise Core::GatewayError, Spree.t(:payment_method_not_supported)
|
||||
end
|
||||
def process!
|
||||
return unless ready_to_process?
|
||||
|
||||
if payment_method.auto_capture?
|
||||
purchase!(offline)
|
||||
purchase!
|
||||
else
|
||||
authorize!
|
||||
end
|
||||
end
|
||||
|
||||
def process_offline!
|
||||
return unless ready_to_process?
|
||||
|
||||
if payment_method.auto_capture?
|
||||
charge_offline!
|
||||
else
|
||||
authorize!
|
||||
end
|
||||
@@ -27,9 +28,14 @@ module Spree
|
||||
gateway_action(source, :authorize, :pend)
|
||||
end
|
||||
|
||||
def purchase!(offline = false)
|
||||
def purchase!
|
||||
started_processing!
|
||||
gateway_action(source, offline ? :charge_offline : :purchase, :complete)
|
||||
gateway_action(source, :purchase, :complete)
|
||||
end
|
||||
|
||||
def charge_offline!
|
||||
started_processing!
|
||||
gateway_action(source, :charge_offline, :complete)
|
||||
end
|
||||
|
||||
def capture!
|
||||
@@ -193,6 +199,20 @@ module Spree
|
||||
|
||||
private
|
||||
|
||||
def ready_to_process?
|
||||
return false unless payment_method&.source_required?
|
||||
|
||||
raise Core::GatewayError, Spree.t(:payment_processing_failed) unless source
|
||||
|
||||
return false if processing?
|
||||
|
||||
unless payment_method.supports?(source)
|
||||
invalidate!
|
||||
raise Core::GatewayError, Spree.t(:payment_method_not_supported)
|
||||
end
|
||||
true
|
||||
end
|
||||
|
||||
def calculate_refund_amount(refund_amount = nil)
|
||||
refund_amount ||= if credit_allowed >= order.outstanding_balance.abs
|
||||
order.outstanding_balance.abs
|
||||
|
||||
Reference in New Issue
Block a user