2016-07-28 74 views
0

我试图设置web支付与Payola创业板发票支付成功时插入数据到表中(即事件invoice.payment_succeeded。由于某种原因,这是行不通的,但在创建发票时的作品类似的事件发送电子邮件这是代码我在我的payola.rb初始化文件:Rails 4 Payola条款webhook在发票支付时插入表

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 

我缺少什么

回答

0

尝试到这种修剪器,直到我们得到一些简单的跑步:

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

停止Rails服务器与bin/spring stop停止春季确保payola.rb初始化变化回升(尝试每次payola.rb初始化变化停止春季)。

进行测试支付并查看上述错误消息是否在您的日志和/或输出中引发。请回报你发现的内容。因为这个愚蠢的错误

+0

宾果@艾略特。我把代码拼凑起来,看看它失败的地方......我的错误是愚蠢的错误。我使用'subscription.update'而不是'subscription.save'。上面编辑的行解决了这个问题。感谢您的帮助和指点。 – Alex

0

因此,如果任何人越来越头疼,这里就是我错在哪里:

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没有任何问题......我完全错误。