20

我知道还有另一个与此类似的问题,但我认为它的问/答不是很好。

基本上我有一个可用的 Rails 应用程序,用户可以在其中注册我的订阅,输入信用卡信息等。这一切都在工作。但我需要处理在此定期订阅期间某个时候用户的卡被拒绝的情况。

他们发送的事件类型在这里:https ://stripe.com/docs/api?lang=ruby#event_types 。

我无法在我的应用程序中访问 c​​harge.failed 对象。

webhook 上的文档也在这里:https ://stripe.com/docs/webhooks ,任何帮助将不胜感激。

4

3 回答 3

40

您需要创建一个控制器来基本上接受和处理请求。这很简单,尽管最初并不那么简单。这是我的 hooks_controller.rb 的示例:

class HooksController < ApplicationController
  require 'json'

  Stripe.api_key = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

  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)
    @profile = Profile.find(User.find_by_stripe_customer_token(data['data']['object']['customer']).profile)
    if @profile.payment_received == false
      @profile.payment_received = true
      @profile.save!
    end
  end

  def make_inactive(data_event)
    @profile = Profile.find(User.find_by_stripe_customer_token(data['data']['object']['customer']).profile)
    if @profile.payment_received == true
      @profile.payment_received = false
      @profile.save!
    end
  end
end

def 接收器是您必须将 webhook 指向条带接口上的视图。视图接收 json,我使用它来更新用户的个人资料,以防支付失败或成功。

于 2012-03-10T21:25:49.030 回答
10

现在使用stripe_eventgem 容易多了:

https://github.com/integrallis/stripe_event

于 2013-05-08T17:05:12.013 回答
0

这是一个不太理想的测试情况……

Stripe 需要一种方法来“强制” webhook 以进行测试。目前,您可以订阅的最短时间为 1 周(在测试模式下);如果您可以将其设置为 1 分钟、1 小时,甚至只是让回调实时发生,这将更有帮助,这样您就可以测试您的 API 响应系统。

本地测试很棒,但没有什么能取代现实世界的、现场的、通过互联网的、webhook/callbacks。不得不等待一周(!)严重减慢了项目的速度。

于 2014-04-08T20:08:54.020 回答