2015-06-11 18 views
0

我有以下服務類:無法通過API來創建帶區計劃

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 

當我嘗試在軌控制檯執行我越來越:

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的計劃。

==編輯

我已經改變了不少代碼,但我仍然得到同樣的錯誤。

這裏的服務:

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 

我通過DB播種執行這樣的:

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

我會建議您嘗試使用byebug gem來查看您的代碼,看看它究竟在哪裏失敗。我猜測它是檢查計劃是否有效的地方?你可以發佈你的計劃模型的代碼? –

+0

另外,它在控制檯中說:「Plan Exists(0.4ms)」並不意味着它存在,rails控制檯只是告訴你它正在檢查是否存在某些東西。 –

回答

0

我現在不小心關閉了包含條紋控制檯的瀏覽器選項卡,並重新打開它 - 瞧 - 計劃在那裏!看起來,即使您重複點擊計劃鏈接,條紋控制檯也不刷新。您需要關閉瀏覽器選項卡並重新打開它。這真的很蹩腳。無論如何,謝謝你,@Mark,試圖幫助。

0

我在想,你可能會被解釋控制檯輸出錯誤,因爲我在過去所做的那樣:

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

這並不意味着該計劃的存在。鐵軌控制檯本質上只是告訴你,它正在檢查計劃的存在。控制檯不會顯示該查詢的結果,因此它是否存在未指示。

我懷疑你的計劃沒有被保存一些其他的原因,但您的代碼不記錄任何錯誤到控制檯,而是返回一個計劃實例,該實例你可以檢查有關錯誤:

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 

試一下,看看是否有其他的東西阻止計劃被保存。