<commit_before>class RewardsController < ApplicationController
  before_action :authenticate_user!

  def new
    @reward = Reward.new
  end

  def premium
    @reward = Reward.new
    @reward.rewardable_type = params[:rewardable_type]
    @reward.rewardable_id = params[:rewardable_id]
  end

  def create
    @reward = Reward.new(reward_params)
    @reward.user = current_user
    @reward = set_reward_plan(params[:plan], true)
    @reward.funding_source = 'coinkite'
    if @reward.save
      coinkite_pay_url = setup_coinkite(@reward)
      redirect_to coinkite_pay_url
    else
      flash[:alert] = "There was a problem creating your Phez Premium transaction."
      render :action => :new
    end
  end

  def create_rewardable
    @reward = Reward.new(reward_params)
    @reward.user = current_user
    if @reward.rewardable.nil? || @reward.rewardable.user.nil? 
      flash[:alert] = "There was a problem rewarding Phez Premium: post/comment or user not found"
      redirect_to root_path and return
    end
    if params[:plan] && params[:funding_source]
      flash[:alert] = "Please select either your account as a funding source or a new premium purchase plan, but not both."
      render :action => :premium and return
    end

    if params[:plan]
      @reward = set_reward_plan(params[:plan])
      if @reward.save
        coinkite_pay_url = setup_coinkite(@reward)
        redirect_to coinkite_pay_url
      else
        flash[:alert] = "There was a problem creating your Phez Premium transaction."
        render :action => :premium and return
      end
    else
      # Rewarding Premium from existing account months
      if params[:months].blank?
        flash[:alert] = "Please select the number of premium months you'd like to give."
        render :action => :premium and return
      end
      months = params[:months].to_i
      if months > current_user.rewardable_months
        flash[:alert] = "You cannot give more premium months than the number present in your account at this time: #{current_user.premium_months} months"
        render :action => :premium and return
      end
      @reward.months = months
      @reward.amount_usd = Reward.cost(months)
      @reward.rewarded_user_id = @reward.rewardable.user.id
      @reward.payment_confirmed = true
      @reward.funding_source = 'internal'
      if @reward.save
        @reward.activate!
        redirect_to root_path, notice: "Success! You have rewarded #{@reward.rewardable.user.username} with Phez Premium."
      else
        flash[:alert] = "There was a problem creating your Phez Premium transaction."
        render :action => :premium
      end
    end

  end

  def thanks
    @reward = Reward.where(id: params[:id]).first
    if @reward.nil?
      redirect_to new_reward_path, alert: 'Premium Reward purchase not found.' and return
    end
    @reward.activate!
  end

  private

    def reward_params
      params.require(:reward).permit(:rewardable_id, :rewardable_type)
    end

    def set_reward_plan(plan, rewardable = false)
      case plan
      when 'one_month'
        @reward.amount_usd = Reward.cost(1)
        @reward.months = 1
        @reward.rewardable_months = 1 if rewardable
      when 'three_months'
        @reward.amount_usd = Reward.cost(3)
        @reward.months = 3
        @reward.rewardable_months = 3 if rewardable 
      when 'six_months'
        @reward.amount_usd = Reward.cost(6)
        @reward.months = 6
        @reward.rewardable_months = 6 if rewardable
      when 'one_year'
        @reward.amount_usd = Reward.cost(12)
        @reward.months = 12
        @reward.rewardable_months = 12 if rewardable
      when 'custom'
        @reward.amount_usd = params[:custom_amount]
        @reward.months = Reward.months_given_cost(params[:custom_amount])
        @reward.rewardable_months = @reward.months if rewardable
      end
      @reward
    end

    def setup_coinkite(reward)
      payload = {'t' => reward.id, 'p' => reward.amount_usd, 'c' => 'USD', 'r' => "http://#{Figaro.env.app_domain}/rewards/#{reward.id}/thanks"}
      token = JWT.encode payload, Figaro.env.coinkite_api_secret, 'HS256'
      "#{Figaro.env.coinkite_button_url}?#{token}"
    end

