<button id="checkout-button">Checkout</button>
<script src="https://js.stripe.com/v3/"></script>
<script src="{{ asset('js/app.js') }}"></script>
<script type="text/javascript">
// Create an instance of the Stripe object with your publishable API key
var stripe = Stripe('pk_test_51Hl77PKBzirhUGIcEQ0WvzMnWDLNHuT5dJ19DXvrATFr8SFcgzcjTAc1anvLqX8GRY36vnovlZYyR4LZZvnwkJrA00fzBPjbh2');
var checkoutButton = document.getElementById('checkout-button');
checkoutButton.addEventListener('click', function() {
// Create a new Checkout Session using the server-side endpoint you
// created in step 3.
axios.post('/create-checkout-session', {})
.then(function(response) {
return stripe.redirectToCheckout({ sessionId: response.data });
})
.then(function(result) {
// If `redirectToCheckout` fails due to a browser or network
// error, you should display the localized error message to your
// customer using `error.message`.
if (result.error) {
alert(result.error.message);
}
})
.catch(function(error) {
console.error('Error:', error);
});
});
</script>
Route::post('create-checkout-session', 'CheckoutController@checkoutSession')->name('checkout.session')->middleware('auth');
Route::get('success', 'CheckoutController@success')->middleware('auth');
Route::post('create-checkout-session', 'CheckoutController@checkoutSession')->name('checkout.session')->middleware('auth');
Route::get('success', 'CheckoutController@success')->middleware('auth');
public function checkoutSession()
{
\Stripe\Stripe::setApiKey(config('services.stripe.key'));
$session = \Stripe\Checkout\Session::create([
'payment_method_types' => ['card'],
'line_items' => [[
'price_data' => [
'currency' => 'usd',
'product_data' => [
'name' => 'T-shirt',
],
'unit_amount' => 2000,
],
'quantity' => 1,
]],
'mode' => 'payment',
'success_url' => 'http://localhost:8000/success?session_id={CHECKOUT_SESSION_ID}',
'cancel_url' => 'http://localhost:8000/success',
]);
return $session->id;
return response()->json(['id' => $session->id]);
//->withStatus(200)
}
public function success(Request $request)
{
$stripe = new \Stripe\StripeClient(
config('services.stripe.key')
);
$returndata = $stripe->checkout->sessions->retrieve(
$request->session_id,
[]
);
dd($returndata);
}
public function afterPayment()
{
echo 'Payment Has been Received';
}