Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Features/pdf receipt #223

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ gem 'mustache'

gem 'dotenv-rails'

gem "prawn", :github => "prawnpdf/prawn"
gem "prawn-table", "= 0.1.0"
gem "prawn-receipt"

group :development do
gem 'dotenv-deployment'
gem 'capistrano'
Expand Down
15 changes: 15 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
GIT
remote: git://github.com/prawnpdf/prawn.git
revision: 6d175ab27663119513eedbfdd3413c13d2566854
specs:
prawn (1.3.0)
pdf-core (~> 0.4.0)
ttfunk (~> 1.3.0)

GEM
remote: https://rubygems.org/
specs:
Expand Down Expand Up @@ -204,6 +212,7 @@ GEM
slop (~> 3.4, >= 3.4.5)
parsley-rails (1.2.2.0)
railties (>= 3.0.0)
pdf-core (0.4.0)
pg (0.16.0)
poltergeist (1.1.2)
capybara (~> 2.0.1)
Expand All @@ -212,6 +221,8 @@ GEM
polyglot (0.3.5)
posix-spawn (0.3.6)
powerpack (0.0.9)
prawn-receipt (0.0.1)
prawn-table (0.1.0)
rack (1.4.5)
rack-cache (1.2)
rack (>= 0.4)
Expand Down Expand Up @@ -307,6 +318,7 @@ GEM
treetop (1.4.15)
polyglot
polyglot (>= 0.3.1)
ttfunk (1.3.0)
turbo-sprockets-rails3 (0.3.9)
railties (> 3.2.8, < 4.0.0)
sprockets (>= 2.0.0)
Expand Down Expand Up @@ -375,6 +387,9 @@ DEPENDENCIES
parsley-rails
pg (= 0.16.0)
poltergeist
prawn!
prawn-receipt
prawn-table (= 0.1.0)
rack-google_analytics
rack-pjax (~> 0.6.0)
rails (= 3.2.17)
Expand Down
Binary file added app/assets/images/pr-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 23 additions & 2 deletions app/mailers/account_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,30 @@ def failed_payment(user, charge)

def payment_created(user, payment)
@payment = payment
file = Tempfile.new('receipt.pdf')
begin
logo_image_path = File.expand_path("../assets/images/pr-logo.png",
File.dirname(__FILE__))

mail(:to => user.contact_email,
:subject => "Receipt for your payment to practicingruby.com").deliver
receipt = Prawn::Receipt.new :logo_image_path => logo_image_path,
:company_name => "Practicing Ruby",
:company_email => "[email protected]",
:customer_email => user.contact_email,
:customer_name => user.name,
:amount_billed => @payment.amount,
:credit_card => "xxxx-xxxx-xxxx-#{@payment.credit_card_last_four}",
:transaction_id => @payment.id

receipt.render_file file.path

attachments["receipt.pdf"] = File.read file.path
mail(:to => user.contact_email,
:subject => "Receipt for your payment to practicingruby.com"
).deliver
ensure
file.close
file.unlink
end

@payment.update_attributes(:email_sent => true)
end
Expand Down
17 changes: 14 additions & 3 deletions test/unit/stripe/payment_gateway_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ class StripePaymentGatewayTest < ActiveSupport::TestCase
setup do
ActionMailer::Base.deliveries.clear

@user = FactoryGirl.create(:user, :payment_provider => 'stripe')
@payment_gateway = @user.payment_gateway
@user = FactoryGirl.create(:user, :payment_provider => 'stripe')
@credit_card = FactoryGirl.create(:credit_card)
@user.credit_card = @credit_card
@payment_gateway = @user.payment_gateway
end

test 'for_customer' do
Expand Down Expand Up @@ -94,7 +96,16 @@ class StripePaymentGatewayTest < ActiveSupport::TestCase

message = ActionMailer::Base.deliveries.first

assert message.body.to_s[/#{payment.invoice_date}/], "Invoice date missing"
if message.has_attachments?
assert message.text_part.body.to_s[/#{payment.invoice_date}/], "Invoice date missing"
else
assert message.body.to_s[/#{payment.invoice_date}/], "Invoice date missing"
end

assert message.attachments.length >= 1, "No attachment"
File.open("/tmp/receipt_test.pdf", "wb") do |f|
f.write message.attachments['receipt.pdf'].body.raw_source
end
end

test 'update_credit_card' do
Expand Down