Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow bad payloads to be saved in payment log entries #4953

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions core/app/models/spree/log_entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ class LogEntry < Spree::Base
ActiveSupport::TimeZone
].freeze

# Raised when a disallowed class is tried to be loaded
class DisallowedClass < RuntimeError
class SerializationError < RuntimeError
attr_reader :psych_exception

def initialize(psych_exception:)
@psych_exception = psych_exception
super(default_message)
end
end

# Raised when a disallowed class is tried to be loaded
class DisallowedClass < SerializationError
private

def default_message
Expand All @@ -40,14 +42,7 @@ def default_message
end

# Raised when YAML contains aliases and they're not enabled
class BadAlias < RuntimeError
attr_reader :psych_exception

def initialize(psych_exception:)
@psych_exception = psych_exception
super(default_message)
end

class BadAlias < SerializationError
private

def default_message
Expand Down Expand Up @@ -89,6 +84,17 @@ def parsed_details=(value)
end
end

def parsed_payment_response_details_with_fallback=(response)
self.parsed_details = response
rescue SerializationError, YAML::Exception => e
# Fall back on wrapping the response and signaling the error to the end user.
self.parsed_details = ActiveMerchant::Billing::Response.new(
response.success?,
"[WARNING: An error occurred while trying to serialize the payment response] #{response.message}",
{ 'data' => response.inspect, 'error' => e.message.to_s },
)
end

private

def handle_psych_serialization_errors
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/payment/processing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def handle_response(response)
end

def record_response(response)
log_entries.create!(parsed_details: response)
log_entries.create!(parsed_payment_response_details_with_fallback: response)
end

def protect_from_connection_error
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/refund.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def perform!
credit_cents = money.cents

@perform_response = process!(credit_cents)
log_entries.build(details: perform_response.to_yaml)
log_entries.build(parsed_payment_response_details_with_fallback: perform_response)

self.transaction_id = perform_response.authorization
save!
Expand Down
19 changes: 19 additions & 0 deletions core/spec/models/spree/log_entry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,23 @@
)
end
end

describe '#parsed_payment_response_details_with_fallback=' do
it 'wraps non serializable responses' do
log_entry = described_class.new
bad_response = ActiveMerchant::Billing::Response.new(
true,
'FooBar',
{ foo: { bar: "Symbol keys are not allowed" } }
)

log_entry.parsed_payment_response_details_with_fallback = bad_response
details = log_entry.parsed_details

expect(details.success?).to eq(true)
expect(details.message).to eq("[WARNING: An error occurred while trying to serialize the payment response] FooBar")
expect(details.params['data']).to include(':bar=>"Symbol keys are not allowed"')
expect(details.params['error']).to include('Tried to dump unspecified class: Symbol')
end
end
end
3 changes: 1 addition & 2 deletions core/spec/models/spree/payment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -518,8 +518,7 @@
it "should do nothing" do
expect(payment).not_to receive(:complete)
expect(payment.payment_method).not_to receive(:capture)
expect(payment.log_entries).not_to receive(:create!)
subject
expect{ subject }.not_to change(payment.log_entries, :count)
end
end
end
Expand Down