简体   繁体   English

Braintree Dropin形式问题-未生成随机数字符串

[英]Braintree dropin form issue - nonce string not being generated

I'm trying to use the dropin form from braintree but it produces a weird behaviour. 我正在尝试使用braintree的dropin 形式 ,但是会产生奇怪的行为。

I can successfully place the form in a view: 我可以将表格成功放置在视图中:

<form id="createTransactionForm" method="post" action="#">
    <div id="payment-form"></div>
    <input type="submit" value="Test - Pay">
</form>

<script>

    var braintreeToken = @Html.Raw(Json.Encode(Model.brainTreeToken));

    braintree.setup(
        braintreeToken,
        "dropin", {
            container: "payment-form"
        });

    ...

Here's the result: 结果如下:

Braintree DropIn UI表单

They state in their documentation (3rd paragraph) that braintree.js will add a hidden input named payment_method_nonce to your form . 他们在文档 (第3段)中指出braintree.js will add a hidden input named payment_method_nonce to your form

On my controller, I got the action method to catch the form collection and there's a payment_method_nonce key in the form collection with an empty string as a value but no other keys, no other form fields are caught here. 在我的控制器上,我有一个action方法来捕获表单集合,并且表单集合中有一个payment_method_nonce键,其中有一个空字符串作为值,但没有其他键,这里没有其他表单字段被捕获。 I was expecting the card number and the expiration date, as seen in the picture above. 如上图所示,我期望的是卡号和有效期。

Using the inspector, I see that the braintree dropin cretes an HTML structure with nested forms: 使用检查器,我发现braintree dropin创建具有嵌套形式的HTML结构:

嵌套表格

My question is, how can I use the braintree dropin form and catch all their form input values in the action method? 我的问题是,如何使用braintree dropin表单并在action方法中捕获其所有表单输入值?

EDIT 编辑

OK, so thanks to @MikeMiller, I learned that I don't need to catch the CC values in the controller method, just the nonce string that's supposedly generated from them. 好的,因此感谢@MikeMiller,我了解到我不需要在controller方法中捕获CC值,只需捕获据推测是由它们生成的现时字符串即可。 However, I am getting an empty string. 但是,我得到一个空字符串。

From his suggestion, I tried to add to the form a specific value for the action attribute: 根据他的建议,我尝试将action属性的特定值添加到表单中:

<form id="createTransactionForm" method="post" action="@Url.Action("MyMethod", "MyController")">

But the result is the same, an empty string for the nonce. 但是结果是一样的,随机数为空字符串。

I got mine working with the following code (Laravel5 blade). 我让我使用以下代码(Laravel5刀片)。 My issue was submitting the form with jQuery - without a button element in the form the nonce value doesnt get populated. 我的问题是使用jQuery提交表单-表单中没有按钮元素,无法填充现时值。

<form id="checkout" method="post" action="{{URL('admin/checkout')}}">
<div id="payment-form"></div>
<br>
<div class="input-group">
    <div class="input-group-btn" >
        <div class="btn-group">
            <button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown" style="width:50px">
                <span data-bind="label" id="currency">&pound;</span>&nbsp;<span class="caret"></span>
            </button>
            <ul class="dropdown-menu" role="menu" style="min-width:50px">
                <li style="width:50px"><a href="GBP">&pound;</a></li>
                <li style="width:50px"><a href="USD">&dollar;</a></li>
                <li style="width:50px"><a href="EUR">&euro;</a></li>
            </ul>
        </div>
    </div>
    <input type="text" name="amount" id="amount" class="form-control" />


    <div class="input-group-btn">
        <button role="submit" class="btn  btn-primary btn-block" id="submit">Make Payment</button>
    </div>
</div>
</form>
<script src="https://js.braintreegateway.com/v2/braintree.js"></script>
<script>
  braintree.setup("@braintreeClientToken", "dropin", {
    container: "payment-form",
    form: 'checkout'
  });
</script>

When you get an empty nonce, I was having the same trouble. 当您得到一个空的随机数时,我遇到了同样的麻烦。 However, in my case, I was doing 'custom' instead of 'dropin'. 但是,就我而言,我正在执行“自定义”而不是“直接执行”。 I did lots of tests in all kinds of scenarios, such as creating a static tokenization token from the dashboard, or generating one on the fly when the page loaded. 我在各种情况下都进行了很多测试,例如从仪表板创建静态令牌化令牌,或者在页面加载时即时生成一个令牌。 They have some anti-fraud measures in place, I suspect, and it creates odd scenarios where the form submits no matter what, even if form validation routines fail. 我怀疑,他们采取了一些反欺诈措施,这会造成奇怪的情况,即使表单验证例程失败,表单也不管提交什么。 Or, the form submits and you get no nonce. 或者,表单提交,您将不会得到随机数。 The docs are also extremely confusing on this. 这些文档在这方面也非常令人困惑。 So, this is the fix that I finally got going. 所以,这就是我最终要解决的问题。 What they don't tell you in the docs is that the onPaymentMethodReceived is only called on a form submit operation. 他们在文档中没有告诉您的onPaymentMethodReceived仅在表单提交操作上调用。 The other remarkable discovery I made was that if I use the following code and add <input type="hidden" name="payment_method_nonce" id="payment_method_nonce" /> to the form and then replace the $('#token') line below with a call to update the payment_method_nonce hidden field -- the Braintree API erases it right before form submit! 我做出的另一个显着发现是,如果我使用以下代码并将<input type="hidden" name="payment_method_nonce" id="payment_method_nonce" />到表单中,然后替换$('#token')行下面的调用来更新payment_method_nonce隐藏字段-Braintree API会在表单提交之前立即删除它! So, the fix on that is to rename that hidden field to "token" and then on the form submit code in PHP I can process $_POST['token'] instead of $_POST['payment_method_nonce'] , but treat it like a nonce. 因此,解决此问题的方法是将隐藏字段重命名为“ token”,然后在PHP的表单提交代码中,我可以处理$_POST['token']而不是$_POST['payment_method_nonce'] ,但将其视为随机数。

function invalidForm(){
    // use the Stripe or Braintree credit card form validator and any other form validations you want here
    // Braintree: https://github.com/braintree/card-validator
    // Stripe: https://github.com/stripe/jquery.payment
    // return a string value of the problem
    return '';
}

jQuery(document).ready(function(){

    $('FORM#checkout').append('<input type="hidden" id="token" name="token" />');
    // Generate the static client token from your dashboard > Account > My User > 
    // Authorizations > Tokenization Keys
    var clientToken = 'sandbox_555555_555555555555';
    braintree.setup(clientToken, 'custom', {
        id:'checkout',
        onPaymentMethodReceived: function (paymentMethod) {
            $('#btnPurchase').addClass('disabled').attr('disabled');
            var sErr = invalidForm();
            if (sErr) {
                alert(sErr); // obviously do something better than this
                $('#btnPurchase').removeClass('disabled').removeAttr('disabled');
                return false;
            } // else...
            $('#token').val(paymentMethod.nonce);
            $('FORM#checkout').submit();
            return true;
        }
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM