Skip to content

Instantly share code, notes, and snippets.

@Showtimes
Created August 6, 2012 19:38
Show Gist options
  • Save Showtimes/3277867 to your computer and use it in GitHub Desktop.
Save Showtimes/3277867 to your computer and use it in GitHub Desktop.
Stripe Gist Supscription Model
class Subscription < ActiveRecord::Base
# Associations
belongs_to :user
has_many :payments, :dependent => :destroy
has_many :fulfillments, :dependent => :destroy
# Accessibles
attr_accessible :plan_type, :major_focus, :minor_focus, :start_date
# Validations
#validates :user, :presence => true
def self.create_subscription(amount, interval, name, id)
Stripe::Plan.create(
:amount => amount,
:interval => interval,
:name => name,
:currency => "usd",
:id => id
)
=begin
# Example
Stripe::Plan.create(
:amount => 27600,
:interval => 'year',
:name => 'Yearly',
:currency => "usd",
:id => 'yearly'
)
=end
end
def self.plans
[
{:value => Subscription.MONTHLY,
:desc => "Subscribe month-to-month",
:price => "$30/month",
:checked => true},
{:value => Subscription.YEARLY,
:desc => "Subscribe for a year in advance",
:price => "$276/year (or $23/month)",
:checked => false}
]
end
def next_payment_due
if !(most_recent = self.most_recent_payment)
Date.today
else
if self.plan_type == Subscription.MONTHLY
most_recent.received_on + Subscription.MONTH_PERIOD
elsif self.plan_type == Subscription.YEARLY
most_recent.received_on + Subscription.YEAR_PERIOD
else
raise "You subscribed to a plan type that isn't monthly or yearly"
end
end
end
def overdue_payment?
#stub
end
def next_fulfillment_due
if !(most_recent = self.most_recent_fulfillment)
next_fulfillment = Date.today + 2.days
else
next_fulfillment = most_recent.ship_date + 1.month + 2.days
end
end
def most_recent_payment
self.payments.find(:first, :order => 'received_on DESC')
end
def most_recent_fulfillment
self.fulfillments.find(:first, :order => 'ship_date DESC')
end
def as_hash
{
:id => id,
:plan => plan_type.capitalize,
:charge => "",
:address => "#{user.address.line1}, #{user.address.city}, #{user.address.state}, zipcode #{user.address.zipcode}",
:name => user.name,
:box => BoxOfTheMonth.current_box.name,
:user_id => user.id,
:type => "subscription"
}
end
class << self
def due_today_monthly
self.due_today(self.MONTH_PERIOD)
end
def due_today_yearly
self.due_today(self.YEAR_PERIOD)
end
def due_today(period)
self.joins(:payments).where('payments.received_on=?', Date.today - period)
end
def deliver_today
if self.joins(:fulfillments).empty?
return self.all
else
shipping = Date.today - 10.years
self.joins(:fulfillments).each do |subscription|
subscription.fulfillments.each do |fulfillment|
if shipping < fulfillment.ship_date
shipping = fulfillment.ship_date
end
end
end
if shipping == Date.today - 1.month - 2.days
return self.joins(:fulfillments).where("fulfillments.ship_date = ?", Date.today - 1.month - 2.days)
end
end
end
def MONTH_PERIOD
1.month
end
def YEAR_PERIOD
1.year
end
def MONTHLY
"monthly"
end
def YEARLY
"yearly"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment