Browse files

Add CC expiration notifications

  • Loading branch information...
1 parent 9a761ce commit b470a8be843277fb26744b145ce48d36a946ce8d @jordanbyron jordanbyron committed Nov 9, 2012
View
8 app/mailers/account_mailer.rb
@@ -20,4 +20,12 @@ def failed_payment(user, charge)
mail(:to => user.contact_email,
:subject => "There was a problem with your payment for Practicing Ruby :(").deliver
end
+
+ def card_expiring(card)
+ @card = card
+ user = card.user
+
+ mail(:to => user.contact_email,
+ :subject => "Oh No! Your credit card is expiring next month.").deliver
+ end
end
View
7 app/models/card_expirer.rb
@@ -0,0 +1,7 @@
+CardExpirer = ->(date) {
+ cards = CreditCard.where(:expiration_year => date.year,
+ :expiration_month => date.month )
+ cards.each do |card|
+ AccountMailer.card_expiring(card)
+ end
+}
View
13 app/views/account_mailer/card_expiring.text.erb
@@ -0,0 +1,13 @@
+We just wanted to let you know that your credit card ending in
+<%= @card.last_four %> will expire on
+<%= "#{@card.expiration_month}/#{@card.expiration_year}" %>. Please update your
+billing information by visiting the following URL and
+clicking 'Update your credit card':
+
+<%= billing_settings_url %>
+
+If you don't update your card before it expires, the billing gremlins may
+disable your account and that makes us sad. As always if you have any questions
+or problems don't hesitate to contact us.
+
+Thanks for subscribing to Practicing Ruby! <3 <3 <3
View
11 lib/tasks/stripe.rake
@@ -1,5 +1,7 @@
+include RakeExceptionNotification
+
namespace :stripe do
- desc 'Loads all active credit cards'
+ desc 'Adds and updates customers active credit cards'
task :sync_credit_cards => :environment do
User.where(:payment_provider => 'stripe').find_each do |user|
payment_gateway = user.payment_gateway
@@ -15,4 +17,11 @@ namespace :stripe do
card.save
end
end
+
+ desc 'Notifies customers that their credit card is about to expire'
+ task :card_exipration_notice => :environment do
+ exception_notify do
+ CardExpirer.call(Date.today.next_month)
+ end
+ end
end
View
10 test/factories/credit_card.rb
@@ -0,0 +1,10 @@
+FactoryGirl.define do
+ sequence(:credit_card_number) { rand(1000..9999).to_s }
+
+ factory :credit_card do |c|
+ c.last_four { FactoryGirl.generate(:credit_card_number) }
+ c.expiration_month { Date.today.month }
+ c.expiration_year { Date.today.year }
+ c.user
+ end
+end
View
33 test/integration/credit_card_expiration_test.rb
@@ -0,0 +1,33 @@
+require 'test_helper'
+require 'rake'
+
+class CreditCardExpirationTest < ActionMailer::TestCase
+
+ test "emails are sent to customers when their cards are about to expire" do
+ date = Date.today.next_month
+
+ card = FactoryGirl.create(:credit_card, :expiration_year => date.year,
+ :expiration_month => date.month)
+
+ CardExpirer.call(date)
+
+ message = ActionMailer::Base.deliveries.first
+
+ assert message.to.include?(card.user.contact_email)
+ assert message.body.to_s.include?(card.last_four)
+ end
+
+ test "emails are only sent for cards that expire next month" do
+ this_month = Date.today
+ FactoryGirl.create(:credit_card, :expiration_year => this_month.year,
+ :expiration_month => this_month.month )
+
+ future_month = Date.today + 2.months
+ FactoryGirl.create(:credit_card, :expiration_year => future_month.year,
+ :expiration_month => future_month.month )
+
+ CardExpirer.call(Date.today.next_month)
+
+ assert ActionMailer::Base.deliveries.empty?
+ end
+end

0 comments on commit b470a8b

Please sign in to comment.