Braintree 中的订阅 laravel/cashier-braintree / Laravel 5.2
Subscriptions in Braintree with laravel/cashier-braintree / Laravel 5.2
需要在我的 Laravel 应用中集成 laravel/cashier-braintree。几乎没有 Braintree 集成的经验,真的很难让它工作。有人可以逐步解释它是如何工作的,或者一些对此有好处的教程吗?尝试遵循 Stripe 的集成,因为它们相似但没有成功。
这就是我的代码现在的样子
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?
解法:
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>
需要在我的 Laravel 应用中集成 laravel/cashier-braintree。几乎没有 Braintree 集成的经验,真的很难让它工作。有人可以逐步解释它是如何工作的,或者一些对此有好处的教程吗?尝试遵循 Stripe 的集成,因为它们相似但没有成功。
这就是我的代码现在的样子
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?
解法:
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>