activemerchant 1.118.0 → 1.123.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG +185 -0
  3. data/README.md +4 -2
  4. data/lib/active_merchant/billing/check.rb +19 -12
  5. data/lib/active_merchant/billing/credit_card.rb +4 -0
  6. data/lib/active_merchant/billing/credit_card_formatting.rb +1 -0
  7. data/lib/active_merchant/billing/credit_card_methods.rb +44 -17
  8. data/lib/active_merchant/billing/gateways/adyen.rb +36 -19
  9. data/lib/active_merchant/billing/gateways/authorize_net.rb +19 -11
  10. data/lib/active_merchant/billing/gateways/authorize_net_cim.rb +3 -0
  11. data/lib/active_merchant/billing/gateways/blue_pay.rb +29 -0
  12. data/lib/active_merchant/billing/gateways/blue_snap.rb +2 -2
  13. data/lib/active_merchant/billing/gateways/braintree_blue.rb +52 -8
  14. data/lib/active_merchant/billing/gateways/cashnet.rb +7 -2
  15. data/lib/active_merchant/billing/gateways/checkout_v2.rb +31 -0
  16. data/lib/active_merchant/billing/gateways/credorax.rb +15 -9
  17. data/lib/active_merchant/billing/gateways/cyber_source.rb +53 -6
  18. data/lib/active_merchant/billing/gateways/d_local.rb +1 -1
  19. data/lib/active_merchant/billing/gateways/decidir.rb +15 -3
  20. data/lib/active_merchant/billing/gateways/elavon.rb +76 -25
  21. data/lib/active_merchant/billing/gateways/element.rb +2 -0
  22. data/lib/active_merchant/billing/gateways/eway_rapid.rb +13 -0
  23. data/lib/active_merchant/billing/gateways/forte.rb +12 -0
  24. data/lib/active_merchant/billing/gateways/global_collect.rb +25 -16
  25. data/lib/active_merchant/billing/gateways/hps.rb +64 -1
  26. data/lib/active_merchant/billing/gateways/kushki.rb +23 -0
  27. data/lib/active_merchant/billing/gateways/litle.rb +4 -4
  28. data/lib/active_merchant/billing/gateways/mercado_pago.rb +5 -4
  29. data/lib/active_merchant/billing/gateways/merchant_warrior.rb +2 -0
  30. data/lib/active_merchant/billing/gateways/moka.rb +277 -0
  31. data/lib/active_merchant/billing/gateways/monei.rb +228 -144
  32. data/lib/active_merchant/billing/gateways/mundipagg.rb +14 -5
  33. data/lib/active_merchant/billing/gateways/netbanx.rb +37 -2
  34. data/lib/active_merchant/billing/gateways/nmi.rb +14 -9
  35. data/lib/active_merchant/billing/gateways/orbital.rb +127 -64
  36. data/lib/active_merchant/billing/gateways/pay_arc.rb +390 -0
  37. data/lib/active_merchant/billing/gateways/pay_trace.rb +404 -0
  38. data/lib/active_merchant/billing/gateways/payeezy.rb +34 -6
  39. data/lib/active_merchant/billing/gateways/payflow/payflow_common_api.rb +1 -0
  40. data/lib/active_merchant/billing/gateways/payflow.rb +9 -0
  41. data/lib/active_merchant/billing/gateways/payment_express.rb +10 -5
  42. data/lib/active_merchant/billing/gateways/paymentez.rb +5 -0
  43. data/lib/active_merchant/billing/gateways/paypal_express.rb +1 -0
  44. data/lib/active_merchant/billing/gateways/paysafe.rb +291 -0
  45. data/lib/active_merchant/billing/gateways/payu_latam.rb +3 -3
  46. data/lib/active_merchant/billing/gateways/payway_dot_com.rb +253 -0
  47. data/lib/active_merchant/billing/gateways/qvalent.rb +23 -9
  48. data/lib/active_merchant/billing/gateways/redsys.rb +44 -25
  49. data/lib/active_merchant/billing/gateways/safe_charge.rb +20 -11
  50. data/lib/active_merchant/billing/gateways/spreedly_core.rb +13 -4
  51. data/lib/active_merchant/billing/gateways/stripe.rb +8 -8
  52. data/lib/active_merchant/billing/gateways/stripe_payment_intents.rb +79 -25
  53. data/lib/active_merchant/billing/gateways/usa_epay_transaction.rb +1 -1
  54. data/lib/active_merchant/billing/gateways/vpos.rb +220 -0
  55. data/lib/active_merchant/billing/gateways/worldpay.rb +64 -19
  56. data/lib/active_merchant/billing/three_d_secure_eci_mapper.rb +27 -0
  57. data/lib/active_merchant/billing.rb +1 -0
  58. data/lib/active_merchant/version.rb +1 -1
  59. data/lib/certs/cacert.pem +1582 -2431
  60. metadata +10 -3
