<commit_before>class Api::PaymentAccountsController < Api::BaseController
  before_filter :current_donor_id, :except => [:index, :create, :one_time_payment]
  skip_before_filter :current_donor_id, :only => [:all_donation_list, :cancel_subscription, :cancel_all_subscription]
  skip_before_filter :require_authentication, :only => :one_time_payment

  def index

    payment_accounts = current_donor.payment_accounts
    payment_accounts ||= []
    accounts_list = []

    payment_accounts.each do |account|
      stripe_customer = Stripe::Customer.retrieve(account.stripe_cust_id)

      cards_list = []
      stripe_customer.cards.data.each do |card|
        cards_hash = [ card.id => {
          "type" => card.type,
          "last4" => card.last4,
          "exp_month" => card.exp_month,
          "exp_year" => card.exp_month
        } ]
        cards_list << cards_hash
      end

      accounts_hash = [ account.id => {
        "created_at" => account.created_at,
        "updated_at" => account.updated_at,
        "processor" => account.processor,
        "requires_reauth" => account.requires_reauth,
        "stripe_cust_id" => account.stripe_cust_id,
	"cards" => cards_list
      } ]
      accounts_list << accounts_hash
    end

    render json: accounts_list

  end

  def create
    set_token = params[:stripeToken]

    if set_token.blank?
      render json: { :message => "Please provide your stripe token" }.to_json
    else
      if params.has_key?(:processor)
        payment = PaymentAccount.new_account(set_token, current_donor.id, {:donor => current_donor, :processor => params[:processor]})
        render json: payment.to_json
      else
        render json: { :message => "Wrong parameters" }.to_json
      end
    end
  end

  def update
    set_token = params[:stripeToken]
    if set_token.blank?
      render json: { :message => "Please provide your stripe token"}.to_json
    else
      params[:payment_account] = {"processor"=> params[:processor]}#, "stripeToken"=> params[:stripeToken]}

      if params.has_key?(:payment_account)
        payment = PaymentAccount.update_account(set_token, current_donor.id, current_donor_id, {:donor => current_donor}.merge(params[:payment_account]))

        respond_to do |format|
          if current_donor_id && current_donor_id.update_attributes(params[:payment_account])
            format.json { render json: current_donor_id }
          elsif current_donor_id
            format.json { render json: current_donor_id.errors, status: :unprocessable_entity }
          else
            format.json { head :not_found }
          end
        end

      else
        render json: {:message => "Wrong parameters"}.to_json
      end
    end
  end

  def show
    respond_to do |format|
      if current_donor_id
        format.json { render json: current_donor_id }
      else
        format.json { head :not_found }
      end
    end

  end

  def destroy
    respond_to do |format|
      if current_donor_id
        current_donor_id.destroy
        render json: { :message => "Payment account has been delete" }.to_json
      else
        format.json { head :not_found }
      end
    end
  end

  def donate_subscription
    respond_to do |format|
      if current_donor_id && donation = current_donor_id.donate_subscription(params[:amount], params[:endowment_id], params[:id], current_donor.email)
        format.json { render json: donation }
      else
        format.json { head :not_found }
      end
    end
  end

  def one_time_payment
    if params.has_key?(:password)
      password = secure_password(params[:password])
    else
      password = params[:password]
    end

    respond_to do |format|
      donation = PaymentAccount.one_time_payment(params[:amount].to_i, params[:endowment_id], params[:email], params[:stripeToken], params[:payment_account_id], password)
      format.json { render json: donation }
    end<commit_msg>wrap everything in json to avoid 406 status code?<commit_after>class Api::PaymentAccountsController < Api::BaseController
  before_filter :current_donor_id, :except => [:index, :create, :one_time_payment]
  skip_before_filter :current_donor_id, :only => [:all_donation_list, :cancel_subscription, :cancel_all_subscription]
  skip_before_filter :require_authentication, :only => :one_time_payment

  def index

    payment_accounts = current_donor.payment_accounts
    payment_accounts ||= []
    accounts_list = []

    payment_accounts.each do |account|
      stripe_customer = Stripe::Customer.retrieve(account.stripe_cust_id)

      cards_list = []
      stripe_customer.cards.data.each do |card|
        cards_hash = [ card.id => {
          "type" => card.type,
          "last4" => card.last4,
          "exp_month" => card.exp_month,
          "exp_year" => card.exp_year
        } ]
        cards_list << cards_hash
      end

      accounts_hash = [ account.id => {
        "created_at" => account.created_at,
        "updated_at" => account.updated_at,
        "processor" => account.processor,
        "requires_reauth" => account.requires_reauth,
        "stripe_cust_id" => account.stripe_cust_id,
	"cards" => cards_list
      } ]
      accounts_list << accounts_hash
    end

    render json: accounts_list

  end

  def create
    set_token = params[:stripeToken]

    if set_token.blank?
      render json: { :message => "Please provide your stripe token" }.to_json
    else
      if params.has_key?(:processor)
        payment = PaymentAccount.new_account(set_token, current_donor.id, {:donor => current_donor, :processor => params[:processor]})
        render json: payment.to_json
      else
        render json: { :message => "Wrong parameters" }.to_json
      end
    end
  end

  def update
    set_token = params[:stripeToken]
    if set_token.blank?
      render json: { :message => "Please provide your stripe token"}.to_json
    else
      params[:payment_account] = {"processor"=> params[:processor]}#, "stripeToken"=> params[:stripeToken]}

      if params.has_key?(:payment_account)
        payment = PaymentAccount.update_account(set_token, current_donor.id, current_donor_id, {:donor => current_donor}.merge(params[:payment_account]))

        respond_to do |format|
          if current_donor_id && current_donor_id.update_attributes(params[:payment_account])
            format.json { render json: current_donor_id }
          elsif current_donor_id
            format.json { render json: current_donor_id.errors, status: :unprocessable_entity }
          else
            format.json { head :not_found }
          end
        end

      else
        render json: {:message => "Wrong parameters"}.to_json
      end
    end
  end

  def show
    respond_to do |format|
      if current_donor_id
        format.json { render json: current_donor_id }
      else
        format.json { head :not_found }
      end
    end

  end

  def destroy
    respond_to do |format|
      if current_donor_id
        account = current_donor.payment_accounts.find(params[:id])
        account.destroy
        format.json { render json: { :message => "Payment account has been deleted" }.to_json }
      else
        format.json { head :not_found }
      end
    end
  end

  def donate_subscription
    respond_to do |format|
      if current_donor_id && donation = current_donor_id.donate_subscription(params[:amount], params[:endowment_id], params[:id], current_donor.email)
        format.json { render json: donation }
      else
        format.json { head :not_found }
      end
    end
  end

  def one_time_payment
    if params.has_key?(:password)
      password = secure_password(params[:password])
    else
      password = params[:password]
    end

    respond_to do |format|
      donation = PaymentAccount.one_time_payment(params[:amount].to_i, params[:endowment_id], params[:email], params[:stripeToken], params[:payment_account_id], password)
      format.json { render json: donation }
    end
{'fraction_non_alphanumeric': 0.10025188916876575, 'fraction_numerical': 0.0008816120906801007, 'mean_word_length': 2.915680473372781, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 53, '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': '129196', 'n_tokens_mistral': 2459, 'n_tokens_neox': 2405, 'n_words': 524}