Created
December 21, 2017 19:49
-
-
Save gregblass/33c92a58187450c82f1eba4e144c680f to your computer and use it in GitHub Desktop.
Better Braintree errors for Rails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module BraintreeHelper | |
SUCCESS_STATUSES = [ | |
Braintree::Transaction::Status::Authorizing, | |
Braintree::Transaction::Status::Authorized, | |
Braintree::Transaction::Status::Settled, | |
Braintree::Transaction::Status::SettlementConfirmed, | |
Braintree::Transaction::Status::SettlementPending, | |
Braintree::Transaction::Status::Settling, | |
Braintree::Transaction::Status::SubmittedForSettlement, | |
] | |
def braintree_errors(result) | |
errors = [] | |
verification = result.credit_card_verification | |
if verification | |
status = verification.status | |
if status == "processor_declined" | |
e = "Processor Declined: " | |
e += "(#{verification.processor_response_code}) " | |
e += verification.processor_response_text | |
errors << e | |
end | |
if status == "gateway_rejected" | |
e = "Gateway Rejected: " | |
reason = verification.gateway_rejection_reason | |
if reason == "avs" or reason == "cvv" | |
e += reason.upcase | |
else | |
e += reason.titleize | |
end | |
errors << e | |
end | |
end | |
transaction = result.transaction | |
if transaction and !SUCCESS_STATUSES.include? transaction.status | |
e = transaction.status.titleize | |
errors << e | |
end | |
if result.errors.size > 0 | |
result.errors.each do |error| | |
e = "(#{error.code}) " | |
e += error.message | |
errors << e | |
end | |
end | |
errors | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment