Skip to content

Instantly share code, notes, and snippets.

@jasny
Created April 23, 2020 02:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasny/19ce2830a6dd70b425e13201b86a7bdd to your computer and use it in GitHub Desktop.
Save jasny/19ce2830a6dd70b425e13201b86a7bdd to your computer and use it in GitHub Desktop.
Stripe example PHP code
<?php
require 'vendor/autoload.php';
// This is a sample test API key. Sign in to see examples pre-filled with your key.
\Stripe\Stripe::setApiKey('sk_test_4eC39HqLyjWDarjtT1zdp7dc');
# retrieve JSON from POST body
header('Content-Type: application/json');
$json_str = file_get_contents('php://input');
$json_obj = json_decode($json_str);
function calculateOrderAmount($items) {
// Replace this constant with a calculation of the order's amount
// Calculate the order total on the server to prevent
// customers from directly manipulating the amount on the client
return 1400;
}
$paymentIntent = \Stripe\PaymentIntent::create([
'amount' => calculateOrderAmount($json_obj->items),
'currency' => 'usd',
]);
$output = [
'clientSecret' => $paymentIntent->client_secret,
];
echo json_encode($output);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment