我正在开发一个允许一次性付款的网络应用程序。为此,我正在使用 Laravel 和 Cashier。
我用典型的 Stripe 支付按钮展示了一些产品。单击它应该会显示 checkout.js 表单,但这不会发生。
产品视图基本上是一个显示 Stripe 按钮的循环:
<div class="row">
@foreach ($products as $product)
<form action="{{ route('frontend.user.pay', $product->id) }}" method="POST">
{{ csrf_field() }}
<div class="col-sm-5 col-md-5">
<div class="thumbnail">
<div class="caption">
<h3>{{ $product->name }}</h3>
<p>{{ $product->small_description }}</p>
<p>Buy for ${{ substr_replace($product->price, '.', 2, 0) }}</p>
<p>
<script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="{{ env('STRIPE_PUBLIC') }}"
data-amount="{{ $product->price }}"
data-name="Stripe.com"
data-description="Widget"
data-locale="auto"
data-currency="usd">
</script>
</p>
</div>
</div>
</div>
</form>
@endforeach
</div><!--row-->
当我单击该按钮时,Stripe pay 覆盖不会加载。
相关路线是
Route::get('product', 'ProductController@index')->name('product');
Route::post('pay/{product}', 'OrderController@payWithStripe')->name('pay');
控制器文件如下:
public function payWithStripe(Request $request, Product $product) {
$token = $request->input('_token');
return $this->chargeCustomer($product->id, $product->price, $product->name, $token);
}
public function chargeCustomer($product_id, $product_price, $product_name, $token) {
\Stripe\Stripe::setApiKey(env('STRIPE_SECRET'));
if (!$this->isStripeCustomer()) {
$customer = $this->createStripeCustomer($token);
}
else {
$customer = \Stripe\Customer::retrieve(access()->user()->stripe_id);
}
return $this->createStripeCharge($product_id, $product_price, $product_name, $customer);
}
public function createStripeCharge($product_id, $product_price, $product_name, $customer) {
try {
$charge = \Stripe\Charge::create(array(
"amount" => $product_price,
"currency" => "usd",
"customer" => $customer->id,
"description" => $product_name
));
} catch(\Stripe\Error\Card $e) {
return redirect()
->route('frontend.user.product')
->with('error', 'Your credit card was been declined. Please try again or contact us.');
}
return $this->postStoreOrder($product_id);
}
public function createStripeCustomer($token) {
\Stripe\Stripe::setApiKey(env('STRIPE_SECRET'));
$customer = \Stripe\Customer::create(array(
"description" => access()->user()->email,
"source" => $token
));
access()->user()->stripe_id = $customer->id;
access()->user()->save();
return $customer;
}
public function isStripeCustomer() {
return access()->user() && \App\Models\Access\User\User::where('id', access()->user()->id)->whereNotNull('stripe_id')->first();
}
问题是:
- 1) 不显示叠加支付(应输入VISA卡详细信息的地方)
- 2)在chargeCustomer函数中,客户不是在Stripe中生成的。我收到一个错误“没有这样的令牌:xxxxxxxx”。打印出来绝对是隐藏的令牌(从综合浏览量中检查)。问题可能与 _token 有关,我看到应该使用 stripeToken。但是 stripeToken 总是返回 null。