我正在尝试使用Laravel,Laravel Cashier和Braintree创建订阅服务。我收到以下错误:
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.
我在HTML中完成了以下操作:
<form class="form-horizontal" role="form" method="POST" action="{{ route('register') }}">
<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="https://js.braintreegateway.com/js/braintree-2.31.0.min.js"></script>
<script>
braintree.setup('{{ $braintreeToken }}', 'dropin', {
container: 'dropin-container'
});
</script>
</form>
然后我有以下RegisterController.php
。最重要的一点是create
方法:
<?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;
}
}
当我输入以下信用卡详细信息(这些应该是沙盒中使用的测试信用卡号)时,会发生错误:
Credit card number: 4111 1111 1111 1111
Expiration date: 08/2018
CVV: 123
我试过谷歌搜索错误,但没有任何有用的东西出现。
答案 0 :(得分:0)
在我需要使用_token
时,问题是我使用了帖子数据中的payment_method_nonce
。