diff --git a/app/jobs/subscription_confirm_job.rb b/app/jobs/subscription_confirm_job.rb index acf241e4e0..f3d16d2ea9 100644 --- a/app/jobs/subscription_confirm_job.rb +++ b/app/jobs/subscription_confirm_job.rb @@ -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 diff --git a/app/models/spree/gateway/stripe_sca.rb b/app/models/spree/gateway/stripe_sca.rb index 02681ae210..ade64f157b 100644 --- a/app/models/spree/gateway/stripe_sca.rb +++ b/app/models/spree/gateway/stripe_sca.rb @@ -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, diff --git a/app/models/spree/order.rb b/app/models/spree/order.rb index 029f5bb602..817b55d319 100644 --- a/app/models/spree/order.rb +++ b/app/models/spree/order.rb @@ -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 diff --git a/app/models/spree/payment/processing.rb b/app/models/spree/payment/processing.rb index c5a9979ee3..de0c05d422 100644 --- a/app/models/spree/payment/processing.rb +++ b/app/models/spree/payment/processing.rb @@ -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?