Class: ActiveMerchant::Billing::MitGateway

Inherits:
Gateway
  • Object
show all
Defined in:
lib/active_merchant/billing/gateways/mit.rb

Constant Summary

Constants inherited from Gateway

Gateway::CREDIT_DEPRECATION_MESSAGE, Gateway::RECURRING_DEPRECATION_MESSAGE, Gateway::STANDARD_ERROR_CODE

Instance Attribute Summary

Attributes inherited from Gateway

#options

Instance Method Summary collapse

Methods inherited from Gateway

#add_field_to_post_if_present, #add_fields_to_post_if_present, #card_brand, card_brand, #generate_unique_id, inherited, #supported_countries, supported_countries, supported_countries=, supports?, #supports_network_tokenization?, #test?

Methods included from CreditCardFormatting

#expdate, #format

Methods included from PostsData

included, #raw_ssl_request, #ssl_get, #ssl_post, #ssl_request

Constructor Details

#initialize(options = {}) ⇒ MitGateway

Returns a new instance of MitGateway.



20
21
22
23
# File 'lib/active_merchant/billing/gateways/mit.rb', line 20

def initialize(options = {})
  requires!(options, :commerce_id, :user, :api_key, :key_session)
  super
end

Instance Method Details

#authorize(money, payment, options = {}) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/active_merchant/billing/gateways/mit.rb', line 78

def authorize(money, payment, options = {})
  post = {
    operation: 'Authorize',
    commerce_id: @options[:commerce_id],
    user: @options[:user],
    apikey: @options[:api_key],
    testMode: (test? ? 'YES' : 'NO')
  }
  add_invoice(post, money, options)
  # Payments contains the card information
  add_payment(post, payment)
  add_customer_data(post, options)
  post[:key_session] = @options[:key_session]

  post_to_json = post.to_json
  post_to_json_encrypt = encrypt(post_to_json, @options[:key_session])

  final_post = '<authorization>' + post_to_json_encrypt + '</authorization><dataID>' + @options[:user] + '</dataID>'
  json_post = {}
  json_post[:payload] = final_post
  commit('sale', json_post)
end

#capture(money, authorization, options = {}) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/active_merchant/billing/gateways/mit.rb', line 101

def capture(money, authorization, options = {})
  post = {
    operation: 'Capture',
    commerce_id: @options[:commerce_id],
    user: @options[:user],
    apikey: @options[:api_key],
    testMode: (test? ? 'YES' : 'NO'),
    transaction_id: authorization,
    amount: amount(money)
  }
  post[:key_session] = @options[:key_session]

  post_to_json = post.to_json
  post_to_json_encrypt = encrypt(post_to_json, @options[:key_session])

  final_post = '<capture>' + post_to_json_encrypt + '</capture><dataID>' + @options[:user] + '</dataID>'
  json_post = {}
  json_post[:payload] = final_post
  commit('capture', json_post)
end

#cipher_keyObject



32
33
34
# File 'lib/active_merchant/billing/gateways/mit.rb', line 32

def cipher_key
  @options[:key_session]
end

#decrypt(val, keyinhex) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/active_merchant/billing/gateways/mit.rb', line 36

def decrypt(val, keyinhex)
  # Splits the first 16 bytes (the IV bytes) in array format
  unpacked = val.unpack('m')
  iv_base64 = unpacked[0].bytes.slice(0, 16)
  # Splits the second bytes (the encrypted text bytes) these would be the
  # original message
  full_data = unpacked[0].bytes.slice(16, unpacked[0].bytes.length)
  # Creates the engine
  engine = OpenSSL::Cipher::AES128.new(:CBC)
  # Set engine as decrypt mode
  engine.decrypt
  # Converts the key from hex to bytes
  engine.key = [keyinhex].pack('H*')
  # Converts the ivBase64 array into bytes
  engine.iv = iv_base64.pack('c*')
  # Decrypts the texts and returns the original string
  engine.update(full_data.pack('c*')) + engine.final
end

#encrypt(val, keyinhex) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/active_merchant/billing/gateways/mit.rb', line 55

def encrypt(val, keyinhex)
  # Creates the engine motor
  engine = OpenSSL::Cipher::AES128.new(:CBC)
  # Set engine as encrypt mode
  engine.encrypt
  # Converts the key from hex to bytes
  engine.key = [keyinhex].pack('H*')
  # Generates a random iv with this settings
  iv_rand = engine.random_iv
  # Packs IV as a Base64 string
  iv_base64 = [iv_rand].pack('m')
  # Converts the packed key into bytes
  unpacked = iv_base64.unpack('m')
  iv = unpacked[0]
  # Sets the IV into the engine
  engine.iv = iv
  # Encrypts the texts and stores the bytes
  encrypted_bytes = engine.update(val) + engine.final
  # Concatenates the (a) IV bytes and (b) the encrypted bytes then returns a
  # base64 representation
  [iv << encrypted_bytes].pack('m')