@@ -143,15 +143,24 @@ module ActiveMerchant #:nodoc:
143
143
  post[:sg_Descriptor] = options[:merchant_descriptor] if options[:merchant_descriptor]
144
144
  post[:sg_MerchantPhoneNumber] = options[:merchant_phone_number] if options[:merchant_phone_number]
145
145
  post[:sg_MerchantName] = options[:merchant_name] if options[:merchant_name]
146
+ post[:sg_ProductID] = options[:product_id] if options[:product_id]
146
147
  end
147
148
 
148
149
  def add_payment(post, payment, options = {})
149
- post[:sg_NameOnCard] = payment.name
150
- post[:sg_CardNumber] = payment.number
151
150
  post[:sg_ExpMonth] = format(payment.month, :two_digits)
152
151
  post[:sg_ExpYear] = format(payment.year, :two_digits)
153
- post[:sg_CVV2] = payment.verification_value
154
- post[:sg_StoredCredentialMode] = (options[:stored_credential_mode] == true ? 1 : 0)
152
+ post[:sg_CardNumber] = payment.number
153
+
154
+ if payment.is_a?(NetworkTokenizationCreditCard) && payment.source == :network_token
155
+ post[:sg_CAVV] = payment.payment_cryptogram
156
+ post[:sg_ECI] = options[:three_d_secure] && options[:three_d_secure][:eci] || '05'
157
+ post[:sg_IsExternalMPI] = 1
158
+ post[:sg_ExternalTokenProvider] = 5
159
+ else
160
+ post[:sg_CVV2] = payment.verification_value
161
+ post[:sg_NameOnCard] = payment.name
162
+ post[:sg_StoredCredentialMode] = (options[:stored_credential_mode] == true ? 1 : 0)
163
+ end
155
164
  end
156
165
 
157
166
  def add_customer_details(post, payment, options)
@@ -170,14 +179,14 @@ module ActiveMerchant #:nodoc:
170
179
  end
171
180
 
172
181
  def add_external_mpi_data(post, options)
173
- version = options[:three_d_secure][:ds_transaction_id] ? '2' : '1'
174
-
175
- post[:sg_eci] = options[:three_d_secure][:eci] if options[:three_d_secure][:eci]
176
- post[:sg_cavv] = options[:three_d_secure][:cavv] if options[:three_d_secure][:cavv]
177
- post[:sg_dsTransID] = options[:three_d_secure][:ds_transaction_id] if version == '2'
178
- post[:sg_threeDSProtocolVersion] = version
179
- post[:sg_xid] = options[:three_d_secure][:xid] if version == '1'
182
+ post[:sg_ECI] = options[:three_d_secure][:eci] if options[:three_d_secure][:eci]
183
+ post[:sg_CAVV] = options[:three_d_secure][:cavv] if options[:three_d_secure][:cavv]
184
+ post[:sg_dsTransID] = options[:three_d_secure][:ds_transaction_id] if options[:three_d_secure][:ds_transaction_id]
185
+ post[:sg_threeDSProtocolVersion] = options[:three_d_secure][:ds_transaction_id] ? '2' : '1'
186
+ post[:sg_Xid] = options[:three_d_secure][:xid]
180
187
  post[:sg_IsExternalMPI] = 1
