Skip to content

Commit

Permalink
OPPS-225 Worldpay Idempotency key fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jherreraa committed Dec 4, 2024
1 parent 599f2cc commit bd5245f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
* FlexCharge: Update homePage url [javierpedrozaing] #5351
* Nuvei: Fix send savePM in false by default [javierpedrozaing] #5353
* Decidir and DecicirPlus: Add the wallet_id field [yunnydang] #5354
* Worldpay: Idempotency key fix [jherreraa] #5343

== Version 1.137.0 (August 2, 2024)
* Unlock dependency on `rexml` to allow fixing a CVE (#5181).
Expand Down
7 changes: 6 additions & 1 deletion lib/active_merchant/billing/gateways/worldpay.rb
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,12 @@ def headers(options)
cookie = options[:cookie] || cookie
headers['Cookie'] = cookie if cookie

headers['Idempotency-Key'] = idempotency_key if idempotency_key
# Required because Worldpay does not accept duplicate idempotency keys
# for different transactions, such as in the case of an authorize => capture flow.
if idempotency_key
headers['Idempotency-Key'] = idempotency_key
options[:idempotency_key] = SecureRandom.uuid
end
headers
end

Expand Down
10 changes: 5 additions & 5 deletions test/remote/gateways/remote_worldpay_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -306,17 +306,17 @@ def test_unsucessfull_authorize_without_token_number_apple_pay
response = @gateway.authorize(@amount, @apple_pay_network_token, @options)

assert_failure response
assert_equal response.error_code, '5'
assert_equal "Element 'tokenNumber' must have valid numeric content.", response.message
assert_equal response.error_code, '2'
assert_match "Missing required elements 'tokenNumber'", response.message
end

def test_unsucessfull_authorize_with_token_number_as_empty_string_apple_pay
@apple_pay_network_token.number = ''
response = @gateway.authorize(@amount, @apple_pay_network_token, @options)

assert_failure response
assert_equal response.error_code, '5'
assert_equal "Element 'tokenNumber' must have valid numeric content.", response.message
assert_equal response.error_code, '2'
assert_match "Missing required elements 'tokenNumber'", response.message
end

def test_unsucessfull_authorize_with_invalid_token_number_apple_pay
Expand Down Expand Up @@ -1342,7 +1342,7 @@ def test_failed_refund_synchronous_response

refund = @cftgateway.refund(@amount * 2, auth.authorization, authorization_validated: true)
assert_failure refund
assert_equal 'Refund amount too high', refund.message
assert_equal 'Invalid amount: The refund amount should be equal to the captured value', refund.message
end

def test_successful_purchase_with_options_synchronous_response
Expand Down
6 changes: 6 additions & 0 deletions test/unit/gateways/worldpay_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,12 @@ def test_payment_type_for_credit_card
assert_equal payment, :credit
end

def test_idempotency_header
options = { idempotency_key: 'test123' }
headers = @gateway.send(:headers, options)
assert_not_equal options[:idempotency_key], headers['Idempotency-Key']
end

def test_successful_authorize
response = stub_comms do
@gateway.authorize(@amount, @credit_card, @options)
Expand Down

0 comments on commit bd5245f

Please sign in to comment.