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

Conversation

ruanwz
Copy link
Contributor

@ruanwz ruanwz commented Sep 17, 2014

When sending the notification email for the payment, generate the pdf receipt and added as an attachment in the notification email

@practicingruby
Copy link
Member

Thanks David, I'll take a closer look at this soon.

@jordanbyron: I asked @ruanwz to work on this, so no need to review unless I ping you. 😄

@practicingruby
Copy link
Member

After some tweaks and manual testing I was able to get this to work, but as-is this will produce a broken email. (The attachments are not encoded properly, so they come across as a plain text garbled email in base64 encoding)

To fix this problem, we need to do something like this:

  def payment_created(user, payment)
    @payment = payment
    file = Tempfile.new('receipt.pdf')
    begin

      receipt = Prawn::Receipt.new :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
      # verify the receipt.pdf by looking at the doc
      receipt.render_file "/tmp/test_receipt.pdf" if ENV['RAILS_ENV'] == 'test'

      contents = File.binread(file.path)

      attachments["receipt.pdf"] =contents

      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

@ruanwz: I'll follow up via email, but I think I can take care of fixing this patch up myself. Thanks!

@ruanwz
Copy link
Contributor Author

ruanwz commented Sep 18, 2014

@sandal I didn't noticed the email encoding issue, because I have no idea how to test it. Thank you to let me know it.

Last time, I only read the actionmailer api in http://api.rubyonrails.org/classes/ActionMailer/Base.html and follow the example:

class ApplicationMailer < ActionMailer::Base
  def welcome(recipient)
    attachments['free_book.pdf'] = File.read('path/to/file.pdf')
    mail(to: recipient, subject: "New account information")
  end
end

Seems that they don't have special handling for the attachment file encoding, maybe there are some changes in difference rails versions?

@practicingruby
Copy link
Member

@ruanwz: I'm not sure why the encoding was messed up, but it appears that calling attachment on the ActionMailer instance rather than the object returned by mail() solves the problem.

Practicing Ruby uses mailcatcher, so you can test email in development by running foreman start to fire up the web app, worker, and mail server, and then view a web-based email client at localhost:1080.

To get it to send an email, I basically copied and pasted the relevant portions of the test file into the Rails console.

@ruanwz
Copy link
Contributor Author

ruanwz commented Sep 21, 2014

update the pullrequest, to fix the issues in the discussion. But remaining one: how to add private gem into Travis to make test passed?

@practicingruby
Copy link
Member

@ruanwz: I think we will probably open source the prawn-receipt gem. I will send you an email about that soon... there are a couple small changes I'd like to make before we release it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants