<commit_before>module Spree
  class AdyenRedirectController < StoreController
    before_filter :restore_session
    before_filter :check_signature, only: :confirm

    skip_before_filter :verify_authenticity_token

    # This is the entry point after an Adyen HPP payment is completed
    def confirm
      if @order.complete?
        confirm_order_already_completed
      else
        confirm_order_incomplete
      end
    end

    private

    def confirm_order_incomplete
      source = Adyen::HppSource.new(source_params)

      unless source.authorised?
        flash.notice = Spree.t(:payment_processing_failed)
        redirect_to checkout_state_path(@order.state)
        return
      end

      # payment is created in a 'checkout' state so that the payment method
      # can attempt to auth it. The payment of course is already auth'd and
      # adyen hpp's authorize implementation just returns a dummy response.
      @order.payments.create!(
        amount: @order.total,
        payment_method: @payment_method,
        source: source,
        response_code: psp_reference,
        state: "checkout"
      )

      if @order.complete
        redirect_to_order
      else
        #TODO void/cancel payment
        redirect_to checkout_state_path(@order.state)
      end
    end

    # If an authorization notification is received before the redirection the
    # payment is created there.In this case we just need to assign the addition
    # parameters received about the source.
    #
    # We do this because there is a chance that we never get redirected back
    # so we need to make sure we complete the payment and order.
    def confirm_order_already_completed
      payment = @order.payments.find_by(response_code: psp_reference)
      payment.source.update(source_params)

      redirect_to_order
    end

    def redirect_to_order
      flash.notice = Spree.t(:order_processed_successfully)
      redirect_to order_path(@order)<commit_msg>Should raise error if can't find payment<commit_after>module Spree
  class AdyenRedirectController < StoreController
    before_filter :restore_session
    before_filter :check_signature, only: :confirm

    skip_before_filter :verify_authenticity_token

    # This is the entry point after an Adyen HPP payment is completed
    def confirm
      if @order.complete?
        confirm_order_already_completed
      else
        confirm_order_incomplete
      end
    end

    private

    def confirm_order_incomplete
      source = Adyen::HppSource.new(source_params)

      unless source.authorised?
        flash.notice = Spree.t(:payment_processing_failed)
        redirect_to checkout_state_path(@order.state)
        return
      end

      # payment is created in a 'checkout' state so that the payment method
      # can attempt to auth it. The payment of course is already auth'd and
      # adyen hpp's authorize implementation just returns a dummy response.
      @order.payments.create!(
        amount: @order.total,
        payment_method: @payment_method,
        source: source,
        response_code: psp_reference,
        state: "checkout"
      )

      if @order.complete
        redirect_to_order
      else
        #TODO void/cancel payment
        redirect_to checkout_state_path(@order.state)
      end
    end

    # If an authorization notification is received before the redirection the
    # payment is created there.In this case we just need to assign the addition
    # parameters received about the source.
    #
    # We do this because there is a chance that we never get redirected back
    # so we need to make sure we complete the payment and order.
    def confirm_order_already_completed
      payment = @order.payments.find_by!(response_code: psp_reference)
      payment.source.update(source_params)

      redirect_to_order
    end

    def redirect_to_order
      flash.notice = Spree.t(:order_processed_successfully)
      redirect_to order_path(@order)
{'fraction_non_alphanumeric': 0.049974760222110046, 'fraction_numerical': 0.0, 'mean_word_length': 2.8513119533527695, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '2268387', 'n_tokens_mistral': 1091, 'n_tokens_neox': 1044, 'n_words': 347}