RailsApps / rails-stripe-membership-saas

An example Rails 4.2 app with Stripe and the Payola gem for a membership or subscription site.
http://railsapps.github.io/rails-stripe-membership-saas
1.14k stars 232 forks source link

Free Plan #154

Closed morganric closed 8 years ago

morganric commented 9 years ago

After creating a free plan locally:

<Plan id: 4, name: "Free", stripe_id: "free", interval: "month", amount: 0, created_at: "2015-07-20 19:31:31", updated_at: "2015-07-20 19:44:36">

I get json errors when trying to signup with it...

{ guid: null, status: "pending", error: "Email can't be blank and Stripe token can't be blank" }

Stack trace ->

Started POST "/users" for 127.0.0.1 at 2015-07-20 20:45:41 +0100 Processing by RegistrationsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"JARYfmmuQKhcYmkGZTMY5NxrEDOLXb6zwVr+aVTDHBcLK+zqqv+TyBsBOF93dA/3TUXSF/9zlTO8zuqhyoziDg==", "user"=>{"name"=>"morganricfree", "email"=>"morganric+freeer@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "plan_id"=>"4"}, "commit"=>"Sign up"} Unpermitted parameter: name Plan Load (0.2ms) SELECT "plans".* FROM "plans" WHERE "plans"."id" = ? LIMIT 1 [["id", 4]] CACHE (0.0ms) SELECT "plans".* FROM "plans" WHERE "plans"."id" = ? LIMIT 1 ["id", 4] begin transaction Plan Exists (0.2ms) SELECT 1 AS one FROM "plans" WHERE ("plans"."stripe_id" = 'free' AND "plans"."id" != 4) LIMIT 1 User Exists (0.1ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = 'morganric+freeer@gmail.com' LIMIT 1 SQL (0.3ms) INSERT INTO "users" ("email", "encrypted_password", "plan_id", "role", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["email", "morganric+freeer@gmail.com"], ["encrypted_password", "$2a$10$uJ5LPHKg.kWOUPjRjeuo3eNSKkSz1rGLQmWoS4rd9aEGXGRS55DDy"], ["plan_id", 4], ["role", 5], ["created_at", "2015-07-20 19:45:41.372302"], ["updated_at", "2015-07-20 19:45:41.372302"]] User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 4]] SQL (0.1ms) INSERT INTO "profiles" ("user_id", "created_at", "updated_at") VALUES (?, ?, ?) ["user_id", 4], ["created_at", "2015-07-20 19:45:41.375129"], ["updated_at", "2015-07-20 19:45:41.375129"] commit transaction (0.1ms) begin transaction SQL (0.4ms) UPDATE "users" SET "last_sign_in_at" = ?, "current_sign_in_at" = ?, "last_sign_in_ip" = ?, "current_sign_in_ip" = ?, "sign_in_count" = ?, "updated_at" = ? WHERE "users"."id" = ? ["last_sign_in_at", "2015-07-20 19:45:41.386955"], ["current_sign_in_at", "2015-07-20 19:45:41.386955"], ["last_sign_in_ip", "127.0.0.1"], ["current_sign_in_ip", "127.0.0.1"], ["sign_in_count", 1], ["updated_at", "2015-07-20 19:45:41.388375"], ["id", 4] commit transaction (0.1ms) begin transaction Payola::Subscription Exists (0.2ms) SELECT 1 AS one FROM "payola_subscriptions" WHERE "payola_subscriptions"."guid" IS NULL LIMIT 1 (0.1ms) rollback transaction (0.1ms) begin transaction Plan Exists (0.2ms) SELECT 1 AS one FROM "plans" WHERE ("plans"."stripe_id" = 'free' AND "plans"."id" != 4) LIMIT 1 (0.1ms) commit transaction Completed 400 Bad Request in 135ms (Views: 0.3ms | ActiveRecord: 11.9ms)

archonic commented 9 years ago

Note Unpermitted parameter: name. You'll have to permit that parameter if you want Users to have a name attribute.

Registering users with this app depends upon the gem Payola to create the associated subscriber on Stripe. Despite the plan being free, the way Payola is currently built assumes that you will never create a subscriber without a credit card. If you supply credit card info on the form (like the app came with), those errors shouldn't be present. But obviously you want to create users on a free plan without taking a valid credit card.

I have a pull request open on Payola which lets you do just that. No promises but I think I'll get it merged this week (just need to write a test). I'm curious - let me know what happens when replacing the payola gem with this:

gem 'payola', :git => "git://github.com/archonic/payola.git", :branch => "subscriptions-free"

I believe the email can't be blank error is just because the rescue is being nonspecific. It looks ok to me.

kathyonu commented 9 years ago

Also, Name does not exist as a User attribute in the database, this from the db/schema.rb line 123 :

create_table "users", force: :cascade do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    ... 

It has to be migrated in and coded in to work.

morganric commented 9 years ago

@archonic @kathyonu Thanks both!

I've added the :name attribute to permitted attributes of the registrations controller.

@kathyonu name is in my users table in the schema @archonic I've added your gem / branch gem 'payola-payments', :git => "git://github.com/archonic/payola.git", :branch => "subscriptions-free"

replacing the payloa-payments in test, development.

I now just get the following error message:

{ guid: null, status: "pending", error: "Email can't be blank" }

Incidentally, the registration actually works and if I navigate to root I am signed in with a free account.

morganric commented 9 years ago

These now seems to be the pertinent lines from the trace...

Payola::Subscription Exists (0.8ms) SELECT 1 AS one FROM "payola_subscriptions" WHERE "payola_subscriptions"."guid" IS NULL LIMIT 1 (0.3ms) rollback transaction (0.2ms) begin transaction Plan Exists (0.3ms) SELECT 1 AS one FROM "plans" WHERE ("plans"."stripe_id" = 'free' AND "plans"."id" != 4) LIMIT 1 (0.3ms) commit transaction Completed 400 Bad Request in 48885ms (Views: 1.0ms | ActiveRecord: 147.2ms)

morganric commented 9 years ago

@archonic

Got it to work but had to hack through the registrations controller and bypass the subscribe method with the hard coded free plan...

def subscribe

# return if resource.admin?

if resource.role == "free" || resource.admin?
  redirect_to root_path and return
else

  params[:plan] = current_user.plan
  subscription = Payola::CreateSubscription.call(params, current_user)
  current_user.save
  render_payola_status(subscription)
end

end

archonic commented 9 years ago

Sorry my instructions weren't complete. The guid issue should be solved by changing the class on the registration form, which will have it use the right form handler. Try changing the form class in app\views\devise\registrations.html.haml to payola-register-form.

Then you should be able to go back to this subscribe method:

def subscribe
  return if resource.admin?
  params[:plan] = current_user.plan
  subscription = Payola::CreateSubscription.call(params, current_user)
  current_user.save
  render_payola_status(subscription)
end
archonic commented 8 years ago

@morganric My pull request was merged in payola so the solution here will work without specifying a branch in your gemfile. I believe this issue can now be closed?

DanielKehoe commented 8 years ago

Great!