braintree / braintree_python

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

gateway.transaction.find raises a TypeError when initialised with client_id and secret #128

Open jvn145 opened 3 years ago

jvn145 commented 3 years ago

General information

Issue description

When I try and gateway.transaction.find a transaction using a gateway that was initialised with a client_id and secret, I receive a TypeError "can only concatenate str (not "NoneType") to str".

Had a look, this happens because the BraintreeGateway.merchant_id is not set if you initialise it using a client_id and secret.

Steps to reproduce:

# gateway using my credentials
gateway = braintree.BraintreeGateway(
    client_id=os.getenv('BT_CID'), 
    client_secret=os.getenv('BT_SECRET')
)

result = gateway.transaction.sale({
    "amount": '1.00',
    "payment_method_nonce": 'fake-valid-nonce',
    "options": {
        "submit_for_settlement": True
    }
})

transaction = result.transaction

result = gateway.transaction.find(transaction.id)

Traceback:

Traceback (most recent call last):
  File "/test.py", line 89, in <module>
    result = my_gateway.transaction.sale({
  File "/usr/local/lib/python3.9/site-packages/braintree/transaction_gateway.py", line 84, in sale
    return self.create(params)
  File "/usr/local/lib/python3.9/site-packages/braintree/transaction_gateway.py", line 31, in create
    return self._post("/transactions", {"transaction": params})
  File "/usr/local/lib/python3.9/site-packages/braintree/transaction_gateway.py", line 168, in _post
    response = self.config.http().post(self.config.base_merchant_path() + url, params)
  File "/usr/local/lib/python3.9/site-packages/braintree/configuration.py", line 113, in base_merchant_path
    return "/merchants/" + self.merchant_id
TypeError: can only concatenate str (not "NoneType") to str
hollabaq86 commented 3 years ago

👋 @jvn145 thanks for reaching out. Creating a Braintree gateway using a client ID and client secret is only intended for oAuth management, not transaction management like transaction.sale calls. If you're trying to create a transaction using a shared vault, you'll need to create an access token first and create your gateway instance with that access token.

If you're not using an oAuth scenario to manage your vault and transact with a shared vault, you should be creating your gateway using API credentials. For any assistance with oAuth or setting up your gateway with API credentials, please contact Support.

All this said, this error message isn't helpful in understanding the limitations of making a gateway via oAuth, and we should definitely update the SDK to surface a better error message. I'll leave this issue open to track improving our error messaging around this scenario.

hollabaq86 commented 1 year ago

for internal tracking, ticket 836