activemerchant 1.125.0 → 1.129.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG +316 -0
  3. data/lib/active_merchant/billing/check.rb +40 -8
  4. data/lib/active_merchant/billing/credit_card.rb +28 -1
  5. data/lib/active_merchant/billing/credit_card_methods.rb +91 -23
  6. data/lib/active_merchant/billing/gateway.rb +2 -1
  7. data/lib/active_merchant/billing/gateways/adyen.rb +74 -12
  8. data/lib/active_merchant/billing/gateways/airwallex.rb +370 -0
  9. data/lib/active_merchant/billing/gateways/alelo.rb +256 -0
  10. data/lib/active_merchant/billing/gateways/authorize_net.rb +21 -4
  11. data/lib/active_merchant/billing/gateways/barclaycard_smartpay.rb +2 -1
  12. data/lib/active_merchant/billing/gateways/beanstream.rb +18 -0
  13. data/lib/active_merchant/billing/gateways/blue_pay.rb +1 -1
  14. data/lib/active_merchant/billing/gateways/blue_snap.rb +53 -22
  15. data/lib/active_merchant/billing/gateways/bogus.rb +4 -0
  16. data/lib/active_merchant/billing/gateways/borgun.rb +56 -16
  17. data/lib/active_merchant/billing/gateways/braintree/braintree_common.rb +6 -1
  18. data/lib/active_merchant/billing/gateways/braintree/token_nonce.rb +113 -0
  19. data/lib/active_merchant/billing/gateways/braintree_blue.rb +151 -32
  20. data/lib/active_merchant/billing/gateways/card_connect.rb +28 -10
  21. data/lib/active_merchant/billing/gateways/card_stream.rb +23 -0
  22. data/lib/active_merchant/billing/gateways/checkout_v2.rb +228 -57
  23. data/lib/active_merchant/billing/gateways/commerce_hub.rb +361 -0
  24. data/lib/active_merchant/billing/gateways/credorax.rb +56 -26
  25. data/lib/active_merchant/billing/gateways/cyber_source/cyber_source_common.rb +36 -0
  26. data/lib/active_merchant/billing/gateways/cyber_source.rb +112 -58
  27. data/lib/active_merchant/billing/gateways/cyber_source_rest.rb +456 -0
  28. data/lib/active_merchant/billing/gateways/d_local.rb +93 -5
  29. data/lib/active_merchant/billing/gateways/decidir.rb +32 -5
  30. data/lib/active_merchant/billing/gateways/decidir_plus.rb +185 -14
  31. data/lib/active_merchant/billing/gateways/ebanx.rb +39 -26
  32. data/lib/active_merchant/billing/gateways/element.rb +21 -1
  33. data/lib/active_merchant/billing/gateways/global_collect.rb +98 -37
  34. data/lib/active_merchant/billing/gateways/ipg.rb +14 -10
  35. data/lib/active_merchant/billing/gateways/iveri.rb +39 -3
  36. data/lib/active_merchant/billing/gateways/kushki.rb +21 -1
  37. data/lib/active_merchant/billing/gateways/litle.rb +118 -6
  38. data/lib/active_merchant/billing/gateways/mastercard.rb +1 -8
  39. data/lib/active_merchant/billing/gateways/mercado_pago.rb +17 -0
  40. data/lib/active_merchant/billing/gateways/merchant_e_solutions.rb +44 -10
  41. data/lib/active_merchant/billing/gateways/monei.rb +2 -0
  42. data/lib/active_merchant/billing/gateways/moneris.rb +55 -13
  43. data/lib/active_merchant/billing/gateways/mundipagg.rb +3 -0
  44. data/lib/active_merchant/billing/gateways/nmi.rb +12 -7
  45. data/lib/active_merchant/billing/gateways/ogone.rb +35 -7
  46. data/lib/active_merchant/billing/gateways/openpay.rb +20 -3
  47. data/lib/active_merchant/billing/gateways/orbital.rb +378 -335
  48. data/lib/active_merchant/billing/gateways/pay_trace.rb +64 -18
  49. data/lib/active_merchant/billing/gateways/payeezy.rb +59 -4
  50. data/lib/active_merchant/billing/gateways/payflow.rb +62 -0
  51. data/lib/active_merchant/billing/gateways/paymentez.rb +44 -13
  52. data/lib/active_merchant/billing/gateways/paypal/paypal_express_response.rb +4 -0
  53. data/lib/active_merchant/billing/gateways/paysafe.rb +37 -29
  54. data/lib/active_merchant/billing/gateways/payu_latam.rb +28 -15
  55. data/lib/active_merchant/billing/gateways/plexo.rb +308 -0
  56. data/lib/active_merchant/billing/gateways/priority.rb +185 -140
  57. data/lib/active_merchant/billing/gateways/rapyd.rb +319 -0
  58. data/lib/active_merchant/billing/gateways/reach.rb +277 -0
  59. data/lib/active_merchant/billing/gateways/redsys.rb +9 -5
  60. data/lib/active_merchant/billing/gateways/safe_charge.rb +1 -4
  61. data/lib/active_merchant/billing/gateways/sage_pay.rb +1 -1
  62. data/lib/active_merchant/billing/gateways/securion_pay.rb +40 -0
  63. data/lib/active_merchant/billing/gateways/shift4.rb +342 -0
  64. data/lib/active_merchant/billing/gateways/simetrik.rb +368 -0
  65. data/lib/active_merchant/billing/gateways/stripe.rb +25 -3
  66. data/lib/active_merchant/billing/gateways/stripe_payment_intents.rb +155 -70
  67. data/lib/active_merchant/billing/gateways/tns.rb +2 -5
  68. data/lib/active_merchant/billing/gateways/trans_first_transaction_express.rb +1 -1
  69. data/lib/active_merchant/billing/gateways/trust_commerce.rb +14 -3
  70. data/lib/active_merchant/billing/gateways/vanco.rb +12 -3
  71. data/lib/active_merchant/billing/gateways/visanet_peru.rb +6 -2
  72. data/lib/active_merchant/billing/gateways/vpos.rb +7 -4
  73. data/lib/active_merchant/billing/gateways/wompi.rb +8 -4
  74. data/lib/active_merchant/billing/gateways/worldpay.rb +117 -9
  75. data/lib/active_merchant/billing/response.rb +15 -1
  76. data/lib/active_merchant/connection.rb +0 -2
  77. data/lib/active_merchant/country.rb +1 -0
  78. data/lib/active_merchant/errors.rb +4 -1
  79. data/lib/active_merchant/version.rb +1 -1
  80. metadata +28 -3
