【问题标题】:Unable to apply changes from Stripe webhook无法从 Stripe webhook 应用更改
【发布时间】:2015-01-14 01:02:33
【问题描述】:

我已经在 hooks_controller 中设置了我的 webhook,并且我成功地接收到了 webhook。

但是由于某种原因,“invoice.payment_succeeded”的更改不想工作。

class HooksController < ApplicationController
  protect_from_forgery :except => :receiver
  require 'json'

  Stripe.api_key = "key"

  def receiver

    data_json = JSON.parse request.body.read

    p data_json['data']['object']['customer']

    if data_json[:type] == "invoice.payment_succeeded"
      make_active(data_event)
    end

    if data_json[:type] == "invoice.payment_failed"
      make_inactive(data_event)
    end
  end

  def make_active(data_event)
    @user = User.find_by_stripe_customer_id(data['data']['object']['customer'])
    if @user.service_id.nil?
      @user.service_id = "test"
      @user.save!
    end
  end
end

我知道了,整个 webhook 似乎工作正常。

[2015-01-13 16:47:39] POST http://localhost:3000/hooks/receiver - 200

我还在我的 rails 控制台中重新创建了 make_active 方法,我在其中输入了 User.find_by_stripe_customer_id("value") 并成功保存。

响应日志: 在 2015-01-13 16:47:39 -0800 为 127.0.0.1 开始 POST "/hooks/receiver" HooksController#receiver 处理为 XML 参数:{"id"=>"evt_15KfJsKSH0PjiOYJ1MSTQxdi", "created"=>1421196456, "livemode"=>false, "type"=>"invoice.payment_succeeded", "data"=>{"object"=>{" date"=>1421196455, "id"=>"in_15KfJrKSH0PjiOYJJKOb0p1F", "period_start"=>1421196455, "period_end"=>1421196455, "lines"=>{"object"=>"list", "total_count"=>1 , "has_more"=>false, "url"=>"/v1/invoices/in_15KfJrKSH0PjiOYJJKOb0p1F/lines", "data"=>[{"id"=>"sub_5VghThK2pwN5kM", "object"=>"line_item", " type"=>"subscription", "livemode"=>false, "amount"=>500, "currency"=>"usd", "proration"=>false, "period"=>{"start"=>1421196455 , "end"=>1423874855}, "subscription"=>nil, "quantity"=>1, "plan"=>{"interval"=>"month", "name"=>"monkeyjr", "created" =>1421185125, "amount"=>500, "currency"=>"usd", "id"=>"1", "object"=>"plan", "livemode"=>false, "interval_count"=> 1, "trial_period_days"=>nil, "metadata"=>{}, "statement_descriptor"=>"monkeywork - monkeyjr"}, "description"=>nil, "metadata"=>{}}]}, "subtotal" =>500,“总计”=>500,“客户”=>“cus_5VghMM74h7yUFG”, "object"=>"invoice", "attempted"=>true, "close"=>true, "forgiven"=>false, "paid"=>true, "livemode"=>false, "attempt_count"=>1 , "amount_due"=>500, "currency"=>"usd", "starting_balance"=>0, "ending_balance"=>0, "next_payment_attempt"=>nil, "webhooks_delivered_at"=>nil, "charge"=> "ch_15KfJrKSH0PjiOYJ03y6dXP1", "discount"=>nil, "application_fee"=>nil, "subscription"=>"sub_5VghThK2pwN5kM", "tax_percent"=>nil, "metadata"=>{}, "statement_descriptor"=>nil, " description"=>nil, "receipt_number"=>nil}}, "object"=>"event", "pending_webhooks"=>1, "request"=>"iar_5VghmqLO5Fy6Rt", "api_version"=>"2014-12- 22", "hook"=>{"id"=>"evt_15KfJsKSH0PjiOYJ1MSTQxdi", "created"=>1421196456, "livemode"=>false, "type"=>"invoice.payment_succeeded", "data"=>{" object"=>{"date"=>1421196455, "id"=>"in_15KfJrKSH0PjiOYJJKOb0p1F", "period_start"=>1421196455, "period_end"=>1421196455, "lines"=>{"object"=>"list", "total_count"=>1, "has_more"=>false, "url"=>"/v1/invoices/in_15KfJrKSH0PjiOYJJKOb0p1F/lines", "data"=>[{"id"=>" sub_5FghThK2pwN5kM", "object"=>"line_item", "type"=>"subscription", "livemode"=>false, "amount"=>500, "currency"=>"usd", "proration"=>false , "期间"=>{"开始"=>1421196455, "结束"=>1423874855}, "订阅"=>nil, "数量"=>1, "计划"=>{"间隔"=>"月" , "name"=>"monkeyjr", "created"=>1421185125, "amount"=>500, "currency"=>"usd", "id"=>"1", "object"=>"plan" , "livemode"=>false, "interval_count"=>1, "trial_period_days"=>nil, "metadata"=>{}, "statement_descriptor"=>"monkeywork - monkeyjr"}, "description"=>nil, " metadata"=>{}}]}, "subtotal"=>500, "total"=>500, "customer"=>"cus_5VghMF74h7yUFG", "object"=>"invoice", "attempted"=>true, "已关闭"=>true, "forgiven"=>false, "paid"=>true, "livemode"=>false, "attempt_count"=>1, "amount_due"=>500, "currency"=>"usd", "starting_balance"=>0, "ending_balance"=>0, "next_payment_attempt"=>nil, "webhooks_delivered_at"=>nil, "charge"=>"ch_15KfJrKSHPjiOYJ03y6dXP1", "discount"=>nil, "application_fee"=>nil , "订阅"=>"sub_5VghhK2pwN5kM", "tax_percent "=>nil, "metadata"=>{}, "statement_descriptor"=>nil, "description"=>nil, "receipt_number"=>nil}}, "object"=>"event", "pending_webhooks"=> 1、“请求”=>“iar_5VghmqLO5Fy6Rt”、“api_version”=>“2014-12-22”}} “cus_5VghMF74h7yUFG” 在布局/应用程序中渲染 hooks/receiver.html.erb (0.1ms) 渲染布局/导航/_default.html.erb (0.8ms) 渲染布局/_header.html.erb (12.2ms) 渲染布局/_alerts.html.erb (0.1ms) 渲染布局/_footer.html.erb (0.1ms) 在 183 毫秒内完成 200 次 OK(查看次数:182.4 毫秒 | ActiveRecord:0.0 毫秒)

