From 43a62c6bb2c9536eb7c807ce7e2bccc4cb46f6c3 Mon Sep 17 00:00:00 2001 From: Alma Malambo Date: Thu, 10 Oct 2024 16:00:57 -0500 Subject: [PATCH] Stripe: Remove StripePaymenToken & ApplePayPaymentToken --- .../billing/gateways/stripe.rb | 59 ++----- .../gateways/stripe_payment_intents.rb | 12 +- .../gateways/remote_stripe_apple_pay_test.rb | 86 ----------- test/unit/gateways/stripe_test.rb | 146 +----------------- 4 files changed, 12 insertions(+), 291 deletions(-) diff --git a/lib/active_merchant/billing/gateways/stripe.rb b/lib/active_merchant/billing/gateways/stripe.rb index 18e16cb7e95..94bcd47703d 100644 --- a/lib/active_merchant/billing/gateways/stripe.rb +++ b/lib/active_merchant/billing/gateways/stripe.rb @@ -88,18 +88,10 @@ def authorize(money, payment, options = {}) return Response.new(false, direct_bank_error) end - MultiResponse.run do |r| - if payment.is_a?(ApplePayPaymentToken) - r.process { tokenize_apple_pay_token(payment) } - payment = StripePaymentToken.new(r.params['token']) if r.success? - end - r.process do - post = create_post_for_auth_or_purchase(money, payment, options) - add_application_fee(post, options) if emv_payment?(payment) - post[:capture] = 'false' - commit(:post, 'charges', post, options) - end - end.responses.last + post = create_post_for_auth_or_purchase(money, payment, options) + add_application_fee(post, options) if emv_payment?(payment) + post[:capture] = 'false' + commit(:post, 'charges', post, options) end # To create a charge on a card or a token, call @@ -115,17 +107,9 @@ def purchase(money, payment, options = {}) return Response.new(false, direct_bank_error) end - MultiResponse.run do |r| - if payment.is_a?(ApplePayPaymentToken) - r.process { tokenize_apple_pay_token(payment) } - payment = StripePaymentToken.new(r.params['token']) if r.success? - end - r.process do - post = create_post_for_auth_or_purchase(money, payment, options) - post[:card][:processing_method] = 'quick_chip' if quickchip_payment?(payment) - commit(:post, 'charges', post, options) - end - end.responses.last + post = create_post_for_auth_or_purchase(money, payment, options) + post[:card][:processing_method] = 'quick_chip' if quickchip_payment?(payment) + commit(:post, 'charges', post, options) end def capture(money, authorization, options = {}) @@ -199,12 +183,7 @@ def store(payment, options = {}) params = {} post = {} - if payment.is_a?(ApplePayPaymentToken) - token_exchange_response = tokenize_apple_pay_token(payment) - params = { card: token_exchange_response.params['token']['id'] } if token_exchange_response.success? - elsif payment.is_a?(StripePaymentToken) - add_payment_token(params, payment, options) - elsif payment.is_a?(Check) + if payment.is_a?(Check) bank_token_response = tokenize_bank_account(payment) return bank_token_response unless bank_token_response.success? @@ -255,17 +234,6 @@ def unstore(identification, options = {}, deprecated_options = {}) commit(:delete, "customers/#{CGI.escape(customer_id)}/cards/#{CGI.escape(card_id)}", nil, options) end - def tokenize_apple_pay_token(apple_pay_payment_token, options = {}) - token_response = api_request(:post, "tokens?pk_token=#{CGI.escape(apple_pay_payment_token.payment_data.to_json)}") - success = !token_response.key?('error') - - if success && token_response.key?('id') - Response.new(success, nil, token: token_response) - else - Response.new(success, token_response['error']['message']) - end - end - def verify_credentials begin ssl_get(live_url + 'charges/nonexistent', headers) @@ -368,12 +336,7 @@ def list_webhook_endpoints(options) def create_post_for_auth_or_purchase(money, payment, options) post = {} - if payment.is_a?(StripePaymentToken) - add_payment_token(post, payment, options) - else - add_creditcard(post, payment, options) - end - + add_creditcard(post, payment, options) add_charge_details(post, money, payment, options) post end @@ -537,10 +500,6 @@ def add_emv_creditcard(post, icc_data, options = {}) post[:card] = { emv_auth_data: icc_data } end - def add_payment_token(post, token, options = {}) - post[:card] = token.payment_data['id'] - end - def add_customer(post, payment, options) post[:customer] = options[:customer] if options[:customer] && !payment.respond_to?(:number) end diff --git a/lib/active_merchant/billing/gateways/stripe_payment_intents.rb b/lib/active_merchant/billing/gateways/stripe_payment_intents.rb index f5d2a8b8fae..af82445eb30 100644 --- a/lib/active_merchant/billing/gateways/stripe_payment_intents.rb +++ b/lib/active_merchant/billing/gateways/stripe_payment_intents.rb @@ -258,7 +258,7 @@ def store(payment_method, options = {}) if new_apple_google_pay_flow(payment_method, options) options[:customer] = customer(payment_method, options).params['id'] unless options[:customer] verify(payment_method, options.merge!(action: :store)) - elsif payment_method.is_a?(StripePaymentToken) || payment_method.is_a?(ActiveMerchant::Billing::CreditCard) + elsif payment_method.is_a?(ActiveMerchant::Billing::CreditCard) result = add_payment_method_token(params, payment_method, options) return result if result.is_a?(ActiveMerchant::Billing::Response) @@ -405,14 +405,6 @@ def add_return_url(post, options) def add_payment_method_token(post, payment_method, options, responses = []) case payment_method - when StripePaymentToken - post[:payment_method_data] = { - type: 'card', - card: { - token: payment_method.payment_data['id'] || payment_method.payment_data - } - } - post[:payment_method] = payment_method.payment_data['id'] || payment_method.payment_data when String extract_token_from_string_and_maybe_add_customer_id(post, payment_method) when ActiveMerchant::Billing::CreditCard @@ -691,7 +683,7 @@ def setup_future_usage(post, options = {}) end def add_billing_address(post, payment_method, options = {}) - return if payment_method.nil? || payment_method.is_a?(StripePaymentToken) || payment_method.is_a?(String) + return if payment_method.nil? || payment_method.is_a?(String) post[:payment_method_data] ||= {} if billing = options[:billing_address] || options[:address] diff --git a/test/remote/gateways/remote_stripe_apple_pay_test.rb b/test/remote/gateways/remote_stripe_apple_pay_test.rb index b1a2f8c92aa..1387880aff2 100644 --- a/test/remote/gateways/remote_stripe_apple_pay_test.rb +++ b/test/remote/gateways/remote_stripe_apple_pay_test.rb @@ -12,92 +12,6 @@ def setup description: 'ActiveMerchant Test Purchase', email: 'wow@example.com' } - @apple_pay_payment_token = apple_pay_payment_token - end - - def test_successful_purchase_with_apple_pay_payment_token - assert response = @gateway.purchase(@amount, @apple_pay_payment_token, @options) - assert_success response - assert_equal 'charge', response.params['object'] - assert response.params['paid'] - assert_equal 'ActiveMerchant Test Purchase', response.params['description'] - assert_equal 'wow@example.com', response.params['metadata']['email'] - assert_match CHARGE_ID_REGEX, response.authorization - end - - def test_authorization_and_capture_with_apple_pay_payment_token - assert authorization = @gateway.authorize(@amount, @apple_pay_payment_token, @options) - assert_success authorization - refute authorization.params['captured'] - assert_equal 'ActiveMerchant Test Purchase', authorization.params['description'] - assert_equal 'wow@example.com', authorization.params['metadata']['email'] - - assert capture = @gateway.capture(@amount, authorization.authorization) - assert_success capture - end - - def test_authorization_and_void_with_apple_pay_payment_token - assert authorization = @gateway.authorize(@amount, @apple_pay_payment_token, @options) - assert_success authorization - refute authorization.params['captured'] - - assert void = @gateway.void(authorization.authorization) - assert_success void - end - - def test_successful_void_with_apple_pay_payment_token - assert response = @gateway.purchase(@amount, @apple_pay_payment_token, @options) - assert_success response - assert response.authorization - assert void = @gateway.void(response.authorization) - assert_success void - end - - def test_successful_store_with_apple_pay_payment_token - assert response = @gateway.store(@apple_pay_payment_token, { description: 'Active Merchant Test Customer', email: 'email@example.com' }) - assert_success response - assert_equal 'customer', response.params['object'] - assert_equal 'Active Merchant Test Customer', response.params['description'] - assert_equal 'email@example.com', response.params['email'] - first_card = response.params['cards']['data'].first - assert_equal response.params['default_card'], first_card['id'] - assert_equal '4242', first_card['dynamic_last4'] # when stripe is in test mode, token exchanged will return a test card with dynamic_last4 4242 - assert_equal '0000', first_card['last4'] # last4 is 0000 when using an apple pay token - end - - def test_successful_store_with_existing_customer_and_apple_pay_payment_token - assert response = @gateway.store(@credit_card, { description: 'Active Merchant Test Customer' }) - assert_success response - - assert response = @gateway.store(@apple_pay_payment_token, { customer: response.params['id'], description: 'Active Merchant Test Customer', email: 'email@example.com' }) - assert_success response - assert_equal 2, response.responses.size - - card_response = response.responses[0] - assert_equal 'card', card_response.params['object'] - assert_equal '4242', card_response.params['dynamic_last4'] # when stripe is in test mode, token exchanged will return a test card with dynamic_last4 4242 - assert_equal '0000', card_response.params['last4'] # last4 is 0000 when using an apple pay token - - customer_response = response.responses[1] - assert_equal 'customer', customer_response.params['object'] - assert_equal 'Active Merchant Test Customer', customer_response.params['description'] - assert_equal 'email@example.com', customer_response.params['email'] - assert_equal 2, customer_response.params['cards']['count'] - end - - def test_successful_recurring_with_apple_pay_payment_token - assert response = @gateway.store(@apple_pay_payment_token, { description: 'Active Merchant Test Customer', email: 'email@example.com' }) - assert_success response - assert recharge_options = @options.merge(customer: response.params['id']) - assert response = @gateway.purchase(@amount, nil, recharge_options) - assert_success response - assert_equal 'charge', response.params['object'] - assert response.params['paid'] - end - - def test_purchase_with_unsuccessful_apple_pay_token_exchange - assert response = @gateway.purchase(@amount, ApplePayPaymentToken.new('garbage'), @options) - assert_failure response end def test_successful_purchase_with_apple_pay_raw_cryptogram_with_eci diff --git a/test/unit/gateways/stripe_test.rb b/test/unit/gateways/stripe_test.rb index 36af54cb72c..261db9f94ec 100644 --- a/test/unit/gateways/stripe_test.rb +++ b/test/unit/gateways/stripe_test.rb @@ -24,10 +24,8 @@ def setup callback_url: 'http://www.example.com/callback' } - @apple_pay_payment_token = apple_pay_payment_token @emv_credit_card = credit_card_with_icc_data - @payment_token = StripeGateway::StripePaymentToken.new(token_params) - @token_string = @payment_token.payment_data['id'] + @token_string = 'tok_14uq3k2gKyKnHxtYUAZZZlH3' @check = check({ bank_name: 'STRIPE TEST BANK', @@ -48,18 +46,6 @@ def test_successful_new_customer_with_card assert response.test? end - def test_successful_new_customer_with_apple_pay_payment_token - @gateway.expects(:ssl_request).returns(successful_new_customer_response) - @gateway.expects(:tokenize_apple_pay_token).returns(Response.new(true, nil, token: successful_apple_pay_token_exchange)) - - assert response = @gateway.store(@apple_pay_payment_token, @options) - assert_instance_of Response, response - assert_success response - - assert_equal 'cus_3sgheFxeBgTQ3M|card_483etw4er9fg4vF3sQdrt3FG', response.authorization - assert response.test? - end - def test_successful_new_customer_with_emv_credit_card @gateway.expects(:ssl_request).returns(successful_new_customer_response) @@ -91,18 +77,6 @@ def test_successful_new_card assert response.test? end - def test_successful_new_card_via_apple_pay_payment_token - @gateway.expects(:ssl_request).returns(successful_new_card_response) - @gateway.expects(:tokenize_apple_pay_token).returns(Response.new(true, nil, token: successful_apple_pay_token_exchange)) - - assert response = @gateway.store(@apple_pay_payment_token, customer: 'cus_3sgheFxeBgTQ3M') - assert_instance_of MultiResponse, response - assert_success response - - assert_equal 'cus_3sgheFxeBgTQ3M|card_483etw4er9fg4vF3sQdrt3FG', response.authorization - assert response.test? - end - def test_successful_new_card_with_emv_credit_card @gateway.expects(:ssl_request).returns(successful_new_card_response) @gateway.expects(:add_creditcard) @@ -127,18 +101,6 @@ def test_successful_new_card_with_token_string assert response.test? end - def test_successful_new_card_with_payment_token - @gateway.expects(:ssl_request).returns(successful_new_card_response) - @gateway.expects(:add_payment_token) - - assert response = @gateway.store(@payment_token, customer: 'cus_3sgheFxeBgTQ3M') - assert_instance_of MultiResponse, response - assert_success response - - assert_equal 'cus_3sgheFxeBgTQ3M|card_483etw4er9fg4vF3sQdrt3FG', response.authorization - assert response.test? - end - def test_successful_new_card_and_customer_update @gateway.expects(:ssl_request).twice.returns(successful_new_card_response, successful_new_customer_response) @gateway.expects(:add_creditcard) @@ -154,21 +116,6 @@ def test_successful_new_card_and_customer_update assert response.test? end - def test_successful_new_card_and_customer_update_via_apple_pay_payment_token - @gateway.expects(:ssl_request).twice.returns(successful_new_card_response, successful_new_customer_response) - @gateway.expects(:tokenize_apple_pay_token).returns(Response.new(true, nil, token: successful_apple_pay_token_exchange)) - - assert response = @gateway.store(@apple_pay_payment_token, customer: 'cus_3sgheFxeBgTQ3M', email: 'test@test.com') - assert_instance_of MultiResponse, response - assert_success response - - assert_equal 'cus_3sgheFxeBgTQ3M|card_483etw4er9fg4vF3sQdrt3FG', response.authorization - assert_equal 2, response.responses.size - assert_equal 'cus_3sgheFxeBgTQ3M|card_483etw4er9fg4vF3sQdrt3FG', response.responses[0].authorization - assert_equal 'cus_3sgheFxeBgTQ3M', response.responses[1].authorization - assert response.test? - end - def test_successful_new_card_and_customer_update_with_emv_credit_card @gateway.expects(:ssl_request).twice.returns(successful_new_card_response, successful_new_customer_response) @@ -197,20 +144,6 @@ def test_successful_new_card_and_customer_update_with_token_string assert response.test? end - def test_successful_new_card_and_customer_update_with_payment_token - @gateway.expects(:ssl_request).twice.returns(successful_new_card_response, successful_new_customer_response) - - assert response = @gateway.store(@payment_token, customer: 'cus_3sgheFxeBgTQ3M', email: 'test@test.com') - assert_instance_of MultiResponse, response - assert_success response - - assert_equal 'cus_3sgheFxeBgTQ3M|card_483etw4er9fg4vF3sQdrt3FG', response.authorization - assert_equal 2, response.responses.size - assert_equal 'cus_3sgheFxeBgTQ3M|card_483etw4er9fg4vF3sQdrt3FG', response.responses[0].authorization - assert_equal 'cus_3sgheFxeBgTQ3M', response.responses[1].authorization - assert response.test? - end - def test_successful_new_default_card @gateway.expects(:ssl_request).twice.returns(successful_new_card_response, successful_new_customer_response) @gateway.expects(:add_creditcard) @@ -226,21 +159,6 @@ def test_successful_new_default_card assert response.test? end - def test_successful_new_default_card_via_apple_pay_payment_token - @gateway.expects(:ssl_request).twice.returns(successful_new_card_response, successful_new_customer_response) - @gateway.expects(:tokenize_apple_pay_token).returns(Response.new(true, nil, token: successful_apple_pay_token_exchange)) - - assert response = @gateway.store(@apple_pay_payment_token, @options.merge(customer: 'cus_3sgheFxeBgTQ3M', set_default: true)) - assert_instance_of MultiResponse, response - assert_success response - - assert_equal 'cus_3sgheFxeBgTQ3M|card_483etw4er9fg4vF3sQdrt3FG', response.authorization - assert_equal 'cus_3sgheFxeBgTQ3M|card_483etw4er9fg4vF3sQdrt3FG', response.responses[0].authorization - assert_equal 2, response.responses.size - assert_equal 'cus_3sgheFxeBgTQ3M', response.responses[1].authorization - assert response.test? - end - def test_successful_new_default_card_with_emv_credit_card @gateway.expects(:ssl_request).twice.returns(successful_new_card_response, successful_new_customer_response) @@ -270,21 +188,6 @@ def test_successful_new_default_card_with_token_string assert response.test? end - def test_successful_new_default_card_with_payment_token - @gateway.expects(:ssl_request).twice.returns(successful_new_card_response, successful_new_customer_response) - @gateway.expects(:add_payment_token) - - assert response = @gateway.store(@payment_token, @options.merge(customer: 'cus_3sgheFxeBgTQ3M', set_default: true)) - assert_instance_of MultiResponse, response - assert_success response - - assert_equal 'cus_3sgheFxeBgTQ3M|card_483etw4er9fg4vF3sQdrt3FG', response.authorization - assert_equal 2, response.responses.size - assert_equal 'cus_3sgheFxeBgTQ3M|card_483etw4er9fg4vF3sQdrt3FG', response.responses[0].authorization - assert_equal 'cus_3sgheFxeBgTQ3M', response.responses[1].authorization - assert response.test? - end - def test_passing_validate_false_on_store response = stub_comms(@gateway, :ssl_request) do @gateway.store(@credit_card, validate: false) @@ -329,30 +232,6 @@ def test_successful_authorization_with_token_string assert response.test? end - def test_successful_authorization_with_payment_token - @gateway.expects(:add_payment_token) - @gateway.expects(:ssl_request).returns(successful_authorization_response) - - assert response = @gateway.authorize(@amount, @payment_token, @options) - assert_instance_of Response, response - assert_success response - - assert_equal 'ch_test_charge', response.authorization - assert response.test? - end - - def test_successful_authorization_with_apple_pay_token_exchange - @gateway.expects(:tokenize_apple_pay_token).returns(Response.new(true, nil, token: successful_apple_pay_token_exchange)) - @gateway.expects(:ssl_request).returns(successful_authorization_response) - - assert response = @gateway.authorize(@amount, @apple_pay_payment_token, @options) - assert_instance_of Response, response - assert_success response - - assert_equal 'ch_test_charge', response.authorization - assert response.test? - end - def test_successful_authorization_with_emv_credit_card @gateway.expects(:ssl_request).returns(successful_authorization_response_with_icc_data) @@ -464,29 +343,6 @@ def test_successful_purchase_with_token_string assert response.test? end - def test_successful_purchase_with_payment_token - @gateway.expects(:add_payment_token) - @gateway.expects(:ssl_request).returns(successful_purchase_response) - - assert response = @gateway.purchase(@amount, @payment_token, @options) - assert_success response - - assert_equal 'ch_test_charge', response.authorization - assert response.test? - end - - def test_successful_purchase_with_apple_pay_token_exchange - @gateway.expects(:tokenize_apple_pay_token).returns(Response.new(true, nil, token: successful_apple_pay_token_exchange)) - @gateway.expects(:ssl_request).returns(successful_purchase_response) - - assert response = @gateway.purchase(@amount, @apple_pay_payment_token, @options) - assert_instance_of Response, response - assert_success response - - assert_equal 'ch_test_charge', response.authorization - assert response.test? - end - def test_successful_purchase_with_level3_data @gateway.expects(:add_creditcard)