@@ -0,0 +1,370 @@
1
+ module ActiveMerchant #:nodoc:
2
+ module Billing #:nodoc:
3
+ class AirwallexGateway < Gateway
4
+ self.test_url = 'https://api-demo.airwallex.com/api/v1'
5
+ self.live_url = 'https://pci-api.airwallex.com/api/v1'
6
+
7
+ # per https://www.airwallex.com/docs/online-payments__overview, cards are accepted in all EU countries
8
+ self.supported_countries = %w[AT AU BE BG CY CZ DE DK EE GR ES FI FR GB HK HR HU IE IT LT LU LV MT NL PL PT RO SE SG SI SK]
9
+ self.default_currency = 'AUD'
10
+ self.supported_cardtypes = %i[visa master]
11
+
12
+ self.homepage_url = 'https://airwallex.com/'
13
+ self.display_name = 'Airwallex'
14
+
15
+ ENDPOINTS = {
16
+ login: '/authentication/login',
17
+ setup: '/pa/payment_intents/create',
18
+ sale: '/pa/payment_intents/%{id}/confirm',
19
+ capture: '/pa/payment_intents/%{id}/capture',
20
+ refund: '/pa/refunds/create',
21
+ void: '/pa/payment_intents/%{id}/cancel'
22
+ }
23
+
24
+ # Provided by Airwallex for testing purposes
25
+ TEST_NETWORK_TRANSACTION_IDS = {
26
+ visa: '123456789012345',
27
+ master: 'MCC123ABC0101'
28
+ }
29
+
30
+ def initialize(options = {})
31
+ requires!(options, :client_id, :client_api_key)
32
+ @client_id = options[:client_id]
33
+ @client_api_key = options[:client_api_key]
34
+ super
35
+ @access_token = setup_access_token
36
+ end
37
+
38
+ def purchase(money, card, options = {})
39
+ payment_intent_id = create_payment_intent(money, options)
40
+ post = {
41
+ 'request_id' => request_id(options),
42
+ 'merchant_order_id' => merchant_order_id(options)
43
+ }
44
+ add_card(post, card, options)
45
+ add_descriptor(post, options)
46
+ add_stored_credential(post, options)
47
+ add_return_url(post, options)
48
+ post['payment_method_options'] = { 'card' => { 'auto_capture' => false } } if authorization_only?(options)
49
+
50
+ add_three_ds(post, options)
51
+ commit(:sale, post, payment_intent_id)
52
+ end
53
+
54
+ def authorize(money, payment, options = {})
55
+ # authorize is just a purchase w/o an auto capture
56
+ purchase(money, payment, options.merge({ auto_capture: false }))
57
+ end
58
+
59
+ def capture(money, authorization, options = {})
60
+ raise ArgumentError, 'An authorization value must be provided.' if authorization.blank?
61
+
62
+ post = {
63
+ 'request_id' => request_id(options),
64
+ 'merchant_order_id' => merchant_order_id(options),
65
+ 'amount' => amount(money)
66
+ }
67
+ add_descriptor(post, options)
68
+
69
+ commit(:capture, post, authorization)
70
+ end
71
+
72
+ def refund(money, authorization, options = {})
73
+ raise ArgumentError, 'An authorization value must be provided.' if authorization.blank?
74
+
75
+ post = {}
76
+ post[:amount] = amount(money)
77
+ post[:payment_intent_id] = authorization
78
+ post[:request_id] = request_id(options)
79
+ post[:merchant_order_id] = merchant_order_id(options)
80
+
81
+ commit(:refund, post)
82
+ end
83
+
84
+ def void(authorization, options = {})
85
+ raise ArgumentError, 'An authorization value must be provided.' if authorization.blank?
86
+
87
+ post = {}
88
+ post[:request_id] = request_id(options)
89
+ post[:merchant_order_id] = merchant_order_id(options)
90
+ add_descriptor(post, options)
91
+
92
+ commit(:void, post, authorization)
93
+ end
94
+
95
+ def verify(credit_card, options = {})
96
+ MultiResponse.run(:use_first_response) do |r|
97
+ r.process { authorize(100, credit_card, options) }
98
+ r.process(:ignore_result) { void(r.authorization, options) }
99
+ end
100
+ end
101
+
102
+ def supports_scrubbing?
103
+ true
104
+ end
105
+
106
+ def scrub(transcript)
107
+ transcript.
108
+ gsub(/(\\\"number\\\":\\\")\d+/, '\1[REDACTED]').
109
+ gsub(/(\\\"cvc\\\":\\\")\d+/, '\1[REDACTED]')
110
+ end
111
+
112
+ private
113
+
114
+ def request_id(options)
115
+ options[:request_id] || generate_uuid
116
+ end
117
+
118
+ def merchant_order_id(options)
119
+ options[:merchant_order_id] || options[:order_id] || generate_uuid
120
+ end
121
+
122
+ def add_return_url(post, options)
123
+ post[:return_url] = options[:return_url] if options[:return_url]
124
+ end
125
+
126
+ def generate_uuid
127
+ SecureRandom.uuid
128
+ end
129
+
130
+ def setup_access_token
131
+ token_headers = {
132
+ 'Content-Type' => 'application/json',
133
+ 'x-client-id' => @client_id,
134
+ 'x-api-key' => @client_api_key
135
+ }
136
+ response = ssl_post(build_request_url(:login), nil, token_headers)
137
+ JSON.parse(response)['token']
138
+ end
139
+
140
+ def build_request_url(action, id = nil)
141
+ base_url = (test? ? test_url : live_url)
142
+ base_url + ENDPOINTS[action].to_s % { id: id }
143
+ end
144
+
145
+ def add_referrer_data(post)
146
+ post[:referrer_data] = { type: 'spreedly' }
147
+ end
148
+
149
+ def create_payment_intent(money, options = {})
150
+ post = {}
151
+ add_invoice(post, money, options)
152
+ add_order(post, options)
153
+ post[:request_id] = "#{request_id(options)}_setup"
154
+ post[:merchant_order_id] = merchant_order_id(options)
155
+ add_referrer_data(post)
156
+ add_descriptor(post, options)
157
+ post['payment_method_options'] = { 'card' => { 'risk_control' => { 'three_ds_action' => 'SKIP_3DS' } } } if options[:skip_3ds]
158
+
159
+ response = commit(:setup, post)
160
+ raise ArgumentError.new(response.message) unless response.success?
161
+
162
+ response.params['id']
163
+ end
164
+
165
+ def add_billing(post, card, options = {})
166
+ return unless has_name_info?(card)
167
+
168
+ billing = post['payment_method']['card']['billing'] || {}
169
+ billing['email'] = options[:email] if options[:email]
170
+ billing['phone'] = options[:phone] if options[:phone]
171
+ billing['first_name'] = card.first_name
172
+ billing['last_name'] = card.last_name
173
+ billing_address = options[:billing_address]
174
+ billing['address'] = build_address(billing_address) if has_required_address_info?(billing_address)
175
+
176
+ post['payment_method']['card']['billing'] = billing
177
+ end
178
+
179
+ def has_name_info?(card)
180
+ # These fields are required if billing data is sent.
181
+ card.first_name && card.last_name
182
+ end
183
+
184
+ def has_required_address_info?(address)
185
+ # These fields are required if address data is sent.
186
+ return unless address
187
+
188
+ address[:address1] && address[:country]
189
+ end
190
+
191
+ def build_address(address)
192
+ return unless address
193
+
194
+ address_data = {} # names r hard
195
+ address_data[:country_code] = address[:country]
196
+ address_data[:street] = address[:address1]
197
+ address_data[:city] = address[:city] if address[:city] # required per doc, not in practice
198
+ address_data[:postcode] = address[:zip] if address[:zip]
199
+ address_data[:state] = address[:state] if address[:state]
200
+ address_data
201
+ end
202
+
203
+ def add_invoice(post, money, options)
204
+ post[:amount] = amount(money)
205
+ post[:currency] = (options[:currency] || currency(money))
206
+ end
207
+
208
+ def add_card(post, card, options = {})
209
+ post['payment_method'] = {
210
+ 'type' => 'card',
211
+ 'card' => {
212
+ 'expiry_month' => format(card.month, :two_digits),
213
+ 'expiry_year' => card.year.to_s,
214
+ 'number' => card.number.to_s,
215
+ 'name' => card.name,
216
+ 'cvc' => card.verification_value,
217
+ 'brand' => card.brand
218
+ }
219
+ }
220
+ add_billing(post, card, options)
221
+ end
222
+
223
+ def add_order(post, options)
224
+ return unless shipping_address = options[:shipping_address]
225
+
226
+ physical_address = build_shipping_address(shipping_address)
227
+ first_name, last_name = split_names(shipping_address[:name])
228
+ shipping = {}
229
+ shipping[:first_name] = first_name if first_name
230
+ shipping[:last_name] = last_name if last_name
231
+ shipping[:phone_number] = shipping_address[:phone_number] if shipping_address[:phone_number]
232
+ shipping[:address] = physical_address
233
+ post[:order] = { shipping: shipping }
234
+ end
235
+
236
+ def build_shipping_address(shipping_address)
237
+ address = {}
238
+ address[:city] = shipping_address[:city]
239
+ address[:country_code] = shipping_address[:country]
240
+ address[:postcode] = shipping_address[:zip]
241
+ address[:state] = shipping_address[:state]
242
+ address[:street] = shipping_address[:address1]
243
+ address
244
+ end
245
+
246
+ def add_stored_credential(post, options)
247
+ return unless stored_credential = options[:stored_credential]
248
+
249
+ external_recurring_data = post[:external_recurring_data] = {}
250
+
251
+ case stored_credential.dig(:reason_type)
252
+ when 'recurring', 'installment'
253
+ external_recurring_data[:merchant_trigger_reason] = 'scheduled'
254
+ when 'unscheduled'
255
+ external_recurring_data[:merchant_trigger_reason] = 'unscheduled'
256
+ end
257
+
258
+ external_recurring_data[:original_transaction_id] = test_mit?(options) ? test_network_transaction_id(post) : stored_credential.dig(:network_transaction_id)
259
+ external_recurring_data[:triggered_by] = stored_credential.dig(:initiator) == 'cardholder' ? 'customer' : 'merchant'
260
+ end
261
+
262
+ def test_network_transaction_id(post)
263
+ case post['payment_method']['card']['brand']
264
+ when 'visa'
265
+ TEST_NETWORK_TRANSACTION_IDS[:visa]
266
+ when 'master'
267
+ TEST_NETWORK_TRANSACTION_IDS[:master]
268
+ end
269
+ end
270
+
271
+ def test_mit?(options)
272
+ test? && options.dig(:stored_credential, :initiator) == 'merchant'
273
+ end
274
+
275
+ def add_three_ds(post, options)
276
+ return unless three_d_secure = options[:three_d_secure]
277
+
278
+ pm_options = post.dig('payment_method_options', 'card')
279
+
280
+ external_three_ds = {
281
+ 'version': format_three_ds_version(three_d_secure),
282
+ 'eci': three_d_secure[:eci]
283
+ }.merge(three_ds_version_specific_fields(three_d_secure))
284
+
285
+ pm_options ? pm_options.merge!('external_three_ds': external_three_ds) : post['payment_method_options'] = { 'card': { 'external_three_ds': external_three_ds } }
286
+ end
287
+
288
+ def format_three_ds_version(three_d_secure)
289
+ version = three_d_secure[:version].split('.')
290
+
291
+ version.push('0') until version.length == 3
292
+ version.join('.')
293
+ end
294
+
295
+ def three_ds_version_specific_fields(three_d_secure)
296
+ if three_d_secure[:version].to_f >= 2
297
+ {
298
+ 'authentication_value': three_d_secure[:cavv],
299
+ 'ds_transaction_id': three_d_secure[:ds_transaction_id],
300
+ 'three_ds_server_transaction_id': three_d_secure[:three_ds_server_trans_id]
301
+ }
302
+ else
303
+ {
304
+ 'cavv': three_d_secure[:cavv],
305
+ 'xid': three_d_secure[:xid]
306
+ }
307
+ end
308
+ end
309
+
310
+ def authorization_only?(options = {})
311
+ options.include?(:auto_capture) && options[:auto_capture] == false
312
+ end
313
+
314
+ def add_descriptor(post, options)
315
+ post[:descriptor] = options[:description] if options[:description]
316
+ end
317
+
318
+ def parse(body)
319
+ JSON.parse(body)
320
+ end
321
+
322
+ def commit(action, post, id = nil)
323
+ url = build_request_url(action, id)
324
+
325
+ post_headers = { 'Authorization' => "Bearer #{@access_token}", 'Content-Type' => 'application/json' }
326
+ response = parse(ssl_post(url, post_data(post), post_headers))
327
+
328
+ Response.new(
329
+ success_from(response),
330
+ message_from(response),
331
+ response,
332
+ authorization: authorization_from(response),
333
+ avs_result: AVSResult.new(code: response.dig('latest_payment_attempt', 'authentication_data', 'avs_result')),
334
+ cvv_result: CVVResult.new(response.dig('latest_payment_attempt', 'authentication_data', 'cvc_code')),
335
+ test: test?,
336
+ error_code: error_code_from(response)
337
+ )
338
+ end
339
+
340
+ def handle_response(response)
341
+ case response.code.to_i
342
+ when 200...300, 400, 404
343
+ response.body
344
+ else
345
+ raise ResponseError.new(response)
346
+ end
347
+ end
348
+
349
+ def post_data(post)
350
+ post.to_json
351
+ end
352
+
353
+ def success_from(response)
354
+ %w(REQUIRES_PAYMENT_METHOD SUCCEEDED RECEIVED REQUIRES_CAPTURE CANCELLED).include?(response['status'])
355
+ end
356
+
357
+ def message_from(response)
358
+ response.dig('latest_payment_attempt', 'status') || response['status'] || response['message']
359
+ end
360
+
361
+ def authorization_from(response)
362
+ response.dig('latest_payment_attempt', 'payment_intent_id')
363
+ end
364
+
365
+ def error_code_from(response)
366
+ response['provider_original_response_code'] || response['code'] unless success_from(response)
367
+ end
368
+ end
369
+ end
370
+ end
@@ -0,0 +1,256 @@
1
+ require 'jose'
2
+
3
+ module ActiveMerchant #:nodoc:
4
+ module Billing #:nodoc:
5
+ class AleloGateway < Gateway
6
+ class_attribute :prelive_url
7
+
8
+ self.test_url = 'https://sandbox-api.alelo.com.br/alelo/sandbox/'
9
+ self.live_url = 'https://api.alelo.com.br/alelo/prd/'
10
+ self.prelive_url = 'https://api.homologacaoalelo.com.br/alelo/uat/'
11
+
12
+ self.supported_countries = ['BR']
13
+ self.default_currency = 'BRL'
14
+ self.supported_cardtypes = %i[visa master american_express discover]
15
+
16
+ self.homepage_url = 'https://www.alelo.com.br'
17
+ self.display_name = 'Alelo'
18
+
19
+ def initialize(options = {})
20
+ requires!(options, :client_id, :client_secret)
21
+ super
22
+ end
23
+
24
+ def purchase(money, payment, options = {})
25
+ post = {}
26
+ add_order(post, options)
27
+ add_amount(post, money)
28
+ add_payment(post, payment)
29
+ add_geolocation(post, options)
30
+ add_extra_data(post, options)
31
+
32
+ commit('capture/transaction', post, options)
33
+ end
34
+
35
+ def refund(money, authorization, options = {})
36
+ request_id = authorization.split('#').first
37
+ options[:http] = { method: :put, prevent_encrypt: true }
38
+ commit('capture/transaction/refund', { requestId: request_id }, options, :put)
39
+ end
40
+
41
+ def supports_scrubbing?
42
+ true
43
+ end
44
+
45
+ def scrub(transcript)
46
+ force_utf8(transcript.encode).
47
+ gsub(%r((Authorization: Bearer )[\w -]+), '\1[FILTERED]').
48
+ gsub(%r((client_id=|Client-Id:)[\w -]+), '\1[FILTERED]\2').
49
+ gsub(%r((client_secret=|Client-Secret:)[\w -]+), '\1[FILTERED]\2').
50
+ gsub(%r((access_token\":\")[^\"]*), '\1[FILTERED]').
51
+ gsub(%r((publicKey\":\")[^\"]*), '\1[FILTERED]')
52
+ end
53
+
54
+ private
55
+
56
+ def force_utf8(string)
57
+ return nil unless string
58
+
59
+ # binary = string.encode('BINARY', invalid: :replace, undef: :replace, replace: '?')
60
+ string.encode('UTF-8', invalid: :replace, undef: :replace, replace: '?')
61
+ end
62
+
63
+ def add_amount(post, money)
64
+ post[:amount] = amount(money).to_f
65
+ end
66
+
67
+ def add_order(post, options)
68
+ post[:requestId] = options[:order_id]
69
+ end
70
+
71
+ def add_extra_data(post, options)
72
+ post.merge!({
73
+ establishmentCode: options[:establishment_code],
74
+ playerIdentification: options[:player_identification],
75
+ captureType: '3', # send fixed value 3 to ecommerce
76
+ subMerchantCode: options[:sub_merchant_mcc],
77
+ externalTraceNumber: options[:external_trace_number]
78
+ }.compact)
79
+ end
80
+
81
+ def add_geolocation(post, options)
82
+ return if options[:geo_latitude].blank? || options[:geo_longitude].blank?
83
+
84
+ post.merge!(geolocation: {
85
+ latitude: options[:geo_latitude],
86
+ longitude: options[:geo_longitude]
87
+ })
88
+ end
89
+
90
+ def add_payment(post, payment)
91
+ post.merge!({
92
+ cardNumber: payment.number,
93
+ cardholderName: payment.name,
94
+ expirationMonth: payment.month,
95
+ expirationYear: format(payment.year, :two_digits).to_i,
96
+ securityCode: payment.verification_value
97
+ })
98
+ end
99
+
100
+ def fetch_access_token
101
+ params = {
102
+ grant_type: 'client_credentials',
103
+ client_id: @options[:client_id],
104
+ client_secret: @options[:client_secret],
105
+ scope: '/capture'
106
+ }
107
+
108
+ headers = {
109
+ 'Accept' => 'application/json',
110
+ 'Content-Type' => 'application/x-www-form-urlencoded'
111
+ }
112
+
113
+ parsed = parse(ssl_post(url('captura-oauth-provider/oauth/token'), post_data(params), headers))
114
+ Response.new(true, parsed[:access_token], parsed)
115
+ end
116
+
117
+ def remote_encryption_key(access_token)
118
+ response = parse(ssl_get(url('capture/key'), request_headers(access_token)))
119
+ Response.new(true, response[:publicKey], response)
120
+ end
121
+
122
+ def ensure_credentials(try_again = true)
123
+ multiresp = MultiResponse.new
124
+ access_token = @options[:access_token]
125
+ key = @options[:encryption_key]
126
+ uuid = @options[:encryption_uuid]
127
+
128
+ if access_token.blank?
129
+ multiresp.process { fetch_access_token }
130
+ access_token = multiresp.message
131
+ key = nil
132
+ uuid = nil
133
+ end
134
+
135
+ if key.blank?
136
+ multiresp.process { remote_encryption_key(access_token) }
137
+ key = multiresp.message
138
+ uuid = multiresp.params['uuid']
139
+ end
140
+
141
+ {
142
+ key: key,
143
+ uuid: uuid,
144
+ access_token: access_token,
145
+ multiresp: multiresp.responses.present? ? multiresp : nil
146
+ }
147
+ rescue ResponseError => error
148
+ # retry to generate a new access_token when the provided one is expired
149
+ raise error unless try_again && %w(401 404).include?(error.response.code) && @options[:access_token].present?
150
+
151
+ @options.delete(:access_token)
152
+ @options.delete(:encryption_key)
153
+ ensure_credentials false
154
+ end
155
+
156
+ def encrypt_payload(body, credentials, options)
157
+ key = OpenSSL::PKey::RSA.new(Base64.decode64(credentials[:key]))
158
+ jwk = JOSE::JWK.from_key(key)
159
+ alg_enc = { 'alg' => 'RSA-OAEP-256', 'enc' => 'A128CBC-HS256' }
160
+
161
+ token = JOSE::JWE.block_encrypt(jwk, body.to_json, alg_enc).compact
162
+
163
+ encrypted_body = {
164
+ token: token,
165
+ uuid: credentials[:uuid]
166
+ }
167
+
168
+ encrypted_body.to_json
169
+ end
170
+
171
+ def parse(body)
172
+ JSON.parse(body, symbolize_names: true)
173
+ end
174
+
175
+ def post_data(params)
176
+ params.map { |k, v| "#{k}=#{CGI.escape(v.to_s)}" }.join('&')
177
+ end
178
+
179
+ def commit(action, body, options, try_again = true)
180
+ credentials = ensure_credentials
181
+ payload = encrypt_payload(body, credentials, options)
182
+
183
+ if options.dig :http, :method
184
+ payload = body.to_json if options.dig :http, :prevent_encrypt
185
+ response = parse ssl_request(options[:http][:method], url(action), payload, request_headers(credentials[:access_token]))
186
+ else
187
+ response = parse ssl_post(url(action), payload, request_headers(credentials[:access_token]))
188
+ end
189
+
190
+ resp = Response.new(
191
+ success_from(action, response),
192
+ message_from(response),
193
+ response,
194
+ authorization: authorization_from(response, options),
195
+ test: test?
196
+ )
197
+
198
+ return resp unless credentials[:multiresp].present?
199
+
200
+ multiresp = credentials[:multiresp]
201
+ resp.params.merge!({
202
+ 'access_token' => credentials[:access_token],
203
+ 'encryption_key' => credentials[:key],
204
+ 'encryption_uuid' => credentials[:uuid]
205
+ })
206
+ multiresp.process { resp }
207
+
208
+ multiresp
209
+ rescue ActiveMerchant::ResponseError => e
210
+ # Retry on a possible expired encryption key
211
+ if try_again && %w(401 404).include?(e.response.code) && @options[:encryption_key].present?
212
+ @options.delete(:encryption_key)
213
+ commit(action, body, options, false)
214
+ else
215
+ res = parse(e.response.body)
216
+ Response.new(false, res[:messageUser] || res[:error], res, test: test?)
217
+ end
218
+ end
219
+
220
+ def success_from(action, response)
221
+ case action
222
+ when 'capture/transaction/refund'
223
+ response[:status] == 'ESTORNADA'
224
+ when 'capture/transaction'
225
+ response[:status] == 'CONFIRMADA'
226
+ else
227
+ false
228
+ end
229
+ end
230
+
231
+ def message_from(response)
232
+ response[:messages] || response[:messageUser]
233
+ end
234
+
235
+ def authorization_from(response, options)
236
+ [response[:requestId]].join('#')
237
+ end
238
+
239
+ def url(action)
240
+ return prelive_url if @options[:url_override] == 'prelive'
241
+
242
+ "#{test? ? test_url : live_url}#{action}"
243
+ end
244
+
245
+ def request_headers(access_token)
246
+ {
247
+ 'Accept' => 'application/json',
248
+ 'X-IBM-Client-Id' => @options[:client_id],
249
+ 'X-IBM-Client-Secret' => @options[:client_secret],
250
+ 'Content-Type' => 'application/json',
251
+ 'Authorization' => "Bearer #{access_token}"
252
+ }
253
+ end
254
+ end
255
+ end
256
+ end
@@ -90,7 +90,6 @@ module ActiveMerchant
90
90
  }.freeze
91
91
 
92
92
  APPLE_PAY_DATA_DESCRIPTOR = 'COMMON.APPLE.INAPP.PAYMENT'
93
-
94
93
  PAYMENT_METHOD_NOT_SUPPORTED_ERROR = '155'
95
94
  INELIGIBLE_FOR_ISSUING_CREDIT_ERROR = '54'
96
95
 
@@ -176,11 +175,29 @@ module ActiveMerchant
176
175
  end
177
176
  end
178
177
 
179
- def verify(credit_card, options = {})
178
+ def verify(payment_method, options = {})
179
+ amount = amount_for_verify(options)
180
+
180
181
  MultiResponse.run(:use_first_response) do |r|
181
- r.process { authorize(100, credit_card, options) }
182
- r.process(:ignore_result) { void(r.authorization, options) }
182
+ r.process { authorize(amount, payment_method, options) }
183
+ r.process(:ignore_result) { void(r.authorization, options) } unless amount == 0
183
184
  end
185
+ rescue ArgumentError => e
186
+ Response.new(false, e.message)
187
+ end
188
+
189
+ def amount_for_verify(options)
190
+ return 100 unless options[:verify_amount].present?
191
+
192
+ amount = options[:verify_amount]
193
+ raise ArgumentError.new 'verify_amount value must be an integer' unless amount.is_a?(Integer) && !amount.negative? || amount.is_a?(String) && amount.match?(/^\d+$/) && !amount.to_i.negative?
194
+ raise ArgumentError.new 'Billing address including zip code is required for a 0 amount verify' if amount.to_i.zero? && !validate_billing_address_values?(options)
195
+
196
+ amount.to_i
197
+ end
198
+
199
+ def validate_billing_address_values?(options)
200
+ options.dig(:billing_address, :zip).present? && options.dig(:billing_address, :address1).present?
184
201
  end
185
202
 
186
203
  def store(credit_card, options = {})
@@ -6,9 +6,10 @@ module ActiveMerchant #:nodoc:
6
6
 
7
7
  self.supported_countries = %w[AL AD AM AT AZ BY BE BA BG HR CY CZ DK EE FI FR DE GR HU IS IE IT KZ LV LI LT LU MK MT MD MC ME NL NO PL PT RO RU SM RS SK SI ES SE CH TR UA GB VA]
8
8
  self.default_currency = 'EUR'
9
- self.currencies_with_three_decimal_places = %w(BHD KWD OMR RSD TND)
9
+ self.currencies_with_three_decimal_places = %w(BHD KWD OMR RSD TND IQD JOD LYD)
10
10
  self.money_format = :cents
11
11
  self.supported_cardtypes = %i[visa master american_express discover diners_club jcb dankort maestro]
12
+ self.currencies_without_fractions = %w(CVE DJF GNF IDR JPY KMF KRW PYG RWF UGX VND VUV XAF XOF XPF)
12
13
 
13
14
  self.homepage_url = 'https://www.barclaycardsmartpay.com/'
14
15
  self.display_name = 'Barclaycard Smartpay'