2

需要在我的 Laravel 应用程序中集成 laravel/cashier-braintree。几乎没有使用 Braintree 集成的经验,很难让它发挥作用。有人可以逐步解释它是如何工作的,或者一些对此有好处的教程吗?试图遵循 Stripe 的集成,因为它们相似但没有成功。

这就是我的代码现在的样子

订阅控制器.php

public function createToken()
    {
        $clientToken = \Braintree_ClientToken::generate(array('customerId' => ""));
        return $clientToken;
    }

    public function checkout()
    {
        $creditCardToken = $this->createToken();
        $data = [
            'paymentMethodNonce' => $creditCardToken,
            'creditCard' => [
                'number' => Input::get('number'),
                'expirationDate' => Input::get('expiration_date'),
                'cvv' => Input::get('cvv')
            ],
        ];
        $this->user->newSubscription(Input::get('plan'), Input::get('plan'))->create($creditCardToken, $data);

        if ($this->user->subscribed('Small')) {
            return 'Done!';
        }

        var_dump($this->user);
    }

所以,我得到

Unable to create Braintree customer: Unknown payment_method_nonce.
Expiration date is required.
Credit card number is required.
Credit card must include number, payment_method_nonce, or venmo_sdk_payment_method_code.

如果我输入我的表格name="",我会得到 Unable to create Braintree customer: Unknown payment_method_nonce. 所以,它的名字可以通过,但是 payment_method_nonce 有问题。无法理解为什么付款不能与 data-braintree-name 一起使用,因为出于安全考虑,仅 name 不是选项。如果有人知道我为什么会收到未知的 payment_method_nonce?

4

1 回答 1

2

解决方案:

订阅.blade.php

{!! Form::open(['route' => 'subscription', 'method' => 'post', 'id' => 'checkout'])!!}

<h5>Choose:</h5>

<select name="plan" id="plan" class="form-control">
    <option value="sm">Small</option>
    <option value="lg">Large</option>
</select>

<div class="form-group">
    <h5>Card number:</h5>

    <div id="number" class="form-control"></div>

</div>

<div class="row">
    <div class="col-md-6 col-sm-6">
        <div class="form-group">
            <h5>Date:</h5>

            <div id="expiration-date" class="form-control"></div>

        </div>
    </div>

    <div class="col-md-6 col-sm-6">
        <div class="form-group">

            <div id="cvv" class="form-control"></div>

        </div>
    </div>
</div>

{!! Form::close() !!}

订阅控制器.php

public function join()
    {
        $data = [
            'paymentMethodNonce' => Input::get('payment_method_nonce'),
        ];
        $this->user->newSubscription('main', Input::get('plan'))->create(Input::get('payment_method_nonce'), $data);

        return redirect('/');
    }

脚本

<script src="https://js.braintreegateway.com/v2/braintree.js"></script>
<script>
    var colorTransition = 'color 100ms ease-out';

    braintree.setup("@braintreeClientToken", "custom", {
        id: "checkout",
        hostedFields: {

            number: {
                selector: "#number"
            },

            expirationDate: {
                selector: "#expiration-date"
            },

            cvv: {
                selector: "#cvv"
            }
        }
    });
</script>
于 2016-05-25T12:54:16.590 回答