Ask A Question

Notifications

You’re not receiving notifications from this thread.

PDF Receipts not working

Adrian DeGus asked in Rails

I just finished working through the pdf receipts video but can't get it to work. I have the webhook working and all charges are displayed properly, but the link to the pdf 404's.

Can you see where I went wrong?

# config/initializers/stripe.rb

class RecordCharges
  def call(event)
    charge = event.data.object

    user = User.find_by(stripe_id: charge.customer)

    user.charges.create(
                    stripe_id: charge.id,
                    amount: charge.amount,
                    card_last4: charge.source.last4,
                    card_type: charge.source.brand,
                    card_exp_month: charge.source.exp_month,
                    card_exp_year: charge.source.exp_year
    )
  end
end

StripeEvent.configure do |events|
  events.subscribe 'charge.succeeded', RecordCharges.new
end


# charge.rb

belongs_to :user

def receipt
    Receipts::Receipt.new(
        id: id,
        product: "Nuvro",
        company: {
            name: "Nuvro LLC",
            address: "2040 S Alma School Rd Suite 1-484\nChandler, AZ 85286",
            email: "support@nuvro.com",
            logo: Rails.root.join("app/assets/images/nuvro-50.png")
        },
        line_items: [
            ["Date",           created_at.strftime('%B' '%d,' '%Y')],
            ["Account Billed", "#{user.first_name} #{user.last_name} (#{user.email})"],
            ["Product",        "Nuvro"],
            ["Amount",         "$#{amount / 100}.00"],
            ["Charged to",     "#{card_type} (**** **** **** #{card_last4})"],
            ["Transaction ID", uuid]
        ]
    )
  end


    # user.rb

    has_many :charges


# charges_controller.rb

def show
    @charge = current_user.charges.find(params[:id])
    respond_to do |format|
      format.pdf {
        send_data @charge.receipt.render,
                  filename: "#{@charge.created_at.strftime("%m-%d-%Y")}-nuvro-receipt.pdf",
                  type: "application/pdf",
                  disposition: :inline
      }
    end
  end


    # routes.rb

    resource :charge # charges#show


    # views/companies/edit.html.erb

    <% current_user.charges.each do |charge| %>
    ....
    <%= link_to "View Receipt", charge_path(@charge, format: :pdf) %>

    ...
    <%= link_to "View Receipt", charge_path(charge, format: :pdf) %> # tried this, didn't work

I'm missing something obvious, I just know it :)

Reply

If the link is 404ing, then you either don't have routes, or your links are wrong. They should look something like this:

# config/routes.rb
resources :charges
<% current_user.charges.each do |charge| %>
    <%= link_to "View Receipt", charge_path(charge, format: :pdf) %> # tried this, didn't work
<% end %>
Reply

When I used resources :charges in my routes it threw an exception due to the lack of a charge ID. It's because of this that I changed my route to resource :charge

Reply

Well your snippet there was using @charge which would cause that because you didn't set the charge variable (and you can't since the user has multiple charges. You want resources plural because there are many charges and the user should be able to view each charge and the ID needs to be passed in to the charge_path. If you use both those that I mentioned you should be good.

Reply

That did the trick!

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 87,563+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.