Skip to content

Commit

Permalink
Ensure we keep recording payment log entries that cannot be deserialized
Browse files Browse the repository at this point in the history
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
1 parent df9ab52 commit a36e681
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
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

0 comments on commit a36e681

Please sign in to comment.