class Muck::SubscriptionsController < ApplicationController
unloadable
before_filter :login_required
ssl_required :new, :receipt , :confirm, :create, :update
def show
@subscription = current_user.subscriptions.find(params[:id]) rescue nil
respond_to do |format|
format.html { render :template => 'subscriptions/show' }
end
end
def receipt
@subscription = current_user.subscriptions.find(params[:id]) rescue nil
respond_to do |format|
format.html { render :template => 'subscriptions/receipt' }
end
end
def new
if !current_user.billing_information
@billing_information = BillingInformation.create(:first_name => current_user.first_name,
:last_name => current_user.last_name)
current_user.billing_informations << @billing_information
end
new_action
end
def confirm
respond_to do |format|
format.html { render :template => 'subscriptions/confirm' }
end
end
def confirm_unsubscribe
respond_to do |format|
format.html { render :template => 'subscriptions/confirm_unsubscribe' }
end
end
def create
@coupon_code = params[:coupon_code]
@billing_information = current_user.create_update_billing_information!(params[:billing_information])
@subscription_plan = get_subscription_plan # get or create a subscription plan as needed
@subscription = Subscription.subscribe(current_user, @subscription_plan.id, @coupon_code)
MuckCommerceMailer.deliver_user_subscribe_notification(current_user, @subscription)
flash[:notice] = translate('muck.commerce.subscribe_thanks')
redirect_to subscription_path(@subscription)
rescue ActiveRecord::RecordInvalid
new_action(t('muck.commerce.subscription_values_problem'))
rescue MuckCommerce::Exceptions::PaymentGatewayError => ex
new_action(ex)
rescue MuckCommerce::Exceptions::SubscriptionError => ex
new_action(ex)
rescue MuckCommerce::Exceptions::CouponError => ex
new_action(translate('muck.commerce.coupon_problem', :error => ex))
end
def update
@subscription = Subscription.find(params[:id])
@coupon_code = params[:coupon_code]
@subscription.update_attributes!(params[:subscription])
redirect_to subscription_path(@subscription)
rescue MuckCommerce::Exceptions::CouponError, MuckCommerce::Exceptions::PaymentGatewayError => ex
new_action(translate('muck.commerce.coupon_problem', :error => ex))
rescue MuckCommerce::Exceptions::PaymentGatewayError => ex
new_action(translate('muck.commerce.subscription_problem', :error => ex))
rescue MuckCommerce::Exceptions::SubscriptionError => ex
new_action(translate('muck.commerce.subscription_problem', :error => ex))
rescue ActiveRecord::RecordInvalid => ex
new_action(translate('muck.commerce.subscription_problem', :error => ex))
end
def destroy
@subscription = Subscription.find(params[:id])
@subscription.destroy
flash[:notice] = translate('muck.commerce.subscription_deleted')
redirect_to new_subscription_path
end
# Paypal functionality
# def express
# response = PAYPAL_EXPRESS_GATEWAY.setup_purchase(calculate_amount,
# :ip => request.remote_ip,
# :return_url => process_express_user_lookup_url(@user),
# :cancel_return_url => edit_user_lookup_url(@user, @lookup)
# )
# redirect_to PAYPAL_EXPRESS_GATEWAY.redirect_url_for(response.token)
# end
#
# def process_express
# @lookup.amount = calculate_amount
# @lookup.save!
# response = PAYPAL_EXPRESS_GATEWAY.purchase(@lookup.amount, :ip => request.remote_ip, :token => params[:token], :payer_id => params[:PayerID], :order_id => @lookup.order.order_number)
# purchase = OrderTransaction.parse_response(response, 'paypal express purcahse', @lookup.amount)
# @lookup.transactions << purchase
# @lookup.finish!
# respond_to do |format|
# format.html { redirect_to user_lookup_path(@user, @lookup) }
# end
# end
#
# # let the user pay with paypal
# def express
# description = 'sign up for stuff'
# response = PAYPAL_EXPRESS_RECURRING_GATEWAY.setup_agreement(
# :description => description,
# :ip => request.remote_ip,
# :return_url => process_express_new_order_url,
# :cancel_return_url => root_url)
# redirect_to PAYPAL_EXPRESS_RECURRING_GATEWAY.redirect_url_for(response.token)
# end
#
# def process_express
# amount = 100
# token = params[:token]
# response = PAYPAL_EXPRESS_RECURRING_GATEWAY.create_profile(params[:token],
# :ip => request.remote_ip,
# :description => description,
# :start_date => start_date,
# :frequency => frequency_in_months,
# :amount => amount_in_dollars,
# :auto_bill_outstanding => true,
# :payer_id => params[:PayerID],
# :order_id => '101')
#
# # Save this profile_id in your transactions table. This is used to cancel/modify the plan in the future.
# profile_id = response.params["profile_id"]
#
# if response.success?
# flash[:notice] = "Your PayPal account was successfully set up for the #{description} payment plan."
# redirect_to login_path
# else
# flash.now[:notice] = "There was a problem setting up your PayPal account for the #{description} payment plan"
# render cancel_url
# end
# end
#
# def cancel
# response = PAYPAL_EXPRESS_GATEWAY.cancel_profile(paypal_profile_id, :note => 'Payment plan was canceled by user')
# flash[:notice] = 'You have successfully canceled your membership'
# end
protected
def new_action(message = nil)
flash[:error] = message if message
respond_to do |format|
format.html { render :template => 'subscriptions/new' }
end
end
def get_subscription_plan
if params[:subscription_plan_sku]
SubscriptionPlan.find_by_sku(params[:subscription_plan_sku])
else
SubscriptionPlan.create(params[:subscription_plan])
end
end
def access_denied_path
subscriber_signup_path
end
end