1

在为 customer.subscription.deleted 测试 Stripe 的 webhook 时,我不断收到 422 错误

我把它放在我的配置路线中

post 'stripewebhooks/receive'

这是我的控制器

class StripewebhooksController < ApplicationController

    Stripe::api_key = ENV['STRIPE_SECRET_KEY']

    require 'json'

    def receive

    data_json = JSON.parse request.body.read

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

    if data_json[:type] == "customer.subscription.deleted"
      cancel_subscription(data_event)
    end

  end

    def cancel_subscription(data_event)
    @subscription = Subscription.find_by_stripe_customer_token(data['data']['object']['customer'])
    @subscription.update_attribute(:subscription_status, "inactive")
  end
end

我不清楚之后括号中应该包含什么

def cancel_subscription

我不确定我是否应该放置 data_event 或这意味着什么。

4

1 回答 1

0

当您从条带中获取帖子数据时,您需要从您的应用程序中返回 200 状态码。

尝试这个

def receive

    data_json = JSON.parse request.body.read

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

    if data_json[:type] == "customer.subscription.deleted"
      # Why did you send data_event? send the parsed data_json as parameter
      cancel_subscription(data_json)
    end

    # Return a 200 status code
    render :text => '{}', :status => :ok

end
于 2013-11-03T06:26:02.220 回答