diff --git a/app/mailers/standing_order_mailer.rb b/app/mailers/standing_order_mailer.rb
index 11bbea53d0..558cd1597d 100644
--- a/app/mailers/standing_order_mailer.rb
+++ b/app/mailers/standing_order_mailer.rb
@@ -21,6 +21,11 @@ class StandingOrderMailer < Spree::BaseMailer
send_mail(order)
end
+ def failed_payment_email(order)
+ @order = order
+ send_mail(order)
+ end
+
private
def send_mail(order)
diff --git a/app/views/standing_order_mailer/failed_payment_email.html.haml b/app/views/standing_order_mailer/failed_payment_email.html.haml
new file mode 100644
index 0000000000..b14e7830fb
--- /dev/null
+++ b/app/views/standing_order_mailer/failed_payment_email.html.haml
@@ -0,0 +1,20 @@
+= render 'header', intro: t("email_so_failed_payment_intro_html")
+
+%p.callout
+ = t("email_so_failed_payment_explainer_html", distributor: @order.distributor.name)
+ = t("email_so_edit_false_html",
+ orders_close_at: l(@order.order_cycle.orders_close_at, format: "%a %b %d @ %l:%M%p"),
+ order_url: spree.order_url(@order))
+ = t("email_so_contact_distributor_html", distributor: @order.distributor.name, email: @order.distributor.email)
+
+- if @order.errors.any?
+ %p
+ %h4
+ = t :email_confirm_customer_number_html, number: @order.number
+ %p
+ = t("email_so_failed_payment_details_html", distributor: @order.distributor.name)
+ - @order.errors.full_messages.each do |message|
+ = message
+ %br
+
+= render 'spree/order_mailer/signoff'
diff --git a/config/locales/en.yml b/config/locales/en.yml
index b0075cd130..7364a717b1 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -1234,6 +1234,9 @@ See the %{link} to find out more about %{sitename}'s features and to start using
email_so_empty_intro_html: "We tried to place a new order with %{distributor}, but had some problems..."
email_so_empty_explainer_html: "Unfortunately, none of products that you ordered were available, so no order has been placed. The original quantities that you requested appear crossed-out below."
email_so_empty_details_html: "Here are the details of the unplaced order for %{distributor}:"
+ email_so_failed_payment_intro_html: "We tried to process a payment, but had some problems..."
+ email_so_failed_payment_explainer_html: "The payment for your subscription with %{distributor} failed because of a problem with your credit card. %{distributor} has been notified of this failed payment."
+ email_so_failed_payment_details_html: "Here are the details of the failure provided by the payment gateway:"
email_shipping_delivery_details: Delivery details
email_shipping_delivery_time: "Delivery on:"
email_shipping_delivery_address: "Delivery address:"
diff --git a/spec/mailers/standing_order_mailer_spec.rb b/spec/mailers/standing_order_mailer_spec.rb
index 3977765a5d..b84e171b98 100644
--- a/spec/mailers/standing_order_mailer_spec.rb
+++ b/spec/mailers/standing_order_mailer_spec.rb
@@ -1,6 +1,8 @@
require 'spec_helper'
describe StandingOrderMailer do
+ include ActionView::Helpers::SanitizeHelper
+
let!(:mail_method) { create(:mail_method, preferred_mails_from: 'spree@example.com') }
describe "order placement" do
@@ -77,4 +79,28 @@ describe StandingOrderMailer do
expect(body).to include "Unfortunately, none of products that you ordered were available"
end
end
+
+ describe "failed payment notification" do
+ let(:standing_order) { create(:standing_order, with_items: true) }
+ let(:proxy_order) { create(:proxy_order, standing_order: standing_order) }
+ let!(:order) { proxy_order.initialise_order! }
+
+ before do
+ order.errors.add(:base, "This is a payment failure error")
+
+ expect do
+ StandingOrderMailer.failed_payment_email(order).deliver
+ end.to change{StandingOrderMailer.deliveries.count}.by(1)
+ end
+
+ it "sends the email" do
+ body = strip_tags(StandingOrderMailer.deliveries.last.body.encoded)
+ expect(body).to include I18n.t("email_so_failed_payment_intro_html")
+ explainer = I18n.t("email_so_failed_payment_explainer_html", distributor: standing_order.shop.name)
+ expect(body).to include strip_tags(explainer)
+ details = I18n.t("email_so_failed_payment_details_html", distributor: standing_order.shop.name)
+ expect(body).to include strip_tags(details)
+ expect(body).to include "This is a payment failure error"
+ end
+ end
end