Stripe Plan ID not saving to user

97 views Asked by At

I'm building a rails app that involves signing up customers to different plans. I'm using Rails and the gems Devise and Stripe, and Figaro. I have three plans, plan_id 1 is going to be admin and is free. plan_id 2 is basic, and free, and will allow customers to view the site but not interact, and plan_id 3 is subcription based. All forms work fine, and I am able to sign-up but in the console none of the users have plan_id's assigned to them? Their email addresses and passwords are stored but nothing else. Please help, i can't spot the problem!

PagesController

class PagesController < ApplicationController
    def home
        @admin_plan = Plan.find(1)
        @partner_plan = Plan.find(2)
        @paidsubscription_plan = Plan.find(3)
    end
end

RegistrationsController

class Users::RegistrationsController < Devise::RegistrationsController

  before_filter :select_plan, only: :new

  def create
    super do |resource|
      if params[:plan]
        resource.plan_id = params[:plan]
        if resource.plan_id == 3
          resource.save_with_payment
        else
          resource.save
        end
      end
    end
  end

  private  
  def select_plan
    unless params[:plan] && (params[:plan] == '1' || params[:plan] == '2' || params[:plan] == '3')
      flash[:notice] = "Please select a valid membership plan."
      redirect_to root_url
    end 
  end
end

models/user.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

   belongs_to :plan
   attr_accessor :stripe_card_token
   def save_with_payment
    if valid?
      customer = Stripe::Customer.create(description: email, plan: plan_id, card: stripe_card_token)
      self.stripe_customer_token = customer.id
      save!
    end
  end
end

schema.rb

ActiveRecord::Schema.define(version: 20150820123651) do

  create_table "plans", force: :cascade do |t|
    t.string   "name"
    t.decimal  "price"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "users", force: :cascade do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
    t.integer  "plan_id"
    t.string   "stripe_customer_token"
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true
  add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

end

/views/devise/registrations/new.html.erb

<div class="row">
  <div class="col-md-4 col-md-offset-4">
    <div class="well">
      <% if params[:plan] == '3' %>
        <%= render 'paid' %>
      <% else %>
        <%= render 'basic' %>
      <% end %>


      <%= render "devise/shared/links" %>
    </div>
  </div>
</div>

a basic ../views/pages/home

<h1>Home Page</h1>

<div class="col-md-4">
   <%= link_to "Admin Sign-Up", new_user_registration_path(plan: @admin_plan.id), class: "btn btn-info btn-lg btn-block" %> 
</div>
<div class="col-md-4">
   <%= link_to "Partner Sign-Up", new_user_registration_path(plan: @partner_plan.id), class: "btn btn-warning btn-lg btn-block" %> 
</div> 
<div class="col-md-4">
   <%= link_to "Paid Subscription Sign-Up", new_user_registration_path(plan: @paidsubscription_plan.id), class: "btn btn-success btn-lg btn-block" %> 
</div>

/views/devise/registrations/_paid.html.erb

<h2>Sign up </h2>
      <%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
        <%= devise_error_messages! %>

        <div class="field form-group">
            <%= f.label :email %><br />
            <%= f.email_field :email, autofocus: true, class: "form-control" %>
        </div>

        <div class="field form-group">
            <%= f.label :password %>
            <% if @minimum_password_length %>
            <em>(<%= @minimum_password_length %> characters minimum)</em>
            <% end %><br />
            <%= f.password_field :password, autocomplete: "off", class: "form-control" %>
        </div>

        <div class="field form-group">
            <%= f.label :password_confirmation %><br />
            <%= f.password_field :password_confirmation, autocomplete: "off", class: "form-control" %>
        </div>

        <div class="form-group">
            <%= label_tag :card_number, "Credit Card Number" %>
            <%= text_field_tag :card_number, nil, name: nil, class: "form-control" %>
        </div>

        <div class="form-group">
            <%= label_tag :card_code, "Security Code on Card (CVV)" %>
            <%= text_field_tag :card_code, nil, name: nil, class: "form-control" %>
        </div>

        <div class="form-group">
            <%= label_tag :card_month, "Card Expiration" %>
            <%= select_month nil, {add_month_numbers: true}, {name: nil, id: "card_month"}%>
            <%= select_year nil, {start_year: Date.today.year, end_year: Date.today.year+15}, {name: nil, id: "card_year"}%>
        </div>

        <div class="actions form-group">
          <%= f.submit "Sign up", class: "btn btn-info" %>
        </div>
      <% end %>

