Created
December 16, 2012 17:13
-
-
Save daluzluiz/4309700 to your computer and use it in GitHub Desktop.
This file contains 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
<h2>Sign up</h2> | |
<div id="stripe_error" class="alert alert-error" style="display:none" > | |
</div> | |
<%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => {:class => 'card_form form-vertical' }) do |f| %> | |
<h3><%= params[:plan].titleize if params[:plan] %> Subscription Plan</h3> | |
<%= hidden_field_tag 'plan', params[:plan] %> | |
<%= f.error_notification %> | |
<%= f.input :f_name, :autofocus => true %> | |
<%= f.input :l_name %> | |
<%= f.input :email, :required => true %> | |
<%= f.input :password, :required => true %> | |
<%= f.input :password_confirmation, :required => true %> | |
<% if @user.stripe_token %> | |
Credit card has been accepted. | |
<% else %> | |
<div class="field"> | |
<%= label_tag :card_number, "Credit Card Number" %> | |
<%= text_field_tag :card_number, nil, name: nil %> | |
</div> | |
<div class="field"> | |
<%= label_tag :card_code, "Card Security Code (CVV)" %> | |
<%= text_field_tag :card_code, nil, name: nil %> | |
</div> | |
<div class="field"> | |
<%= 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+10}, {name: nil, id: "card_year"}%> | |
</div> | |
<div class="field"> | |
<%= f.input :coupon %> | |
</div> | |
<% end %> | |
<%= f.hidden_field :stripe_token %> | |
<%= f.button :submit, 'Sign up', :class => 'btn-primary' %> | |
<% end %> |
This file contains 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 | |
rolify | |
# Include default devise modules. Others available are: | |
# :token_authenticatable, :confirmable, | |
# :lockable, :timeoutable and :omniauthable | |
devise :database_authenticatable, :registerable, | |
:recoverable, :rememberable, :trackable, :validatable | |
# Setup accessible (or protected) attributes for your model | |
attr_accessible :name, :email, :password, :password_confirmation, :remember_me, :stripe_token, :coupon | |
attr_accessor :stripe_token, :current_password, :coupon | |
before_save :update_stripe | |
before_destroy :cancel_subscription | |
def update_plan(role) | |
self.role_ids = [] | |
self.add_role(role.name) | |
unless customer_id.nil? | |
customer = Stripe::Customer.retrieve(customer_id) | |
customer.update_subscription(:plan => role.name) | |
end | |
true | |
rescue Stripe::StripeError => e | |
logger.error "Stripe Error: " + e.message | |
errors.add :base, "Unable to update your subscription. #{e.message}." | |
false | |
end | |
def update_stripe | |
return if email.include?('@example.com') and not Rails.env.production? | |
if customer_id.nil? | |
if !stripe_token.present? | |
raise "Stripe token not present. Can't create account." | |
end | |
customer = Stripe::Customer.create( | |
:email => email, | |
:description => name, | |
:card => stripe_token, | |
:plan => roles.first.name, | |
:coupon => coupon | |
) | |
else | |
customer = Stripe::Customer.retrieve(customer_id) | |
if stripe_token.present? | |
customer.card = stripe_token | |
end | |
customer.email = email | |
customer.description = name | |
customer.save | |
end | |
self.last_4_digits = customer.active_card.last4 | |
self.customer_id = customer.id | |
self.stripe_token = nil | |
rescue Stripe::StripeError => e | |
logger.error "Stripe Error: " + e.message | |
errors.add :base, "#{e.message}." | |
self.stripe_token = nil | |
false | |
end | |
def cancel_subscription | |
unless customer_id.nil? | |
customer = Stripe::Customer.retrieve(customer_id) | |
if (!customer.nil?) && (customer.subscription.status == 'active') | |
customer.cancel_subscription | |
end | |
end | |
rescue Stripe::StripeError => e | |
logger.error "Stripe Error: " + e.message | |
errors.add :base, "Unable to cancel your subscription. #{e.message}." | |
false | |
end | |
def expire | |
UserMailer.expire_email(self).deliver | |
destroy | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment