Last active
December 31, 2015 12:29
-
-
Save raderj89/7986832 to your computer and use it in GitHub Desktop.
trying to get account upgrades working
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="panel panel-default"> | |
<div class="panel-heading"> | |
<div class="panel-title"> | |
<h1>Edit <%= resource_name.to_s.humanize %></h1> | |
</div> | |
Current account: <%= resource.plan.name %> | |
</div> | |
<div class="panel-body"> | |
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %> | |
<%= devise_error_messages! %> | |
<div class="form-group"> | |
<%= f.label :email %> | |
<%= f.email_field :email, class: "form-control", :autofocus => true %> | |
</div> | |
<div class="form-group"> | |
<%= f.label :password %> <i>(leave blank if you don't want to change it)</i> | |
<%= f.password_field :password, class: "form-control", :autocomplete => "off" %> | |
</div> | |
<div class="form-group"> | |
<%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i> | |
<%= f.password_field :current_password, class: "form-control" %> | |
</div> | |
<div class="form-group"> | |
<%= f.submit "Update", class: "btn btn-primary" %> | |
</div> | |
<% end %> | |
<hr> | |
<% if resource.plan.name == "Free" %> | |
<h2>Upgrade to paid</h2> | |
<%= form_for(resource, :as => resource_name, :url => users_update_plan_path, :html => { id: 'upgrade-plan', :method => :put }) do |f| %> | |
<%= hidden_field_tag 'plan', @premium_plan.id %> | |
<%= hidden_field_tag 'email', resource.email %> | |
<%= f.hidden_field :stripe_card_token, id: 'new-token' %> | |
<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 id="stripe_error"> | |
<noscript>JavaScript is not enabled and is required for this form. First enable it in your web browser settings.</noscript> | |
</div> | |
<div class="form-group"> | |
<%= f.submit "Upgrade", class: "btn btn-primary" %> | |
</div> | |
<% end %> | |
<% end %> | |
</div> | |
<div class="panel-footer"> | |
<h3>Cancel my account</h3> | |
<p>Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), :data => { :confirm => "Are you sure?" }, :method => :delete, class: "btn btn-warning" %></p> | |
<%= link_to "Back", :back %> | |
</div> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Users::RegistrationsController < Devise::RegistrationsController | |
before_filter :setup, only: [:edit] | |
def update_plan | |
@user = current_user | |
@user.update_attributes(plan_id: params[:plan], email: params[:email]) | |
if @user.plan_id == 2 | |
@user.save_with_payment | |
redirect_to edit_user_registration_path, notice: "Updated plan!" | |
else | |
flash[:error] = "Unable to update plan." | |
render :edit | |
end | |
binding.pry | |
end | |
private | |
def build_resource(*args) | |
super | |
if params[:plan] | |
resource.plan_id = params[:plan] | |
if resource.plan_id == 2 | |
resource.save_with_payment | |
else | |
resource.save | |
end | |
binding.pry | |
end | |
end | |
def setup | |
plans = Plan.all | |
plans.each do |plan| | |
unless plan.id == 1 | |
@premium_plan = plan | |
end | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Blocipedia::Application.routes.draw do | |
devise_for :users, controllers: { registrations: 'users/registrations' } | |
devise_scope :user do | |
put 'users/update_plan', :to => 'users/registrations#update_plan' | |
end | |
resources :users, only: [:index, :show] | |
resources :wikis | |
resources :wiki_collaborations | |
root to: 'pages#home' | |
match "about" => 'pages#about' | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery -> | |
Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content')) | |
subscription.setupForm() | |
subscription = | |
setupForm: -> | |
$('#upgrade-plan').submit -> | |
$('input[type=submit]').prop('disabled', true) | |
subscription.processCard() | |
false | |
processCard: -> | |
card = | |
number: $('#card_number').val() | |
cvc: $('#card_code').val() | |
expMonth: $('#card_month').val() | |
expYear: $('#card_year').val() | |
Stripe.createToken(card, subscription.handleStripeResponse) | |
handleStripeResponse: (status, response) -> | |
if status == 200 | |
$('#new-token').val(response.id) | |
$('#upgrade-plan')[0].submit() | |
else | |
$('#stripe_error').text(response.error.message) | |
$('input[type=submit]').prop('disabled', false) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# == Schema Information | |
# | |
# Table name: users | |
# | |
# id :integer not null, primary key | |
# username :string(255) not null | |
# email :string(255) default(""), not null | |
# encrypted_password :string(255) default(""), not null | |
# reset_password_token :string(255) | |
# reset_password_sent_at :datetime | |
# remember_created_at :datetime | |
# sign_in_count :integer default(0), not null | |
# current_sign_in_at :datetime | |
# last_sign_in_at :datetime | |
# current_sign_in_ip :string(255) | |
# last_sign_in_ip :string(255) | |
# confirmation_token :string(255) | |
# confirmed_at :datetime | |
# confirmation_sent_at :datetime | |
# unconfirmed_email :string(255) | |
# created_at :datetime not null | |
# updated_at :datetime not null | |
# | |
class User < ActiveRecord::Base | |
# Include default devise modules. Others available are: | |
# :confirmable, :lockable, :timeoutable and :omniauthable | |
devise :database_authenticatable, :registerable, | |
:recoverable, :rememberable, :trackable, :validatable #:confirmable, | |
attr_accessible :username, :email, :password, :password_confirmation, :remember_me, :subscription, | |
:stripe_customer_token, :plan_id, :stripe_card_token | |
has_many :wikis | |
has_many :wiki_collaborations | |
has_many :shared_wikis, through: :wiki_collaborations, source: :wiki | |
belongs_to :plan | |
validates_presence_of :plan_id | |
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 | |
rescue Stripe::InvalidRequestError => e | |
logger.error "Stripe error while creating customer: #{e.message}" | |
errors.add :base, "There was a problem with your credit card." | |
false | |
end | |
# def update_user_plan | |
# if valid? | |
# customer = Stripe::Customer.create(description: email, plan: plan_id, card: stripe_card_token) | |
# self.stripe_customer_token = customer.id | |
# save! | |
# end | |
# rescue Stripe::InvalidRequestError => e | |
# logger.error "Stripe error while creating customer: #{e.message}" | |
# errors.add :base, "There was a problem with your credit card." | |
# false | |
# end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment