Skip to content

Commit

Permalink
Govpay error debug logging (#1284)
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulDoyle-EA authored Dec 14, 2022
1 parent 443adfe commit 1bb3b5a
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module WasteCarriersEngine
class GovpayFormsController < ::WasteCarriersEngine::FormsController
include UnsubmittableForm
include CanAddDebugLogging

def new
super(GovpayForm, "govpay_form")
Expand Down Expand Up @@ -31,8 +32,9 @@ def payment_callback
end
end
rescue ArgumentError
log_transient_registration_details("Invalid payment uuid in payment callback", @transient_registration)
Rails.logger.warn "Govpay payment callback error: invalid payment uuid \"#{params[:uuid]}\""
Airbrake.notify("Govpay callback error", "Invalid payment uuid \"#{params[:uuid]}\"")
Airbrake.notify("Govpay callback error: Invalid payment uuid", payment_uuid: params[:uuid])
flash[:error] = I18n.t(".waste_carriers_engine.govpay_forms.new.internal_error")
go_back
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module WasteCarriersEngine
module CanAddDebugLogging
extend ActiveSupport::Concern

# rubocop:disable Metrics/MethodLength
# rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
def log_transient_registration_details(description, transient_registration)
return unless FeatureToggle.active?(:additional_debug_logging)

Expand All @@ -23,7 +23,9 @@ def log_transient_registration_details(description, transient_registration)
"expires_on: #{transient_registration.expires_on}, " \
"renew_token: #{renew_token(transient_registration)}, " \
"metaData.route: #{transient_registration.metaData.route}, " \
"created_at: #{transient_registration.created_at}"
"created_at: #{transient_registration.created_at}, " \
"orders: #{transient_registration.finance_details&.orders}, " \
"payments: #{transient_registration.finance_details&.payments}"
)
end
Airbrake.notify(error, reg_identifier: transient_registration.reg_identifier) if defined?(Airbrake)
Expand All @@ -34,7 +36,7 @@ def log_transient_registration_details(description, transient_registration)
Airbrake.notify(e, reg_identifier: transient_registration.reg_identifier) if defined?(Airbrake)
Rails.logger.warn "Error writing transient registration details to the log: #{e}"
end
# rubocop:enable Metrics/MethodLength
# rubocop:enable Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity

private

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def govpay_payment_status
status
rescue StandardError => e
Rails.logger.error "Failed to retrieve payment status: #{e}"
Airbrake.notify "Failed to retrieve payment status: #{e}"
"error"
end

Expand Down Expand Up @@ -64,8 +65,8 @@ def order
.finance_details
.orders
.find_by(payment_uuid: payment_uuid)
rescue StandardError
raise ArgumentError, "Order not found for payment uuid \"#{payment_uuid}\""
rescue StandardError => e
raise ArgumentError, "Order not found for payment uuid \"#{payment_uuid}\": #{e}"
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,14 @@ def valid_order?
return true if @order.present?

Rails.logger.error "Invalid Govpay response: Cannot find matching order"
Airbrake.notify "Invalid Govpay response: Cannot find matching order", { payment_uuid: @payment_uuid }
false
end

def valid_payment_uuid?
unless @payment_uuid.present?
Rails.logger.error "Invalid Govpay response: Missing payment uuid"
Airbrake.notify "Invalid Govpay response: Missing payment uuid", order_id: @order&.order_id
return false
end

Expand All @@ -71,14 +73,16 @@ def valid_payment_uuid?

return true if order.present?

Rails.logger.error "Invalid Govpay response: Cannot find matching order for payment uuid #{@payment_uuid}"
Rails.logger.error "Invalid Govpay response: No matching order for payment uuid #{@payment_uuid}"
Airbrake.notify "Invalid Govpay response: No matching order for payment uuid", payment_uuid: @payment_uuid
false
end

def valid_status?(response_type)
return true if GovpayValidatorService.valid_govpay_status?(response_type, @govpay_status)

Rails.logger.error "Invalid Govpay response: #{@status} is not a valid payment status for #{response_type}"
Airbrake.notify "Invalid Govpay response: \"#{@status}\" is not a valid payment status for \"#{response_type}\""
false
end
end
Expand Down
13 changes: 10 additions & 3 deletions spec/requests/waste_carriers_engine/govpay_forms_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module WasteCarriersEngine
# TODO: Remove these when the feature flag is no longer required
allow(WasteCarriersEngine::FeatureToggle).to receive(:active?).with(:govpay_payments).and_return(true)
allow(WasteCarriersEngine::FeatureToggle).to receive(:active?).with(:use_extended_grace_window).and_return(true)
allow(WasteCarriersEngine::FeatureToggle).to receive(:active?).with(:additional_debug_logging).and_return true
end

context "when a valid user is signed in" do
Expand Down Expand Up @@ -171,17 +172,23 @@ module WasteCarriersEngine
status: 200,
body: File.read("./spec/fixtures/files/govpay/get_payment_response_not_found.json")
)

get payment_callback_govpay_forms_path(token, "invalid_uuid")
end

it "does not create a payment" do
get payment_callback_govpay_forms_path(token, "invalid_uuid")
expect(transient_registration.reload.finance_details.payments.first).to be_nil
end

it "redirects to payment_summary_form" do
get payment_callback_govpay_forms_path(token, "invalid_uuid")
expect(response).to redirect_to(new_payment_summary_form_path(token))
end

it "notifies Airbrake" do
expect(Airbrake)
.to have_received(:notify)
.with("Govpay callback error: Invalid payment uuid", { payment_uuid: "invalid_uuid" })
end
end
end

Expand Down Expand Up @@ -249,7 +256,7 @@ module WasteCarriersEngine
it "logs an error" do
get payment_callback_govpay_forms_path(token, order.payment_uuid)

expect(Airbrake).to have_received(:notify)
expect(Airbrake).to have_received(:notify).at_least(:once)
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module WasteCarriersEngine
:has_finance_details,
temp_cards: 0)
end
let(:order) { transient_registration.finance_details.orders.first }
let(:current_user) { build(:user) }

before do
Expand Down Expand Up @@ -42,8 +43,13 @@ module WasteCarriersEngine
end
end

let(:govpay_id) { "a-valid-govpay-payment-id" }

before do
stub_request(:get, /.*#{govpay_host}.*/).to_return(
order.govpay_id = govpay_id
transient_registration.save!

stub_request(:get, %r{.*#{govpay_host}/payments.+}).to_return(
status: 200,
body: File.read("./spec/fixtures/files/govpay/#{response_fixture}")
)
Expand Down

0 comments on commit 1bb3b5a

Please sign in to comment.