end

#purchase(money, payment, options = {}) ⇒ Object



25
26
27
28
29
30
# File 'lib/active_merchant/billing/gateways/mit.rb', line 25

def purchase(money, payment, options = {})
  MultiResponse.run do |r|
    r.process { authorize(money, payment, options) }
    r.process { capture(money, r.authorization, options) }
  end
end

#refund(money, authorization, options = {}) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/active_merchant/billing/gateways/mit.rb', line 122

def refund(money, authorization, options = {})
  post = {
    operation: 'Refund',
    commerce_id: @options[:commerce_id],
    user: @options[:user],
    apikey: @options[:api_key],
    testMode: (test? ? 'YES' : 'NO'),
    transaction_id: authorization,
    auth: authorization,
    amount: amount(money)
  }
  post[:key_session] = @options[:key_session]

  post_to_json = post.to_json
  post_to_json_encrypt = encrypt(post_to_json, @options[:key_session])

  final_post = '<refund>' + post_to_json_encrypt + '</refund><dataID>' + @options[:user] + '</dataID>'
  json_post = {}
  json_post[:payload] = final_post
  commit('refund', json_post)
end

#scrub(transcript) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/active_merchant/billing/gateways/mit.rb', line 148

def scrub(transcript)
  ret_transcript = transcript
  auth_origin = ret_transcript[/<authorization>(.*?)<\/authorization>/, 1]
  unless auth_origin.nil?
    auth_decrypted = decrypt(auth_origin, @options[:key_session])
    auth_json = JSON.parse(auth_decrypted)
    auth_json['card'] = '[FILTERED]'
    auth_json['cvv'] = '[FILTERED]'
    auth_json['apikey'] = '[FILTERED]'
    auth_json['key_session'] = '[FILTERED]'
    auth_to_json = auth_json.to_json
    auth_tagged = '<authorization>' + auth_to_json + '</authorization>'
    ret_transcript = ret_transcript.gsub(/<authorization>(.*?)<\/authorization>/, auth_tagged)
  end

  cap_origin = ret_transcript[/<capture>(.*?)<\/capture>/, 1]
  unless cap_origin.nil?
    cap_decrypted = decrypt(cap_origin, @options[:key_session])
    cap_json = JSON.parse(cap_decrypted)
    cap_json['apikey'] = '[FILTERED]'
    cap_json['key_session'] = '[FILTERED]'
    cap_to_json = cap_json.to_json
    cap_tagged = '<capture>' + cap_to_json + '</capture>'
    ret_transcript = ret_transcript.gsub(/<capture>(.*?)<\/capture>/, cap_tagged)
  end

  ref_origin = ret_transcript[/<refund>(.*?)<\/refund>/, 1]
  unless ref_origin.nil?
    ref_decrypted = decrypt(ref_origin, @options[:key_session])
    ref_json = JSON.parse(ref_decrypted)
    ref_json['apikey'] = '[FILTERED]'
    ref_json['key_session'] = '[FILTERED]'
    ref_to_json = ref_json.to_json
    ref_tagged = '<refund>' + ref_to_json + '</refund>'
    ret_transcript = ret_transcript.gsub(/<refund>(.*?)<\/refund>/, ref_tagged)
  end

  res_origin = ret_transcript[/#{Regexp.escape('reading ')}(.*?)#{Regexp.escape('read')}/m, 1]
  loop do
    break if res_origin.nil?

    resp_origin = res_origin[/#{Regexp.escape('"')}(.*?)#{Regexp.escape('"')}/m, 1]
    resp_decrypted = decrypt(resp_origin, @options[:key_session])
    ret_transcript[/#{Regexp.escape('reading ')}(.*?)#{Regexp.escape('read')}/m, 1] = resp_decrypted
    ret_transcript = ret_transcript.sub('reading ', 'response: ')
    res_origin = ret_transcript[/#{Regexp.escape('reading ')}(.*?)#{Regexp.escape('read')}/m, 1]
  end

  ret_transcript
end

#supports_scrubbing?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/active_merchant/billing/gateways/mit.rb', line 144

def supports_scrubbing?
  true
end