python - 布伦特里订阅搜索

标签 python braintree

我正在进行 Braintree 订阅搜索,以便检索客户 ID 以及与这些 ID 相关的订阅价格。在我的代码中,我遵循 this post 的建议.

这是我的代码摘录:

gateway = braintree.BraintreeGateway(
    braintree.Configuration(
        environment=braintree.Environment.Production,
        merchant_id= 'our_merchant_id',
        public_key='our_public_key',
        private_key='our_private_key'
    )
)
subscriptions = gateway.subscription.search(
    braintree.SubscriptionSearch.status.in_list(
        braintree.Subscription.Status.Active,
        braintree.Subscription.Status.PastDue,
        braintree.Subscription.Status.Pending
    )
)

result = {}

for subscription in subscriptions.items:
    payment_method = gateway.payment_method.find(subscription.payment_method_token)
    result[payment_method.customer_id] = subscription.price

"""do something with result """

此方法在 BT 沙箱和大约 100 条记录的小型查询中运行良好。然而,每当我尝试查询超过 120 个订阅时,BT 服务器都会一致响应 504 错误。我想在生产中一次查询大约 5000 个订阅。有什么建议么?

回溯:

Traceback (most recent call last):
File "BT_subscrAmount.py", line 22, in <module>
for subscription in subscriptions.items:
File "/home/karen/miniconda3/lib/python3.6/site- 
packages/braintree/resource_collection.py", line 38, in items
for item in self.__method(self.__query, batch):
File "/home/karen/miniconda3/lib/python3.6/site- 
packages/braintree/subscription_gateway.py", line 79, in __fetch
response = self.config.http().post(self.config.base_merchant_path() + 
"/subscriptions/advanced_search", {"search": criteria})
File "/home/karen/miniconda3/lib/python3.6/site- 
packages/braintree/util/http.py", line 56, in post
return self.__http_do("POST", path, Http.ContentType.Xml, params)
File "/home/karen/miniconda3/lib/python3.6/site- 
packages/braintree/util/http.py", line 86, in __http_do
Http.raise_exception_from_status(status)
File "/home/karen/miniconda3/lib/python3.6/site- 
packages/braintree/util/http.py", line 49, in raise_exception_from_status
raise UnexpectedError("Unexpected HTTP_RESPONSE " + str(status))
braintree.exceptions.unexpected_error.UnexpectedError: Unexpected 
HTTP_RESPONSE 504

最佳答案

在一位出色的 BT 支持工程师的帮助下,我们终于找到了解决问题的方法。

解释:

当 BT 返回搜索结果时,它们会收集所有关联数据并向客户端返回一个大型(序列化)响应对象。在我们的例子中,一些订阅具有大量关联事务,这使得获取、序列化并将它们返回给客户端的过程变慢。发出请求时,BT 网关以 50 个为一组返回页面,默认超时为 60 秒。任何具有多个大型响应对象的组都可能会超时并产生 504 错误。

解决办法:

为了避免这种延迟,我们可以使用 ResourceCollections class 中的 id 方法,而不是使用 items 方法。并仅返回对象的 id。通过个人订阅 ID,我们可以获取个人订阅以及我们需要的属性,如下所示:

subscriptions = gateway.subscription.search(
    braintree.SubscriptionSearch.status.in_list(
    braintree.Subscription.Status.Active
    )
)

subscription_ids = subscriptions.ids

def find_customer_id(id):
    subscription = gateway.subscription.find(id)
    payment_method = gateway.payment_method.find(subscription.payment_method_token)
    return (payment_method.customer_id, subscription.price) 

希望这对其他人有帮助!

关于python - 布伦特里订阅搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52503289/

相关文章:

python - Braintree:Python Heroku 服务器

ios - 通过 Braintree PayPal 支付总是失败 iOS

python - 为什么我的脚本不写入文件?

python - 类方法 lambda Python

python - 如何在 PySMT 中使用数组?

swift - 安装了 CocoaPods 和 Braintree 现在我的项目无法构建

ios - BrainTree试飞上传unsupported architecture报错

javascript - 无法加载资源 : braintree js php integration aws ec2 lamp stack

python - Pandas - 用特定组的平均值替换列中的 NaN

javascript - 在 Python 中创建数学级数的高效、优雅的方法