<commit_before>module Spree
  module AdyenCommon
    extend ActiveSupport::Concern

    class RecurringDetailsNotFoundError < StandardError; end
    class MissingCardSummaryError < StandardError; end

    included do
      preference :api_username, :string
      preference :api_password, :string
      preference :merchant_account, :string

      def merchant_account
        ENV['ADYEN_MERCHANT_ACCOUNT'] || preferred_merchant_account
      end

      def provider_class
        ::Adyen::API
      end

      def provider
        ::Adyen.configuration.api_username = (ENV['ADYEN_API_USERNAME'] || preferred_api_username)
        ::Adyen.configuration.api_password = (ENV['ADYEN_API_PASSWORD'] || preferred_api_password)
        ::Adyen.configuration.default_api_params[:merchant_account] = merchant_account

        provider_class
      end

      # NOTE Override this with your custom logic for scenarios where you don't
      # want to redirect customer to 3D Secure auth
      def require_3d_secure?(payment)
        true
      end

      # Receives a source object (e.g. CreditCard) and a shopper hash
      def require_one_click_payment?(source, shopper)
        false
      end

      def capture(amount, response_code, gateway_options = {})
        value = { currency: gateway_options[:currency], value: amount }
        response = provider.capture_payment(response_code, value)

        if response.success?
          def response.authorization; psp_reference; end
          def response.avs_result; {}; end
          def response.cvv_result; {}; end
        else
          # TODO confirm the error response will always have these two methods
          def response.to_s
            "#{result_code} - #{refusal_reason}"
          end
        end

        response
      end

      # According to Spree Processing class API the response object should respond
      # to an authorization method which return value should be assigned to payment
      # response_code
      def void(response_code, source, gateway_options = {})
        response = provider.cancel_payment(response_code)

        if response.success?
          def response.authorization; psp_reference; end
        else
          # TODO confirm the error response will always have these two methods
          def response.to_s
            "#{result_code} - #{refusal_reason}"
          end
        end
        response
      end

      def credit(credit_cents, response_code, gateway_options = {})
        currency = gateway_options[:currency]
        currency ||= gateway_options[:originator].payment.currency
        amount = { currency: currency, value: credit_cents }
        response = provider.refund_payment response_code, amount

        if response.success?
          def response.authorization; psp_reference; end
        else
          def response.to_s<commit_msg>Renamed variable<commit_after>module Spree
  module AdyenCommon
    extend ActiveSupport::Concern

    class RecurringDetailsNotFoundError < StandardError; end
    class MissingCardSummaryError < StandardError; end

    included do
      preference :api_username, :string
      preference :api_password, :string
      preference :merchant_account, :string

      def merchant_account
        ENV['ADYEN_MERCHANT_ACCOUNT'] || preferred_merchant_account
      end

      def provider_class
        ::Adyen::API
      end

      def provider
        ::Adyen.configuration.api_username = (ENV['ADYEN_API_USERNAME'] || preferred_api_username)
        ::Adyen.configuration.api_password = (ENV['<PASSWORD>API_PASSWORD'] || preferred_api_password)
        ::Adyen.configuration.default_api_params[:merchant_account] = merchant_account

        provider_class
      end

      # NOTE Override this with your custom logic for scenarios where you don't
      # want to redirect customer to 3D Secure auth
      def require_3d_secure?(payment)
        true
      end

      # Receives a source object (e.g. CreditCard) and a shopper hash
      def require_one_click_payment?(source, shopper)
        false
      end

      def capture(amount, response_code, gateway_options = {})
        value = { currency: gateway_options[:currency], value: amount }
        response = provider.capture_payment(response_code, value)

        if response.success?
          def response.authorization; psp_reference; end
          def response.avs_result; {}; end
          def response.cvv_result; {}; end
        else
          # TODO confirm the error response will always have these two methods
          def response.to_s
            "#{result_code} - #{refusal_reason}"
          end
        end

        response
      end

      # According to Spree Processing class API the response object should respond
      # to an authorization method which return value should be assigned to payment
      # response_code
      def void(response_code, source, gateway_options = {})
        response = provider.cancel_payment(response_code)

        if response.success?
          def response.authorization; psp_reference; end
        else
          # TODO confirm the error response will always have these two methods
          def response.to_s
            "#{result_code} - #{refusal_reason}"
          end
        end
        response
      end

      def credit(credit_cents, transaction_id, gateway_options = {})
        currency = gateway_options[:currency]
        currency ||= gateway_options[:originator].payment.currency
        amount = { currency: currency, value: credit_cents }
        response = provider.refund_payment transaction_id, amount

        if response.success?
          def response.authorization; psp_reference; end
        else
          def response.to_s
{'fraction_non_alphanumeric': 0.06625766871165645, 'fraction_numerical': 0.0007011393514460999, 'mean_word_length': 2.5886792452830187, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 4, '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': '7297746', 'n_tokens_mistral': 1498, 'n_tokens_neox': 1452, 'n_words': 422}