无法为Braintree示例建立快速测试

时间:2018-08-26 09:53:16

标签: braintree

我们尝试使用Braintree设置每月订阅计划。

我们看看https://developers.braintreepayments.com/start/hello-client/javascript/v3

我下载了以下代码片段,并将其另存为本地HTML文件在我的计算机中。 (我知道我需要使用后端服务器来为客户端浏览器生成令牌和下面的HTML代码片段。我只是想做一个快速测试,而无需先设置服务器。)

<head>
  <meta charset="utf-8">
  <script src="https://js.braintreegateway.com/web/dropin/1.12.0/js/dropin.min.js"></script>
</head>
<body>
  <div id="dropin-container"></div>
  <button id="submit-button">Request payment method</button>
  <script>
    var button = document.querySelector('#submit-button');

    braintree.dropin.create({
      authorization: 'CLIENT_TOKEN_FROM_SERVER',
      container: '#dropin-container'
    }, function (createErr, instance) {
      button.addEventListener('click', function () {
        instance.requestPaymentMethod(function (err, payload) {
          // Submit payload.nonce to your server
        });
      });
    });
  </script>
</body>

我们单击按钮。 instance.requestPaymentMethod正在执行。

我希望客户端浏览器将开始与braintree服务器通信,如步骤3所示。

enter image description here

我可以知道我错过了什么步骤吗?

1 个答案:

答案 0 :(得分:1)

您需要设置适当的后端服务器,才能使前端代码正常工作。因为没有正确的client_token(从后端服务器生成),前端代码将无法工作。

前端代码不会对client_token错误提供任何警告。

请注意,由于Braintree文档编写得不好,您将很难设置后端服务器

连续花费2天后,我发布了有关每月定期结算的代码段。

views.py

from django.views.generic import TemplateView
import braintree
import json
from django.shortcuts import redirect

from pprint import pprint

gateway = braintree.BraintreeGateway(
    braintree.Configuration(
        braintree.Environment.Sandbox,
        merchant_id="???",
        public_key="???",
        private_key="???"
    )
)

class PaymentMethodView(TemplateView):
    template_name = 'payment_method.html'

    def get_context_data(self, **kwargs):        
        # Call the base implementation first to get a context
        context = super(PaymentMethodView, self).get_context_data(**kwargs)

        subscribe = self.request.GET.get('subscribe')

        client_token = gateway.client_token.generate()     

        context['client_token_from_server'] = client_token
        return context


class SubscribeView(TemplateView):
    template_name = 'subscribe.html'

    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super(SubscribeView, self).get_context_data(**kwargs)

        payload_nonce = self.request.GET.get('payload_nonce')

        user = self.request.user

        result = gateway.customer.create({
            "email": user.email,
            "id": str(user.id),
            "payment_method_nonce": payload_nonce
        })

        if result.is_success:
            customer_id = result.customer.id

            payment_method_token = result.customer.payment_methods[0].token

            result = gateway.subscription.create({
                "payment_method_token": payment_method_token,
                "plan_id": "individual",
            })
        else:
            result = gateway.payment_method.create({
                "customer_id": str(user.id),
                "payment_method_nonce": payload_nonce
            })

            #pprint(vars(result))

            payment_method_token = result.payment_method.token

            result = gateway.subscription.create({
                "payment_method_token": payment_method_token,
                "plan_id": "individual",
            })

        context['is_success'] = result.is_success

        if result.is_success:
            print('success')
        else:
            for error in result.errors.deep_errors:
                print(error.attribute)
                print(error.code)
                print(error.message)

        return context

payment_method.html

<head>
  <meta charset="utf-8">
  <script src="https://js.braintreegateway.com/web/dropin/1.12.0/js/dropin.min.js"></script>
</head>
<body>
  <div id="dropin-container"></div>
  <button id="submit-button">Request payment method</button>
  <script>
    var button = document.querySelector('#submit-button');

    braintree.dropin.create({
      authorization: '{{ client_token_from_server }}',
      container: '#dropin-container'
    }, function (createErr, instance) {
      button.addEventListener('click', function () {
        instance.requestPaymentMethod(function (err, payload) {
          // Submit payload.nonce to your server
          url = '{% url 'subscribe' %}' + '?payload_nonce=' + payload.nonce;
          window.location.replace(url);
        });
      });
    });
  </script>
</body>

旁注

  1. 不同的API调用将产生不同的Result对象,但具有不同的属性。我怎么知道我需要打电话给payment_method_token = result.payment_method.token来获取payment_method_token。您无法通过阅读文档来分辨。您需要使用pprint(vars(result))对结果对象执行调试。 如果来自Braintree的人可以告诉我,我将非常高兴,如何通过阅读文档来查找此类信息。

  2. 命名很糟糕且令人困惑。例如,商家帐户商家帐户ID 是两种不同的方式。