Skip to content

Instantly share code, notes, and snippets.

@cjavilla-stripe
Created December 11, 2020 19:11
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save cjavilla-stripe/872a3509c902ec32a8cef82b39d8e0b8 to your computer and use it in GitHub Desktop.
Save cjavilla-stripe/872a3509c902ec32a8cef82b39d8e0b8 to your computer and use it in GitHub Desktop.
Simple one button Checkout with php
<?php
require_once('vendor/autoload.php');
\Stripe\Stripe::setApiKey('sk_test_...');
$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:4242/success',
'cancel_url' => 'http://example.com/cancel',
]);
?>
<html>
<head>
<title>Buy cool new product</title>
<script src="https://js.stripe.com/v3/"></script>
</head>
<body>
<button id="checkout-button">Checkout</button>
<script>
var stripe = Stripe('pk_test_vAZ3gh1LcuM7fW4rKNvqafgB00DR9RKOjN');
const btn = document.getElementById("checkout-button")
btn.addEventListener('click', function(e) {
e.preventDefault();
stripe.redirectToCheckout({
sessionId: "<?php echo $session->id; ?>"
});
});
</script>
</body>
</html>
@snez1990
Copy link

Nice example

@Jhkier
Copy link

Jhkier commented Nov 16, 2021

Created a Fatal 500 error for me...

@obi1arena
Copy link

Jhkier:

The key is http://localhost:4242/success. What platform and/or version are you running?

@obi1arena
Copy link

obi1arena commented Mar 11, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment