I have the following service class:
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
When I try to execute in the rails console I am getting:
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>
Yet there is no plan whatsoever in the DB table. Also, there is no plan with such stripe_id in Stripe.
==EDIT
I've changed quite a bit the code but I am still getting the same error.
Here is the Service:
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
I'm executing it via DB seeding like this:
CreatePlan.call(stripe_id: 'basic', name: 'Basic', amount: 999, interval: 'month', currency: 'gbp', trial_period_days: 10)
And I am getting:
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
What am I doing wrong?
I now accidentally closed the browser tab containing the Stripe console and re-opened it and - voila - the Plans were there! It seems that the Stripe console does not refresh even if you click the Plans link repeatedly. You need to close the browser tab and re-open it. That's really lame. Anyway, thank you, @Mark, for trying to help.