Skip to content

Instantly share code, notes, and snippets.

@tkawa
Created December 27, 2011 03:38
Show Gist options
  • Save tkawa/1522645 to your computer and use it in GitHub Desktop.
Save tkawa/1522645 to your computer and use it in GitHub Desktop.
Using subscription_fu
class SubscriptionsController < ApplicationController
before_filter :authenticate_user!
def show
@page_css = 'user'
@subscription = current_user.active_subscription
@upcoming = current_user.upcoming_subscription
@latest = current_user.latest_valid_subscription
end
def update
@subscription = current_user.build_next_subscription(params[:plan])
@subscription.save!
@transaction = @subscription.initiate_activation(current_user)
url = @transaction.start_checkout(current_user_subscription_transaction_url, abort_current_user_subscription_transaction_url)
url += "?token=#{@transaction.identifier}" if @transaction.gateway == 'nogw'
logger.info url
redirect_to url
end
end
class TransactionsController < ApplicationController
before_filter :authenticate_user!
before_filter :require_valid_transaction
def show
end
def abort
@transaction.abort
flash[:notice] = "Transaction aborted."
redirect_to current_user_subscription_url
end
def update
if @transaction.complete
flash[:notice] = "Sucessfully updated your subscription: #{@transaction.sub_plan.human_name}"
else
flash[:notice] = "Transaction was not successfull, please try again."
end
redirect_to current_user_subscription_url
end
private
def require_valid_transaction
@token = params[:token]
@transaction = current_user.pending_transaction(@token)
unless @transaction
logger.info("Invalid transaction for token: #{@token}")
flash[:error] = "Invalid transaction, please try again."
redirect_to current_user_subscription_url
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment