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 1 commit
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
Prev Previous commit
Ensure we keep recording payment log entries that cannot be deserialized
Raising an exception at this time could prevent a payment from being
properly processed. Any incompatible response will be wrapped in a
valid one and saved with proper reporting.

This allows it to be both recorded and reported without breaking the
payment details page in the admin.
  • Loading branch information
elia committed Feb 23, 2023
commit a36e68177a7f6ef36e53ab2e479a3d7e25393d70
11 changes: 11 additions & 0 deletions core/app/models/spree/log_entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,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