Ejemplo n.º 1
0
 public function store()
 {
     if (empty($_SESSION)) {
         session_start();
     }
     empty($_SESSION['old']) ?: ($_SESSION['old'] = []);
     empty($_SESSION['error']) ?: ($_SESSION['error'] = []);
     $rules = ['email' => FILTER_VALIDATE_EMAIL, 'number' => ['filter' => FILTER_CALLBACK, 'options' => function ($nb) {
         if (preg_match('/[0-9]{16}/', $nb)) {
             return (int) $nb;
         }
         return false;
     }], 'address' => FILTER_SANITIZE_STRING];
     $sanitize = filter_input_array(INPUT_POST, $rules);
     var_dump($sanitize);
     $error = false;
     if (!$sanitize['email']) {
         $error = true;
         $_SESSION['error']['email'] = "Email Invalid";
     }
     if (!$sanitize['number']) {
         $error = true;
         $_SESSION['error']['number'] = "Blue Card number Invalid";
     }
     if (!$sanitize['address']) {
         $error = true;
         $_SESSION['error']['address'] = "You must give your address";
     }
     if ($error) {
         $_SESSION['old']['email'] = $sanitize['email'];
         $_SESSION['old']['address'] = $sanitize['address'];
         $this->redirect(url('cart'));
     }
     try {
         \Connect::$pdo->beginTransaction();
         $history = new History();
         $customer = new Customer();
         $customer->create(['email' => $sanitize['email'], 'number' => $sanitize['number'], 'addess' => $sanitize['address']]);
         $customerId = \Connect::$pdo->LastInsertID;
         $storage = $this->cart->all();
         $products = [];
         foreach ($storage as $id => $total) {
             $p = new Product();
             $stmt = $p->find($id);
             $history->create(['product_id' => $id, 'price' => (double) $stmt->price, 'total' => $total, 'quantity' => $total / $stmt->price, 'commandet_at' => date('Y-m-d h:i:s')]);
             $this->cart->reset();
             $this->redirect(url());
         }
         \Connect::$pdo->commit();
     } catch (\PDOException $e) {
         \Connect::$pdo->rollback();
     }
 }
Ejemplo n.º 2
0
 public function finalize($cart)
 {
     if (!empty($_POST)) {
         $rules = ['email' => FILTER_VALIDATE_EMAIL, 'address' => FILTER_SANITIZE_STRING];
         //id customer
         //var_dump(\Connect::$pdo->lastInsertId());
         $sanitize = filter_input_array(INPUT_POST, $rules);
         $error = false;
         if (!$sanitize['email']) {
             $_SESSION['error']['email'] = 'Votre email n\'est pas valide';
             $error = true;
         }
         if (preg_match('/[0-9]{16}/', $_POST['number'], $m) !== 1 || strlen($_POST['number']) >= 17) {
             echo 'lol';
             $_SESSION['error']['number'] = 'Votre numéro de carte bancaire n\'est pas valide';
             $error = true;
         }
         if (!$sanitize['address']) {
             $_SESSION['error']['address'] = 'Votre adresse mail n\'est pas valide';
             $error = true;
         }
         if ($error) {
             header('Location: /cart');
             exit;
         }
         $history = new History();
         $carts = new Cart();
         foreach ($_SESSION['Star Wars'] as $name => $price) {
             $products[] = $carts->ShowCart($name);
         }
         foreach ($products as $product) {
             $data = ['name' => $product->title, 'product_id' => $product->id, 'numbercard' => $_POST['number'], 'address' => $_POST['address'], 'email' => $_POST['email'], 'price' => $product->price, 'total' => $_SESSION['Star Wars'][$product->title], 'quantity' => $_SESSION['Star Wars'][$product->title] / $product->price, 'date' => date("Y-m-d H:i:s")];
             $history->create($data);
         }
         $_SESSION['message'] = 'Votre commande est terminé, veuillez checkez vos mails !';
         $cart->reset();
         header('Location: /');
     }
 }
