Braintree + Python: Configure credential at transaction level rather than module

时光怂恿深爱的人放手 提交于 2020-01-14 04:40:07

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/21237863/braintree-python-configure-credential-at-transaction-level-rather-than-module

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!