Skip to content

Commit dd12d1d

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 0e5758a commit dd12d1d

File tree

3 files changed

+51
-46
lines changed

3 files changed

+51
-46
lines changed

CHANGELOG

+1
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@
144144
* CyberSource: Void Acepts nil values [gasb150] #5442
145145
* Ebanx: Handle special characters in email [ankurspreedly] #5444
146146
* Orbital: Add support for more currencies [ankurspreedly] #5438
147+
* CheckoutV2: Update Authorization from Basic to Bearer [sinourain] #5381
147148

148149
== Version 1.137.0 (August 2, 2024)
149150
* 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

+38-40
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)
@@ -458,8 +457,8 @@ def test_successful_purchase_includes_avs_result
458457
response = @gateway.purchase(@amount, @credit_card, @options)
459458
assert_success response
460459
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']
460+
assert_equal 'G', response.avs_result['code']
461+
assert_equal 'Non-U.S. issuing bank does not support AVS.', response.avs_result['message']
463462
end
464463

465464
def test_successful_purchase_includes_avs_result_via_oauth
@@ -474,8 +473,8 @@ def test_successful_authorize_includes_avs_result
474473
response = @gateway.authorize(@amount, @credit_card, @options)
475474
assert_success response
476475
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']
476+
assert_equal 'G', response.avs_result['code']
477+
assert_equal 'Non-U.S. issuing bank does not support AVS.', response.avs_result['message']
479478
end
480479

481480
def test_successful_purchase_includes_cvv_result
@@ -528,7 +527,7 @@ def test_successful_authorize_with_estimated_type_via_oauth
528527
end
529528

