Rails 支付发票后,4 个 Payola Stripe webhook 插入 table

Rails 4 Payola Stripe webhook insert into table when invoice paid

我正在尝试使用 Payola gem 设置一个 webhook,以便在发票支付成功时(即事件 invoice.payment_succeeded)将数据插入 table。出于某种原因,这不起作用,但创建发票时发送电子邮件的类似事件有效。这是我在 payola.rb 初始化文件中的代码:

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="me@example.com"

# 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

我错过了什么?

尝试将块缩小到这个,直到我们得到一些简单的东西 运行:

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

停止 Rails 服务器并用 bin/spring stop 停止 Spring 以确保选择 payola.rb 初始化程序更改(每次尝试停止 Spring payola.rb 初始化程序更改)。

进行测试付款,看看上面的错误消息是否在您的日志 and/or rails server 输出中出现。请报告您的发现。

因此,如果有人因为这个愚蠢的错误而头疼,那就是我出错的地方:

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)

Payola 没有任何问题...完全是我的错误。