Created
April 23, 2020 02:34
-
-
Save jasny/19ce2830a6dd70b425e13201b86a7bdd to your computer and use it in GitHub Desktop.
Stripe example PHP code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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