Skip to content
This repository was archived by the owner on Jul 20, 2026. It is now read-only.

Commit cc782d1

Browse files
committed
payment saving
1 parent 666767b commit cc782d1

6 files changed

Lines changed: 171 additions & 25 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
4+
require __DIR__ .'/../models/Cart.php';
5+
require __DIR__ .'/../models/Payment.php';
6+
7+
class CheckoutController {
8+
private $cartModel;
9+
private $paymentModel;
10+
11+
public function __construct(PDO $dbConnection) {
12+
$this->cartModel = new Cart();
13+
$this->paymentModel = new Payment($dbConnection);
14+
}
15+
16+
public function viewCheckout() {
17+
$total = $this->cartModel->computeTotal();
18+
require_once __DIR__ . "/../views/checkout.php";
19+
}
20+
21+
public function saveOrder() {
22+
$json = file_get_contents('php://input');
23+
$data = json_decode($json, true);
24+
25+
if (!$data) {
26+
$this->jsonResponse(false, "Données invalides.");
27+
}
28+
29+
$this->paymentModel->createPayment($data['simulate_status']);
30+
31+
// TODO: take into account all payment statuses and check in a cleaner way
32+
if($data['simulate_status'] == 3) {
33+
$this->jsonResponse(false, "Paiement refusé par la banque");
34+
} else {
35+
$this->jsonResponse(true, "Commande enregistrée");
36+
}
37+
}
38+
39+
private function jsonResponse($success, $message) {
40+
header('Content-Type: application/json');
41+
echo json_encode([
42+
'success' => $success,
43+
'message' => $message
44+
]);
45+
exit;
46+
}
47+
48+
}
49+
50+
?>

src/app/models/Payment.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
require_once __DIR__ . '/BaseModel.php';
4+
5+
/**
6+
* Product model that handles all database interactions related to payments.
7+
* This includes CRUD operations and any payment-specific queries.
8+
*/
9+
class Payment extends BaseModel {
10+
/**
11+
* Creates a payment record
12+
* @param int $status Whether the payment failed, succeeded, is pending or was refunded
13+
*/
14+
public function createPayment($status) {
15+
$query = "INSERT INTO payment (date, mode_id, status_id)
16+
VALUES (:date, :mode_id, :status_id)";
17+
18+
19+
$stmt = $this->conn->prepare($query);
20+
21+
$stmt->bindValue(":date", date("Y-m-d"), PDO::PARAM_STR);
22+
$stmt->bindValue(":mode_id", 2, PDO::PARAM_INT);
23+
$stmt->bindValue(":status_id", $status, PDO::PARAM_INT);
24+
25+
return $stmt->execute();
26+
}
27+
}
28+
?>

