在Braintree订阅laravel / cashier-braintree / Laravel 5.2

时间:2016-04-10 11:39:40

标签: php laravel-5.2 subscription braintree laravel-cashier

需要在我的Laravel应用程序中集成laravel / cashier-braintree。几乎没有Braintree集成的经验,它很难让它工作。有人可以分步说明它是如何工作的,或者是一些对此有益的教程?试图遵循St​​ripe的集成,因为它们相似但没有成功。

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

SubscriptionController.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一起使用,因为只是因为安全而不是选项。如果有人知道为什么我会得到未知的payment_method_nonce?

1 个答案:

答案 0 :(得分:2)

<强>解决方案:

<强> subscription.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() !!}

<强> SubscriptionsController.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>