Last active
December 30, 2015 17:48
-
-
Save raderj89/7862939 to your computer and use it in GitHub Desktop.
my horrible awful stripe/signup integration
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="jumbotron center"> | |
<h1 class="center-header">Blocipedia</h1> | |
<h2 class="center-header">Social, Markdown Wikis</h2> | |
<br> | |
<div class="row"> | |
<div class="col-sm-5"> | |
<h3>Free Account</h3> | |
<ul> | |
<li>Create unlimited public wikis</li> | |
<li>Collaborate with unlimited users</li> | |
</ul> | |
<%= link_to "Sign up now!", new_user_registration_path(plan: @free_plan.id), class: 'btn btn-default btn-lg btn-block' %> | |
</div> | |
<div class="col-sm-5 col-sm-offset-2"> | |
<h3>Premium Account</h3> | |
<ul> | |
<li>All the benefits of basic</li> | |
<li>Unlimited private wikis</li> | |
</ul> | |
<%= link_to "Sign up now!", new_user_registration_path(plan: @premium_plan.id), class: 'btn btn-success btn-lg btn-block' %> | |
</div> | |
</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
<!-- registrations/new.html.erb --> | |
<div class="panel panel-default"> | |
<div class="panel-heading"> | |
<h1>Sign up with <%#= @plan.name %>Plan</h1> | |
</div> | |
<div class="panel-body"> | |
<% if params[:plan] == "2" %> | |
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %> | |
<%#= devise_error_messages! %> | |
<%= hidden_field_tag 'plan', params[:plan] %> | |
<div class="form-group"> | |
<%= f.label :username %> | |
<%= f.text_field :username, autofocus: true, class: "form-control" %> | |
</div> | |
<div class="form-group"> | |
<%= f.label :email %> | |
<%= f.email_field :email, class: "form-control" %> | |
</div> | |
<div class="form-group"> | |
<%= f.label :password %> | |
<%= f.password_field :password, class: "form-control" %> | |
</div> | |
<div class="form-group"> | |
<%= f.label :password_confirmation %> | |
<%= f.password_field :password_confirmation, class: "form-control" %> | |
</div> | |
<%= f.hidden_field :stripe_card_token %> | |
<div class="form-group"> | |
<%= label_tag :card_number, "Credit Card Number" %> | |
<%= text_field_tag :card_number, nil, name: nil %> | |
</div> | |
<div class="form-group"> | |
<%= label_tag :card_code, "Security Code on Card (CVV)" %> | |
<%= text_field_tag :card_code, nil, name: nil %> | |
</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 "Sign up", class: "btn btn-primary" %> | |
</div> | |
<% end %> | |
</div> | |
<% else %> | |
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), html: { id: "free_user"}) do |f| %> | |
<%#= devise_error_messages! %> | |
<%= hidden_field_tag 'plan', params[:plan] %> | |
<div class="form-group"> | |
<%= f.label :username %> | |
<%= f.text_field :username, autofocus: true, class: "form-control" %> | |
</div> | |
<div class="form-group"> | |
<%= f.label :email %> | |
<%= f.email_field :email, class: "form-control" %> | |
</div> | |
<div class="form-group"> | |
<%= f.label :password %> | |
<%= f.password_field :password, class: "form-control" %> | |
</div> | |
<div class="form-group"> | |
<%= f.label :password_confirmation %> | |
<%= f.password_field :password_confirmation, class: "form-control" %> | |
</div> | |
<div class="form-group"> | |
<%= f.submit "Sign up", class: "btn btn-primary" %> | |
</div> | |
<% end %> | |
<% end %> | |
<div class="panel-footer"> | |
<%= render "devise/shared/links" %> | |
</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 PagesController < ApplicationController | |
before_filter :setup, only: [:home] | |
def home | |
@free_plan | |
@premium_plan | |
end | |
def about | |
end | |
private | |
def setup | |
plans = Plan.all | |
plans.each do |plan| | |
if plan.id == 1 | |
@free_plan = plan | |
else | |
@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
class Plan < ActiveRecord::Base | |
attr_accessible :name, :price | |
has_many :users | |
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: -> | |
$('#new_user').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 | |
$('#user_stripe_card_token').val(response.id) | |
$('#new_user')[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
class Users::RegistrationsController < Devise::RegistrationsController | |
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 | |
end | |
binding.pry | |
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
class User < ActiveRecord::Base | |
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 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment