我目前正在使用 Python 与 Braintree 集成。在模块级别,我们配置我们的 API key 。来自文档:
import braintree
braintree.Configuration.configure(...)
def my_transaction():
braintree.Transaction.sale(...)
如何在方法级别配置 braintree?也就是说,如果我想为每笔交易使用不同的凭证,我怎么能在不更新全局配置的情况下这样做呢?例如:
import braintree
def my_transaction():
braintree.Transaction.sale({
'configuration': {...},
'amount': ...
})
我希望能够根据交易来源使用不同的 API key 。我还希望能够更轻松地在 Sandbox 和 Production 凭据之间切换。
我将如何完成这个?
最佳答案
我在布伦特里工作。如需更多帮助,请get in touch with our support team .
配置对象 can be instantiated :
config = braintree.Configuration(
environment=braintree.Environment.Sandbox,
merchant_id='my_merchant_id',
public_key='public_key',
private_key='private_key'
)
并传递给 Braintree gateway object :
gateway = braintree.BraintreeGateway(config)
然后你可以use to run transactions :
result = gateway.transaction.create({'amount': ...})
因此,您可以使用适当的凭据为每个事务实例化一个新网关,或者使用每组凭据保留一个网关并使用适当的一个。
关于python - 布伦特里 + Python : Configure credential at transaction level rather than module,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21237863/