32.-Imprimir nuestra factura en PDF en PHP
En este tutorial vamos a imprimir nuestra factura en PDF en PHP siguiendo estos pasos:
- 1.- Analizaremos el metodo de seguridad que nos entrega stripe para evitar compras repetidas.
- 2.- Crear un nuevo archivo con su propia conexion a la bd.
Codigo: https://github.com/programadornovato/ecommerce/commit/50bc92e2767acc1d15357685d814c194154946bc
factura.php
<?php
$total=$_REQUEST['total']??'';
include_once "stripe/init.php";
\Stripe\Stripe::setApiKey("sk_test_JaTXHOFLk3lllnj1PnpahfxR00NLGmUe8M");
$toke=$_POST['stripeToken'];
$charge=\Stripe\Charge::create([
'amount'=>$total,
'currency'=>'usd',
'description'=>'Pago de ecommerce',
'source'=>$toke
]);
if($charge['captured']){
$queryVenta="INSERT INTO ventas
(idCli ,idPago ,fecha) values
('".$_SESSION['idCliente']."','".$charge['id']."',now());
";
$resVenta=mysqli_query($con,$queryVenta);
$id=mysqli_insert_id($con);
/*
if($resVenta){
echo "La venta fue exitosa con el id=".$id;
}
*/
$insertaDetalle="";
$cantProd=count($_REQUEST['id']);
for($i=0;$i<$cantProd;$i++){
$subTotal=$_REQUEST['precio'][$i]*$_REQUEST['cantidad'][$i];
$insertaDetalle=$insertaDetalle."('".$_REQUEST['id'][$i]."','$id','".$_REQUEST['cantidad'][$i]."','".$_REQUEST['precio'][$i]."','$subTotal'),";
}
$insertaDetalle=rtrim($insertaDetalle,",");
$queryDetalle="INSERT INTO detalleVentas
(idProd, idVenta, cantidad, precio, subTotal) values
$insertaDetalle;";
$resDetalle=mysqli_query($con,$queryDetalle);
if($resVenta && $resDetalle){
?>
<div class="row">
<div class="col-6">
<?php muestraRecibe($id); ?>
</div>
<div class="col-6">
<?php muestraDetalle($id); ?>
</div>
</div>
<?php
borrarCarrito();
}
}
function borrarCarrito(){
?>
<script>
$.ajax({
type: "post",
url: "ajax/borrarCarrito.php",
dataType: "json",
success: function (response) {
$("#badgeProducto").text("");
$("#listaCarrito").text("");
}
});
</script>
<?php
}
function muestraRecibe($idVenta){
?>
<table class="table">
<thead>
<tr>
<th colspan="3" class="text-center">Persona que recibe</th>
</tr>
<tr>
<th>Nombre</th>
<th>Email</th>
<th>Direccion</th>
</tr>
</thead>
<tbody>
<?php
global $con;
$queryRecibe="SELECT nombre,email,direccion
from recibe
where idCli='".$_SESSION['idCliente']."';";
$resRecibe=mysqli_query($con,$queryRecibe);
$row=mysqli_fetch_assoc($resRecibe);
?>
<tr>
<td><?php echo $row['nombre'] ?></td>
<td><?php echo $row['email'] ?></td>
<td><?php echo $row['direccion'] ?></td>
</tr>
</tbody>
</table>
<?php
}
function muestraDetalle($idVenta){
?>
<table class="table">
<thead>
<tr>
<th colspan="3" class="text-center">Detalle de venta</th>
</tr>
<tr>
<th>Nombre</th>
<th>Cantidad</th>
<th>Precio</th>
<th>SubTotal</th>
</tr>
</thead>
<tbody>
<?php
global $con;
$queryDetalle="SELECT
p.nombre,
dv.cantidad,
dv.precio,
dv.subTotal
FROM
ventas AS v
INNER JOIN detalleVentas AS dv ON dv.idVenta = v.id
INNER JOIN productos AS p ON p.id = dv.idProd
WHERE
v.id = '$idVenta'";
$resDetalle=mysqli_query($con,$queryDetalle);
$total=0;
while($row=mysqli_fetch_assoc($resDetalle)){
$total=$total+$row['subTotal'];
?>
<tr>
<td><?php echo $row['nombre'] ?></td>
<td><?php echo $row['cantidad'] ?></td>
<td><?php echo $row['precio'] ?></td>
<td><?php echo $row['subTotal'] ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="3" class="text-right">Total:</td>
<td><?php echo $total; ?></td>
</tr>
</tbody>
</table>
<a class="btn btn-secondary float-right" target="_blank" href="imprimirFactura.php?idVenta=<?php echo $idVenta; ?>" role="button">Imprimir factura <i class="fas fa-file-pdf"></i> </a>
<?php
}
?>
ecommerce/imprimirFactura.php
<?php
session_start();
include_once "admin/db_ecommerce.php";
$con = mysqli_connect($host, $user, $pass, $db);
$queryRecibe="SELECT nombre,email,direccion
from recibe
where idCli='".$_SESSION['idCliente']."';";
$resRecibe=mysqli_query($con,$queryRecibe);
$rowRecibe=mysqli_fetch_assoc($resRecibe);
$queryCli="SELECT nombre,email,direccion
from clientes
where id='".$_SESSION['idCliente']."';";
$resCli=mysqli_query($con,$queryCli);
$rowCli=mysqli_fetch_assoc($resCli);
$idVenta= mysqli_real_escape_string($con,$_REQUEST['idVenta']??'');
$queryVenta="SELECT v.id,v.fecha
FROM ventas AS v
WHERE v.id = '$idVenta';";
$resVenta=mysqli_query($con,$queryVenta);
$rowVenta=mysqli_fetch_assoc($resVenta);
?>
<div>
<img src="admin/images/pn.png" style="width: 30px;" >
My eccomerce
</div>
<table style="width: 750px;margin-top: 20px;">
<thead>
<tr>
<th>Cliente</th>
<th>Recibe</th>
<th>Datos de la factura</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<strong>Nombre:</strong><?php echo $rowCli['nombre'] ?><br>
<strong>Email:</strong><?php echo $rowCli['email'] ?><br>
<strong>Direccion:</strong><?php echo $rowCli['direccion'] ?><br>
</td>
<td>
<strong>Nombre:</strong><?php echo $rowRecibe['nombre'] ?><br>
<strong>Email:</strong><?php echo $rowRecibe['email'] ?><br>
<strong>Direccion:</strong><?php echo $rowRecibe['direccion'] ?><br>
</td>
<td>
<strong>Nombre:</strong><?php echo $rowVenta['id'] ?><br>
<strong>Email:</strong><?php echo $rowVenta['fecha'] ?><br>
</td>
</tr>
</tbody>
</table>
Navicat: https://www.navicat.com
Cuenta de github de stripe: https://github.com/stripe/stripe-php
Documentacion de stripe: https://stripe.com/docs/stripe-js
Datos de tarjeta de prueba: https://developers.todopago.com.ar/site/datos-de-prueba
🎦[Curso] Bootstrap de 0 a 100 🌈: https://www.youtube.com/playlist?list=PLCTD_CpMeEKSVmsZJIumVvfDviuW-c9AT
🎦Curso de PHP🐘 y MySql🐬: https://www.youtube.com/playlist?list=PLCTD_CpMeEKS2Dvb-WNrAuDAXObB8GzJ0
🎦Chart.js Tutorial [En español con código de ejemplo] 📊: https://youtu.be/_vRS87AT1Yk
🎦Curso de PHP🐘 y MySql🐬: https://www.youtube.com/playlist?list=PLCTD_CpMeEKS2Dvb-WNrAuDAXObB8GzJ0
🎦[Curso] Laravel Tutorial en Español: https://www.youtube.com/playlist?list=PLCTD_CpMeEKQcVcM4u4qddLYRE37S_0XS
🎦Curso]Ajax con Jquery de 0 a 100 🌇: https://www.youtube.com/watch?v=52yI0xiB73A&list=PLCTD_CpMeEKSYJ1J15M8PknOMwOpeqsXz
🎦Mysql configurar una replicación maestro – esclavo 🐬: https://www.youtube.com/watch?v=RY-EdBOJWEs
🎦[Curso] Visual Studio Code 🆚 de 0 a 100: https://www.youtube.com/playlist?list=PLCTD_CpMeEKQbdlT8efsS-veXuvYZ1UWn
🎦[Curso] Bootstrap de 0 a 100 🌈: https://www.youtube.com/playlist?list=PLCTD_CpMeEKSVmsZJIumVvfDviuW-c9AT
🎦[Curso] HTML y CSS de 0 a 100 🌐: https://www.youtube.com/playlist?list=PLCTD_CpMeEKS1SmufBGPOV1mjNfCiLwek
🎦 Esta lista de reproducción: https://www.youtube.com/playlist?list=PLCTD_CpMeEKQhRiJx7Wv3pM3rYvT9_CS9 .
Codigos en gdrive: https://drive.google.com/file/d/1QW8ExkL8eS7nQ5HTDvUuSkkGJMSmecGV/view?usp=sharing
Gracias por apoyar este canal: https://www.patreon.com/programadornovato?fan_landing=true
🔗 Facebook: https://facebook.com/ProgramadorNovatoOficial
🔗 Twitter: https://twitter.com/programadornova
🔗 Linkedin: https://www.linkedin.com/in/programadornovato/
🔗 Instagram: https://www.instagram.com/programadornovato/
<<Anterior tutorial Siguiente tutorial >>