src/app/models/Product.php

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,7 @@
88
*/
99
class Product extends BaseModel {
1010

11-
/**
12-
* Creates a new product in the database.
13-
* @param int $id The unique identifier for the product.
14-
* @param string $name The name of the product.
15-
* @param string|null $description A description of the product.
16-
* @param float $price The price of the product.
17-
* @param int $supplier_id The ID of the supplier providing the product.
18-
* @return bool Returns true on successful creation, false otherwise.
19-
*/
20-
public function createProduct($id, $name, $description, $price, $supplier_id) {
21-
$query = "INSERT INTO product (id, name, description, price, supplier_id)
22-
VALUES (:id, :name, :description, :price, :supplier_id);";
23-
24-
$stmt = $this->conn->prepare($query);
25-
26-
$stmt->bindValue(":id", $id, PDO::PARAM_INT);
27-
$stmt->bindValue(":name", $name, PDO::PARAM_STR);
28-
$stmt->bindValue(":description", $description, PDO::PARAM_STR);
29-
$stmt->bindValue(":price", $price);
30-
$stmt->bindValue(":supplier_id", $supplier_id, PDO::PARAM_INT);
31-
32-
return $stmt->execute();
33-
}
34-
11+
/*
3512
/**
3613
* Retrieves all products from the database.
3714
* @param bool $showHidden Whether to include hidden products in the results. Defaults to true.

src/app/views/cart.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@
5050
</li>
5151
<?php endforeach; ?>
5252

53+
<a href="/checkout">Checkout</a>
5354
<!-- TODO: Display Menus -->
5455
<?php } ?>
5556

56-
<?php include 'partials/footer.php'; ?>
57+
<?php include 'partials/footer.php'; ?>

src/app/views/checkout.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php include 'partials/header.php'; ?>
2+
<h1>Simulation de paiement</h1>
3+
<h2>Total: <?= htmlspecialchars((string) $total) ?> €</h2>
4+
5+
<form id="payment-form">
6+
<!-- Nom Complet -->
7+
<label for="full-name">Nom Complet</label>
8+
<input type="text" id="full-name" name="customer_name"/>
9+
10+
<!-- Email -->
11+
<label for="email">Email</label>
12+
<input type="email" id="email" name="email"/>
13+
14+
<!-- Adresse -->
15+
<label for="address">Adresse de livraison</label>
16+
<input type="text" id="address" name="address"/>
17+
18+
<hr>
19+
20+
<!-- Numéro de carte -->
21+
<label for="card-number">Numéro de carte</label>
22+
<input type="text" id="card-number" name="card-number"/>
23+
24+
<!-- CVC (Corrigé : l'ID doit correspondre au FOR) -->
25+
<label for="cvc-number">CVC</label>
26+
<input type="text" id="cvc-number" name="cvc-number"/>
27+
28+
<!-- Date d'expiration (Corrigé : Ajout de l'ID manquant) -->
29+
<label for="expiration-date">Date d'expiration</label>
30+
<input type="date" id="expiration-date" name="expiration-date"/>
31+
32+
<!-- Boutons -->
33+
<button type="submit" onclick="this.form.dataset.status=2" name="pay-success">Simuler succès</button>
34+
<button type="submit" onclick="this.form.dataset.status=3" name="pay-failure">Simuler échec</button>
35+
</form>
36+
<h3 id="checkout-info"></h3>
37+
38+
<script>
39+
40+
fetch('/account-data')
41+
.then(response => {
42+
// Session has been lost
43+
if (response.status === 401) {
44+
window.location.href = '/sign-in';
45+
return;
46+
}
47+
48+
return response.json();
49+
})
50+
.then(data => {
51+
if (data && !data.error) {
52+
document.getElementById("full-name").placeholder =
53+
data.first_name +
54+
" " +
55+
data.last_name;
56+
57+
document.getElementById("email").placeholder = data.email
58+
}
59+
})
60+
61+
document.getElementById('payment-form').addEventListener('submit', function(e) {
62+
e.preventDefault();
63+
64+
const formData = new FormData(this);
65+
const data = Object.fromEntries(formData.entries());
66+
const checkoutInfo = document.getElementById("checkout-info");
67+
68+
data.simulate_status = this.dataset.status;
69+
fetch('/checkout/saveOrder', {
70+
method: 'POST',
71+
headers: { 'Content-Type': 'application/json' },
72+
body: JSON.stringify(data)
73+
})
74+
.then(response => response.json())
75+
.then(result => {
76+
checkoutInfo.innerText = result.message;
77+
});
78+
});
79+
80+
</script>

src/routes/web.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@
146146
'action' => 'gdpr',
147147
'access' => ['guest', 'client', 'admin']
148148
],
149+
'/checkout' => [
150+
'controller' => 'CheckoutController',
151+
'action' => 'viewCheckout',
152+
'access' => ['guest', 'client', 'admin']
153+
],
149154
],
150155
'POST' => [
151156
'/sign-in' => [
@@ -178,6 +183,11 @@
178183
'action' => 'cartAction',
179184
'access' => ['client']
180185
],
186+
'/checkout/saveOrder' => [
187+
'controller' => 'CheckoutController',
188+
'action' => 'saveOrder',
189+
'access' => ['client']
190+
],
181191
'/admin/accounts/update' => [
182192
'controller' => 'AdminController',
183193
'action' => 'updateAccountAdmin',

0 commit comments

Comments
 (0)