Sha256: e99f76d0384fa8ebfe03fe1c39a9b6727a163205a9c6f2f1ae6f4301a1cb616e

Contents?: true

Size: 1.98 KB

Versions: 1

Compression:

Stored size: 1.98 KB

Contents

module Spree
  class SubscriptionsController < StoreController
    prepend_before_filter :load_object
    before_action :find_active_plan, only: [:new, :create]
    before_action :find_plan, only: [:show, :destroy]
    before_action :find_subscription, only: [:show, :destroy]
    before_action :authenticate_subscription, only: [:new, :create]

    def new
      @subscription = @plan.subscriptions.build
    end

    def create
      @subscription = @plan.subscriptions.build(subscription_params.merge(user_id: spree_current_user.id))
      if @subscription.save_and_manage_api
        redirect_to recurring_plan_subscription_url(@plan, @subscription), notice: "Thank you for subscribing!"
      else
        render :new
      end
    end

    def destroy
      if @subscription.save_and_manage_api(unsubscribed_at: Time.current)
        redirect_to plans_path, notice: "Subscription has been cancelled."
      else
        render :show
      end
    end

    private

    def find_active_plan
      unless @plan = Spree::Plan.active.where(id: params[:plan_id]).first
        flash[:error] = "Plan not found."
        redirect_to plans_url
      end
    end

    def find_plan
      unless @plan = Spree::Plan.where(id: params[:plan_id]).first
        flash[:error] = "Plan not found."
        redirect_to plans_url
      end
    end

    def find_subscription
      unless @subscription = @plan.subscriptions.undeleted.where(id: params[:id]).first
        flash[:error] = "Subscription not found."
        redirect_to root_url
      end
    end

    def subscription_params
      params.require(:subscription).permit(:email, :card_token)
    end

    def load_object
      @user ||= spree_current_user
      authorize! params[:action].to_sym, @user
    end

    def authenticate_subscription
      if subscription = spree_current_user.subscriptions.undeleted.first
        flash[:alert] = "You have already subscribed."
        redirect_to recurring_plan_subscription_url(@plan, subscription)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
spree_account_recurring-1.0.0 app/controllers/spree/subscriptions_controller.rb