From fce8d3a2f8acec42e98c69d4fdf00f30fdde00f5 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Fri, 19 Mar 2021 16:55:49 +1100 Subject: [PATCH] Record invalid payment states for debugging We observed invalid payment states in Bugsnag but we don't actually know in which state the payment intent was in. From the context we can guess that it was "succeeded" but it would be good to validate this. And in the future it would be good to know if there are other invalid states we can end up in. The notification to Bugsnag happens in another part of the code. --- config/locales/en.yml | 2 +- lib/stripe/payment_intent_validator.rb | 5 +++-- spec/lib/stripe/payment_intent_validator_spec.rb | 2 +- spec/models/spree/gateway/stripe_sca_spec.rb | 10 ++++++++++ 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/config/locales/en.yml b/config/locales/en.yml index fe50504b40..e95f488c1c 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -294,7 +294,7 @@ en: error: Error processing_payment: "Processing payment..." no_pending_payments: "No pending payments" - invalid_payment_state: "Invalid payment state" + invalid_payment_state: "Invalid payment state: %{state}" filter_results: Filter Results quantity: Quantity pick_up: Pick up diff --git a/lib/stripe/payment_intent_validator.rb b/lib/stripe/payment_intent_validator.rb index b6d1e52b7a..075e064fcc 100644 --- a/lib/stripe/payment_intent_validator.rb +++ b/lib/stripe/payment_intent_validator.rb @@ -23,9 +23,10 @@ module Stripe end def raise_if_not_in_capture_state(payment_intent_response) - return unless payment_intent_response.status != 'requires_capture' + state = payment_intent_response.status + return unless state != 'requires_capture' - raise Stripe::StripeError, I18n.t(:invalid_payment_state) + raise Stripe::StripeError, I18n.t(:invalid_payment_state, state: state) end end end diff --git a/spec/lib/stripe/payment_intent_validator_spec.rb b/spec/lib/stripe/payment_intent_validator_spec.rb index a6b983b367..ac4a06a83c 100644 --- a/spec/lib/stripe/payment_intent_validator_spec.rb +++ b/spec/lib/stripe/payment_intent_validator_spec.rb @@ -40,7 +40,7 @@ module Stripe it "raises Stripe error with an invalid_payment_state message" do expect { validator.call(payment_intent_id, stripe_account_id) - }.to raise_error Stripe::StripeError, "Invalid payment state" + }.to raise_error Stripe::StripeError, "Invalid payment state: failed" end end diff --git a/spec/models/spree/gateway/stripe_sca_spec.rb b/spec/models/spree/gateway/stripe_sca_spec.rb index 9c05d514e0..e62293ae12 100644 --- a/spec/models/spree/gateway/stripe_sca_spec.rb +++ b/spec/models/spree/gateway/stripe_sca_spec.rb @@ -39,6 +39,16 @@ describe Spree::Gateway::StripeSCA, type: :model do expect(response.success?).to eq true end + + it "provides an error message to help developer debug" do + stub_request(:get, "https://api.stripe.com/v1/payment_intents/12345"). + to_return(status: 200, body: capture_successful) + + response = subject.purchase(order.total, credit_card, gateway_options) + + expect(response.success?).to eq false + expect(response.message).to eq "Invalid payment state: succeeded" + end end def payment_intent(amount, status)