Braintree付款不适用于AJAX提交

问题描述:

我有一个动态的AJAX提交.我正在尝试使用AJAX将Braintree(PayPal)付款数据提交到payment.php.不幸的是,布伦特里给我一个随机数错误. Braintree会在提交时创建一个带有代码的输入(即刻),但是我的提交是在创建代码之前提交的.

I have a dynamic AJAX submit. I am trying to submit Braintree (PayPal) payment data into payment.php using AJAX. Unfortunately, Braintree is giving me a nonce error. Braintree creates an input with a code (nonce) on submit, but my submit is submitted before the code is created.

Braintree给了我一个创建代码的脚本

Braintree gives me a script that creates the code

<script src="https://js.braintreegateway.com/v2/braintree.js"></script>

我用类似的东西

$(document).on("submit","form",function(event){
  if(!$(this).is("[action]")){
    event.preventDefault()
    formdata=new FormData(this)
    submit(this)
}

submit(this)调用ajax.我试图延迟提交,但是没有任何效果.例如.如果我在提交过程中调用了alert(),则会添加代码,并且提交工作正常;除了现在我有一个警报.问题在于两个代码同时运行,而Braintree代码太慢而无法响应.我也尝试将链接重新定位在我的JS代码上方和下方,但是没有运气.

submit(this) calls the ajax. I tried to delay the submit, but then nothing works. For example. If I call an alert() during my submit, the code is added and the submit works fine; except for the fact that now I have an alert. The problem is that both codes run at the same time and the Braintree code is too slow to react. I also tried to re-position the link above and below my JS code with no luck.

如所述

As mentioned here, I think you should use onPaymentMethodReceived callback from GlobalSetup of BrainTree. Instead of handling form submit on your own using jQuery, you can configure this callback in the setup like below.

    braintree.setup("CLIENT-TOKEN-FROM-SERVER", "dropin", {
      container: "dropin-container",
      onPaymentMethodReceived: function (paymentMethod) {
        // Do some logic in here.
        // When you're ready to submit the form:
        myForm.submit();
      }
   });

当Drop-in生成payment_method_nonce(作为表单提交的结果)时,将调用onPaymentMethodReceived.将使用paymentMethod对象调用该对象,该对象包含随机数作为字符串.

The onPaymentMethodReceived is called when a payment_method_nonce has been generated by Drop-in (as the result of a form submission). It will be called with a paymentMethod object, which contains the nonce as a string.

您可以此处找到更多详细信息.传递给onPaymentMethodReceived回调的paymentMethod对象,它具有一个名为nonce的属性.

You can find more details here about the paymentMethod object passed to onPaymentMethodReceived callback, it has a property called nonce.

希望这会有所帮助:)