我试图在我的Laravel项目5.6中实现Stripe.js,但是当我签出时,我看不到条纹仪表板中注册的项目的价值。我不知道我可能有什么错误,请你帮忙。
代码PHP:
public function postCheckout(Request $request) {
if(!Session::has('cart')) {
return redirect()->route('shop.shoopingCart');
}
$oldCart = Session::get('cart');
$cart = new Cart($oldCart);
Stripe::setApiKey('pk_test_H7JdFZrdbUPYredN7cIG3woA');
try {
$charge = Charge::create(array(
"amount" => $cart->totalPrice * 100,
"currency" => "usd",
"source" => $request->input('stripeToken'), // obtained with Stripe.js
"description" => "Test Charge"
));
$order = new Order();
$order->cart = serialize($cart);
$order->address = $request->input('address');
$order->name = $request->input('name');
$order->payment_id = $charge->id;
Auth::user()->orders()->save($order);
}
catch(\Exception $e) {
return redirect()->route('checkout')->with('error', $e->getMessage());
}
Session::forget('cart');
return redirect()->route('product.index')->with('success', 'Successfully purchased products!');
}
代码JavaScript:
Stripe.setPublishableKey('pk_test_H7JdFZrdbUPYredN7cIG3woA');
var $form = $('#checkout-form');
$form.submit(function(event){
$('#charge-error').addClass('hidden');
$form.find('button').prop('disabled', true);
Stripe.card.createdToken({
number: $('#card-number').val(),
cvc: $('#card-cvc').val(),
exp_month: $('#card-exp-month').val(),
exp_year: $('#card-exp-year').val(),
name: $('#card-name').val(),
}, stripeResponseHandler);
return false;
});
function stripeResponseHandler(status, response) {
if(response.error) {
$('#charge-error').removeClass('hidden');
$('#charge-error').text(response.error.message);
$form.find('button').prop('disabled', false);
}
else {
var token = response.id;
$form.append($('<input type="hidden"name="stripeToken"/>').val(token));
$form.get(0).submit();
}
}
答案 0 :(得分:0)
尝试一下因为setTimeout对我来说很好。
setTimeout(function(){
// Request a token from Stripe:
Stripe.card.createdToken({
number: $('#card-number').val(),
cvc: $('#card-cvc').val(),
exp_month: $('#card-exp-month').val(),
exp_year: $('#card-exp-year').val(),
name: $('#card-name').val(),
}, stripeResponseHandler);
}, 500);