spree / spree_gateway

Huge collection of payment gateways for @spree. Stripe, Braintree, Apple Pay, Authorize.net and many others!
https://spreecommerce.org
BSD 3-Clause "New" or "Revised" License
219 stars 438 forks source link

Error in handle_response method when params['response'] is nil #413

Open rahul2103 opened 8 months ago

rahul2103 commented 8 months ago

Steps to Reproduce:

Send request to the Spree::Api::V2::Storefront::IntentsController#handle_response endpoint. In some cases, the params['response'] parameter is sent as nil in the request payload.

Expected Behavior:

I expect the handle_response method to gracefully handle cases where params['response'] is nil, preventing a NoMethodError. Instead of raising an error, the method should handle this scenario and respond appropriately.

Actual Behavior:

When params['response'] is nil, a NoMethodError is raised in the Spree::Api::V2::Storefront::IntentsController#handle_response method when attempting to access nested attributes. This can be seen in the server logs:

NoMethodError Spree::Api::V2::Storefront::IntentsController#handle_response
undefined method `[]' for nil:NilClass

Code Snippets:

def handle_response
  if params['response']['error']
     invalidate_payment
     render_error_payload(params['response']['error']['message'])
  else
     render_serialized_payload { { message: I18n.t('spree.payment_successfully_authorized') } }
  end
end

Screenshots:

image

Additional Context:

This issue occurs intermittently and is challenging to reproduce consistently. It seems to be related to scenarios where the expected response structure is not received.

Thanks.

damianlegawiec commented 8 months ago

Hi @rahul2103

Do you use some error tracking software like Sentry? Backtrace, stripe API request logs - that would help

rahul2103 commented 8 months ago

Hey @damianlegawiec , I am using Sentry for tracking error logs:

spree_gateway (3.9.3) lib/controllers/spree/api/v2/storefront/intents_controller.rb in handle_response at line 9
actionpack (6.1.3) lib/action_controller/metal/basic_implicit_render.rb in send_action at line 6
actionpack (6.1.3) lib/abstract_controller/base.rb in process_action at line 228
scout_apm (4.0.4) lib/scout_apm/instruments/action_controller_rails_3_rails4.rb in process_action at line 100
actionpack (6.1.3) lib/action_controller/metal/rendering.rb in process_action at line 30
actionpack (6.1.3) lib/abstract_controller/callbacks.rb in block in process_action at line 42
activesupport (6.1.3) lib/active_support/callbacks.rb in run_callbacks at line 106
actionpack (6.1.3) lib/abstract_controller/callbacks.rb in process_action at line 41
actionpack (6.1.3) lib/action_controller/metal/rescue.rb in process_action at line 22
actionpack (6.1.3) lib/action_controller/metal/instrumentation.rb in block in process_action at line 34
activesupport (6.1.3) lib/active_support/notifications.rb in block in instrument at line 203
activesupport (6.1.3) lib/active_support/notifications/instrumenter.rb in instrument at line 24
activesupport (6.1.3) lib/active_support/notifications.rb in instrument at line 203
actionpack (6.1.3) lib/action_controller/metal/instrumentation.rb in process_action at line 33
actionpack (6.1.3) lib/action_controller/metal/params_wrapper.rb in process_action at line 249
searchkick (4.6.3) lib/searchkick/logging.rb in process_action at line 212
activerecord (6.1.3) lib/active_record/railties/controller_runtime.rb in process_action at line 27
scout_apm (4.0.4) lib/scout_apm/instruments/action_controller_rails_3_rails4.rb in process_action at line 117
actionpack (6.1.3) lib/abstract_controller/base.rb in process at line 165
actionview (6.1.3) lib/action_view/rendering.rb in process at line 39
actionpack (6.1.3) lib/action_controller/metal.rb in dispatch at line 190
actionpack (6.1.3) lib/action_controller/metal.rb in dispatch at line 254
actionpack (6.1.3) lib/action_dispatch/routing/route_set.rb in dispatch at line 50
actionpack (6.1.3) lib/action_dispatch/routing/route_set.rb in serve at line 33
actionpack (6.1.3) lib/action_dispatch/journey/router.rb in block in serve at line 50
actionpack (6.1.3) lib/action_dispatch/journey/router.rb in each at line 32
actionpack (6.1.3) lib/action_dispatch/journey/router.rb in serve at line 32
actionpack (6.1.3) lib/action_dispatch/routing/route_set.rb in call at line 842
scout_apm (4.0.4) lib/scout_apm/instruments/rails_router.rb in call_with_scout_instruments at line 29
railties (6.1.3) lib/rails/engine.rb in call at line 539
railties (6.1.3) lib/rails/railtie.rb in public_send at line 207
railties (6.1.3) lib/rails/railtie.rb in method_missing at line 207
actionpack (6.1.3) lib/action_dispatch/routing/mapper.rb in block in <class:Constraints> at line 20
actionpack (6.1.3) lib/action_dispatch/routing/mapper.rb in serve at line 49
actionpack (6.1.3) lib/action_dispatch/journey/router.rb in block in serve at line 50
actionpack (6.1.3) lib/action_dispatch/journey/router.rb in each at line 32
actionpack (6.1.3) lib/action_dispatch/journey/router.rb in serve at line 32
actionpack (6.1.3) lib/action_dispatch/routing/route_set.rb in call at line 842
scout_apm (4.0.4) lib/scout_apm/instruments/rails_router.rb in call_with_scout_instruments at line 29
scout_apm (4.0.4) lib/scout_apm/middleware.rb in call at line 17
rack-attack (6.6.1) lib/rack/attack.rb in call at line 127
rack (2.2.3) lib/rack/deflater.rb in call at line 44
warden (1.2.9) lib/warden/manager.rb in block in call at line 36
warden (1.2.9) lib/warden/manager.rb in catch at line 34
warden (1.2.9) lib/warden/manager.rb in call at line 34
rack (2.2.3) lib/rack/tempfile_reaper.rb in call at line 15
rack (2.2.3) lib/rack/etag.rb in call at line 27
rack (2.2.3) lib/rack/conditional_get.rb in call at line 40
rack (2.2.3) lib/rack/head.rb in call at line 12
actionpack (6.1.3) lib/action_dispatch/http/permissions_policy.rb in call at line 22
actionpack (6.1.3) lib/action_dispatch/http/content_security_policy.rb in call at line 18
rack (2.2.3) lib/rack/session/abstract/id.rb in context at line 266
rack (2.2.3) lib/rack/session/abstract/id.rb in call at line 260
actionpack (6.1.3) lib/action_dispatch/middleware/cookies.rb in call at line 689
actionpack (6.1.3) lib/action_dispatch/middleware/callbacks.rb in block in call at line 27
activesupport (6.1.3) lib/active_support/callbacks.rb in run_callbacks at line 98
actionpack (6.1.3) lib/action_dispatch/middleware/callbacks.rb in call at line 26
actionpack (6.1.3) lib/action_dispatch/middleware/actionable_exceptions.rb in call at line 18
sentry-rails (5.7.0) lib/sentry/rails/rescued_exception_interceptor.rb in call at line 12
actionpack (6.1.3) lib/action_dispatch/middleware/debug_exceptions.rb in call at line 29
sentry-ruby (5.7.0) lib/sentry/rack/capture_exceptions.rb in block (2 levels) in call at line 28
sentry-ruby (5.7.0) lib/sentry/hub.rb in with_session_tracking at line 220
sentry-ruby (5.7.0) lib/sentry-ruby.rb in with_session_tracking at line 375
sentry-ruby (5.7.0) lib/sentry/rack/capture_exceptions.rb in block in call at line 19
sentry-ruby (5.7.0) lib/sentry/hub.rb in with_scope at line 59
sentry-ruby (5.7.0) lib/sentry-ruby.rb in with_scope at line 355
sentry-ruby (5.7.0) lib/sentry/rack/capture_exceptions.rb in call at line 18
actionpack (6.1.3) lib/action_dispatch/middleware/show_exceptions.rb in call at line 33
railties (6.1.3) lib/rails/rack/logger.rb in call_app at line 37
railties (6.1.3) lib/rails/rack/logger.rb in block in call at line 26
activesupport (6.1.3) lib/active_support/tagged_logging.rb in block in tagged at line 99
activesupport (6.1.3) lib/active_support/tagged_logging.rb in tagged at line 37
activesupport (6.1.3) lib/active_support/tagged_logging.rb in tagged at line 99
railties (6.1.3) lib/rails/rack/logger.rb in call at line 26
actionpack (6.1.3) lib/action_dispatch/middleware/remote_ip.rb in call at line 81
actionpack (6.1.3) lib/action_dispatch/middleware/request_id.rb in call at line 26
rack (2.2.3) lib/rack/method_override.rb in call at line 24
rack (2.2.3) lib/rack/runtime.rb in call at line 22
activesupport (6.1.3) lib/active_support/cache/strategy/local_cache_middleware.rb in call at line 29
actionpack (6.1.3) lib/action_dispatch/middleware/executor.rb in call at line 14
actionpack (6.1.3) lib/action_dispatch/middleware/static.rb in call at line 24
rack (2.2.3) lib/rack/sendfile.rb in call at line 110
actionpack (6.1.3) lib/action_dispatch/middleware/host_authorization.rb in call at line 92
rack-cors (1.1.1) lib/rack/cors.rb in call at line 100
scout_apm (4.0.4) lib/scout_apm/instruments/middleware_summary.rb in call at line 58
railties (6.1.3) lib/rails/engine.rb in call at line 539
puma (5.2.1) lib/puma/configuration.rb in call at line 247
puma (5.2.1) lib/puma/request.rb in block in handle_request at line 76
puma (5.2.1) lib/puma/thread_pool.rb in with_force_shutdown at line 337
puma (5.2.1) lib/puma/request.rb in handle_request at line 75
puma (5.2.1) lib/puma/server.rb in process_client at line 431
puma (5.2.1) lib/puma/thread_pool.rb in block in spawn_thread at line 145

Thanks.