From bbdbf387b7bd2904fb2440acd207b67a47ed8ffb Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Thu, 16 Dec 2021 17:13:16 +0000 Subject: [PATCH] Combine SendAuthorizationEmails and StripeScaPaymentAuthorize --- app/jobs/subscription_confirm_job.rb | 4 +--- .../order/send_authorization_emails.rb | 16 ------------- .../order/stripe_sca_payment_authorize.rb | 12 ++++++++-- .../stripe_sca_payment_authorize_spec.rb | 24 ++++++++++++------- 4 files changed, 26 insertions(+), 30 deletions(-) delete mode 100644 engines/order_management/app/services/order_management/order/send_authorization_emails.rb diff --git a/app/jobs/subscription_confirm_job.rb b/app/jobs/subscription_confirm_job.rb index e492036220..d0db6636e6 100644 --- a/app/jobs/subscription_confirm_job.rb +++ b/app/jobs/subscription_confirm_job.rb @@ -88,9 +88,7 @@ class SubscriptionConfirmJob < ActiveJob::Base def authorize_payment!(order) return if order.subscription.payment_method.class != Spree::Gateway::StripeSCA - OrderManagement::Order::StripeScaPaymentAuthorize.new(order). - extend(OrderManagement::Order::SendAuthorizationEmails). - call! + OrderManagement::Order::StripeScaPaymentAuthorize.new(order, off_session: true).call! end def send_confirmation_email(order) diff --git a/engines/order_management/app/services/order_management/order/send_authorization_emails.rb b/engines/order_management/app/services/order_management/order/send_authorization_emails.rb deleted file mode 100644 index 5b4fc46924..0000000000 --- a/engines/order_management/app/services/order_management/order/send_authorization_emails.rb +++ /dev/null @@ -1,16 +0,0 @@ -# frozen_string_literal: true - -module OrderManagement - module Order - module SendAuthorizationEmails - def call!(redirect_url = full_order_path(@order)) - super(redirect_url) - - return unless @payment.requires_authorization? - - PaymentMailer.authorize_payment(@payment).deliver_now - PaymentMailer.authorization_required(@payment).deliver_now - end - end - end -end diff --git a/engines/order_management/app/services/order_management/order/stripe_sca_payment_authorize.rb b/engines/order_management/app/services/order_management/order/stripe_sca_payment_authorize.rb index 80f5364490..0f802f92b6 100644 --- a/engines/order_management/app/services/order_management/order/stripe_sca_payment_authorize.rb +++ b/engines/order_management/app/services/order_management/order/stripe_sca_payment_authorize.rb @@ -5,9 +5,10 @@ module OrderManagement class StripeScaPaymentAuthorize include FullUrlHelper - def initialize(order) + def initialize(order, off_session: false) @order = order @payment = OrderPaymentFinder.new(@order).last_pending_payment + @off_session = off_session end def call!(redirect_url = full_order_path(@order)) @@ -19,8 +20,15 @@ module OrderManagement @order.errors.add(:base, I18n.t('authorization_failure')) end - @payment + return @payment unless @payment.requires_authorization? && off_session + + PaymentMailer.authorize_payment(@payment).deliver_now + PaymentMailer.authorization_required(@payment).deliver_now end + + private + + attr_reader :off_session end end end diff --git a/engines/order_management/spec/services/order_management/order/stripe_sca_payment_authorize_spec.rb b/engines/order_management/spec/services/order_management/order/stripe_sca_payment_authorize_spec.rb index 00447c6d78..9cfb512cfa 100644 --- a/engines/order_management/spec/services/order_management/order/stripe_sca_payment_authorize_spec.rb +++ b/engines/order_management/spec/services/order_management/order/stripe_sca_payment_authorize_spec.rb @@ -70,15 +70,6 @@ module OrderManagement } end - it "sends an email requesting authorization and an email notifying the shop owner when requested" do - payment_authorize.extend(OrderManagement::Order::SendAuthorizationEmails).call! - - expect(order.errors.size).to eq 0 - expect(PaymentMailer).to have_received(:authorize_payment) - expect(PaymentMailer).to have_received(:authorization_required) - expect(mail_mock).to have_received(:deliver_now).twice - end - it "doesn't send emails by default" do payment_authorize.call! @@ -87,6 +78,21 @@ module OrderManagement expect(PaymentMailer).to_not have_received(:authorization_required) expect(mail_mock).to_not have_received(:deliver_now) end + + context "when the processing is off-session (via backoffice/subscription)" do + let(:payment_authorize) { + OrderManagement::Order::StripeScaPaymentAuthorize.new(order, off_session: true) + } + + it "sends an email requesting authorization and an email notifying the shop owner when requested" do + payment_authorize.call! + + expect(order.errors.size).to eq 0 + expect(PaymentMailer).to have_received(:authorize_payment) + expect(PaymentMailer).to have_received(:authorization_required) + expect(mail_mock).to have_received(:deliver_now).twice + end + end end end end