-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckout.php
More file actions
50 lines (41 loc) · 1.34 KB
/
checkout.php
File metadata and controls
50 lines (41 loc) · 1.34 KB
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
<?php
session_start();
include('config.php');
// Verifica se está autenticado como cliente
$id_sessao = $_SESSION['id_sessao'] ?? null;
$tipo_utilizador = $id_sessao ? verificarTipoUsuario($id_sessao) : 'visitante';
if (!$id_sessao || $tipo_utilizador !== 'cliente') {
header("Location: login.php");
exit;
}
$id_cliente = $id_sessao;
$itens = buscarItensCarrinho($id_cliente);
$total = 0;
// Calcula o total da encomenda
foreach ($itens as $item) {
$total += $item['preco'] * $item['quantidade'];
}
$total = number_format($total, 2, '.', '');
// Cria a encomenda
$id_encomenda = criarEncomenda($id_cliente, $total);
foreach ($itens as $item) {
adicionarProdutoEncomenda(
$id_encomenda,
$item['id_produto'],
$item['quantidade'],
$item['preco']
);
}
// Redireciona para o PayPal Sandbox
$query = http_build_query([
'cmd' => '_xclick',
'business' => '[email protected]',
'item_name' => 'Encomenda LuxFragrance',
'amount' => $total,
'currency_code' => 'EUR',
'return' => 'http://localhost/MiguelCarvalho2425/sucesso.php?id_encomenda=' . $id_encomenda,
'cancel_return' => 'http://localhost/MiguelCarvalho2425/carrinho.php',
'notify_url' => 'http://localhost/MiguelCarvalho2425/ipn.php'
]);
header("Location: https://www.sandbox.paypal.com/cgi-bin/webscr?" . $query);
exit;