mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
refactor sub confirm job; move email to service
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user