From 194ce0af49a7661f270f5ed917d195e780b24979 Mon Sep 17 00:00:00 2001 From: Aidan Cornelius-Bell Date: Thu, 10 Oct 2024 09:14:33 +1030 Subject: [PATCH] Added welcome emails for paying members --- app/controllers/subscriptions_controller.rb | 2 + app/mailers/subscription_mailer.rb | 15 ++++ .../confirmation_email.html.erb | 16 +++++ app/views/subscriptions/new.html.erb | 70 ++++++++++--------- .../previews/subscription_mailer_preview.rb | 7 ++ test/mailers/subscription_mailer_test.rb | 11 +++ 6 files changed, 87 insertions(+), 34 deletions(-) create mode 100644 app/mailers/subscription_mailer.rb create mode 100644 app/views/subscription_mailer/confirmation_email.html.erb create mode 100644 test/mailers/previews/subscription_mailer_preview.rb create mode 100644 test/mailers/subscription_mailer_test.rb diff --git a/app/controllers/subscriptions_controller.rb b/app/controllers/subscriptions_controller.rb index a601317..6a0d466 100644 --- a/app/controllers/subscriptions_controller.rb +++ b/app/controllers/subscriptions_controller.rb @@ -27,11 +27,13 @@ class SubscriptionsController < ApplicationController if support_type == 'non_financial' current_user.update(support_type: 'non_financial') AdminMailer.new_non_financial_member(current_user).deliver_later + SubscriptionMailer.confirmation_email(current_user, nil, 'non_financial').deliver_later redirect_to subscriptions_path, notice: 'Thank you for your non-financial support!' elsif ['one_time', 'ongoing'].include?(support_type) begin process_payment(support_type, payment_amount) AdminMailer.new_paid_member(current_user).deliver_later + SubscriptionMailer.confirmation_email(current_user, payment_amount, support_type).deliver_later redirect_to subscriptions_path, notice: 'Thank you for your support, it means the world to me! I will be in touch.' rescue Stripe::CardError => e flash.now[:error] = e.message diff --git a/app/mailers/subscription_mailer.rb b/app/mailers/subscription_mailer.rb new file mode 100644 index 0000000..e2587b5 --- /dev/null +++ b/app/mailers/subscription_mailer.rb @@ -0,0 +1,15 @@ +class SubscriptionMailer < ApplicationMailer + # Subject can be set in your I18n file at config/locales/en.yml + # with the following lookup: + # + # en.subscription_mailer.confirmation_email.subject + # + def confirmation_email(user, amount, support_type) + @user = user + @amount = amount + @support_type = support_type + @date = Time.current + + mail(to: @user.email, subject: 'Thank you for supporting mind reader!') + end +end diff --git a/app/views/subscription_mailer/confirmation_email.html.erb b/app/views/subscription_mailer/confirmation_email.html.erb new file mode 100644 index 0000000..0da35cf --- /dev/null +++ b/app/views/subscription_mailer/confirmation_email.html.erb @@ -0,0 +1,16 @@ +

Dear <%= @user.first_name %>,

+ +

My endless thanks for your generous support of mind reader. I am absolutely thrilled to have you on board as a <%= @support_type %> supporter.

+ +<% if @amount.present? %> +

Receipt Details:

+

Amount: <%= number_to_currency(@amount, unit: "AU$") %>

+

Date: <%= @date.strftime("%B %d, %Y") %>

+

Support Type: <%= @support_type.titleize %>

+<% end %> + +

Your support means the world to me and helps me continue this anti-capitalist mission.

+ +

If you have any questions or need any assistance, please don’t hesitate to reach out.

+ +

In solidarity,
Aidan

diff --git a/app/views/subscriptions/new.html.erb b/app/views/subscriptions/new.html.erb index 7904959..e8711ab 100644 --- a/app/views/subscriptions/new.html.erb +++ b/app/views/subscriptions/new.html.erb @@ -43,7 +43,7 @@
- <%= submit_tag "Subscribe", class: "button primary" %> + <%= submit_tag "Pay now", class: "button primary" %>
<% end %> <% end %> @@ -116,40 +116,42 @@ form.submit(); } - // Format the payment amount input with AU$ prefix - var paymentAmountInput = document.getElementById('payment_amount'); - if (paymentAmountInput) { - paymentAmountInput.addEventListener('input', function(e) { - var cursorPosition = e.target.selectionStart; - var value = e.target.value.replace(/[^0-9.]/g, ''); - - if (value !== '') { - var floatValue = parseFloat(value); - if (!isNaN(floatValue)) { - var formattedValue = 'AU$' + floatValue.toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ","); - e.target.value = formattedValue; - - // Adjust cursor position - var newPosition = cursorPosition + (formattedValue.length - value.length); - e.target.setSelectionRange(newPosition, newPosition); + // Format the payment amount input with AU$ prefix + var paymentAmountInput = document.getElementById('payment_amount'); + if (paymentAmountInput) { + // Set initial value + paymentAmountInput.value = 'AU$0.00'; + + paymentAmountInput.addEventListener('input', function(e) { + var cursorPosition = e.target.selectionStart; + var value = e.target.value.replace(/[^0-9.]/g, ''); + + var floatValue = parseFloat(value) || 0; + var formattedValue = 'AU$' + floatValue.toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ","); + + var oldLength = e.target.value.length; + e.target.value = formattedValue; + var newLength = e.target.value.length; + + // Adjust cursor position + var newPosition = cursorPosition + (newLength - oldLength); + newPosition = Math.max(3, Math.min(newPosition, formattedValue.length)); + e.target.setSelectionRange(newPosition, newPosition); + }); + + paymentAmountInput.addEventListener('focus', function(e) { + if (e.target.value === 'AU$0.00') { + setTimeout(function() { + e.target.setSelectionRange(3, 3); + }, 0); } - } else { - e.target.value = ''; - } - }); - - paymentAmountInput.addEventListener('focus', function(e) { - if (e.target.value === '') { - e.target.value = 'AU$'; - } - }); - - paymentAmountInput.addEventListener('blur', function(e) { - if (e.target.value === 'AU$') { - e.target.value = ''; - } - }); - } + }); + + paymentAmountInput.addEventListener('blur', function(e) { + var value = parseFloat(e.target.value.replace(/[^0-9.]/g, '')) || 0; + e.target.value = 'AU$' + value.toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ","); + }); + } }); <% end %> diff --git a/test/mailers/previews/subscription_mailer_preview.rb b/test/mailers/previews/subscription_mailer_preview.rb new file mode 100644 index 0000000..4bcb8aa --- /dev/null +++ b/test/mailers/previews/subscription_mailer_preview.rb @@ -0,0 +1,7 @@ +# Preview all emails at http://localhost:3000/rails/mailers/subscription_mailer +class SubscriptionMailerPreview < ActionMailer::Preview + # Preview this email at http://localhost:3000/rails/mailers/subscription_mailer/confirmation_email + def confirmation_email + SubscriptionMailer.confirmation_email + end +end diff --git a/test/mailers/subscription_mailer_test.rb b/test/mailers/subscription_mailer_test.rb new file mode 100644 index 0000000..83c53cf --- /dev/null +++ b/test/mailers/subscription_mailer_test.rb @@ -0,0 +1,11 @@ +require "test_helper" + +class SubscriptionMailerTest < ActionMailer::TestCase + test "confirmation_email" do + mail = SubscriptionMailer.confirmation_email + assert_equal "Confirmation email", mail.subject + assert_equal [ "to@example.org" ], mail.to + assert_equal [ "from@example.com" ], mail.from + assert_match "Hi", mail.body.encoded + end +end -- 2.39.5