Здесь, когда пользователь нажимает кнопку оформления заказа, он отправляет запрос на публикацию на мой внутренний сервер (код ниже)
Код: Выделить всё
checkoutBtn.addEventListener('click', () => {
let itemsToBuy = []
const itemRows = document.querySelectorAll('.item-row')
itemRows.forEach(row => {
let obj = {
id: row.getAttribute('item-id'),
quantity: row.children[3].firstElementChild.value
}
itemsToBuy.push(obj)
})
fetch('http://localhost:8080/src/public/backend/myserver.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify(itemsToBuy)
}).then(res => res.json())
.then(data => {
stripe = Stripe('pk_test')
stripe.redirectToCheckout({ sessionId: data.id })
})
})
Код: Выделить всё
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$itemList = json_decode(trim(file_get_contents('php://input')));
$total = 0;
foreach ($itemList as $item) {
$id = $conn->real_escape_string(($item->id));
$quantity = $conn->real_escape_string(($item->quantity));
$result = $conn->query('select price from products where id =' . $id . ';');
if ($result->num_rows) {
$row = $result->fetch_assoc();
$total += $row['price'];
} else {
echo json_encode(['status' => 'no such product found']);
exit();
}
}
$stripe = new Stripe\StripeClient('sk_test');
$session = $stripe->checkout->sessions->create([
'success_url' => 'http://localhost:8080/src/public//html/success.html?sessionId={CHECKOUT_SESSION_ID}',
'cancel_url' => 'http://localhost:8080/src/public/html/cart.html?status=failure',
'payment_method_types' => ['card'],
'mode' => 'payment',
'line_items' => [
[
'quantity' => 1,
'price_data' => [
'currency' => 'gbp',
'unit_amount' => $total,
'product_data' => [
'name' => 'Serve2U',
'description' => 'Your Invoice for Serve2U'
]
]
]
]
]);
echo json_encode(['id' => $session->id]);
}
Несколько удачно. на данный момент камень преткновения и не знаю точно, куда идти дальше.
Подробнее здесь: https://stackoverflow.com/questions/783 ... l-checkout