Skip to content

Commit 4180b1e

Browse files
Luis Urreasinourain
Luis Urrea
authored andcommitted
Checkout V2: Update Authorization to include Bearer for secret_key
Start sending requests using http Auth:Bearer Spreedly reference: [ECS-3487](https://spreedly.atlassian.net/browse/ECS-3487) Unit: 66 tests, 403 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications 100% passed 2821.48 tests/s, 17228.11 assertions/s Remote: 103 tests, 254 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications 100% passed 0.63 tests/s, 1.56 assertions/s
1 parent b60297e commit 4180b1e

File tree

3 files changed

+44
-30
lines changed

3 files changed

+44
-30
lines changed

CHANGELOG

+1
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@
140140
* Adyen: Update shopperInteraction [almalee24] #5430
141141
* Nuvei: Add addtional oct and aft user detail fields [yunnydang] #5433
142142
* CheckoutV2: Add support for partial authorization [yunnydang] #5441
143+
* CheckoutV2: Update Authorization from Basic to Bearer [sinourain] #5381
143144

144145
== Version 1.137.0 (August 2, 2024)
145146
* Unlock dependency on `rexml` to allow fixing a CVE (#5181).

lib/active_merchant/billing/gateways/checkout_v2.rb

+12-6
Original file line numberDiff line numberDiff line change
@@ -677,16 +677,22 @@ def pending_result(response, action)
677677
end
678678

679679
def headers(action, options)
680-
auth_token = @options[:access_token] ? "Bearer #{@options[:access_token]}" : @options[:secret_key]
681-
auth_token = @options[:public_key] if action == :tokens
682-
headers = {
683-
'Authorization' => auth_token,
684-
'Content-Type' => 'application/json;charset=UTF-8'
685-
}
680+
headers = { 'Authorization' => auth_token(action), 'Content-Type' => 'application/json;charset=UTF-8' }
686681
headers['Cko-Idempotency-Key'] = options[:idempotency_key] if options[:idempotency_key]
682+
687683
headers
688684
end
689685

686+
def auth_token(action)
687+
return @options[:public_key] if action == :tokens
688+
689+
token = @options[:access_token] || @options[:secret_key]
690+
691+
return token if token.include?('Bearer')
692+
693+
"Bearer #{token}"
694+
end
695+
690696
def tokenize(payment_method, options = {})
691697
post = {}
692698
add_authorization_type(post, options)

test/remote/gateways/remote_checkout_v2_test.rb

+31-24
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ class RemoteCheckoutV2Test < Test::Unit::TestCase
55
def setup
66
gateway_fixtures = fixtures(:checkout_v2)
77
gateway_token_fixtures = fixtures(:checkout_v2_token)
8-
@gateway = CheckoutV2Gateway.new(secret_key: gateway_fixtures[:secret_key])
9-
@gateway_oauth = CheckoutV2Gateway.new({ client_id: gateway_fixtures[:client_id], client_secret: gateway_fixtures[:client_secret] })
10-
@gateway_token = CheckoutV2Gateway.new(secret_key: gateway_token_fixtures[:secret_key], public_key: gateway_token_fixtures[:public_key])
8+
@gateway = CheckoutV2Gateway.new(gateway_token_fixtures)
9+
@gateway_basic_auth = CheckoutV2Gateway.new(secret_key: gateway_fixtures[:secret_key])
10+
@gateway_oauth = CheckoutV2Gateway.new(client_id: gateway_fixtures[:client_id], client_secret: gateway_fixtures[:client_secret])
1111

1212
@amount = 200
1313
@credit_card = credit_card('4242424242424242', verification_value: '100', month: '6', year: Time.now.year + 1)
@@ -89,8 +89,7 @@ def setup
8989
@additional_options = @options.merge(
9090
card_on_file: true,
9191
transaction_indicator: 2,
92-
previous_charge_id: 'pay_123',
93-
processing_channel_id: 'pc_123'
92+
previous_charge_id: 'pay_123'
9493
)
9594
@additional_options_3ds = @options.merge(
9695
execute_threed: true,
@@ -221,7 +220,7 @@ def test_network_transaction_scrubbing
221220
def test_store_transcript_scrubbing
222221
response = nil
223222
transcript = capture_transcript(@gateway) do
224-
response = @gateway_token.store(@credit_card, @options)
223+
response = @gateway.store(@credit_card, @options)
225224
end
226225
token = response.responses.first.params['token']
227226
transcript = @gateway.scrub(transcript)
@@ -303,6 +302,12 @@ def test_successful_purchase_with_an_expired_access_token
303302
end
304303
end
305304

305+
def test_successful_purchase_for_secret_key_basic_authorization_header
306+
response = @gateway_basic_auth.purchase(@amount, @credit_card, @options)
307+
assert_success response
308+
assert_equal 'Succeeded', response.message
309+
end
310+
306311
def test_successful_purchase_with_vts_network_token
307312
response = @gateway.purchase(100, @vts_network_token, @options)
308313
assert_success response
@@ -458,8 +463,8 @@ def test_successful_purchase_includes_avs_result
458463
response = @gateway.purchase(@amount, @credit_card, @options)
459464
assert_success response
460465
assert_equal 'Succeeded', response.message
461-
assert_equal 'S', response.avs_result['code']
462-
assert_equal 'U.S.-issuing bank does not support AVS.', response.avs_result['message']
466+
assert_equal 'G', response.avs_result['code']
467+
assert_equal 'Non-U.S. issuing bank does not support AVS.', response.avs_result['message']
463468
end
464469

465470
def test_successful_purchase_includes_avs_result_via_oauth
@@ -474,8 +479,8 @@ def test_successful_authorize_includes_avs_result
474479
response = @gateway.authorize(@amount, @credit_card, @options)
475480
assert_success response
476481
assert_equal 'Succeeded', response.message
477-
assert_equal 'S', response.avs_result['code']
478-
assert_equal 'U.S.-issuing bank does not support AVS.', response.avs_result['message']
482+
assert_equal 'G', response.avs_result['code']
483+
assert_equal 'Non-U.S. issuing bank does not support AVS.', response.avs_result['message']
479484
end
480485

481486
def test_successful_purchase_includes_cvv_result
@@ -528,7 +533,7 @@ def test_successful_authorize_with_estimated_type_via_oauth
528533
end
529534

530535
def test_successful_authorize_with_processing_channel_id
531-
response = @gateway.authorize(@amount, @credit_card, @options.merge({ processing_channel_id: 'pc_ovo75iz4hdyudnx6tu74mum3fq' }))
536+
response = @gateway.authorize(@amount, @credit_card, @options)
532537
assert_success response
533538
assert_equal 'Succeeded', response.message
534539
end
@@ -563,7 +568,6 @@ def test_successful_purchase_with_processing_data
563568
options = @options.merge(
564569
processing: {
565570
aft: true,
566-
preferred_scheme: 'cartes_bancaires',
567571
app_id: 'com.iap.linker_portal',
568572
airline_data: [
569573
{
@@ -718,19 +722,22 @@ def test_successful_purchase_with_metadata_via_oauth
718722
end
719723

720724
def test_successful_purchase_with_minimal_options
721-
response = @gateway.purchase(@amount, @credit_card, billing_address: address)
725+
min_options = { billing_address: address, processing_channel_id: 'pc_lxgl7aqahkzubkundd2l546hdm' }
726+
response = @gateway.purchase(@amount, @credit_card, min_options)
722727
assert_success response
723728
assert_equal 'Succeeded', response.message
724729
end
725730

726731
def test_successful_purchase_with_shipping_address
727-
response = @gateway.purchase(@amount, @credit_card, shipping_address: address)
732+
min_options = { shipping_address: address, processing_channel_id: 'pc_lxgl7aqahkzubkundd2l546hdm' }
733+
response = @gateway.purchase(@amount, @credit_card, min_options)
728734
assert_success response
729735
assert_equal 'Succeeded', response.message
730736
end
731737

732738
def test_successful_purchase_without_phone_number
733-
response = @gateway.purchase(@amount, @credit_card, billing_address: address.update(phone: nil))
739+
min_options = { billing_address: address.update(phone: nil), processing_channel_id: 'pc_lxgl7aqahkzubkundd2l546hdm' }
740+
response = @gateway.purchase(@amount, @credit_card, min_options)
734741
assert_success response
735742
assert_equal 'Succeeded', response.message
736743
end
@@ -744,15 +751,15 @@ def test_successful_purchase_without_name
744751
end
745752

746753
def test_successful_purchase_with_ip
747-
response = @gateway.purchase(@amount, @credit_card, ip: '96.125.185.52')
754+
response = @gateway.purchase(@amount, @credit_card, @options.merge(ip: '96.125.185.52'))
748755
assert_success response
749756
assert_equal 'Succeeded', response.message
750757
end
751758

752759
def test_failed_purchase
753760
response = @gateway.purchase(100, @credit_card_dnh, @options)
754761
assert_failure response
755-
assert_equal 'Invalid Card Number', response.message
762+
assert_equal 'Declined - Do Not Honour', response.message
756763
end
757764

758765
def test_failed_purchase_via_oauth
@@ -776,7 +783,7 @@ def test_avs_failed_authorize
776783
def test_invalid_shipping_address
777784
response = @gateway.authorize(@amount, @credit_card, shipping_address: address.update(country: 'Canada'))
778785
assert_failure response
779-
assert_equal 'request_invalid: country_address_invalid', response.message
786+
assert_equal 'request_invalid: address_country_invalid', response.message
780787
end
781788

782789
def test_successful_authorize_and_capture
@@ -963,17 +970,17 @@ def test_money_transfer_payout_handles_blank_destination_address
963970
end
964971

965972
def test_successful_store
966-
response = @gateway_token.store(@credit_card, @options)
973+
response = @gateway.store(@credit_card, @options)
967974
assert_success response
968975
assert_equal 'Succeeded', response.message
969976
end
970977

971978
def test_successful_unstore_after_store
972-
store = @gateway_token.store(@credit_card, @options)
979+
store = @gateway.store(@credit_card, @options)
973980
assert_success store
974981
assert_equal 'Succeeded', store.message
975982
source_id = store.params['id']
976-
response = @gateway_token.unstore(source_id, @options)
983+
response = @gateway.unstore(source_id, @options)
977984
assert_success response
978985
assert_equal response.params['response_code'], '204'
979986
end
@@ -1017,7 +1024,7 @@ def test_failed_store_oauth_credit_card
10171024
end
10181025

10191026
def test_successful_purchase_oauth_after_store_credit_card
1020-
store = @gateway_token.store(@credit_card, @options)
1027+
store = @gateway.store(@credit_card, @options)
10211028
assert_success store
10221029
token = store.params['id']
10231030
response = @gateway_oauth.purchase(@amount, token, @options)
@@ -1193,8 +1200,8 @@ def test_failed_verify
11931200
def test_expired_card_returns_error_code
11941201
response = @gateway.purchase(@amount, @expired_card, @options)
11951202
assert_failure response
1196-
assert_equal 'request_invalid: card_expired', response.message
1197-
assert_equal 'request_invalid: card_expired', response.error_code
1203+
assert_equal 'processing_error: card_expired', response.message
1204+
assert_equal 'processing_error: card_expired', response.error_code
11981205
end
11991206

12001207
def test_successful_purchase_with_idempotency_key

0 commit comments

Comments
 (0)