【问题讨论】:

  • 当 webhook 访问您暴露的 URL 时,您是否得到完整的响应?请在此处粘贴响应日志!
  • @Ajay 添加。但是无法对其进行样式设置,因此它看起来更具可读性。对不起:(

标签: ruby-on-rails


【解决方案1】:

1.在这里,您试图获取 data_json[:type]。而是使用 data_json['type']。使用下面提到的接收器操作。

def receiver
    data_json = JSON.parse(request.body.read)
    data_event = data_json['data']['object']['customer']

    if data_json['type'] == "invoice.payment_succeeded"
      make_active(data_event)
    end

    if data_json['type'] == "invoice.payment_failed"
      make_inactive(data_event)
    end
 end

2:您试图在 make_active 方法中使用数据哈希(仅对接收者本地)。相反,只需将该值分配给变量 data_event 并将其传递给 make_active 方法。检查以下代码:

def make_active(data_event)
   user = User.find_by_stripe_customer_id(data_event)
  if user.service_id.nil?
    user.service_id = "test"
    user.save!
  end
end

检查我的控制台:

我已经创建了我的示例应用程序并将其连接到条带以测试 webhook。 你可以参考我的代码gist link

【讨论】:

  • 由于某种原因,当我使用你的时,我得到了 500 错误,而我的得到了 200。即使我的没有应用更改。
  • 你在哪里得到错误?让我知道!我从未使用过条纹,只是为您配置和检查!请粘贴错误日志,我可以帮忙:)
猜你喜欢
  • 2021-01-27
  • 2017-06-08
  • 2021-09-30
  • 2015-03-30
  • 2018-01-15
  • 2015-04-18
  • 1970-01-01
  • 2019-10-02
  • 1970-01-01
相关资源
最近更新 更多