Skip to content

Instantly share code, notes, and snippets.

@jasny
Created Apr 23, 2020
Embed
What would you like to do?
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