end<commit_msg>reward premium fix<commit_after>class RewardsController < ApplicationController
  before_action :authenticate_user!

  def new
    @reward = Reward.new
  end

  def premium
    @reward = Reward.new
    @reward.rewardable_type = params[:rewardable_type]
    @reward.rewardable_id = params[:rewardable_id]
  end

  def create
    @reward = Reward.new(reward_params)
    @reward.user = current_user
    @reward = set_reward_plan(params[:plan], true)
    @reward.funding_source = 'coinkite'
    if @reward.save
      coinkite_pay_url = setup_coinkite(@reward)
      redirect_to coinkite_pay_url
    else
      flash[:alert] = "There was a problem creating your Phez Premium transaction."
      render :action => :new
    end
  end

  def create_rewardable
    @reward = Reward.new(reward_params)
    @reward.user = current_user
    if @reward.rewardable.nil? || @reward.rewardable.user.nil? 
      flash[:alert] = "There was a problem rewarding Phez Premium: post/comment or user not found"
      redirect_to root_path and return
    end
    if params[:plan] && params[:funding_source]
      flash[:alert] = "Please select either your account as a funding source or a new premium purchase plan, but not both."
      render :action => :premium and return
    end

    if params[:plan]
      @reward = set_reward_plan(params[:plan])
      @reward.rewarded_user_id = @reward.rewardable.user.id
      @reward.funding_source = 'coinkite'
      if @reward.save
        coinkite_pay_url = setup_coinkite(@reward)
        redirect_to coinkite_pay_url
      else
        flash[:alert] = "There was a problem creating your Phez Premium transaction."
        render :action => :premium and return
      end
    else
      # Rewarding Premium from existing account months
      if params[:months].blank?
        flash[:alert] = "Please select the number of premium months you'd like to give."
        render :action => :premium and return
      end
      months = params[:months].to_i
      if months > current_user.rewardable_months
        flash[:alert] = "You cannot give more premium months than the number present in your account at this time: #{current_user.premium_months} months"
        render :action => :premium and return
      end
      @reward.months = months
      @reward.amount_usd = Reward.cost(months)
      @reward.rewarded_user_id = @reward.rewardable.user.id
      @reward.payment_confirmed = true
      @reward.funding_source = 'internal'
      if @reward.save
        @reward.activate!
        redirect_to root_path, notice: "Success! You have rewarded #{@reward.rewardable.user.username} with Phez Premium."
      else
        flash[:alert] = "There was a problem creating your Phez Premium transaction."
        render :action => :premium
      end
    end

  end

  def thanks
    @reward = Reward.where(id: params[:id]).first
    if @reward.nil?
      redirect_to new_reward_path, alert: 'Premium Reward purchase not found.' and return
    end
    @reward.activate!
  end

  private

    def reward_params
      params.require(:reward).permit(:rewardable_id, :rewardable_type)
    end

    def set_reward_plan(plan, rewardable = false)
      case plan
      when 'one_month'
        @reward.amount_usd = Reward.cost(1)
        @reward.months = 1
      when 'three_months'
        @reward.amount_usd = Reward.cost(3)
        @reward.months = 3
      when 'six_months'
        @reward.amount_usd = Reward.cost(6)
        @reward.months = 6
      when 'one_year'
        @reward.amount_usd = Reward.cost(12)
        @reward.months = 12
      when 'custom'
        @reward.amount_usd = params[:custom_amount]
        @reward.months = Reward.months_given_cost(params[:custom_amount])
      end
      @reward
    end

    def setup_coinkite(reward)
      payload = {'t' => reward.id, 'p' => reward.amount_usd, 'c' => 'USD', 'r' => "http://#{Figaro.env.app_domain}/rewards/#{reward.id}/thanks"}
      token = JWT.encode payload, Figaro.env.coinkite_api_secret, 'HS256'
      "#{Figaro.env.coinkite_button_url}?#{token}"
    end

end
{'fraction_non_alphanumeric': 0.09815578743023538, 'fraction_numerical': 0.0037612230041252125, 'mean_word_length': 2.87359022556391, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4424159', 'n_tokens_mistral': 2757, 'n_tokens_neox': 2634, 'n_words': 659}