samedi 29 avril 2017

Laravel cashier "Unable to create Braintree customer" error

I am trying to create a subscription services using Laravel, Laravel Cashier and Braintree. I get the following error:

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

I've done the following in my HTML:

<form class="form-horizontal" role="form" method="POST" action="">
    <select name="plan" id="plan" class="form-control">
        <option value="">Select plan</option>
        <option value="free">Free plan - €0/month</option>
        <option value="cool">Cool plan - €10/month</option>
        <option value="epic">Epic plan - €100/month</option>
    </select>

    <div id="dropin-container"></div>

    <input type="submit" class="btn btn-primary blue-button" value="Sign Up" style="margin-top: 6px;">

    <!-- Load the Client component. -->
    <script src="http://ift.tt/2pIgd2j"></script>

    <script>
        braintree.setup('', 'dropin', {
            container: 'dropin-container'
        });
    </script>
</form>

then I have the following RegisterController.php, the most important bit is in the create method:

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Auth\Events\Registered;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;

class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;

    /**
     * Where to redirect users after registration.
     *
     * @var string
     */
    protected $redirectTo = '/account';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    /**
     * Show the application registration form.
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function showRegistrationForm()
    {
        $braintreeToken = \Braintree\ClientToken::generate();

        return view('auth.register')
            ->with('braintreeToken', $braintreeToken)
            ->with('plan', 'none')
            ->with('route', 'register');
    }

    /**
     * Handle a registration request for the application.
     *
     * @param Request|\Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function register(Request $request)
    {
        $this->validator($request->all())->validate();

        event(new Registered($user = $this->create($request->all())));

        $this->guard()->login($user);

        return $this->registered($request, $user)
            ?: redirect($this->redirectPath());
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|string|max:255',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|string|min:6|confirmed',
            'plan' => 'required|in:free,cool,epic'
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        $limit = 200;
        $plan = 'free';

        $user = User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'plan' => $plan,
            'limit' => $limit,
            'password' => bcrypt($data['password']),
        ]);

        switch($data['plan'])
        {
            case 'cool':
                $limit = 3000;
                $plan = 'cool';
                $planID = 'gt8m';
                break;
            case 'epic':
                $limit = 32000;
                $plan = 'epic';
                $planID = '8v3g';
                break;
        }

        $subscription = $user->newSubscription('main', $planID)->create($data['_token']);

        if ($subscription)
        {
            $user->plan = $plan;
            $user->limit = $limit;
            $user->save();
        }

        return $user;
    }
}

The error happens when I input the following credit card details (these are supposed to be the test credit card numbers used in the sandbox):

Credit card number: 4111 1111 1111 1111
Expiration date: 08/2018
CVV: 123

I've tried googling the error but nothing useful came up.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2qpAZA4
via IFTTT

Aucun commentaire:

Enregistrer un commentaire