Last active
December 30, 2015 07:59
-
-
Save raderj89/7799615 to your computer and use it in GitHub Desktop.
my horrible awful subscription solution
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
<h1>Select a plan</h1> | |
<div class="row"> | |
<%= form_for @subscription, url: user_subscriptions_path, method: :post, html: { class: 'form-horizontal' } do %> | |
<div class="col-sm-4"> | |
<%= hidden_field_tag :stripe_card_token %> | |
<div class="form-group"> | |
<%= label_tag "Premium" %> | |
<%= radio_button_tag :plan_id, @premium_plan.id, false %> | |
</div> | |
<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: 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="row"> | |
<%= submit_tag 'Choose Plan' %> | |
</div> | |
</div> | |
</div> | |
<% 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 :subscriptions | |
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
Plan.create!(name: "Free", price: 0) | |
Plan.create!(name: "Premium", price: 5) |
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 Subscription < ActiveRecord::Base | |
belongs_to :plan | |
belongs_to :user | |
attr_accessible :plan_id, :stripe_customer_token | |
attr_accessor :stripe_card_token | |
validates_presence_of :plan_id | |
# validates_presence_of :stripe_card_token | |
def save_with_payment | |
if valid? | |
customer = Stripe::Customer.create(description: user.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." | |
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
# Place all the behaviors and hooks related to the matching controller here. | |
# All this logic will automatically be available in application.js. | |
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/ | |
jQuery -> | |
Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content')) | |
subscription.setupForm() | |
subscription = | |
setupForm: -> | |
$('#new_subscription').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 | |
$('#stripe_card_token').val(response.id) | |
$('#new_subscription')[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 SubscriptionsController < ApplicationController | |
before_filter :authenticate_user! # TODO: let user know they still need to confirm their account | |
before_filter :setup | |
def new | |
@subscription = current_user.build_subscription | |
end | |
def create | |
@plan_id = params[:plan_id] | |
@subscription = current_user.create_subscription(plan_id: @plan_id) | |
if @subscription.save_with_payment | |
flash[:success] = "You're signed up with the #{@subscription.plan.name} plan!" | |
redirect_to root_path | |
else | |
flash[:notice] = "There was an error." | |
render :new | |
end | |
end | |
private | |
def setup | |
@plans = Plan.all | |
@plans.each do |plan| | |
if plan.id == 1 | |
@free_plan = plan | |
else | |
@premium_plan = plan | |
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 | |
# Include default devise modules. Others available are: | |
# :confirmable, :lockable, :timeoutable and :omniauthable | |
devise :database_authenticatable, :registerable, | |
:recoverable, :rememberable, :trackable, :confirmable, :validatable | |
# Setup accessible (or protected) attributes for your model | |
attr_accessible :username, :email, :password, :password_confirmation, :remember_me, :subscription, :role #actually don't need role | |
has_many :wikis | |
has_many :wiki_collaborations | |
has_many :shared_wikis, through: :wiki_collaborations, source: :wiki | |
has_one :subscription | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment