2016-07-28 38 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沒有任何問題......我完全錯誤。