-
Notifications
You must be signed in to change notification settings - Fork 0
/
stripe.php
74 lines (61 loc) · 2.25 KB
/
stripe.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
require_once 'vendor/autoload.php';
require_once 'secrets.php';
// require_once 'cred.js';
$stripe = new \Stripe\StripeClient($stripeSecretKey);
function calculateOrderAmount(array $items): int
{
$totalAmount = 0;
foreach ($items as $item) {
// Assuming each item has a 'price' property
$totalAmount += $item->price;
}
// Convert total amount to cents (smallest currency unit)
$totalAmountCents = round($totalAmount * 100);
// You may apply additional logic here, such as taxes or discounts
return $totalAmountCents;
}
// Check if the request method is POST
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Check if JSON data is present in the request body
$jsonStr = file_get_contents('php://input');
if (!empty($jsonStr)) {
// Decode the JSON data
$jsonData = json_decode($jsonStr);
// Check if JSON decoding was successful
if ($jsonData === null && json_last_error() !== JSON_ERROR_NONE) {
http_response_code(400);
echo json_encode(['error' => 'Error decoding JSON data']);
exit;
}
// Ensure that $jsonData is not null before accessing its properties
if (!isset($jsonData->items)) {
http_response_code(400);
echo json_encode(['error' => 'JSON data does not contain \'items\' property']);
exit;
}
try {
// Create a PaymentIntent with amount and currency
$paymentIntent = $stripe->paymentIntents->create([
'amount' => calculateOrderAmount($jsonData->items),
'currency' => 'usd',
'payment_method_types' => ['card'],
]);
$output = [
'clientSecret' => $paymentIntent->client_secret,
];
echo json_encode($output);
} catch (Exception $e) {
http_response_code(400); // or appropriate HTTP status code
echo json_encode(['error' => $e->getMessage()]);
}
} else {
http_response_code(400);
echo json_encode(['error' => 'No JSON data received']);
exit;
}
} else {
http_response_code(405); // Method Not Allowed
echo json_encode(['error' => 'Method Not Allowed']);
exit;
}