188
+ post[:sg_EnablePartialApproval] = options[:is_partial_approval]
189
+ post[:sg_challengePreference] = options[:three_d_secure][:challenge_preference] if options[:three_d_secure][:challenge_preference]
181
190
  end
182
191
 
183
192
  def parse(xml)
@@ -254,11 +254,20 @@ module ActiveMerchant #:nodoc:
254
254
  end
255
255
 
256
256
  def childnode_to_response(response, node, childnode)
257
- name = "#{node.name.downcase}_#{childnode.name.downcase}"
258
- if name == 'payment_method_data' && !childnode.elements.empty?
259
- response[name.to_sym] = Hash.from_xml(childnode.to_s).values.first
257
+ node_name = node.name.downcase
258
+ childnode_name = childnode.name.downcase
259
+ composed_name = "#{node_name}_#{childnode_name}"
260
+
261
+ childnodes_present = !childnode.elements.empty?
262
+
263
+ if childnodes_present && composed_name == 'payment_method_data'
264
+ response[composed_name.to_sym] = Hash.from_xml(childnode.to_s).values.first
265
+ elsif childnodes_present && node_name == 'gateway_specific_response_fields'
266
+ response[node_name.to_sym] = {
267
+ childnode_name => Hash.from_xml(childnode.to_s).values.first
268
+ }
260
269
  else
261
- response[name.to_sym] = childnode.text
270
+ response[composed_name.to_sym] = childnode.text
262
271
  end
263
272
  end
264
273
 
@@ -279,14 +279,14 @@ module ActiveMerchant #:nodoc:
279
279
  transcript.
280
280
  gsub(%r((Authorization: Basic )\w+), '\1[FILTERED]').
281
281
  gsub(%r((&?three_d_secure\[cryptogram\]=)[\w=]*(&?)), '\1[FILTERED]\2').
282
- gsub(%r((card\[cryptogram\]=)[^&]+(&?)), '\1[FILTERED]\2').
283
- gsub(%r((card\[cvc\]=)\d+), '\1[FILTERED]').
284
- gsub(%r((card\[emv_approval_data\]=)[^&]+(&?)), '\1[FILTERED]\2').
285
- gsub(%r((card\[emv_auth_data\]=)[^&]+(&?)), '\1[FILTERED]\2').
286
- gsub(%r((card\[encrypted_pin\]=)[^&]+(&?)), '\1[FILTERED]\2').
287
- gsub(%r((card\[encrypted_pin_key_id\]=)[\w=]+(&?)), '\1[FILTERED]\2').
288
- gsub(%r((card\[number\]=)\d+), '\1[FILTERED]').
289
- gsub(%r((card\[swipe_data\]=)[^&]+(&?)), '\1[FILTERED]\2')
282
+ gsub(%r(((\[card\]|card)\[cryptogram\]=)[^&]+(&?)), '\1[FILTERED]\3').
283
+ gsub(%r(((\[card\]|card)\[cvc\]=)\d+), '\1[FILTERED]').
284
+ gsub(%r(((\[card\]|card)\[emv_approval_data\]=)[^&]+(&?)), '\1[FILTERED]\3').
285
+ gsub(%r(((\[card\]|card)\[emv_auth_data\]=)[^&]+(&?)), '\1[FILTERED]\3').
286
+ gsub(%r(((\[card\]|card)\[encrypted_pin\]=)[^&]+(&?)), '\1[FILTERED]\3').
287
+ gsub(%r(((\[card\]|card)\[encrypted_pin_key_id\]=)[\w=]+(&?)), '\1[FILTERED]\3').
288
+ gsub(%r(((\[card\]|card)\[number\]=)\d+), '\1[FILTERED]').
289
+ gsub(%r(((\[card\]|card)\[swipe_data\]=)[^&]+(&?)), '\1[FILTERED]\3')
290
290
  end
291
291
 
292
292
  def supports_network_tokenization?
@@ -30,6 +30,8 @@ module ActiveMerchant #:nodoc:
30
30
  add_shipping_address(post, options)
31
31
  setup_future_usage(post, options)
32
32
  add_exemption(post, options)
