Question

I am currently using Python to integrate with Braintree. At the module-level, we configure our API keys. From the doc:

import braintree
braintree.Configuration.configure(...)

def my_transaction():
    braintree.Transaction.sale(...)

How can I configure braintree at the method level? That is, if I wanted to use a different credential for each transaction, how could I do so without updating a global config? Eg:

import braintree

def my_transaction():
    braintree.Transaction.sale({
        'configuration': {...},
        'amount': ...
    })

I would like to be able to use a different API key, depending on the source of the transaction. I would also like to be able to more easily toggle between Sandbox and Production credentials.

How would I accomplish this?

Was it helpful?

Solution

I work at Braintree. If you need more help, please get in touch with our support team.

Configuration objects can be instantiated:

config = braintree.Configuration(
    environment=braintree.Environment.Sandbox,
    merchant_id='my_merchant_id',
    public_key='public_key',
    private_key='private_key'
)

and passed to a Braintree gateway object:

gateway = braintree.BraintreeGateway(config)

which you can then use to run transactions:

result = gateway.transaction.create({'amount': ...})

So you can either instantiate a new gateway for each transaction with the appropriate credentials, or keep around a gateway with each set of credentials and use the appropriate one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top