Created
August 5, 2011 01:27
-
-
Save allenp/1126742 to your computer and use it in GitHub Desktop.
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 PaymentsController < ApplicationController | |
helper_method :time_ago_or_time_stamp | |
def new | |
@plan = Plan.find(params[:plan_id]) | |
@user = current_user | |
@amount = @plan.amount | |
end | |
def cancel_subscription | |
user = current_user | |
customer = nil | |
if user.customer_id | |
begin | |
customer = Stripe::Customer.retrieve user.customer_id | |
rescue Stripe::InvalidRequestError | |
end | |
end | |
if customer | |
cu = customer.cancel_subscription | |
user.active_subscription = false | |
user.save | |
comp_sub = Subscription.find(:first, | |
:conditions => ["user_id =?", user.id ], | |
:order => "created_at DESC") | |
comp_sub.status = 'cancelled' | |
comp_sub.save | |
end | |
end | |
def confirm | |
if params[:stripeToken].empty? | |
#display an error | |
end | |
#used to store all the things i will pass to the view | |
@viewdata = Struct.new(:type, | |
:period_start, | |
:period_end, | |
:newplan, | |
:oldplan, | |
:user, | |
:success, | |
:message).new(:success => false) | |
plan = Plan.find(params[:plan]['id']) | |
user = current_user | |
customer = nil | |
if user.customer_id | |
begin | |
customer = Stripe::Customer.retrieve user.customer_id | |
rescue Stripe::InvalidRequestError | |
end | |
end | |
if customer #person is an existing customer at stripe and SHOULD have recurring subscription setup | |
begin | |
stripe_sub = customer.update_subscription( | |
:plan => plan.id, | |
:prorate => true ) | |
rescue Exception => msg | |
@viewdata.success = false | |
@viewdata.message = msg | |
return | |
end | |
comp_sub = Subscription.new | |
comp_sub.plan_id = plan.id | |
comp_sub.user_id = user.id | |
comp_sub.period_start = stripe_sub.current_period_start | |
comp_sub.period_end = stripe_sub.current_period_end | |
comp_sub.save | |
if !plan.id.eql? user.plan_id | |
begin | |
@viewdata.oldplan = Plan.find user.plan_id | |
@viewdata.oldplan = @viewdata.oldplan.name | |
rescue | |
#no plan before, that's fine shouldn't die | |
@viewdata.oldplan = '' | |
end | |
else | |
@viewdata.oldplan = '' | |
end | |
user.active_subscription = true | |
user.plan_id = plan.id | |
user.save | |
@viewdata.type = 'change' | |
@viewdata.user = user | |
@viewdata.newplan = plan | |
@viewdata.period_start = comp_sub.period_start | |
@viewdata.period_end = comp_sub.period_end | |
else #first time user is paying, this creates a recurring subscription in stripe | |
stripe_customer = Stripe::Customer.create( | |
:email => user.email, | |
:description => params[:customer]['first_name'] + ' ' + params[:customer]['last_name'], | |
:plan => plan.id, | |
:validate => true, | |
:card => params[:stripeToken]) | |
comp_sub = Subscription.new | |
comp_sub.plan_id = plan.id | |
comp_sub.user_id = user.id | |
comp_sub.period_start = stripe_customer['subscription'].current_period_start | |
comp_sub.period_end = stripe_customer['subscription'].current_period_end | |
comp_sub.save | |
invoice = Invoice.new | |
invoice.description = 'Payment for plan: ' + plan.name | |
invoice.total = plan.amount | |
invoice.asat = Time.now | |
invoice.user_id = user.id | |
invoice.save | |
#get invoice items here | |
user.customer_id = stripe_customer.id | |
user.plan_id = plan.id | |
user.active_subscription = true | |
user.save | |
@viewdata.type = 'new' | |
@viewdata.user = user | |
@viewdata.newplan = plan | |
@viewdata.oldplan = '' | |
@viewdata.period_start = comp_sub.period_start | |
@viewdata.period_end = comp_sub.period_end | |
end | |
end | |
def plans | |
#nothing to put here | |
end | |
def history | |
user = current_user | |
@view = Struct.new(:lastinvoice, | |
:nextinvoice, | |
:last10invoices, | |
:subscription, | |
:activated, | |
:user).new | |
@view.user = user | |
@view.lastinvoice = Invoice.find(:first, | |
:conditions => [ "user_id =?", user.id ], | |
:order => "created_at DESC") | |
@view.subscription = Subscription.find(:first, | |
:conditions => ["user_id =?", user.id ], | |
:order => "created_at DESC") | |
fsub = Subscription.find(:first, | |
:conditions => ["user_id =?", user.id ], | |
:order => "created_at ASC") | |
if fsub | |
@view.activated = time_ago_or_time_stamp Time.at(fsub.period_start), Time.now, false | |
end | |
@view.last10invoices = Invoice.find(:all, | |
:conditions => ["user_id =?", user.id], | |
:limit => 10, :offset => 0, :order => "created_at DESC") | |
begin | |
@view.nextinvoice = Stripe::Invoice.upcoming( | |
:customer => user.customer_id) | |
p @view.nextinvoice.lines | |
rescue Stripe::InvalidRequestError | |
@view.nextinvoice = nil | |
end | |
end | |
def time_ago_or_time_stamp(from_time, to_time = Time.now, include_seconds = true, detail = false) | |
from_time = from_time.to_time if from_time.respond_to?(:to_time) | |
to_time = to_time.to_time if to_time.respond_to?(:to_time) | |
distance_in_minutes = (((to_time - from_time).abs)/60).round | |
distance_in_seconds = ((to_time - from_time).abs).round | |
case distance_in_minutes | |
when 0..1 then time = (distance_in_seconds < 60) ? "#{distance_in_seconds} ago" : '1 minute ago' | |
when 2..59 then time = "#{distance_in_minutes} minutes ago" | |
when 60..90 then time = "1 hour ago" | |
when 90..1440 then time = "#{(distance_in_minutes.to_f / 60.0).round} hours ago" | |
when 1440..2160 then time = '1 day ago' # 1-1.5 days | |
when 2160..2880 then time = "#{(distance_in_minutes.to_f / 1440.0).round} days ago" # 1.5-2 days | |
else time = from_time.strftime("%a, %d %b %Y") | |
end | |
return time_stamp(from_time) if (detail && distance_in_minutes > 2880) | |
return time | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment