braintree / braintree_python

Braintree Python library
https://developer.paypal.com/braintree/docs/start/overview
MIT License
242 stars 115 forks source link

Access to the error response code #57

Closed andymckay closed 9 years ago

andymckay commented 9 years ago

It looks like there are some errors that return an error code, but the result of the parsing in ErrorResult is that the actual response code is thrown away.

Here's an example, given the gist at https://gist.github.com/andymckay/a9340d1b0a7ce483025e:

In [6]: from braintree.error_result import ErrorResult

In [7]: err = ErrorResult(None, data)

In [8]: data['message']
Out[8]: u'Invalid Secure Payment Data'

In [9]: data['transaction']['processor_response_code']
Out[9]: u'2078'

You can access that in the response, but once it gets parsed into a Transaction, its lost, because Transaction doesn't parse that at all:

In [10]: err.params
Out[10]: 
{u'descriptor': {u'name': u'Mozilla*product', u'url': u'mozilla.org'},
 u'payment_method_token': u'7fk5cg',
 u'plan_id': u'mozilla-concrete-brick',
 u'trial_period': u'false'}

In [12]: err.transaction
Out[12]: <Transaction {amount: Decimal('2078.00'), credit_card: {u'bin': u'411111', u'card_type': u'Visa', u'unique_number_identifier': u'21ee4272998b10107aeee9d50d6fe1ae', u'expiration_year': u'2016', u'prepaid': u'Unknown', u'durbin_regulated': u'Unknown', u'commercial': u'Unknown', u'healthcare': u'Unknown', u'payroll': u'Unknown', u'issuing_bank': u'Unknown', u'last_4': u'1111', u'expiration_month': u'12', u'cardholder_name': u'a', u'token': u'7fk5cg', u'customer_location': u'US', u'image_url': u'https://assets.braintreegateway.com/payment_method_logo/visa.png?environment=sandbox', u'country_of_issuance': u'Unknown', u'debit': u'Unknown', u'venmo_sdk': False, u'product_id': u'Unknown'}} at 4426039504>

Note that if you pass the Transaction information to the Verification object, you can get something useful:

In [15]: cc = CreditCardVerification(None, data['transaction'])

In [16]: cc.processor_response_code
Out[16]: u'2078'

We'd like to be able to return that unique to braintree response code up the stack, log it etc. But we can't unless we monkey patch ErrorResult. Unless I'm missing something on how to process the response for this kind of error.

agfor commented 9 years ago

@andymckay I'm able to access the processor response code on the transaction, using the data you provided:

>>> result = braintree.ErrorResult(None, data)
>>> result
<ErrorResult 'Invalid Secure Payment Data' at 10e01b250>
>>> result.transaction
<Transaction {amount: Decimal('2078.00')} at 4529960848>
>>> result.transaction.processor_response_code
u'2078'

This matches the docs for the Transaction object.

andymckay commented 9 years ago

Gah I suck! Sorry about that.

andymckay commented 9 years ago

... and thanks :)