3

为了清楚起见,我在这里提出这个问题并分享我到目前为止所学到的东西。

首先,如果你想要一个简单、易于开发的交易系统,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但这显然是错误的。

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

顾客

支付方式

定期付款

测试

创建订阅

客户响应

信用卡回应

4

1 回答 1

3

custy_result应该有payment_methods属性:

result = braintree.Subscription.create({
    "payment_method_token": custy_result.payment_methods[0].token,
    "plan_id": "planID"
})
于 2017-11-04T10:24:11.710 回答