Skip to content

Instantly share code, notes, and snippets.

@coderforhire
Created September 8, 2012 22:03
Show Gist options
  • Save coderforhire/3680146 to your computer and use it in GitHub Desktop.
Save coderforhire/3680146 to your computer and use it in GitHub Desktop.
<div class="account-container register">
<div class="content clearfix">
<h1>Create Your Account</h1>
<div class="login-fields">
<%= form_for(@subscription) do |f| %>
<%= f.hidden_field :stripe_card_token %>
<%= text_field_tag :card_number, nil, name: nil, :placeholder => "Card Number" %></br>
<%= text_field_tag :card_code, nil, name: nil, :placeholder => "CVV Code" %></br>
<h3>Expiration:</h3>
<%if @user.errors.any? %>
<% @user.errors.full_messages.each do |msg| %>
<li><%= msg %></li></br>
<% end %>
<% end %>
<%= select_month nil, {add_month_numbers: true}, {name: nil, id: "card_month"} %></br>
<%= select_year nil, {start_year: Date.today.year, end_year: Date.today.year+15}, {name: nil, id: "card_year"} %></br>
<h3>Plan:</h3>
<%= f.select :plan, {"19.99/mo" => '1', "25.99/mo" => '2', "59.99/mo" => '3'}, :selected => params[:plan] %>
<%= text_field :user, :email, :placeholder => "Email" %></br>
<%= password_field :user, :password , :placeholder => "Password" %></br>
<%= password_field :user, :password_confirmation, :placeholder => "Confirm Password" %></br>
<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>
<% if @subscription.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@subscription.errors.count, "error") %> prohibited this purchase from being saved:</h2>
<ul>
<% @subscription.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
</div> <!-- /login-fields -->
<div class="login-actions">
<span class="login-checkbox">
<input id="Field" name="Field" type="checkbox" class="field login-checkbox" value="First Choice" tabindex="4" />
<label class="choice" for="Field">I have read and agree with the Terms of Use.</label>
</span>
<%= f.submit "Get me a database.", :class => "button btn btn-primary btn-large" %>
<% end %>
</div>
</div> <!-- /content -->
</div> <!-- /account-container -->
<!-- Text Under Box -->
class Subscription < ActiveRecord::Base
belongs_to :user
attr_accessible :user_id, :stripe_card_token
def save_with_payment(plan, email)
if valid?
customer = Stripe::Customer.create(description: "#{email}", plan: "#{plan}", 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
~
def create
plan = params[:subscription][:plan]
params[:subscription].delete(:plan)
@subscription = Subscription.new(params[:subscription])
@subscription.plan = plan
@user = User.new(params[:user])
respond_to do |format|
if @subscription.save_with_payment(@subscription.plan, @user.email) && @user.save
@subscription.user_id = @user.id
@subscription.save
format.html { redirect_to pages_thankyou_url }
else
format.html { render action: "new" }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment