我有以下服务类:
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>
然而,数据库表中没有任何计划。此外,Stripe中没有计划使用这样的stripe_id。
== EDIT
我已经改变了很多代码,但我仍然遇到同样的错误。
这是服务:
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
我做错了什么?
答案 0 :(得分:0)
我认为您可能正如我过去所做的那样错误地解释了控制台的输出:
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
尝试一下,看看是否有其他因素阻止了计划的保存。
答案 1 :(得分:0)
我现在不小心关闭了包含Stripe控制台的浏览器标签并重新打开它 - 瞧 - 计划就在那里!即使您反复单击“计划”链接,Stripe控制台似乎也不会刷新。您需要关闭浏览器选项卡并重新打开它。那真的很蹩脚。无论如何,谢谢你,@ Mark,试图提供帮助。