本文介绍了Braintree + Python:在事务级别而不是模块配置凭据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Python与Braintree集成。在模块级别,我们配置API密钥。从文档中:

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(...)

如何在方法级别配置Braintree?也就是说,如果我想为每个事务使用不同的凭据,该如何在不更新全局配置的情况下使用呢?例如:

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': ...
    })

我希望能够使用其他API密钥,具体取决于交易的来源。我还希望能够更轻松地在沙箱和生产凭据之间切换。

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.

我将如何实现?

推荐答案

配置对象:

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

并传递给:

gateway = braintree.BraintreeGateway(config)

然后您可以:

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.

这篇关于Braintree + Python:在事务级别而不是模块配置凭据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 00:46