Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.