【问题标题】:Is this the right way to create a subscription for Braintree in Python using the API这是使用 API 在 Python 中为 Braintree 创建订阅的正确方法吗
【发布时间】:2018-04-16 19:03:46
【问题描述】:

为了澄清问题,我在这里提出这个问题并分享我迄今为止所学到的知识。

首先,如果你想要一个简单、易于开发的交易系统,Braintree 就是它。真的很简单,而且和 Django 配合得很好。

但是,订阅方面的事情还不是很清楚。因此,我将分享一些代码以获取反馈并帮助简化。

首先.. 一些假设。

工作流程

使用 API 创建订阅的流程如下:

(请不要在控制面板中向我发送有关如何操作的文档。可以在此处找到订阅工作流程的非常广泛的描述:https://developers.braintreepayments.com/guides/recurring-billing/create/python)

  1. 使用braintree.ClientToken.generate() 创建客户端令牌
  2. 使用braintree.Customer.create() 创建一个添加付款方式的客户
  3. 从Customer.create() 响应中获取客户 ID
  4. 使用braintree.Subscription.create() 创建订阅,传入新客户和名为payment_method_token 的新客户令牌

如果您想知道,这是 Django,我正在尝试在一个视图中完成所有这些操作。

示例代码

custy_result = braintree.Customer.create({
    "first_name": self.request.POST.get('first_name'),
    "last_name": self.request.POST.get('last_name'),
    "company": self.request.POST.get('company_name'),
    "email": self.request.POST.get('office_email'),
    "phone": self.request.POST.get('office_phone'),
    'payment_method_nonce': 'fake-valid-nonce', # for testing
    "credit_card": {
        "billing_address": {
            "street_address": self.request.POST.get('address'),
            "locality": self.request.POST.get('city'),
            "region": self.request.POST.get('state'),
            "postal_code": self.request.POST.get('postal'),
        }
    }
})

if custy_result.is_success:
    print("Customer Success!")
else:
    for error in custy_result.errors.deep_errors:
        print(error.code)
        print(error.message)

# Create the subscription in braintree
sub_result = braintree.Subscription.create({
    "payment_method_token": "the_token", # <-- How do I get this token?
    "plan_id": "the_plan_id_in_braintree"
})

if sub_result.is_success:
    print("Subscription Success!")
else:
    for error in sub_result.errors.deep_errors:
        print(error.code)
        print(error.message)

问题?

我如何获得该令牌?什么是“the_token”?它来自哪里?

我看不到它是如何创建的,也看不到它是从哪里拉出来的。我想做custy_result.customer.token 之类的事情,但这显然是错误的。

作为参考,这是我在文档中查看的内容:

Customers

Payment Methods

Recurring Payments

Testing

Create Subscription

Customer.response

Credit Card Response

【问题讨论】:

    标签: python django django-views braintree braintree-sandbox


    【解决方案1】:

    你的custy_result 应该有payment_methods 属性:

    result = braintree.Subscription.create({
        "payment_method_token": custy_result.payment_methods[0].token,
        "plan_id": "planID"
    })
    

    【讨论】:

    • 不错!那真的很接近。只是,它是custy_result.customer.payment_methods[0].token
    猜你喜欢
    • 1970-01-01
    • 2013-01-12
    • 2014-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-25
    • 2013-11-24
    • 1970-01-01
    相关资源
    最近更新 更多