Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Commit

Permalink
Handle processor and gateway error responses
Browse files Browse the repository at this point in the history
The existing code here will only return error messages if they come from
Braintree. In the case that the transaction is rejected by the gateway
or the processor, we'll return an error response with a blank message.

For these cases, the `errors` array will be empty, and we need to look
at the gateway rejection status or processor response codes as per the
documentation here:
https://developers.braintreepayments.com/reference/response/transaction/ruby#result-object
  • Loading branch information
Luuk Veenis committed Apr 25, 2017
1 parent df6808d commit 9020331
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
14 changes: 12 additions & 2 deletions app/models/solidus_paypal_braintree/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,18 @@ def build_success(result)
end

def build_failure(result)
msg = result.errors.map { |e| "#{e.message} (#{e.code})" }.join(" ")
new(false, msg)
new(false, error_message(result))
end

def error_message(result)
if result.errors.any?
result.errors.map { |e| "#{e.message} (#{e.code})" }.join(" ")
else
[result.transaction.status,
result.transaction.gateway_rejection_reason,
result.transaction.processor_settlement_response_code,
result.transaction.processor_settlement_response_text].compact.join(" ")
end
end
end
end
Expand Down
46 changes: 41 additions & 5 deletions spec/models/solidus_paypal_braintree/response_spec.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
require 'spec_helper'

RSpec.describe SolidusPaypalBraintree::Response do
let(:error_result) do
error = instance_double(
let(:failed_transaction) { nil }
let(:error) do
instance_double(
'Braintree::ValidationError',
code: '12345',
message: "Cannot refund a transaction unless it is settled."
)

end
let(:error_result) do
instance_double(
'Braintree::ErrorResult',
success?: false,
errors: [error]
errors: [error],
transaction: failed_transaction
)
end

Expand Down Expand Up @@ -61,7 +64,40 @@

context "with an error response" do
subject { error_response.message }
it { is_expected.to eq "Cannot refund a transaction unless it is settled. (12345)" }

context "with a Braintree error" do
it { is_expected.to eq "Cannot refund a transaction unless it is settled. (12345)" }
end

context "with a processor error" do
let(:error) { nil }
let(:failed_transaction) do
instance_double(
'Braintree::Transaction',
status: "settlement_declined",
gateway_rejection_reason: nil,
processor_settlement_response_code: "4001",
processor_settlement_response_text: "Settlement Declined"
)
end

it { is_expected.to eq "settlement_declined 4001 Settlement Declined" }
end

context "with a gateway error" do
let(:error) { nil }
let(:failed_transaction) do
instance_double(
'Braintree::Transaction',
status: "gateway_rejected",
gateway_rejection_reason: "cvv",
processor_settlement_response_code: nil,
processor_settlement_response_text: nil
)
end

it { is_expected.to eq "gateway_rejected cvv" }
end
end
end

Expand Down

0 comments on commit 9020331

Please sign in to comment.