Skip to content

Commit

Permalink
Send an email when the invoice is failed to charge
Browse files Browse the repository at this point in the history
We automate the process of sending emails when an invoice fails to
charge, including error details from the Stripe API.

We also add the support email as a CC, allowing the entire team to
follow up with the customer if needed.
  • Loading branch information
enescakir committed May 7, 2024
1 parent 57a87e8 commit 01595d7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
16 changes: 16 additions & 0 deletions model/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def charge
end

Clog.emit("Invoice couldn't charged with any payment method.") { {invoice_not_charged: {ubid: ubid}} }
send_failure_email(errors)
false
end

Expand All @@ -97,6 +98,21 @@ def send_success_email(below_threshold: false)
button_title: "View Invoice",
button_link: "#{Config.base_url}#{project.path}/billing#{ser[:path]}")
end

def send_failure_email(errors)
ser = Serializers::Web::Invoice.new(:detailed).serialize(self)
Util.send_email(ser[:billing_email], "Urgent: Action Required to Prevent Service Disruption",
cc: Config.mail_from,
greeting: "Dear #{ser[:billing_name]},",
body: ["We hope this message finds you well.",
"We've noticed that your credit card on file has been declined with the following errors:",
*errors.map { "- #{_1}" },
"The invoice amount of #{ser[:total]} tried be debited from your credit card on file.",
"To prevent service disruption, please update your payment information within the next two days.",
"If you have any questions, please send us a support request via support@ubicloud.com."],
button_title: "Update Payment Method",
button_link: "#{Config.base_url}#{project.path}/billing")
end
end

Invoice.unrestrict_primary_key
1 change: 1 addition & 0 deletions spec/model/invoice_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
expect(Clog).to receive(:emit).with("Invoice couldn't charged.").and_call_original.twice
expect(Clog).to receive(:emit).with("Invoice couldn't charged with any payment method.").and_call_original
expect(invoice.charge).to be false
expect(Mail::TestMailer.deliveries.length).to eq 1
end

it "can charge from a correct payment method even some of them are not working" do
Expand Down

0 comments on commit 01595d7

Please sign in to comment.