From 83d7d49e44321397e2897f2191b0cb621c0c97bd Mon Sep 17 00:00:00 2001 From: Andy Brett Date: Tue, 2 Feb 2021 10:43:37 -0800 Subject: [PATCH] refactor sub confirm job; move email to service --- app/jobs/subscription_confirm_job.rb | 9 ------- .../stripe_sca_payment_authorize.rb | 4 +++ .../stripe_sca_payment_authorize_spec.rb | 20 ++++++++++++++ spec/jobs/subscription_confirm_job_spec.rb | 26 ------------------- 4 files changed, 24 insertions(+), 35 deletions(-) diff --git a/app/jobs/subscription_confirm_job.rb b/app/jobs/subscription_confirm_job.rb index 09105ba0e7..29ecd6b6d0 100644 --- a/app/jobs/subscription_confirm_job.rb +++ b/app/jobs/subscription_confirm_job.rb @@ -47,7 +47,6 @@ class SubscriptionConfirmJob < ActiveJob::Base process_payment!(order) send_confirmation_email(order) - send_payment_authorization_emails(order) rescue StandardError => e if order.errors.any? send_failed_payment_email(order) @@ -94,14 +93,6 @@ class SubscriptionConfirmJob < ActiveJob::Base SubscriptionMailer.confirmation_email(order).deliver_now end - def send_payment_authorization_emails(order) - order.payments.each do |payment| - if payment.cvv_response_message.present? - PaymentMailer.authorize_payment(payment).deliver_now - end - end - end - def send_failed_payment_email(order, error_message = nil) order.update! record_and_log_error(:failed_payment, order, error_message) diff --git a/engines/order_management/app/services/order_management/subscriptions/stripe_sca_payment_authorize.rb b/engines/order_management/app/services/order_management/subscriptions/stripe_sca_payment_authorize.rb index ea4fbe5ce7..87b55889e8 100644 --- a/engines/order_management/app/services/order_management/subscriptions/stripe_sca_payment_authorize.rb +++ b/engines/order_management/app/services/order_management/subscriptions/stripe_sca_payment_authorize.rb @@ -15,6 +15,10 @@ module OrderManagement @order.errors.add(:base, I18n.t('authorization_failure')) unless @payment.pending? + if @payment.cvv_response_message.present? + PaymentMailer.authorize_payment(@payment).deliver_now + end + @payment end end diff --git a/engines/order_management/spec/services/order_management/subscriptions/stripe_sca_payment_authorize_spec.rb b/engines/order_management/spec/services/order_management/subscriptions/stripe_sca_payment_authorize_spec.rb index 17ece20be4..4240ecc0e8 100644 --- a/engines/order_management/spec/services/order_management/subscriptions/stripe_sca_payment_authorize_spec.rb +++ b/engines/order_management/spec/services/order_management/subscriptions/stripe_sca_payment_authorize_spec.rb @@ -57,6 +57,26 @@ module OrderManagement expect(order.errors[:base].first).to eq "Authorization Failure" end end + + context "and payment authorize requires additional authorization" do + let(:mail_mock) { double(:mailer_mock, deliver_now: true) } + + before do + allow(PaymentMailer).to receive(:authorize_payment) { mail_mock } + allow(payment).to receive(:authorize!) { + payment.state = "pending" + payment.cvv_response_message = "https://stripe.com/redirect" + } + end + + it "adds sends an email requesting authorization" do + payment_authorize.call! + + expect(order.errors.size).to eq 0 + expect(PaymentMailer).to have_received(:authorize_payment) + expect(mail_mock).to have_received(:deliver_now) + end + end end end end diff --git a/spec/jobs/subscription_confirm_job_spec.rb b/spec/jobs/subscription_confirm_job_spec.rb index 52f6324dcc..39bec020b1 100644 --- a/spec/jobs/subscription_confirm_job_spec.rb +++ b/spec/jobs/subscription_confirm_job_spec.rb @@ -232,7 +232,6 @@ describe SubscriptionConfirmJob do it "sends only a subscription confirm email, no regular confirmation emails" do expect{ job.send(:confirm_order!, order) }.to_not enqueue_job ConfirmOrderJob expect(job).to have_received(:send_confirmation_email).once - expect(job).to have_received(:send_payment_authorization_emails).once end end end @@ -272,29 +271,4 @@ describe SubscriptionConfirmJob do expect(mail_mock).to have_received(:deliver_now) end end - - describe "#send_payment_authorization_emails" do - let(:order) { instance_double(Spree::Order) } - let(:mail_mock) { double(:mailer_mock, deliver_now: true) } - let(:payment) { create(:payment, amount: 10) } - - before do - allow(order).to receive(:payments) { [payment] } - allow(PaymentMailer).to receive(:authorize_payment) { mail_mock } - end - - it "sends authorization email if a payment requires it" do - allow(payment).to receive(:cvv_response_message) { "http://redirect_url" } - job.send(:send_payment_authorization_emails, order) - expect(PaymentMailer).to have_received(:authorize_payment) - expect(mail_mock).to have_received(:deliver_now) - end - - it "does not send authorization email if no payment requires it" do - allow(payment).to receive(:cvv_response_message) { nil } - job.send(:send_payment_authorization_emails, order) - expect(PaymentMailer).not_to have_received(:authorize_payment) - expect(mail_mock).not_to have_received(:deliver_now) - end - end end