Ruby on rails 4 - 无法通过 API 创建 Stripe 计划



我有以下服务类:

class CreatePlan
  def self.call(options={})
  plan = Plan.new(options)
  if !plan.valid?
    return plan
  end
  begin
    Stripe::Plan.create(
      id: options[:stripe_id],
      amount: options[:amount],
      currency: 'usd',
      interval: options[:interval],
      name: options[:name],
    )
  rescue Stripe::StripeError => e
    plan.errors[:base] << e.message
    return plan
  end
  plan.save
  return plan
  end
end

当我尝试在 rails 控制台中执行时,我得到:

irb(main):002:0> CreatePlan.call(stripe_id: 'test_plan', name: 'Test Plan', amount: 500, interval: 'month', description: 'Test Plan', published: false)
Plan Exists (0.4ms)  SELECT  1 AS one FROM `plans` WHERE `plans`.`stripe_id` = BINARY 'test_plan' LIMIT 1
=> #<Plan id: nil, stripe_id: "test_plan", name: "Test Plan", description: "Test Plan", amount: 500, interval: "month", published: false, created_at: nil, updated_at: nil>

然而,DB表中没有任何计划。此外,Stripe 中没有此类stripe_id的计划。

==编辑

我已经更改了很多代码,但仍然遇到相同的错误。

以下是服务:

class CreatePlan
def self.call(options={})
Rails.logger.info "creating the new plan.."
plan = Plan.new(stripe_id: options[:stripe_id], amount: options[:amount], name: options[:name])
Rails.logger.info "done creating the new plan.."
if !plan.valid?
  Rails.logger.info "plan not valid.."
  Rails.logger.info plan.errors.full_messages
  plan.errors.full_messages
  return plan
end
begin
  splan = Stripe::Plan.create(
      id: options[:stripe_id],
      amount: options[:amount],
      currency: options[:currency],
      interval: options[:interval],
      trial_period_days: options[:trial_period_days],
      name: options[:name]
  )
  Rails.logger.info "stripe insert went well.."
  Rails.logger.info splan.created
rescue Stripe::StripeError => e
  Rails.logger.info "stripe insert did not go well.."
  if e.message != "Plan already exists."
    Rails.logger.info e.to_s
    Rails.logger.error e.message
    plan.errors[:base] << e.message
    return plan
  else
    Rails.logger.info "Plan already exists."
    Rails.logger.info e.to_s
    Rails.logger.error e.message
  end
end
plan.save
return plan
end
end

我通过数据库种子设定执行它,如下所示:

CreatePlan.call(stripe_id: 'basic', name: 'Basic', amount: 999, interval: 'month', currency: 'gbp', trial_period_days: 10)

我得到:

localhost:rails-devise nnikolo$ rake db:seed
D, [2015-06-28T23:50:00.269488 #26190] DEBUG -- :  ActiveRecord::SchemaMigration Load (0.2ms)  SELECT `schema_migrations`.* FROM `schema_migrations`
I, [2015-06-28T23:50:00.283726 #26190]  INFO -- : creating the new plan..
I, [2015-06-28T23:50:00.290575 #26190]  INFO -- : done creating the new plan..
D, [2015-06-28T23:50:00.299769 #26190] DEBUG -- :   Plan Exists (0.3ms)  SELECT  1 AS one FROM `plans` WHERE `plans`.`stripe_id` = BINARY 'basic' LIMIT 1
I, [2015-06-28T23:50:01.791772 #26190]  INFO -- : stripe insert did not go well..
I, [2015-06-28T23:50:01.791854 #26190]  INFO -- : Plan already exists.
I, [2015-06-28T23:50:01.791892 #26190]  INFO -- : (Status 400) Plan already exists.
E, [2015-06-28T23:50:01.791921 #26190] ERROR -- : Plan already exists.
D, [2015-06-28T23:50:01.792618 #26190] DEBUG -- :    (0.3ms)  BEGIN
D, [2015-06-28T23:50:01.794350 #26190] DEBUG -- :   Plan Exists (0.3ms)  SELECT  1 AS one FROM `plans` WHERE `plans`.`stripe_id` = BINARY 'basic' LIMIT 1
D, [2015-06-28T23:50:01.796032 #26190] DEBUG -- :   SQL (0.2ms)  INSERT INTO `plans` (`stripe_id`, `amount`, `name`, `created_at`, `updated_at`) VALUES ('basic', 999, 'Basic', '2015-06-28 22:50:01.794494', '2015-06-28 22:50:01.794494')
D, [2015-06-28T23:50:01.798160 #26190] DEBUG -- :    (1.7ms)  COMMIT

我做错了什么?

我认为您可能像过去一样错误地解释控制台的输出:

Plan Exists (0.4ms) SELECT  1 AS one FROM `plans` WHERE `plans`.`stripe_id` = BINARY 'test_plan' LIMIT 1

这并不意味着该计划存在。rails控制台本质上只是告诉你它正在检查计划是否存在。控制台不会显示该查询的结果,因此不会指示它是否存在。

我怀疑您的计划由于其他原因没有被保存,但您的代码不会将任何错误记录到您的控制台,而是返回一个 Plan 实例,您可以检查以下错误:

irb(main):002:0> plan = CreatePlan.call(stripe_id: 'test_plan', name: 'Test Plan', amount: 500, interval: 'month', description: 'Test Plan', published: false)
irb(main):002:0> plan.errors.full_messages

尝试一下,看看是否有其他因素阻止计划被保存。

我现在不小心关闭了包含 Stripe 控制台的浏览器选项卡并重新打开它,然后 - 瞧 - 计划就在那里!似乎即使您反复单击计划链接,Stripe 控制台也不会刷新。您需要关闭浏览器选项卡并重新打开它。这真的很蹩脚。无论如何,谢谢你,@Mark,试图提供帮助。

最新更新