Braintree + Python:在事务级别而不是模块配置凭据 [英] Braintree + Python: Configure credential at transaction level rather than module

查看:59
本文介绍了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.

我将如何实现?

推荐答案

我在Braintree工作。如果您需要更多帮助,请与我们的支持团队联系

配置对象可以实例化

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

并传递给 Braintree网关对象

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:在事务级别而不是模块配置凭据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