<commit_before>require 'net/http'

module ActiveMerchant #:nodoc:
  module Billing #:nodoc:
    module Integrations #:nodoc:
      module Allpay
        class Notification < ActiveMerchant::Billing::Integrations::Notification

          def status
            if rtn_code == '1'
              true
            else
              false
            end
          end

          def checksum_ok?
            checksum = @params.delete('CheckMacValue')

            @params.delete('controller')
            @params.delete('action')

            raw_data = @params.map do |x, y|
              "#{x}=#{y}"
            end.join('&')

            hash_raw_data = "HashKey=#{ActiveMerchant::Billing::Integrations::Allpay.hash_key}&#{raw_data}&HashIV=#{ActiveMerchant::Billing::Integrations::Allpay.hash_iv}"

            url_endcode_data = (CGI::escape(hash_raw_data)).downcase

            (Digest::MD5.hexdigest(url_endcode_data) == checksum.to_s.downcase)
          end

          def initialize(params)
            self.params = params
          end

          def params=(params)
            @params = params.inject({}) do |buffer, (name, value)|
              buffer.merge(name.to_s => value)
            end
          end

          def rtn_code
            @params['RtnCode']
          end

          def merchant_id
            @params['MerchantID']
          end

          def merchant_trade_no
            @params['MerchantTradeNo']<commit_msg>Fix bug - 修復 notification check sum 檢查問題, 先 sort params by hash key<commit_after>require 'net/http'

module ActiveMerchant #:nodoc:
  module Billing #:nodoc:
    module Integrations #:nodoc:
      module Allpay
        class Notification < ActiveMerchant::Billing::Integrations::Notification

          def status
            if rtn_code == '1'
              true
            else
              false
            end
          end

          def checksum_ok?
            checksum = @params.delete('CheckMacValue')

            @params.delete('controller')
            @params.delete('action')

            # 把 params 轉成 query string 前必須先依照 hash key 做 sort
            raw_data = Hash[@params.sort].map do |x, y|
              "#{x}=#{y}"
            end.join('&')

            hash_raw_data = "HashKey=#{ActiveMerchant::Billing::Integrations::Allpay.hash_key}&#{raw_data}&HashIV=#{ActiveMerchant::Billing::Integrations::Allpay.hash_iv}"

            url_endcode_data = (CGI::escape(hash_raw_data)).downcase

            (Digest::MD5.hexdigest(url_endcode_data) == checksum.to_s.downcase)
          end

          def initialize(params)
            self.params = params
          end

          def params=(params)
            @params = params.inject({}) do |buffer, (name, value)|
              buffer.merge(name.to_s => value)
            end
          end

          def rtn_code
            @params['RtnCode']
          end

          def merchant_id
            @params['MerchantID']
          end

          def merchant_trade_no
            @params['MerchantTradeNo']
{'fraction_non_alphanumeric': 0.10599536270288175, 'fraction_numerical': 0.001324942033786022, 'mean_word_length': 1.788550323176362, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '3871412', 'n_tokens_mistral': 880, 'n_tokens_neox': 865, 'n_words': 169}