Skip to content

Instantly share code, notes, and snippets.

@gregblass
Created December 21, 2017 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gregblass/33c92a58187450c82f1eba4e144c680f to your computer and use it in GitHub Desktop.
Save gregblass/33c92a58187450c82f1eba4e144c680f to your computer and use it in GitHub Desktop.
Better Braintree errors for Rails
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