33
+ add_stored_credentials(post, options)
34
+ add_ntid(post, options)
33
35
  add_error_on_requires_action(post, options)
34
36
  request_three_d_secure(post, options)
35
37
 
@@ -56,16 +58,22 @@ module ActiveMerchant #:nodoc:
56
58
  end
57
59
 
58
60
  def create_payment_method(payment_method, options = {})
59
- post = {}
60
- post[:type] = 'card'
61
- post[:card] = {}
62
- post[:card][:number] = payment_method.number
63
- post[:card][:exp_month] = payment_method.month
64
- post[:card][:exp_year] = payment_method.year
65
- post[:card][:cvc] = payment_method.verification_value if payment_method.verification_value
66
- add_billing_address(post, options)
61
+ post_data = create_payment_method_data(payment_method, options)
62
+
67
63
  options = format_idempotency_key(options, 'pm')
68
- commit(:post, 'payment_methods', post, options)
64
+ commit(:post, 'payment_methods', post_data, options)
65
+ end
66
+
67
+ def create_payment_method_data(payment_method, options = {})
68
+ post_data = {}
69
+ post_data[:type] = 'card'
70
+ post_data[:card] = {}
71
+ post_data[:card][:number] = payment_method.number
72
+ post_data[:card][:exp_month] = payment_method.month
73
+ post_data[:card][:exp_year] = payment_method.year
74
+ post_data[:card][:cvc] = payment_method.verification_value if payment_method.verification_value
75
+ add_billing_address(post_data, options)
76
+ post_data
69
77
  end
70
78
 
71
79
  def update_intent(money, intent_id, payment_method, options = {})
@@ -102,6 +110,17 @@ module ActiveMerchant #:nodoc:
102
110
  commit(:post, 'setup_intents', post, options)
103
111
  end
104
112
 
113
+ def retrieve_setup_intent(setup_intent_id)
114
+ # Retrieving a setup_intent passing 'expand[]=latest_attempt' allows the caller to
115
+ # check for a network_transaction_id and ds_transaction_id
116
+ # eg (latest_attempt -> payment_method_details -> card -> network_transaction_id)
117
+ #
118
+ # Being able to retrieve these fields enables payment flows that rely on MIT exemptions, e.g: off_session
119
+ commit(:post, "setup_intents/#{setup_intent_id}", {
120
+ 'expand[]': 'latest_attempt'
121
+ }, {})
122
+ end
123
+
105
124
  def authorize(money, payment_method, options = {})
106
125
  create_intent(money, payment_method, options.merge!(confirm: true, capture_method: 'manual'))
107
126
  end
@@ -165,7 +184,6 @@ module ActiveMerchant #:nodoc:
165
184
  if options[:customer]
166
185
  customer_id = options[:customer]
167
186
  else
168
- post[:validate] = options[:validate] unless options[:validate].nil?
169
187
  post[:description] = options[:description] if options[:description]
170
188
  post[:email] = options[:email] if options[:email]
171
189
  options = format_idempotency_key(options, 'customer')
@@ -173,7 +191,9 @@ module ActiveMerchant #:nodoc:
173
191
  customer_id = customer.params['id']
174
192
  end
175
193
  options = format_idempotency_key(options, 'attach')
176
- commit(:post, "payment_methods/#{params[:payment_method]}/attach", { customer: customer_id }, options)
194
+ attach_parameters = { customer: customer_id }
195
+ attach_parameters[:validate] = options[:validate] unless options[:validate].nil?
196
+ commit(:post, "payment_methods/#{params[:payment_method]}/attach", attach_parameters, options)
177
197
  else
178
198
  super(payment_method, options)
179
199
  end
@@ -194,6 +214,10 @@ module ActiveMerchant #:nodoc:
194
214
 
195
215
  private
196
216
 
217
+ def off_session_request?(options = {})
218
+ (options[:off_session] || options[:setup_future_usage]) && options[:confirm] == true
219
+ end
220
+
197
221
  def add_connected_account(post, options = {})
198
222
  super(post, options)
199
223
  post[:application_fee_amount] = options[:application_fee] if options[:application_fee]
@@ -201,25 +225,21 @@ module ActiveMerchant #:nodoc:
201
225
 
202
226
  def add_whitelisted_attribute(post, options, attribute)
203
227
  post[attribute] = options[attribute] if options[attribute]
204
- post
205
228
  end
206
229
 
207
230
  def add_capture_method(post, options)
208
231
  capture_method = options[:capture_method].to_s
209
232
  post[:capture_method] = capture_method if ALLOWED_METHOD_STATES.include?(capture_method)
210
- post
211
233
  end
212
234
 
213
235
  def add_confirmation_method(post, options)
214
236
  confirmation_method = options[:confirmation_method].to_s
215
237
  post[:confirmation_method] = confirmation_method if ALLOWED_METHOD_STATES.include?(confirmation_method)
216
- post
217
238
  end
218
239
 
219
240
  def add_customer(post, options)
220
241
  customer = options[:customer].to_s
221
242
  post[:customer] = customer if customer.start_with?('cus_')
222
- post
223
243
  end
224
244
 
225
245
  def add_return_url(post, options)
@@ -227,22 +247,27 @@ module ActiveMerchant #:nodoc:
227
247
 
228
248
  post[:confirm] = options[:confirm]
229
249
  post[:return_url] = options[:return_url] if options[:return_url]
230
- post
231
250
  end
232
251
 
233
252
  def add_payment_method_token(post, payment_method, options)
234
253
  return if payment_method.nil?
235
254
 
236
255
  if payment_method.is_a?(ActiveMerchant::Billing::CreditCard)
237
- p = create_payment_method(payment_method, options)
238
- return p unless p.success?
256
+ if off_session_request?(options)
257
+ post[:payment_method_data] = create_payment_method_data(payment_method, options)
258
+ return
259
+ else
260
+ p = create_payment_method(payment_method, options)
261
+ return p unless p.success?
239
262
 
240
- payment_method = p.params['id']
263
+ payment_method = p.params['id']
264
+ end
241
265
  end
242
266
 
243
- if payment_method.is_a?(StripePaymentToken)
267
+ case payment_method
268
+ when StripePaymentToken
244
269
  post[:payment_method] = payment_method.payment_data['id']
245
- elsif payment_method.is_a?(String)
270
+ when String
246
271
  if payment_method.include?('|')
247
272
  customer_id, payment_method_id = payment_method.split('|')
248
273
  token = payment_method_id
@@ -252,6 +277,8 @@ module ActiveMerchant #:nodoc:
252
277
  end
253
278
  post[:payment_method] = token
254
279
  end
280
+
281
+ post
255
282
  end
256
283
 
257
284
  def add_payment_method_types(post, options)
@@ -259,7 +286,6 @@ module ActiveMerchant #:nodoc:
259
286
  return if payment_method_types.nil?
260
287
 
261
288
  post[:payment_method_types] = Array(payment_method_types)
262
- post
263
289
  end
264
290
 
265
291
  def add_exemption(post, options = {})
@@ -270,6 +296,36 @@ module ActiveMerchant #:nodoc:
270
296
  post[:payment_method_options][:card][:moto] = true if options[:moto]
271
297
  end
272
298
 
