-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpago.php
More file actions
373 lines (310 loc) · 15.5 KB
/
Copy pathpago.php
File metadata and controls
373 lines (310 loc) · 15.5 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<?php
require 'config/config.php';
require 'config/database.php';
require 'vendor/autoload.php';
MercadoPago\SDK::setAccessToken(TOKEN_MP);
$preference = new MercadoPago\Preference();
$productos_mp = array();
$db = new Database();
$con = $db->conectar();
$productos = isset($_SESSION['carrito']['productos']) ? $_SESSION['carrito']['productos'] : null;
$lista_carrito = array();
if($productos !=null){
foreach($productos as $clave => $cantidad){
$sql = $con->prepare("SELECT id, nombre, precio, descuento, $cantidad AS cantidad FROM productos WHERE id=? AND activo=1");
$sql->execute([$clave]);
$lista_carrito[] = $sql->fetch(PDO::FETCH_ASSOC);
}
} else{
header("Location: index.php");
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ecommerce</title>
<!-- CSS only -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-0evHe/X+R7YkIZDRvuzKMRqM+OrBnVFBL6DOitfPri4tjfHxaWutUpFmBp4vmVor" crossorigin="anonymous">
<link rel="stylesheet" href="css/style.css">
<script src="https://www.paypal.com/sdk/js?client-id=<?php echo CLIENT_ID; ?>&components=buttons"></script>
<script src="https://sdk.mercadopago.com/js/v2"></script>
</head>
<body>
<!--HEADER-->
<nav class="navbar navbar-expand-lg navbar-light bg-light position-fixed top-0 start-0 w-100">
<div class="container">
<a href="index.php" class="navbar-brand d-lg-none ">
E-commerce
</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarHeader">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse p-2 flex-column" id="navbarHeader">
<div class="d-flex justify-content-center justify-content-lg-between flex-column flex-lg-row w-100">
<form class="d-flex">
<input type="search" class="form-control me-2" placeholder="Search"/>
<button class="btn btn-outline-dark" type="submit">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-search-heart" viewBox="0 0 16 16"><path d="M6.5 4.482c1.664-1.673 5.825 1.254 0 5.018-5.825-3.764-1.664-6.69 0-5.018Z"/><path d="M13 6.5a6.471 6.471 0 0 1-1.258 3.844c.04.03.078.062.115.098l3.85 3.85a1 1 0 0 1-1.414 1.415l-3.85-3.85a1.007 1.007 0 0 1-.1-.115h.002A6.5 6.5 0 1 1 13 6.5ZM6.5 12a5.5 5.5 0 1 0 0-11 5.5 5.5 0 0 0 0 11Z"/></svg>
</button>
</form>
<a class="navbar-brand d-none d-lg-block" href="index.php">E-commerce</a>
<ul class="navbar-nav">
<li class="nav-item d-flex align-items-center">
<a class="nav-link mx-2" aria-current="page" href="index.php">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-person-heart" viewBox="0 0 16 16"><path d="M9 5a3 3 0 1 1-6 0 3 3 0 0 1 6 0Zm-9 8c0 1 1 1 1 1h10s1 0 1-1-1-4-6-4-6 3-6 4Zm13.5-8.09c1.387-1.425 4.855 1.07 0 4.277-4.854-3.207-1.387-5.702 0-4.276Z"/></svg>
My Account</a>
</li>
<li class="nav-item d-flex align-items-center">
<a class="nav-link mx-2" href="checkout.php">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-bag-heart" viewBox="0 0 16 16"><path fill-rule="evenodd" d="M10.5 3.5a2.5 2.5 0 0 0-5 0V4h5v-.5Zm1 0V4H15v10a2 2 0 0 1-2 2H3a2 2 0 0 1-2-2V4h3.5v-.5a3.5 3.5 0 1 1 7 0ZM14 14V5H2v9a1 1 0 0 0 1 1h10a1 1 0 0 0 1-1ZM8 7.993c1.664-1.711 5.825 1.283 0 5.132-5.825-3.85-1.664-6.843 0-5.132Z"/></svg>
Bag
</a>
<span id="num_cart" class="badge rounded-pill bg-secondary"><?php echo $num_cart; ?></span>
</li>
</ul>
</div>
<div class="d-block w-100">
<ul class="navbar-nav d-flex justify-content-center align-items-center pt-3">
<li class="nav-item mx-2">
<a class="nav-link" href="#">Muebles</a>
</li>
<li class="nav-item mx-2">
<a class="nav-link" href="#">Mesas</a>
</li>
<li class="nav-item mx-2">
<a class="nav-link" href="#">Sillas</a>
</li>
<li class="nav-item mx-2">
<a class="nav-link" href="#">Sillones</a>
</li>
<li class="nav-item mx-2">
<a class="nav-link" href="#">About</a>
</li>
<li class="nav-item mx-2">
<a class="nav-link" href="#">Contact</a>
</li>
</ul>
</div>
</div>
</div>
</nav>
<!--MAIN-->
<div class="container mt-5 text-white">
<div class="row">
<div class="col-6">
<h4>Detalles de pago</h4>
<div class="row">
<div class="col-12">
<div id="paypal-button-container"></div>
</div>
</div>
<div class="row">
<div class="col-12">
<div class="checkout-btn"></div>
</div>
</div>
</div>
<div class="col-6">
<div class="table-responsive">
<table class="table table-striped table-hover">
<thead>
<tr>
<th class="text-white">Producto</th>
<th class="text-white">Subtotal</th>
<th></th>
</tr>
</thead>
<tbody class="text-white">
<?php if($lista_carrito == null){
echo '<tr>
<td colspan="5" class="text-center">
<b class="text-white">Lista vacia</b>
</td>
</tr>';
} else {
$total = 0;
foreach($lista_carrito as $producto){
$_id = $producto['id'];
$nombre = $producto['nombre'];
$precio = $producto['precio'];
$descuento = $producto['descuento'];
$cantidad = $producto['cantidad'];
$precio_desc = $precio - (($precio * $descuento) / 100);
$subtotal = $cantidad * $precio_desc;
$total += $subtotal;
$item = new MercadoPago\Item();
$item->id = $_id;
$item->title = $nombre;
$item->quantity = $cantidad;
$item->unit_price = $precio_desc;
$item->currency_id = "AR";
array_push($productos_mp, $item);
unset($item);
?>
<tr">
<td class="text-white">
<?php echo $nombre; ?>
</td>
<td class="text-white">
<div id="subtotal_<?php echo $_id; ?>" name="subtotal[]"><?php echo MONEDA . number_format($subtotal,2,'.',','); ?></div>
</td>
</tr>
<?php } ?>
<tr class="text-white">
<td colspan="2">
<p class="h3 text-end text-white" id="total"><?php echo MONEDA . number_format($total, 2,'.',','); ?></p>
</td>
</tr>
</tbody>
<?php } ?>
</table>
</div>
</div>
</div>
</div>
<?php
$preference->items = $productos_mp;
$preference->back_urls = array(
"success" => "http://localhost/misproyectos/pasarela/captura.php",
"failure" => "http://localhost/misproyectos/pasarela/fallo.php"
);
$preference->auto_return = "approved";
$preference->binary_mode = true;
$preference->save();
?>
<section class="my-5 mx-auto py-5" style="max-width:25em;">
<h2>Subscribe To Our Newsletter</h2>
<form class="d-flex my-4">
<input type="search" class="form-control me-2" placeholder="Your e-mail"/>
<button class="btn btn-outline-dark" type="submit">Subscribe</button>
</form>
</section>
<footer class="d-flex justify-content-between my-5 text-start flex-wrap">
<ul class="nav flex-column">
<li class="fw-bold nav-item">
<a href="#" class="nav-link text-dark">Product</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link text-muted">Muebles</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link text-muted">Sillas</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link text-muted">Sillones</a>
</li>
</ul>
<ul class="nav flex-column">
<li class="fw-bold nav-item">
<a href="#" class="nav-link text-dark">Help</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link text-muted">Shopping guide</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link text-muted">Product Care</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link text-muted">Contact US</a>
</li>
</ul>
<ul class="nav flex-column">
<li class="fw-bold nav-item">
<a href="#" class="nav-link text-dark">Content</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link text-muted">About</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link text-muted">Blog</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link text-muted">Presroom</a>
</li>
</ul>
<ul class="nav flex-column">
<li class="fw-bold nav-item">
<a href="#" class="nav-link text-dark">Terms & Conditions</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link text-muted">Terms Of Use</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link text-muted">Privacy Policy</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link text-muted">Cookies Policy</a>
</li>
</ul>
</footer>
<!-- JavaScript Bundle with Popper -->
<script>
paypal.Buttons({
createOrder: function(data, actions) {
// Set up the transaction
return actions.order.create({
purchase_units: [{
amount: {
value: <?php echo $total; ?>
}
}]
});
},
style: {
layout: 'vertical',
color: 'blue',
shape: 'rect',
label: 'paypal'
},
onApprove: function(data, actions) {
actions.order.capture().then(function(detalles) {
console.log(detalles)
let url = 'clases/captura.php'
return fetch(url, {
method: 'post',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
detalles:detalles
})
}).then(function(response){
window.location.href = "completado.php?key=" + detalles['id'];
})
});
},
onCancel: function (data) {
// Show a cancel page, or return to cart
alert("Pago cancelado");
console.log(data);
},
onError: function (err) {
// For example, redirect to a specific error page
alert("Error al procesar el pago");
console.log(data);
}
}).render('#paypal-button-container');
</script>
<script>
// Agrega credenciales de SDK
const mp = new MercadoPago("TEST-31df5324-1bc7-45f3-8a5b-2c4e45b6bcb7", {
locale: "es-AR",
});
// Inicializa el checkout
mp.checkout({
preference: {
id: "<?php echo $preference->id; ?>",
},
render: {
container: ".checkout-btn", // Indica el nombre de la clase donde se mostrará el botón de pago
label: "Pagar con MercadoPago", // Cambia el texto del botón de pago (opcional)
},
});
</script>
</body>
</html>