/views/devise/registrations/_basic.html.erb

<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
        <%= devise_error_messages! %>

        <div class="field form-group">
          <%= f.label :email %><br />
          <%= f.email_field :email, autofocus: true, class: "form-control" %>
        </div>

        <div class="field form-group">
          <%= f.label :password %>
          <% if @minimum_password_length %>
          <em>(<%= @minimum_password_length %> characters minimum)</em>
          <% end %><br />
          <%= f.password_field :password, autocomplete: "off", class: "form-control" %>
        </div>

        <div class="field form-group">
          <%= f.label :password_confirmation %><br />
          <%= f.password_field :password_confirmation, autocomplete: "off", class: "form-control" %>
        </div>

        <div class="actions form-group">
          <%= f.submit "Sign up", class: "btn btn-info" %>
        </div>
      <% end %>

routes.rb

Rails.application.routes.draw do
  devise_for :users, controllers: { registrations: 'users/registrations' }

  root 'pages#home'

/config/application.yml

stripe_api_key: sk_test_bCKoKAs1o2jciSJ6Egi4i5tr
stripe_publishable_key: pk_test_q8xRzIcHxx2NXmAhb2yxKxv5
#
# production:
stripe_api_key: sk_test_bCKoKAs1o2jciSJ6Egi4i5tr
stripe_publishable_key: pk_test_q8xRzIcHxx2NXmAhb2yxKxv5

  • edit *

application_controller.rb

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  before_filter :configure_permitted_parameters, if: :devise_controller?

  protected
    def configure_permitted_parameters
      devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:name, :stripe_card_token, :email, :password, :password_confirmation) }
    end
end

and below is my server when trying to sign up a basic member

Started POST "/users" for 124.149.46.152 at 2015-08-21 02:39:09 +0000
Cannot render console from 124.149.46.152! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by Users::RegistrationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"b70CPfotJ27nM7Sz6SXs1iZzzct3gJcouickmCRgaFdF8N/tI/5XuIXgu0dXnOysifkaRIvjfGxFV6lx9UcbNA==", "user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
   (0.1ms)  begin transaction
  User Exists (0.3ms)  SELECT  1 AS one FROM "users" WHERE "users"."email" = '[email protected]' LIMIT 1
  SQL (0.6ms)  INSERT INTO "users" ("email", "encrypted_password", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["email", "[email protected]"], ["encrypted_password", "$2a$10$DMHp9/uWFU/ChVX5s485deASl9T4x48YUVZ6QgJpsw2O9M.A4R6PS"], ["created_at", "2015-08-21 02:39:09.274684"], ["updated_at", "2015-08-21 02:39:09.274684"]]
   (9.5ms)  commit transaction
   (0.1ms)  begin transaction
  SQL (0.7ms)  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-08-21 02:39:09.288182"], ["current_sign_in_at", "2015-08-21 02:39:09.288182"], ["last_sign_in_ip", "124.149.46.152"], ["current_sign_in_ip", "124.149.46.152"], ["sign_in_count", 1], ["updated_at", "2015-08-21 02:39:09.289989"], ["id", 6]]
   (11.8ms)  commit transaction
Redirected to https://socialplayground-portal-runpixelrun.c9.io/
Completed 302 Found in 191ms (ActiveRecord: 23.1ms)


Started GET "/" for 124.149.46.152 at 2015-08-21 02:39:09 +0000
Cannot render console from 124.149.46.152! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by PagesController#home as HTML
  Plan Load (0.3ms)  SELECT "plans".* FROM "plans"
  Rendered pages/home.html.erb within layouts/application (0.6ms)
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["id", 6]]
Completed 200 OK in 149ms (Views: 146.9ms | ActiveRecord: 0.6ms)

thanks

0

There are 0 answers