Sha256: 77c99c4d07819652b576fcd650065c48997f85d63a127b062dc1a8562153aabd

Contents?: true

Size: 1.24 KB

Versions: 6

Compression:

Stored size: 1.24 KB

Contents

require_dependency "tang/application_controller"

module Tang
  class Account::CouponsController < Account::ApplicationController
    def create
      @coupon = Coupon.find_by(stripe_id: params[:coupon][:stripe_id])
      if @coupon.present?
        if current_customer.subscription.present?
          subscription = ApplySubscriptionDiscount.call(
            current_customer.subscription,
            @coupon
          )
          if subscription.errors.any?
            redirect_to account_subscription_path, alert: 'Coupon could not be applied.'
            return
          end
        else
          current_customer.subscription_coupon = @coupon
          current_customer.save
        end
        redirect_to account_subscription_path, notice: 'Coupon was successfully applied.'
      else
        redirect_to account_subscription_path, alert: 'Coupon could not be applied.'
      end
    end

    def destroy
      if current_customer.subscription.present?
        RemoveSubscriptionDiscount.call(
          current_customer.subscription
        )
      else
        current_customer.subscription_coupon = nil
        current_customer.save
      end
      redirect_to account_subscription_path, notice: 'Coupon was successfully removed.'
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
tang-0.2.2 app/controllers/tang/account/coupons_controller.rb
tang-0.2.1 app/controllers/tang/account/coupons_controller.rb
tang-0.2.0 app/controllers/tang/account/coupons_controller.rb
tang-0.1.0 app/controllers/tang/account/coupons_controller.rb
tang-0.0.9 app/controllers/tang/account/coupons_controller.rb
tang-0.0.8 app/controllers/tang/account/coupons_controller.rb