530529
def test_successful_authorize_with_processing_channel_id
531-
response = @gateway.authorize(@amount, @credit_card, @options.merge({ processing_channel_id: 'pc_ovo75iz4hdyudnx6tu74mum3fq' }))
530+
response = @gateway.authorize(@amount, @credit_card, @options)
532531
assert_success response
533532
assert_equal 'Succeeded', response.message
534533
end
@@ -563,7 +562,6 @@ def test_successful_purchase_with_processing_data
563562
options = @options.merge(
564563
processing: {
565564
aft: true,
566-
preferred_scheme: 'cartes_bancaires',
567565
app_id: 'com.iap.linker_portal',
568566
airline_data: [
569567
{
@@ -718,19 +716,22 @@ def test_successful_purchase_with_metadata_via_oauth
718716
end
719717

720718
def test_successful_purchase_with_minimal_options
721-
response = @gateway.purchase(@amount, @credit_card, billing_address: address)
719+
min_options = { billing_address: address, processing_channel_id: 'pc_lxgl7aqahkzubkundd2l546hdm' }
720+
response = @gateway.purchase(@amount, @credit_card, min_options)
722721
assert_success response
723722
assert_equal 'Succeeded', response.message
724723
end
725724

726725
def test_successful_purchase_with_shipping_address
727-
response = @gateway.purchase(@amount, @credit_card, shipping_address: address)
726+
min_options = { shipping_address: address, processing_channel_id: 'pc_lxgl7aqahkzubkundd2l546hdm' }
727+
response = @gateway.purchase(@amount, @credit_card, min_options)
728728
assert_success response
729729
assert_equal 'Succeeded', response.message
730730
end
731731

732732
def test_successful_purchase_without_phone_number
733-
response = @gateway.purchase(@amount, @credit_card, billing_address: address.update(phone: nil))
733+
min_options = { billing_address: address.update(phone: nil), processing_channel_id: 'pc_lxgl7aqahkzubkundd2l546hdm' }
734+
response = @gateway.purchase(@amount, @credit_card, min_options)
734735
assert_success response
735736
assert_equal 'Succeeded', response.message
736737
end
@@ -744,15 +745,15 @@ def test_successful_purchase_without_name
744745
end
745746

746747
def test_successful_purchase_with_ip
747-
response = @gateway.purchase(@amount, @credit_card, ip: '96.125.185.52')
748+
response = @gateway.purchase(@amount, @credit_card, @options.merge(ip: '96.125.185.52'))
748749
assert_success response
749750
assert_equal 'Succeeded', response.message
750751
end
751752

752753
def test_failed_purchase
753754
response = @gateway.purchase(100, @credit_card_dnh, @options)
754755
assert_failure response
755-
assert_equal 'Invalid Card Number', response.message
756+
assert_equal 'Declined - Do Not Honour', response.message
756757
end
757758

758759
def test_failed_purchase_via_oauth
@@ -776,7 +777,7 @@ def test_avs_failed_authorize
776777
def test_invalid_shipping_address
777778
response = @gateway.authorize(@amount, @credit_card, shipping_address: address.update(country: 'Canada'))
778779
assert_failure response
779-
assert_equal 'request_invalid: country_address_invalid', response.message
780+
assert_equal 'request_invalid: address_country_invalid', response.message
780781
end
781782

782783
def test_successful_authorize_and_capture
@@ -925,7 +926,19 @@ def test_failed_capture_via_oauth
925926
def test_successful_credit
926927
@credit_card.first_name = 'John'
927928
@credit_card.last_name = 'Doe'
928-
response = @gateway_oauth.credit(@amount, @credit_card, @options.merge({ source_type: 'currency_account', source_id: 'ca_spwmped4qmqenai7hcghquqle4', account_holder_type: 'individual' }))
929+
destination = {
930+
account_holder: {
931+
phone: {
932+
number: '9108675309',
933+
country_code: '1'
934+
},
935+
identification: {
936+
type: 'passport',
937+
number: '12345788848438'
938+
}
939+
}
940+
}
941+
response = @gateway_oauth.credit(@amount, @credit_card, @options.merge({ source_type: 'currency_account', source_id: 'ca_spwmped4qmqenai7hcghquqle4', account_holder_type: 'individual', payout: true, destination: }))
929942
assert_success response
930943
assert_equal 'Succeeded', response.message
931944
assert_equal true, response.primary_response.pending
@@ -947,33 +960,18 @@ def test_successful_money_transfer_payout_via_credit_corporate_account_holder_ty
947960
assert_equal 'Succeeded', response.message
948961
end
949962

950-
def test_money_transfer_payout_reverts_to_credit_if_payout_sent_as_nil
951-
@credit_card.first_name = 'John'
952-
@credit_card.last_name = 'Doe'
953-
response = @gateway_oauth.credit(@amount, @credit_card, @payout_options.merge({ account_holder_type: 'individual', payout: nil }))
954-
assert_success response
955-
assert_equal 'Succeeded', response.message
956-
end
957-
958-
def test_money_transfer_payout_handles_blank_destination_address
959-
@payout_options[:billing_address] = nil
960-
response = @gateway_oauth.credit(@amount, @credit_card, @payout_options.merge({ account_holder_type: 'individual', payout: true }))
961-
assert_success response
962-
assert_equal 'Succeeded', response.message
963-
end
964-
965963
def test_successful_store
966-
response = @gateway_token.store(@credit_card, @options)
964+
response = @gateway.store(@credit_card, @options)
967965
assert_success response
968966
assert_equal 'Succeeded', response.message
969967
end
970968

971969
def test_successful_unstore_after_store
972-
store = @gateway_token.store(@credit_card, @options)
970+
store = @gateway.store(@credit_card, @options)
973971
assert_success store
974972
assert_equal 'Succeeded', store.message
975973
source_id = store.params['id']
976-
response = @gateway_token.unstore(source_id, @options)
974+
response = @gateway.unstore(source_id, @options)
977975
assert_success response
978976
assert_equal response.params['response_code'], '204'
979977
end
@@ -1017,7 +1015,7 @@ def test_failed_store_oauth_credit_card
10171015
end
10181016

10191017
def test_successful_purchase_oauth_after_store_credit_card
1020-
store = @gateway_token.store(@credit_card, @options)
1018+
store = @gateway.store(@credit_card, @options)
10211019
assert_success store
10221020
token = store.params['id']
10231021
response = @gateway_oauth.purchase(@amount, token, @options)
@@ -1193,8 +1191,8 @@ def test_failed_verify
11931191
def test_expired_card_returns_error_code
11941192
response = @gateway.purchase(@amount, @expired_card, @options)
11951193
assert_failure response
1196-
assert_equal 'request_invalid: card_expired', response.message
1197-
assert_equal 'request_invalid: card_expired', response.error_code
1194+
assert_equal 'processing_error: card_expired', response.message
1195+
assert_equal 'processing_error: card_expired', response.error_code
11981196
end
11991197

12001198
def test_successful_purchase_with_idempotency_key

0 commit comments

Comments
 (0)