当支付发票时,Rails 4 Payola Stripe webhook插入表格

时间:2016-07-28 04:06:40

标签: ruby-on-rails stripe-payments payola

我正在尝试使用Payola gem设置webhook,以便在发票付款成功时将数据插入表格(即事件invoice.payment_succeeded。出于某种原因,这不起作用,但发送的类似事件创建发票时的电子邮件。这是我在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

我错过了什么?

2 个答案:

答案 0 :(得分:0)

尝试将块修剪到此,直到我们得到一些简单的运行:

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

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

进行测试付款,看看您的日志和/或rails server输出中是否出现上述错误消息。请用你发现的内容报告。

答案 1 :(得分:0)

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

        public HttpCookie SetAndGetHttpCookies()
    {
        HttpCookie cookie = HttpContext.Request.Cookies.Get("MyCookieGitApplication");
        CookieHelper cookieHelper = new CookieHelper();

        if (cookie == null)
        {
            cookie = cookieHelper.SetCookie();
        }

        return cookie;
    }

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