我知道还有一个类似于这个类似的问题,但我认为没有得到良好的问答。

基本上我有一个工作轨道应用程序,用户可以注册我的订阅,输入所有工作的信用卡信息等。但是,我需要处理在这种经常订阅期间在某些时候拒绝用户卡的情况。

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

我在应用程序中访问charge.failed对象时遇到问题。

webhook上的文档也在这里: https://stripe.com/docs/webhooks ,任何帮助都会受到欣赏。

有帮助吗?

解决方案

您需要创建一个控制器以基本上接受并处理请求。它非常直接,虽然不是直接,最初包裹你的思想。这是我的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接收器是您必须将Webhooks指向条带接口上的视图。该视图收到JSON,我将在付款失败或成功的情况下使用它来更新用户的配置文件。

其他提示

现在使用stripe_event gem:更容易

https://github.com/integlis/stripe_event

这是一个少于理想的测试情况...

条纹需要一种“Force”Webhook的方法来测试目的。目前,您可以制作的最短订阅是1周(测试模式);如果您可以将1分钟为1分,1小时甚至只是导致回调,则会更有用,因此您可以测试API响应系统。

本地测试很棒,但没有什么能取代现实世界,直播,通过互联网,Webhooks /回调。不得不等待一周(!)严重减慢项目。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top