299
+ # Stripe Payment Intents does not pass any parameters for cardholder/merchant initiated
300
+ # it also does not support installments for any country other than Mexico (reason for this is unknown)
301
+ # The only thing that Stripe PI requires for stored credentials to work currently is the network_transaction_id
302
+ # network_transaction_id is created when the card is authenticated using the field `setup_for_future_usage` with the value `off_session` see def setup_future_usage below
303
+
304
+ def add_stored_credentials(post, options = {})
305
+ return unless options[:stored_credential] && !options[:stored_credential].values.all?(&:nil?)
306
+
307
+ stored_credential = options[:stored_credential]
308
+ post[:payment_method_options] ||= {}
309
+ post[:payment_method_options][:card] ||= {}
310
+ post[:payment_method_options][:card][:mit_exemption] = {}
311
+
312
+ # Stripe PI accepts network_transaction_id and ds_transaction_id via mit field under card.
313
+ # The network_transaction_id can be sent in nested under stored credentials OR as its own field (add_ntid handles when it is sent in on its own)
314
+ # If it is sent is as its own field AND under stored credentials, the value sent under its own field is what will send.
315
+ post[:payment_method_options][:card][:mit_exemption][:ds_transaction_id] = stored_credential[:ds_transaction_id] if stored_credential[:ds_transaction_id]
316
+ post[:payment_method_options][:card][:mit_exemption][:network_transaction_id] = stored_credential[:network_transaction_id] if stored_credential[:network_transaction_id]
317
+ end
318
+
319
+ def add_ntid(post, options = {})
320
+ return unless options[:network_transaction_id]
321
+
322
+ post[:payment_method_options] ||= {}
323
+ post[:payment_method_options][:card] ||= {}
324
+ post[:payment_method_options][:card][:mit_exemption] = {}
325
+
326
+ post[:payment_method_options][:card][:mit_exemption][:network_transaction_id] = options[:network_transaction_id] if options[:network_transaction_id]
327
+ end
328
+
273
329
  def add_error_on_requires_action(post, options = {})
274
330
  return unless options[:confirm]
275
331
 
@@ -299,7 +355,7 @@ module ActiveMerchant #:nodoc:
299
355
 
300
356
  def setup_future_usage(post, options = {})
301
357
  post[:setup_future_usage] = options[:setup_future_usage] if %w(on_session off_session).include?(options[:setup_future_usage])
302
- post[:off_session] = options[:off_session] if options[:off_session] && options[:confirm] == true
358
+ post[:off_session] = options[:off_session] if off_session_request?(options)
303
359
  post
304
360
  end
305
361
 
@@ -317,7 +373,6 @@ module ActiveMerchant #:nodoc:
317
373
  post[:billing_details][:email] = billing[:email] if billing[:email]
318
374
  post[:billing_details][:name] = billing[:name] if billing[:name]
319
375
  post[:billing_details][:phone] = billing[:phone] if billing[:phone]
320
- post
321
376
  end
322
377
 
323
378
  def add_shipping_address(post, options = {})
@@ -336,7 +391,6 @@ module ActiveMerchant #:nodoc:
336
391
  post[:shipping][:carrier] = shipping[:carrier] if shipping[:carrier]
337
392
  post[:shipping][:phone] = shipping[:phone] if shipping[:phone]
338
393
  post[:shipping][:tracking_number] = shipping[:tracking_number] if shipping[:tracking_number]
339
- post
340
394
  end
341
395
 
342
396
  def format_idempotency_key(options, suffix)
@@ -343,7 +343,7 @@ module ActiveMerchant #:nodoc:
343
343
  parameters[:software] = 'Active Merchant'
344
344
  parameters[:testmode] = (@options[:test] ? 1 : 0) unless parameters.has_key?(:testmode)
345
345
  seed = SecureRandom.hex(32).upcase
346
- hash = Digest::SHA1.hexdigest("#{parameters[:command]}:#{@options[:password]}:#{parameters[:amount]}:#{parameters[:invoice]}:#{seed}")
346
+ hash = Digest::SHA1.hexdigest("#{parameters[:command]}:#{@options[:pin] || @options[:password]}:#{parameters[:amount]}:#{parameters[:invoice]}:#{seed}")
347
347
  parameters[:hash] = "s/#{seed}/#{hash}/n"
348
348
 
349
349
  parameters.collect { |key, value| "UM#{key}=#{CGI.escape(value.to_s)}" }.join('&')
