app/controllers/iugu/account_controller.rb in iugusdk-1.0.0.alpha.21 vs app/controllers/iugu/account_controller.rb in iugusdk-1.0.0.alpha.22

- old
+ new

@@ -2,23 +2,30 @@ before_filter(:only => [:destroy, :cancel_destruction, :update]) { |c| c.must_be :owner, :id } before_filter(:only => [:generate_new_token]) { |c| c.must_be :owner, :account_id } def index + @accounts = current_user.accounts.order(:created_at) render 'iugu/settings/accounts' end def view if params[:id] @account = current_user.accounts.find(params[:id]) else @account = current_user_account.account end + + @has_subscription = false + if IuguSDK::enable_subscription_features - subscription = Iugu::Api::Subscription.find @account.subscription_id - plan = Iugu::Api::Plan.find_by_identifier subscription.plan_identifier - @plan_name = plan.try :name + unless @account.subscription_id.blank? + @has_subscription = true + @subscription = @account.subscription + plan = Iugu::Api::Plan.find_by_identifier @subscription.plan_identifier + @plan_name = plan.try :name + end end @primary_domain = @account.account_domains.where(:primary => true).first if @account render 'iugu/settings/account' end @@ -52,11 +59,17 @@ flash[:group] = :account_update redirect_to account_view_path(params[:id]), :notice => I18n.t("iugu.notices.account_updated") end def create - current_user.accounts << Account.create + create_parameters = {} + if IuguSDK::enable_subscription_features + plan_identifier = params[:plan] || IuguSDK::default_subscription_name + currency = locale_to_currency I18n.locale + create_parameters = {plan_identifier: plan_identifier, currency: currency, email: current_user.email} + end + current_user.accounts << Account.create(create_parameters) redirect_to account_settings_path end def generate_new_token if IuguSDK::enable_account_api @@ -70,25 +83,62 @@ flash[:group] = :api_token redirect_to account_view_path(params[:account_id]), :notice => notice else raise ActionController::RoutingError.new('Not found') end + end + def remove_token + if IuguSDK::enable_account_api + account = current_user.accounts.find params[:account_id] + token = account.tokens.find params[:token] + if token.destroy + notice = I18n.t("iugu.notices.token_removed") + else + notice = token.errors.full_messages + end + flash[:group] = :api_token + redirect_to account_view_path(params[:account_id]), :notice => notice + else + raise ActionController::RoutingError.new('Not found') + end end def payment_history get_account - subscription = Iugu::Api::Subscription.find @account.subscription_id + subscription = Iugu::Api::Subscription.find @account.subscription_id.to_uuid.to_s customer = Iugu::Api::Customer.find subscription.customer_id - @invoices = customer.invoices({status_filter: ["pending", "paid"]}) + @invoices = Iugu::Api::Invoice.find :all, params: {hl: current_user.locale, customer_id: customer.id.to_param, status_filter: ["pending", "paid"], limit: 10} render 'iugu/account/payment_history' end + def activate + get_account + if @account.subscription.try :activate + notice = I18n.t("iugu.notices.account_activated") + @account.clear_cached_subscription_active + else + notice = I18n.t("iugu.notices.error_activating_account") + end + redirect_to account_view_path(params[:account_id]), :notice => notice + end + + def suspend + get_account + if @account.subscription.try :suspend + notice = I18n.t("iugu.notices.account_suspended") + @account.clear_cached_subscription_active + else + notice = I18n.t("iugu.notices.error_suspending_account") + end + redirect_to account_view_path(params[:account_id]), :notice => notice + end + private def get_account - if params[:id] - @account = current_user.accounts.find(params[:id]) + if params[:account_id] + @account = current_user.accounts.find(params[:account_id]) else @account = current_user_account.account end end