Rails 4 Payola Stripe webhook insert into table when invoice paid

167 Views Asked by At

I am trying to setup a webhook with the Payola gem to insert data into a table when invoice payment is successful (ie the event invoice.payment_succeeded. For some reason, this does not work, but a similar event to send email when invoice is created works. This is the code i have in my payola.rb initialization file:

Payola.configure do |config|

  Payola.secret_key =Rails.application.secrets.payola_secret_key
  Payola.publishable_key = Rails.application.secrets.payola_publishable_key

  config.send_email_for :receipt, :admin_receipt
  Payola.support_email="[email protected]"

# this webhook works and I get an email when new invoice is created
  config.subscribe 'invoice.created' do |event|
    subscription = Payola::Subscription.find_by(stripe_id: event.data.object.subscription)
    user=User.find(subscription.owner_id)
    UserMailer.invoicecreated_email(user).deliver_now
  end
# this webhook is supposed to create a new PaymentHistory record and send an email, but none of these actions is performed after invoice is paid.  
  config.subscribe 'invoice.payment_succeeded' do |event|
      subscription = Payola::Subscription.find_by(stripe_id: event.data.object.subscription)
      #subscription = Payola::Subscription.find_by(stripe_id: event.data.object.lines.data[0].id)
      user=User.find(subscription.owner_id)
      subscription.fail_payment_date1 = nil
      subscription.fail_payment_date2 = nil
      subscription.update
      PayolaPaymentHistory.create(owner_id: user.id, subscription_id: subscription.id,
          payment_date: Time.at(event.data.object.date).to_date,amount: event.data.object.amount_due,currency: event.data.object.currency,
          date_start: Time.at(event.data.object.lines.data[0].period.start).to_date,
          date_end: Time.at(event.data.object.lines.data[0].period.end).to_date,
          description: 'Monthly payment')
      UserMailer.successpayment_email(user).deliver_now
  end
end

What am I missing?

2

There are 2 best solutions below

1
On BEST ANSWER

Try trimming the block down to this until we get something simple running:

config.subscribe 'invoice.payment_succeeded' do |event|
  raise 'Yes the invoice.payment_succeeded webhook is running!'
end

Stop the Rails server and stop Spring with bin/spring stop to ensure the payola.rb initializer changes are picked up (try to stop Spring each time the payola.rb initializer changes).

Make a test payment and see if the error message above is raised in your logs and/or rails server output. Please report back with what you find out.

0
On

So if anyone is getting headaches because this silly error, here's where I went wrong:

subscription = Payola::Subscription.find_by(stripe_id: event.data.object.subscription)
# this is wrong
subscription.fail_payment_date1 = nil
subscription.fail_payment_date2 = nil
subscription.update

# this is allowed
subscription.fail_payment_date1 = nil
subscription.fail_payment_date2 = nil
subscription.save

# this is also allowed
subscription.update(fail_payment_date1: nil, fail_payment_date2: nil)

There was no issue with Payola whatsoever...completely my error.