@@ -0,0 +1,220 @@
1
+ require 'digest'
2
+ require 'jwe'
3
+
4
+ module ActiveMerchant #:nodoc:
5
+ module Billing #:nodoc:
6
+ class VposGateway < Gateway
7
+ self.test_url = 'https://vpos.infonet.com.py:8888'
8
+ self.live_url = 'https://vpos.infonet.com.py'
9
+
10
+ self.supported_countries = ['PY']
11
+ self.default_currency = 'PYG'
12
+ self.supported_cardtypes = %i[visa master]
13
+
14
+ self.homepage_url = 'https://comercios.bancard.com.py'
15
+ self.display_name = 'vPOS'
16
+
17
+ self.money_format = :dollars
18
+
19
+ ENDPOINTS = {
20
+ pci_encryption_key: '/vpos/api/0.3/application/encryption-key',
21
+ pay_pci_buy_encrypted: '/vpos/api/0.3/pci/encrypted',
22
+ pci_buy_rollback: '/vpos/api/0.3/pci_buy/rollback',
23
+ refund: '/vpos/api/0.3/refunds'
24
+ }
25
+
26
+ def initialize(options = {})
27
+ requires!(options, :private_key, :public_key)
28
+ @private_key = options[:private_key]
29
+ @public_key = options[:public_key]
30
+ @shop_process_id = options[:shop_process_id] || SecureRandom.random_number(10**15)
31
+ super
32
+ end
33
+
34
+ def purchase(money, payment, options = {})
35
+ commerce = options[:commerce] || @options[:commerce]
36
+ commerce_branch = options[:commerce_branch] || @options[:commerce_branch]
37
+ shop_process_id = options[:shop_process_id] || @shop_process_id
38
+
39
+ token = generate_token(shop_process_id, 'pay_pci', commerce, commerce_branch, amount(money), currency(money))
40
+
41
+ post = {}
42
+ post[:token] = token
43
+ post[:commerce] = commerce.to_s
44
+ post[:commerce_branch] = commerce_branch.to_s
45
+ post[:shop_process_id] = shop_process_id
46
+ post[:number_of_payments] = options[:number_of_payments] || 1
47
+ post[:recursive] = options[:recursive] || false
48
+
49
+ add_invoice(post, money, options)
50
+ add_card_data(post, payment)
51
+ add_customer_data(post, options)
52
+
53
+ commit(:pay_pci_buy_encrypted, post)
54
+ end
55
+
56
+ def void(authorization, options = {})
57
+ _, shop_process_id = authorization.to_s.split('#')
58
+ token = generate_token(shop_process_id, 'rollback', '0.00')
59
+ post = {
60
+ token: token,
61
+ shop_process_id: shop_process_id
62
+ }
63
+ commit(:pci_buy_rollback, post)
64
+ end
65
+
66
+ def credit(money, payment, options = {})
67
+ # Not permitted for foreign cards.
68
+ commerce = options[:commerce] || @options[:commerce]
69
+ commerce_branch = options[:commerce_branch] || @options[:commerce_branch]
70
+
71
+ token = generate_token(@shop_process_id, 'refund', commerce, commerce_branch, amount(money), currency(money))
72
+ post = {}
73
+ post[:token] = token
74
+ post[:commerce] = commerce.to_i
75
+ post[:commerce_branch] = commerce_branch.to_i
76
+ post[:shop_process_id] = @shop_process_id
77
+ add_invoice(post, money, options)
78
+ add_card_data(post, payment)
79
+ add_customer_data(post, options)
80
+ post[:origin_shop_process_id] = options[:original_shop_process_id] if options[:original_shop_process_id]
81
+ commit(:refund, post)
82
+ end
83
+
84
+ def refund(money, authorization, options = {})
85
+ commerce = options[:commerce] || @options[:commerce]
86
+ commerce_branch = options[:commerce_branch] || @options[:commerce_branch]
87
+ shop_process_id = options[:shop_process_id] || @shop_process_id
88
+ _, original_shop_process_id = authorization.to_s.split('#')
89
+
90
+ token = generate_token(shop_process_id, 'refund', commerce, commerce_branch, amount(money), currency(money))
91
+ post = {}
92
+ post[:token] = token
93
+ post[:commerce] = commerce.to_i
94
+ post[:commerce_branch] = commerce_branch.to_i
95
+ post[:shop_process_id] = shop_process_id
96
+ add_invoice(post, money, options)
97
+ add_customer_data(post, options)
98
+ post[:origin_shop_process_id] = original_shop_process_id || options[:original_shop_process_id]
99
+ commit(:refund, post)
100
+ end
101
+
102
+ def supports_scrubbing?
103
+ true
104
+ end
105
+
106
+ def scrub(transcript)
107
+ clean_transcript = remove_invalid_utf_8_byte_sequences(transcript)
108
+ clean_transcript.
109
+ gsub(/(token\\":\\")[.\-\w]+/, '\1[FILTERED]').
110
+ gsub(/(card_encrypted_data\\":\\")[.\-\w]+/, '\1[FILTERED]')
111
+ end
112
+
113
+ def remove_invalid_utf_8_byte_sequences(transcript)
114
+ transcript.encode('UTF-8', 'binary', undef: :replace, replace: '')
115
+ end
116
+
117
+ private
118
+
119
+ # Required to encrypt PAN data.
120
+ def one_time_public_key
121
+ token = generate_token('get_encription_public_key', @public_key)
122
+ response = commit(:pci_encryption_key, token: token)
123
+ OpenSSL::PKey::RSA.new(response.params['encryption_key'])
124
+ end
125
+
126
+ def generate_token(*elements)
127
+ Digest::MD5.hexdigest(@private_key + elements.join)
128
+ end
129
+
130
+ def add_invoice(post, money, options)
131
+ post[:amount] = amount(money)
132
+ post[:currency] = options[:currency] || currency(money)
133
+ end
134
+
135
+ def add_card_data(post, payment)
136
+ card_number = payment.number
137
+ cvv = payment.verification_value
138
+
139
+ payload = { card_number: card_number, 'cvv': cvv }.to_json
140
+
141
+ post[:card_encrypted_data] = JWE.encrypt(payload, one_time_public_key)
142
+ post[:card_month_expiration] = format(payment.month, :two_digits)
143
+ post[:card_year_expiration] = format(payment.year, :two_digits)
144
+ end
145
+
146
+ def add_customer_data(post, options)
147
+ post[:additional_data] = options[:additional_data] || '' # must be passed even if empty
148
+ end
149
+
150
+ def parse(body)
151
+ JSON.parse(body)
152
+ end
153
+
154
+ def commit(action, parameters)
155
+ url = build_request_url(action)
156
+ begin
157
+ response = parse(ssl_post(url, post_data(parameters)))
158
+ rescue ResponseError => response
159
+ # Errors are returned with helpful data,
160
+ # but get filtered out by `ssl_post` because of their HTTP status.
161
+ response = parse(response.response.body)
162
+ end
163
+
164
+ Response.new(
165
+ success_from(response),
166
+ message_from(response),
167
+ response,
168
+ authorization: authorization_from(response),
169
+ avs_result: nil,
170
+ cvv_result: nil,
171
+ test: test?,
172
+ error_code: error_code_from(response)
173
+ )
174
+ end
175
+
176
+ def success_from(response)
177
+ if code = response.dig('confirmation', 'response_code')
178
+ code == '00'
179
+ else
180
+ response['status'] == 'success'
181
+ end
182
+ end
183
+
184
+ def message_from(response)
185
+ %w(confirmation refund).each do |m|
186
+ message =
187
+ response.dig(m, 'extended_response_description') ||
188
+ response.dig(m, 'response_description') ||
189
+ response.dig(m, 'response_details')
190
+ return message if message
191
+ end
192
+ [response.dig('messages', 0, 'key'), response.dig('messages', 0, 'dsc')].join(':')
193
+ end
194
+
195
+ def authorization_from(response)
196
+ response_body = response.dig('confirmation') || response.dig('refund')
197
+ return unless response_body
198
+
199
+ authorization_number = response_body.dig('authorization_number') || response_body.dig('authorization_code')
200
+ shop_process_id = response_body.dig('shop_process_id')
201
+
202
+ "#{authorization_number}##{shop_process_id}"
203
+ end
204
+
205
+ def error_code_from(response)
206
+ response.dig('confirmation', 'response_code') unless success_from(response)
207
+ end
208
+
209
+ def build_request_url(action)
210
+ base_url = (test? ? test_url : live_url)
211
+ base_url + ENDPOINTS[action]
212
+ end
213
+
214
+ def post_data(data)
215
+ { public_key: @public_key,
216
+ operation: data }.compact.to_json
217
+ end
218
+ end
219
+ end
220
+ end