Ejemplo n.º 3
0
 public function store()
 {
     $this->validToken('_token');
     if (empty($_SESSION)) {
         session_start();
     }
     empty($_SESSION['old']) ?: ($_SESSION['old'] = []);
     empty($_SESSION['error']) ?: ($_SESSION['error'] = []);
     $rules = ['email' => FILTER_VALIDATE_EMAIL, 'number' => ['filter' => FILTER_CALLBACK, 'options' => function ($nb) {
         if (iconv_strlen($nb) == 16 && (int) $nb != 0) {
             return (int) $nb;
         }
         return false;
     }], 'address' => FILTER_SANITIZE_STRING];
     $sanitize = filter_input_array(INPUT_POST, $rules);
     $error = false;
     $_SESSION['old']['email'] = $sanitize['email'];
     $_SESSION['old']['address'] = $sanitize['address'];
     if (!$sanitize['email']) {
         $_SESSION['error']['email'] = 'your email is invalid';
         $error = true;
     }
     if (!$sanitize['number']) {
         $_SESSION['error']['number'] = 'your number blue card is invalid';
         $error = true;
     }
     if (!$sanitize['address']) {
         $_SESSION['error']['address'] = 'you must given your address';
         $error = true;
     }
     if ($error) {
         $_SESSION['flashMessage'] = 'there was a problem';
         $this->redirect(url('cart'));
     }
     try {
         \Connect::$pdo->beginTransaction();
         $history = new History();
         $customer = new Customer();
         if ($c = $customer->where('number_card', '=', (string) $sanitize['number'])->get()->fetch()) {
             $customer->update($c->id, ['number_command' => 'number_command+1']);
             $customerId = $c->id;
         } else {
             $customer->create(['email' => $sanitize['email'], 'number_card' => $sanitize['number'], 'address' => $sanitize['address'], 'number_command' => 1]);
             $customerId = \Connect::$pdo->lastInsertId();
         }
         $products = $this->storage();
         foreach ($products as $name => $p) {
             $p['commanded_at'] = date('Y-m-d h:i:s');
             $p['customer_id'] = $customerId;
             $history->create($p);
         }
         \Connect::$pdo->commit();
         $_SESSION['flashMessage'] = 'thank you for your purchase, the team of Star Wars';
         $this->cart->reset();
         $this->redirect(url());
     } catch (\PDOException $e) {
         \Connect::$pdo->rollBack();
         $_SESSION['flashMessage'] = 'there has been a problem for your order, so sorry';
         $this->redirect(url('cart'));
     }
 }
Ejemplo n.º 4
0
 public function store()
 {
     if (!checked_token($_POST['_token'])) {
         $this->redirect(url('cart'));
     }
     //if(empty($_SESSION)) session_start();
     if (!empty($_SESSION['old'])) {
         $_SESSION['old'] = [];
     }
     if (!empty($_SESSION['error'])) {
         $_SESSION['error'] = [];
     }
     $rules = ['email' => FILTER_VALIDATE_EMAIL, 'number' => ['filter' => FILTER_CALLBACK, 'options' => function ($nb) {
         if (preg_match('/[0-9]{16}/', $nb)) {
             return $nb;
         }
         return false;
     }], 'adresse' => FILTER_SANITIZE_STRING];
     $sanitize = filter_input_array(INPUT_POST, $rules);
     //var_dump($sanitize);
     $error = false;
     if (!$sanitize['email']) {
         $error = true;
         $_SESSION['error']['email'] = 'your email is invalid';
     }
     if (!$sanitize['number']) {
         $error = true;
         $_SESSION['error']['number'] = 'your blue card number is invalid';
     }
     if (!empty($sanitize['adresse'])) {
         $error = true;
         $_SESSION['error']['adresse'] = 'you must give your address';
     }
     if ($error) {
         $_SESSION['old']['email'] = $sanitize['email'];
         $_SESSION['old']['adresse'] = $sanitize['adresse'];
         $this->redirect(url('cart'));
     }
     //transactionnelle PDO
     try {
         \Connect::$pdo->beginTransaction();
         $history = new History();
         $customer = new Customer();
         $customer->create(['email' => $sanitize['email'], 'number' => $sanitize['number'], 'adresse' => $sanitize['adresse']]);
         $customer_id = \Connect::$pdo->lastInsertId();
         $storage = $this->cart->all();
         foreach ($storage as $id => $total) {
             $p = new Product();
             // product du Model pas du Cart
             $stmt = $p->find($id);
             $history->create(['product_id' => $id, 'customer_id' => $customer_id, 'price' => (double) $stmt->price, 'total' => $total, 'quantity' => $total / $stmt->price, 'commanded_at' => date('Y-m-d h:i:s')]);
         }
         \Connect::$pdo->commit();
         $this->cart->reset();
         $this->redirect(url());
     } catch (\PDOException $e) {
         \Connect::$pdo->rollBack();
     }
 }