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

    def confirm
      order = current_order

      unless authorized?
        flash.notice = Spree.t(:payment_processing_failed)
        redirect_to checkout_state_path(order.state) and return
      end

      # cant set payment to complete here due to a validation
      # in order transition from payment to complete (it requires at
      # least one pending payment)
      payment = order.payments.create!(
        :amount => order.total,
        :payment_method => payment_method,
        :response_code => params[:pspReference]
      )

      order.next

      if order.complete?
        flash.notice = Spree.t(:order_processed_successfully)
        redirect_to order_path(order, :token => order.token)
      else
        redirect_to checkout_state_path(order.state)
      end
    end

    def adyen_authorise3d
      order = current_order

      if params[:md].present? && params[:pa_res]
        md = params[:md]
        pa_response = params[:pa_res]

        # TODO How to map this to make sure we fetch the right gateway?
        gateway = Gateway::AdyenPaymentEncrypted.last

        if response = gateway.authorise3d(md, pa_response, request.ip, request.headers.env)
          payment = order.payments.create!(
            :amount => order.total,
            :payment_method => gateway,
            :response_code => response.psp_reference
          )

          order.next

          if order.complete?
            flash.notice = Spree.t(:order_processed_successfully)
            redirect_to order_path(order, :token => order.token)
          else
            redirect_to checkout_state_path(order.state)
          end
        else
          flash.notice = response.error.inspect
          redirect_to checkout_state_path(order.state)
        end
      end
    end

    private
      def check_signature
        unless ::Adyen::Form.redirect_signature_check(params, payment_method.preferred_shared_secret)
          raise "Payment Method not found."
        end
      end
<commit_msg>Set up payment recurring source after on 3d redirect<commit_after>module Spree
  class AdyenRedirectController < StoreController
    before_filter :check_signature, :only => :confirm

    def confirm
      order = current_order

      unless authorized?
        flash.notice = Spree.t(:payment_processing_failed)
        redirect_to checkout_state_path(order.state) and return
      end

      # cant set payment to complete here due to a validation
      # in order transition from payment to complete (it requires at
      # least one pending payment)
      payment = order.payments.create!(
        :amount => order.total,
        :payment_method => payment_method,
        :response_code => params[:pspReference]
      )

      order.next

      if order.complete?
        flash.notice = Spree.t(:order_processed_successfully)
        redirect_to order_path(order, :token => order.token)
      else
        redirect_to checkout_state_path(order.state)
      end
    end

    def authorise3d
      order = current_order

      if params[:md].present? && params[:pa_res].present?
        md = params[:md]
        pa_response = params[:pa_res]

        # TODO Use something like session[:adyen_authorise3d_gateway_id] to fetch
        # the proper gateway
        gateway = Gateway::AdyenPaymentEncrypted.last

        if response = gateway.authorise3d(md, pa_response, request.ip, request.headers.env)
          payment = order.payments.create!(
            :amount => order.total,
            :payment_method => gateway,
            :response_code => response.psp_reference
          )

          credit_card = CreditCard.create! do |cc|
            list = gateway.provider.list_recurring_details(order.user_id.present? ? order.user_id : order.email)

            cc.month = list.details.last[:card][:expiry_date].month
            cc.year = list.details.last[:card][:expiry_date].year
            cc.name = list.details.last[:card][:holder_name]
            cc.cc_type = list.details.last[:variant]
            cc.last_digits = list.details.last[:card][:number]
            cc.gateway_customer_profile_id = list.details.last[:recurring_detail_reference]
          end

          # We want to avoid callbacks such as Payment#create_payment_profile on after_save
          payment.update_columns source_id: credit_card.id, source_type: credit_card.class.name

          order.next

          if order.complete?
            flash.notice = Spree.t(:order_processed_successfully)
            redirect_to order_path(order, :token => order.token)
          else
            redirect_to checkout_state_path(order.state)
          end
        else
          flash.notice = response.error.inspect
          redirect_to checkout_state_path(order.state)
        end
      end
    end

    private
      def check_signature
        unless ::Adyen::Form.redirect_signature_check(params, payment_method.preferred_shared_secret)
          raise "Payment Method not found."
        end
      end

{'fraction_non_alphanumeric': 0.07881869743790339, 'fraction_numerical': 0.001173479366321142, 'mean_word_length': 2.3889993373094764, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, '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': '5622500', 'n_tokens_mistral': 1430, 'n_tokens_neox': 1377, 'n_words': 325}