Skip to content

Commit

Permalink
Merge pull request #184 from andrewculver/bullet-train
Browse files Browse the repository at this point in the history
Upgrade to Rails 5.1. Upgrade Stripe API. Add support for cancancan authorizations. (WIP)
  • Loading branch information
yas4891 authored Nov 18, 2017
2 parents 745f983 + f25a2fa commit ebbdbc2
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 85 deletions.
14 changes: 7 additions & 7 deletions app/concerns/koudoku/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Koudoku::Subscription
# client-side after storing the credit card information.
attr_accessor :credit_card_token

belongs_to :plan
belongs_to :plan, optional: true

# update details.
before_save :processing!
Expand Down Expand Up @@ -66,7 +66,7 @@ def processing!
prepare_for_upgrade

begin
raise Koudoku::NilCardToken, "Possible javascript error" if credit_card_token.empty?
raise Koudoku::NilCardToken, "No card token received. Check for JavaScript errors breaking Stripe.js on the previous page." unless credit_card_token.present?
customer_attributes = {
description: subscription_owner_description,
email: subscription_owner_email,
Expand Down Expand Up @@ -96,7 +96,7 @@ def processing!

# store the customer id.
self.stripe_id = customer.id
self.last_four = customer.cards.retrieve(customer.default_card).last4
self.last_four = customer.sources.retrieve(customer.default_source).last4

finalize_new_subscription!
finalize_upgrade!
Expand All @@ -123,18 +123,18 @@ def processing!

# fetch the customer.
customer = Stripe::Customer.retrieve(self.stripe_id)
customer.card = self.credit_card_token
customer.source = self.credit_card_token
customer.save

# update the last four based on this new card.
self.last_four = customer.cards.retrieve(customer.default_card).last4
self.last_four = customer.sources.retrieve(customer.default_source).last4
finalize_card_update!

end
end
end


def describe_difference(plan_to_describe)
if plan.nil?
if persisted?
Expand Down
1 change: 0 additions & 1 deletion app/controllers/koudoku/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ module Koudoku
class ApplicationController < ::ApplicationController
layout Koudoku.layout
helper :application

end
end
49 changes: 31 additions & 18 deletions app/controllers/koudoku/subscriptions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module Koudoku
class SubscriptionsController < ApplicationController
before_filter :load_owner
before_filter :show_existing_subscription, only: [:index, :new, :create], unless: :no_owner?
before_filter :load_subscription, only: [:show, :cancel, :edit, :update]
before_filter :load_plans, only: [:index, :edit]
before_action :load_owner
before_action :show_existing_subscription, only: [:index, :new, :create], unless: :no_owner?
before_action :load_subscription, only: [:show, :cancel, :edit, :update]
before_action :load_plans, only: [:index, :edit]

def load_plans
@plans = ::Plan.order(:display_order)
Expand All @@ -17,21 +17,22 @@ def unauthorized
def load_owner
unless params[:owner_id].nil?
if current_owner.present?

# we need to try and look this owner up via the find method so that we're
# taking advantage of any override of the find method that would be provided
# by older versions of friendly_id. (support for newer versions default behavior
# below.)

searched_owner = current_owner.class.find(params[:owner_id]) rescue nil

# if we couldn't find them that way, check whether there is a new version of
# friendly_id in place that we can use to look them up by their slug.
# in christoph's words, "why?!" in my words, "warum?!!!"
# (we debugged this together on skype.)
if searched_owner.nil? && current_owner.class.respond_to?(:friendly)
searched_owner = current_owner.class.friendly.find(params[:owner_id]) rescue nil
end

if current_owner.try(:id) == searched_owner.try(:id)
@owner = current_owner
else
Expand All @@ -50,21 +51,32 @@ def no_owner?
def load_subscription
ownership_attribute = :"#{Koudoku.subscriptions_owned_by}_id"
@subscription = ::Subscription.where(ownership_attribute => current_owner.id).find_by_id(params[:id])

# also, if cancan methods are available, we should use that to authorize.
if defined?(:can?)
return unauthorized unless can? :manage, @subscription
end

return @subscription.present? ? @subscription : unauthorized
end

# the following two methods allow us to show the pricing table before someone has an account.
# the following three methods allow us to show the pricing table before someone has an account.
# by default these support devise, but they can be overriden to support others.
def current_owner
# e.g. "self.current_user"
send "current_#{Koudoku.subscriptions_owned_by}"
end

def current_owned_through_or_by
# e.g. "self.current_user"
send "current_#{Koudoku.subscriptions_owned_through_or_by}"
end

def redirect_to_sign_up
# this is a Devise default variable and thus should not change its name
# when we change subscription owners from :user to :company
session["user_return_to"] = new_subscription_path(plan: params[:plan])
redirect_to new_registration_path(Koudoku.subscriptions_owned_by.to_s)
# when we change subscription owners from :user to :company
session["#{Koudoku.subscriptions_owned_through_or_by}_return_to"] = new_subscription_path(plan: params[:plan])
redirect_to new_registration_path(Koudoku.subscriptions_owned_through_or_by.to_s)
end

def index
Expand Down Expand Up @@ -94,7 +106,7 @@ def new
else
redirect_to_sign_up
end

else
raise I18n.t('koudoku.failure.feature_depends_on_devise')
end
Expand All @@ -112,13 +124,14 @@ def show_existing_subscription
end

def create

@subscription = ::Subscription.new(subscription_params)
@subscription.subscription_owner = @owner
@subscription.coupon_code = session[:koudoku_coupon_code]

if @subscription.save
flash[:notice] = after_new_subscription_message
redirect_to after_new_subscription_path
redirect_to after_new_subscription_path
else
flash[:error] = I18n.t('koudoku.failure.problem_processing_transaction')
render :new
Expand Down Expand Up @@ -150,7 +163,7 @@ def update

private
def subscription_params

# If strong_parameters is around, use that.
if defined?(ActionController::StrongParameters)
params.require(:subscription).permit(:plan_id, :stripe_id, :current_price, :credit_card_token, :card_type, :last_four)
Expand All @@ -160,15 +173,15 @@ def subscription_params
end

end

def after_new_subscription_path
return super(@owner, @subscription) if defined?(super)
owner_subscription_path(@owner, @subscription)
end

def after_new_subscription_message
controller = ::ApplicationController.new
controller.respond_to?(:new_subscription_notice_message) ?
controller.respond_to?(:new_subscription_notice_message) ?
controller.try(:new_subscription_notice_message) :
I18n.t('koudoku.confirmations.subscription_upgraded')
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ class Subscription < ActiveRecord::Base

<%= "attr_accessible :credit_card_token" if Rails::VERSION::MAJOR == 3 %>
belongs_to :<%= subscription_owner_model %>
belongs_to :coupon
belongs_to :coupon, optional: true

end
12 changes: 6 additions & 6 deletions lib/generators/koudoku/templates/config/initializers/koudoku.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
config.subscriptions_owned_by = :<%= subscription_owner_model %>
config.stripe_publishable_key = ENV['STRIPE_PUBLISHABLE_KEY']
config.stripe_secret_key = ENV['STRIPE_SECRET_KEY']
Stripe.api_version = '2015-01-11' #Making sure the API version used is compatible.

Stripe.api_version = '2017-08-15' # Making sure the API version used is compatible.
# config.prorate = false # Default is true, set to false to disable prorating subscriptions
# config.free_trial_length = 30

# Specify layout you want to use for the subscription pages, default is application
config.layout = 'application'

# you can subscribe to additional webhooks here
# we use stripe_event under the hood and you can subscribe using the
# stripe_event syntax on the config object:
# we use stripe_event under the hood and you can subscribe using the
# stripe_event syntax on the config object:
# config.subscribe 'charge.failed', Koudoku::ChargeFailed

end
38 changes: 23 additions & 15 deletions lib/koudoku.rb
Original file line number Diff line number Diff line change
@@ -1,56 +1,64 @@
require "koudoku/engine"
require "koudoku/errors"
require "generators/koudoku/install_generator"
require "generators/koudoku/views_generator"
require 'stripe_event'

module Koudoku
mattr_accessor :subscriptions_owned_by
@@subscriptions_owned_by = nil


mattr_accessor :subscriptions_owned_through
@@subscriptions_owned_through = nil

def self.subscriptions_owned_through_or_by
@@subscriptions_owned_through || @@subscriptions_owned_by
end

mattr_accessor :stripe_publishable_key
@@stripe_publishable_key = nil

mattr_accessor :stripe_secret_key
@@stripe_secret_key = nil

mattr_accessor :free_trial_length
@@free_trial_length = nil

mattr_accessor :prorate
@@prorate = true


@@layout = nil

def self.layout
@@layout || 'application'
end

def self.layout=(layout)
@@layout = layout
end

def self.webhooks_api_key=(key)
raise "Koudoku no longer uses an API key to secure webhooks, please delete the line from \"config/initializers/koudoku.rb\""
end

def self.setup
yield self

# Configure the Stripe gem.
Stripe.api_key = stripe_secret_key
end

# e.g. :users
def self.owner_resource
subscriptions_owned_by.to_s.pluralize.to_sym
end

# e.g. :user_id
def self.owner_id_sym
:"#{Koudoku.subscriptions_owned_by}_id"
end

# e.g. :user=
def self.owner_assignment_sym
:"#{Koudoku.subscriptions_owned_by}="
Expand All @@ -60,12 +68,12 @@ def self.owner_assignment_sym
def self.owner_class
Koudoku.subscriptions_owned_by.to_s.classify.constantize
end

def self.free_trial?
free_trial_length.to_i > 0
end


#
# STRIPE_EVENT section
#
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy/db/migrate/20130318201927_create_customers.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class CreateCustomers < ActiveRecord::Migration
class CreateCustomers < ActiveRecord::Migration[5.1]
def change
create_table :customers do |t|
t.string :email
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class CreateSubscriptions < ActiveRecord::Migration
class CreateSubscriptions < ActiveRecord::Migration[5.1]
def change
create_table :subscriptions do |t|
t.string :stripe_id
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy/db/migrate/20130318204458_create_plans.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class CreatePlans < ActiveRecord::Migration
class CreatePlans < ActiveRecord::Migration[5.1]
def change
create_table :plans do |t|
t.string :name
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy/db/migrate/20130318204502_create_coupons.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class CreateCoupons < ActiveRecord::Migration
class CreateCoupons < ActiveRecord::Migration[5.1]
def change
create_table :coupons do |t|
t.string :code
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class AddIntervalToPlan < ActiveRecord::Migration
class AddIntervalToPlan < ActiveRecord::Migration[5.1]
def change
add_column :plans, :interval, :string
end
Expand Down
Loading

0 comments on commit ebbdbc2

Please sign in to comment.