separate method for charging offline

This commit is contained in:
Andy Brett
2020-11-18 13:38:14 -08:00
parent cad8a018f9
commit 13ab25ac45
4 changed files with 25 additions and 5 deletions

View File

@@ -62,7 +62,7 @@ class SubscriptionConfirmJob
return unless order.payment_required?
prepare_for_payment!(order)
order.process_payments!
order.process_payments!(true)
raise if order.errors.any?
end

View File

@@ -45,6 +45,19 @@ module Spree
failed_activemerchant_billing_response(e.message)
end
# NOTE: the name of this method is determined by Spree::Payment::Processing
def charge_offline(money, creditcard, gateway_options)
options = basic_options(gateway_options)
customer_id, payment_method_id = Stripe::CreditCardCloner.new.clone(creditcard,
stripe_account_id)
options[:customer] = customer_id
options[:off_session] = true
provider.purchase(money, payment_method_id, options)
rescue Stripe::StripeError => e
failed_activemerchant_billing_response(e.message)
end
# NOTE: the name of this method is determined by Spree::Payment::Processing
def authorize(money, creditcard, gateway_options)
authorize_response = provider.authorize(*options_for_authorize(money,

View File

@@ -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!
def process_payments!(offline = false)
raise Core::GatewayError, Spree.t(:no_pending_payments) if pending_payments.empty?
pending_payments.each do |payment|
break if payment_total >= total
payment.process!
payment.process!(offline)
if payment.completed?
self.payment_total += payment.amount

View File

@@ -3,7 +3,7 @@
module Spree
class Payment < ActiveRecord::Base
module Processing
def process!
def process!(offline = false)
return unless payment_method&.source_required?
raise Core::GatewayError, Spree.t(:payment_processing_failed) unless source
@@ -15,7 +15,9 @@ module Spree
raise Core::GatewayError, Spree.t(:payment_method_not_supported)
end
if payment_method.auto_capture?
if offline
charge_offline!
elsif payment_method.auto_capture?
purchase!
else
authorize!
@@ -32,6 +34,11 @@ module Spree
gateway_action(source, :purchase, :complete)
end
def charge_offline!
started_processing!
gateway_action(source, :charge_offline, :complete)
end
def capture!
return true if completed?