]> gitweb.mndrdr.org Git - arelpe.git/commitdiff
Added welcome emails for paying members
authorAidan Cornelius-Bell <[email protected]>
Wed, 9 Oct 2024 22:44:33 +0000 (09:14 +1030)
committerAidan Cornelius-Bell <[email protected]>
Wed, 9 Oct 2024 22:44:33 +0000 (09:14 +1030)
app/controllers/subscriptions_controller.rb
app/mailers/subscription_mailer.rb [new file with mode: 0644]
app/views/subscription_mailer/confirmation_email.html.erb [new file with mode: 0644]
app/views/subscriptions/new.html.erb
test/mailers/previews/subscription_mailer_preview.rb [new file with mode: 0644]
test/mailers/subscription_mailer_test.rb [new file with mode: 0644]

index a601317815660723f5bca2a824be4545c1f1851e..6a0d46697e8cc85db0609ea654221d008a149a6b 100644 (file)
@@ -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 (file)
index 0000000..e2587b5
--- /dev/null
@@ -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 (file)
index 0000000..0da35cf
--- /dev/null
@@ -0,0 +1,16 @@
+<p>Dear <%= @user.first_name %>,</p>
+
+<p>My endless thanks for your generous support of mind reader. I am absolutely thrilled to have you on board as a <%= @support_type %> supporter.</p>
+
+<% if @amount.present? %>
+  <h3>Receipt Details:</h3>
+  <p>Amount: <%= number_to_currency(@amount, unit: "AU$") %></p>
+  <p>Date: <%= @date.strftime("%B %d, %Y") %></p>
+  <p>Support Type: <%= @support_type.titleize %></p>
+<% end %>
+
+<p>Your support means the world to me and helps me continue this anti-capitalist mission.</p>
+
+<p>If you have any questions or need any assistance, please don’t hesitate to reach out.</p>
+
+<p>In solidarity,<br>Aidan</p>
index 7904959d55e43cb7c0b820c6ed5f9adbd351e081..e8711ab24380c57939a8d6535e574ddeffd355b0 100644 (file)
@@ -43,7 +43,7 @@
        </div>
 
        <div class="actions">
-         <%= submit_tag "Subscribe", class: "button primary" %>
+         <%= submit_tag "Pay now", class: "button primary" %>
        </div>
   <% end %>
 <% end %>
                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, ",");
+                 });
+               }
        });
   </script>
 <% end %>
diff --git a/test/mailers/previews/subscription_mailer_preview.rb b/test/mailers/previews/subscription_mailer_preview.rb
new file mode 100644 (file)
index 0000000..4bcb8aa
--- /dev/null
@@ -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 (file)
index 0000000..83c53cf
--- /dev/null
@@ -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 [ "[email protected]" ], mail.to
+    assert_equal [ "[email protected]" ], mail.from
+    assert_match "Hi", mail.body.encoded
+  end
+end