You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
4.5 KiB

4 years ago
<?php
session_start();
session_id(trim($_GET['sid']));
4 years ago
setlocale(LC_MONETARY, 'it_IT.UTF-8');
4 years ago
include_once './config.php';
include_once './database.php';
4 years ago
include_once './send_mail.php';
4 years ago
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
$databaseService = new DatabaseService();
$conn = $databaseService->getConnection();
4 years ago
4 years ago
$data = json_decode(file_get_contents("php://input"));
$shipping = array(
"full_name" => $data->profile->first_name." ".$data->profile->last_name,
4 years ago
"address" => $data->profile->address,
"city" => $data->profile->city,
"zip_code" => $data->profile->zip_code,
"province" => $data->profile->province
4 years ago
);
$query = "INSERT INTO `orders`
(`id`, `uid`, `date`, `items`, `total`, `status`, `token`, `shipping`, `traking`)
VALUES (NULL, ".intval($data->uid).", NOW(), '".json_encode($data->cart)."', ".floatval($data->total).",
4 years ago
'".($data->paid ? 'PAID' : 'CREATED')."',
4 years ago
'".trim($data->token)."',
'".json_encode($shipping)."', '')";
$stmt = $conn->prepare($query);
if($stmt->execute()) {
4 years ago
4 years ago
$order_id = $conn->lastInsertId();
4 years ago
$toEmail = $data->profile->email;
$toName = $data->profile->first_name." ".$data->profile->last_name;
4 years ago
$subject = 'Ordine n. '.$order_id;
4 years ago
$body = emailHeader();
4 years ago
$body .= "<tr><td colspan=\"4\" style=\"padding: 10px 20px\">Ciao ".$data->profile->first_name.",<br>
siamo contenti che tu abbia acquistato il nostro olio, speriamo sia protagonista di tanti momenti conviviali.<br><br>
4 years ago
Ecco i dati riassuntivi del tuo ordine:<br><br>
<span style=\"color: #92704F\">CODICE IDENTIFICATIVO ORDINE:</span><br>
<b>$order_id</b><br><br>
<span style=\"color: #92704F\">TEMPI DI CONSEGNA:</span><br>
4 years ago
La consegna avverrà in circa 5 giorni lavorativi<br><br>
4 years ago
<span style=\"color: #92704F\">INDIRIZZO DI CONSEGNA:</span><br>".
$data->profile->first_name." ".$data->profile->last_name."<br>".
$data->profile->address."<br>".
$data->profile->city." - ".
$data->profile->zip_code." - ".
4 years ago
$data->profile->city."<br>".
"Contatto telefonico: <br>".
4 years ago
$data->profile->phone."<br><br>
<span style=\"color: #92704F\">PRODOTTI ORDINATI:</span><br>
</td></tr>";
4 years ago
$body .= '<tr style="margin: 0; padding: 10px 20px">
4 years ago
<td style="font-weight: bold">Prodotto</td>
<td style="font-weight: bold">Prezzo</td>
<td style="font-weight: bold">Quantità</td>
<td style="font-weight: bold">Totale</td>
4 years ago
</tr>';
foreach($data->cart as $item) {
4 years ago
$qp = "SELECT * FROM products WHERE id=".$item->pid;
$stmtp = $conn->prepare($qp);
$stmtp->execute();
$p = $stmtp->fetch(PDO::FETCH_ASSOC);
4 years ago
$body .= '<tr style="margin: 0; padding: 10px 20px">
<td style="border-bottom: 1px solid #323232;"><b style="color: #92704F; font-style: italic">'.$p['name'].'</b><br>
<span style="font-size: 10px">'.$p['type'].'</span> </td>
4 years ago
<td style="border-bottom: 1px solid #323232; width: 90px">'.money_format('%.2n', $item->price).' </td>
<td style="border-bottom: 1px solid #323232; width: 90px">'.$item->qty.' </td>
<td style="border-bottom: 1px solid #323232; width: 90px">'.money_format('%.2n', $item->price * $item->qty).' </td>
4 years ago
</tr>';
}
4 years ago
$body .= "<tr><td colspan=\"4\" style=\"padding: 10px 20px\"><br>Ci auguriamo che non ci siano problemi con i prodotti acquistati. In caso contrario puoi contattarci in qualsiasi momento all'indirizzo ordini@iolovolio.com<br><br>
Grazie,<br>il team Iolovolio<br><br></td></tr>";
4 years ago
4 years ago
$body .= emailFooter();
$sent = sendEmail($toEmail, $toName, $subject, $body);
if($sent === true) {
http_response_code(200);
echo json_encode(
array(
"status" => 200,
"id" => $conn->lastInsertId(),
"mail" => $sent
));
} else {
http_response_code(400);
echo json_encode(
4 years ago
array(
4 years ago
"status" => 400,
"message" => $sent,
"query" => $query
4 years ago
));
4 years ago
}
4 years ago
} else {
http_response_code(400);
echo json_encode(
array(
"status" => 400,
4 years ago
"message" => "Error inserting new order",
"query" => $query
4 years ago
));
}
?>