【问题标题】:How do I process responses from using the official Chargify gem in Rails 3.如何在 Rails 3 中使用官方 Chargify gem 处理响应。
【发布时间】:2011-07-29 19:33:27
【问题描述】:

我目前正在开发一个 Rails 应用程序来接受使用 Chargify 的定期计费。我已经安装了their gem 并设法使用 gem 连接到 Chargify。但是,有些订阅会通过,有些则不会。

我的问题是,一旦 gem 与服务器通信,我该如何处理甚至处理响应?

我在开发日志中看不到任何数据传输成功或失败的迹象。 gem 文档也没有提到任何关于此的内容。

感谢收看。

更新

我正在使用的代码在我的结帐控制器中:

默认结帐 @customer = Customer.new(params[:customer])

Chargify::Customer.create(
  :first_name   => "Charlie",
  :last_name    => "Bull",
  :email        => "charlie@example.com",
  :organization => "Chargify"
)

Chargify::Subscription.create(
  :product_handle => 'recurring',
  :customer_attriburtes => {
    :first_name => @customer.shipping_first_name, 
    :last_name => @customer.shipping_last_name, 
    :email => @customer.email
  },
  :payment_profile_attributes => {
    :first_name => @customer.shipping_first_name,
    :last_name => @customer.shipping_last_name,
    :full_number => "1",
    :expiration_month => 1,
    :expiration_year => 2012,
    :billing_address => @customer.shipping_street_address,
    :billing_city => @customer.shipping_city,
    :billing_state => @customer.shipping_state,
    :billing_zip => @customer.shipping_zip_code,
    :billing_country => @customer.shipping_country
  }
)

#if @subscription.save
#  logger.info "saved description"
#  redirect_to process_path
#else
#  redirect_to :back, :alert =>"There was an error."
#end

结束

客户创建正在进行,但订阅没有。我只是在寻找来自服务器的回调,以便我可以根据它是否成功采取行动,并找出订阅未通过的原因。

【问题讨论】:

  • 看起来代码检查了一些请求的响应代码,如果它得到意外的响应,它会引发 UnexpectedResponseError。你能给我们一些示例代码吗?
  • 放上我的控制器代码。感谢您的回复。

标签: ruby-on-rails ruby-on-rails-3 chargify


【解决方案1】:

由于整个 gem 使用 ActiveResource,你不能只调用类似的东西:

# Create a subscription from a customer reference
subscription = Chargify::Subscription.create(
  :customer_reference => 'moklett',
  :product_handle => 'chargify-api-ares-test',
  :credit_card_attributes => {
    :first_name => "Michael",
    :last_name => "Klett",
    :expiration_month => 1,
    :expiration_year => 2020,
    :full_number => "1"
  }
)
if subscription.save
  puts "Created Subscription!"
else
  puts "Subscription Failed!"
end

看看记录是否创建正确?

编辑:您的代码应该可以工作,但我看到保存的调用已被注释掉。当您致电 save 时,它会创建或更新记录,并且测试这应该可以让您确定您的记录是否已创建。

【讨论】:

  • 很抱歉没有指定,但是官方 Chargify API 在这里:rubydoc.info/gems/chargify_api_ares/0.3.9/frames
  • 尝试使用上面的代码。我也不确定为什么你的保存调用被注释掉或者你在哪里定义@subscription。
  • 是的,我在正确的轨道上,但我缺少的是收到错误消息,我最终发现我可以通过调用:messages = '' subscription.errors.full_messages.each {|error| messages += error + "<br />"} logger.info messages
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-25
  • 2016-03-18
  • 2018-09-04
  • 2011-08-23
相关资源
最近更新 更多