<commit_before>module Effective
  module Providers
    module Moneris
      extend ActiveSupport::Concern

      included do
        prepend_before_filter :find_authenticity_token_from_moneris, :only => [:moneris_postback]
      end

      def moneris_postback
        @order ||= Effective::Order.find(params[:response_order_id].to_i - EffectiveOrders.moneris[:order_nudge].to_i)

        EffectiveOrders.authorized?(self, :update, @order)

        # Store the Order Nudge if present, so we can have this information in our order_purchased hash
        params[:order_nudge] = EffectiveOrders.moneris[:order_nudge] if EffectiveOrders.moneris[:order_nudge].to_i > 0

        # Delete the Purchased and Declined Redirect URLs
        purchased_redirect_url = params.delete(:rvar_purchased_redirect_url)
        declined_redirect_url = params.delete(:rvar_declined_redirect_url)

        if params[:result].to_s == '1' && params[:transactionKey].present?
          verify_params = parse_moneris_response(send_moneris_verify_request(params[:transactionKey])) || {}

          response_code = verify_params[:response_code].to_i # Sometimes moneris sends us the string 'null'

          if response_code > 0 && response_code < 50  # Less than 50 means a successful validation
            order_purchased(params.merge(verify_params), purchased_redirect_url)
          else
            order_declined(params.merge(verify_params), declined_redirect_url)
          end
        else
          order_declined(params, declined_redirect_url)
        end
      end

      private

      def parse_moneris_response(text)<commit_msg>Fix obfuscation related bug in moneris_postback<commit_after>module Effective
  module Providers
    module Moneris
      extend ActiveSupport::Concern

      included do
        prepend_before_filter :find_authenticity_token_from_moneris, :only => [:moneris_postback]
      end

      def moneris_postback
        response_order_id = (EffectiveOrders.obfuscate_order_ids == true ? Effective::Order.deobfuscate(params[:response_order_id]).to_i : params[:response_order_id].to_i)
        response_order_id = response_order_id - EffectiveOrders.moneris[:order_nudge].to_i

        @order ||= Effective::Order.find_by_id(response_order_id)
        raise ActiveRecord::RecordNotFound unless @order

        EffectiveOrders.authorized?(self, :update, @order)

        # Store the Order Nudge if present, so we can have this information in our order_purchased hash
        params[:order_nudge] = EffectiveOrders.moneris[:order_nudge] if EffectiveOrders.moneris[:order_nudge].to_i > 0

        # Delete the Purchased and Declined Redirect URLs
        purchased_redirect_url = params.delete(:rvar_purchased_redirect_url)
        declined_redirect_url = params.delete(:rvar_declined_redirect_url)

        if params[:result].to_s == '1' && params[:transactionKey].present?
          verify_params = parse_moneris_response(send_moneris_verify_request(params[:transactionKey])) || {}

          response_code = verify_params[:response_code].to_i # Sometimes moneris sends us the string 'null'

          if response_code > 0 && response_code < 50  # Less than 50 means a successful validation
            order_purchased(params.merge(verify_params), purchased_redirect_url)
          else
            order_declined(params.merge(verify_params), declined_redirect_url)
          end
        else
          order_declined(params, declined_redirect_url)
        end
      end

      private

      def parse_moneris_response(text)
{'fraction_non_alphanumeric': 0.0761608154020385, 'fraction_numerical': 0.003963759909399773, 'mean_word_length': 3.5645994832041343, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '925607', 'n_tokens_mistral': 1059, 'n_tokens_neox': 1015, 'n_words': 207}