Question

I setup stripe event gem to handle my webhooks. It has really simplified things for me. Everything gets done in an initializer. I am trying to send an invoice to my new customers when they sign up for a subscription, but not doing it correctly.

Here is my stripe.rb intializer

Stripe.api_key = ENV["STRIPE_API_KEY"]
STRIPE_PUBLIC_KEY = ENV["STRIPE_PUBLIC_KEY"]


StripeEvent.setup do
  subscribe 'customer.subscription.deleted' do |event|
    subscription = Subscription.find_by_stripe_customer_token(event.data.object.customer)
    subscription.update_attribute(:subscription_status, "inactive")
  end

  subscribe 'invoice.payment_succeeded' do |event|
    subscription = Subscription.find_by_stripe_customer_token(event.data.object.customer)
    InvoiceMailer.invoice_subscription(subscription).deliver
    end
end

Am I not allowed to use the InvoiceMailer in an intializer? Is it only for controllers?

I have a mailer called invoice.rb

class Invoice < ActionMailer::Base

  def invoice_subscription(subscription)
    mail(:to => subscription.email, :subject => "My Subscription Invoice", :from => "invoice@mywebsite.com")
  end

end
Was it helpful?

Solution

Use

Invoice.invoice_subscription(subscription).deliver

instead of InvoiceMailer or change the name of the class in invoice.rb to InvoiceMailer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top