<commit_before>require 'net/http'

module ActiveMerchant #:nodoc:
  module Billing #:nodoc:
    module Integrations #:nodoc:
      module SmilePay
        class Notification < ActiveMerchant::Billing::Integrations::Notification
          attr_accessor :custom_user_confirmation_param #商家認證參數

          # TODO credit card
          def complete?
            # No status except for credit card
            true
          end

          def item_id
            # 訂單號碼
            params['Data_id']
          end

          def transaction_id
            # Smile Pay 端訂單號碼
            params['Smseid']
          end

          # When was this payment received by the client.
          def received_at
            params['Process_date'] + params['Process_time']
          end

          def payer_email
            params['Email']
          end

          def receiver_email
            nil
          end

          def security_key
            # 驗證碼
            params['Mid_smilepay']
          end

          # the money amount we received in X.2 decimal.
          def gross
            ::Money.new(params['Amount'].to_i * 100, currency)
          end

          # Was this a test transaction?
          def test?
            ActiveMerchant::Billing::Base.integration_mode == :test
          end

          def status
            'Completed'
          end

          def currency
            case params['Moneytype']
            when 'TW'
              'TWD'
            when 'CN'
              'CNY'
            end
          end

          # SmilePay 沒有遠端驗證功能，
          # 而以認證碼代替
          def acknowledge
            # TODO 使用查詢功能實作 acknowledge
            params[:Mid_smilepay].to_i == calculated_mid_smile_key
          end

          private

          def calculated_mid_smile_key
            b = "%08d" % (gross().dollars).to_i
            c = params['Smseid'][-4..-1].gsub(/\D/,'9')
            d = ( custom_user_confirmation_param() + b + c ).chars.to_a

            # 偶數位數字（從左算起）
            sum_even = d.values_at(* d.each_index.select(&:odd?)).compact.map(&:to_i).inject{|sum,x| sum + x }
            # 奇數位數字（從左算起）
            sum_odd = d.values_at(* d.each_index.select(&:even?)).compact.map(&:to_i).inject{|sum,x| sum + x }<commit_msg>SmilePay will send invalid Mid_smilepay in test env. Therefore we always set it to true.<commit_after>require 'net/http'

module ActiveMerchant #:nodoc:
  module Billing #:nodoc:
    module Integrations #:nodoc:
      module SmilePay
        class Notification < ActiveMerchant::Billing::Integrations::Notification
          attr_accessor :custom_user_confirmation_param #商家認證參數

          # TODO credit card
          def complete?
            # No status except for credit card
            true
          end

          def item_id
            # 訂單號碼
            params['Data_id']
          end

          def transaction_id
            # Smile Pay 端訂單號碼
            params['Smseid']
          end

          # When was this payment received by the client.
          def received_at
            params['Process_date'] + params['Process_time']
          end

          def payer_email
            params['Email']
          end

          def receiver_email
            nil
          end

          def security_key
            # 驗證碼
            params['Mid_smilepay']
          end

          # the money amount we received in X.2 decimal.
          def gross
            ::Money.new(params['Amount'].to_i * 100, currency)
          end

          # Was this a test transaction?
          def test?
            ActiveMerchant::Billing::Base.integration_mode == :test
          end

          def status
            'Completed'
          end

          def currency
            case params['Moneytype']
            when 'TW'
              'TWD'
            when 'CN'
              'CNY'
            end
          end

          # SmilePay 沒有遠端驗證功能，
          # 而以認證碼代替
          def acknowledge
            if test? # SmilePay 客服回答測試環境時認證碼只會傳0
              true
            else
              # TODO 使用查詢功能實作 acknowledge
              params[:Mid_smilepay].to_i == calculated_mid_smile_key
            end
          end

          private

          def calculated_mid_smile_key
            b = "%08d" % (gross().dollars).to_i
            c = params['Smseid'][-4..-1].gsub(/\D/,'9')
            d = ( custom_user_confirmation_param() + b + c ).chars.to_a

            # 偶數位數字（從左算起）
            sum_even = d.values_at(* d.each_index.select(&:odd?)).compact.map(&:to_i).inject{|sum,x| sum + x }
            # 奇數位數字（從左算起）
            sum_odd = d.values_at(* d.each_index.select(&:even?)).compact.map(&:to_i).inject{|sum,x| sum + x }
{'fraction_non_alphanumeric': 0.0876068376068376, 'fraction_numerical': 0.00405982905982906, 'mean_word_length': 1.4241325737959607, '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': '6614769', 'n_tokens_mistral': 1473, 'n_tokens_neox': 1460, 'n_words': 313}