añadidas las direcciones de pedido

This commit is contained in:
2025-11-16 21:56:03 +01:00
parent 84a822db22
commit d19cd1923c
19 changed files with 1747 additions and 709 deletions

View File

@ -16,6 +16,8 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.imprimelibros.erp.common.Utils;
import com.imprimelibros.erp.datatables.DataTable;
import com.imprimelibros.erp.datatables.DataTablesParser;
@ -98,7 +100,7 @@ public class PaymentController {
Specification<PaymentTransaction> base = Specification.allOf(
(root, query, cb) -> cb.equal(root.get("status"), PaymentTransactionStatus.succeeded));
base = base.and((root, query, cb) -> cb.equal(root.get("type"), PaymentTransactionType.CAPTURE));
base = base.and((root, query, cb) -> cb.notEqual(root.join("payment").get("gateway"), "bank_transfer"));
String clientSearch = dt.getColumnSearch("client");
// 2) Si hay filtro, traducirlo a userIds y añadirlo al Specification
@ -229,10 +231,23 @@ public class PaymentController {
})
.add("transfer_id", pago -> {
if (pago.getPayment() != null) {
return "TRANSF-" + pago.getPayment().getOrderId();
} else {
return "";
String responsePayload = pago.getResponsePayload();
Long cartId = null;
if (responsePayload != null && !responsePayload.isBlank()) {
try {
JsonNode node = new ObjectMapper().readTree(responsePayload);
if (node.has("cartId")) {
cartId = node.get("cartId").asLong();
}
} catch (Exception e) {
cartId = null;
}
}
if (cartId != null) {
return "TRANSF-" + cartId;
}
}
return "";
})
.add("order_id", pago -> {
if (pago.getStatus() != PaymentTransactionStatus.pending) {

View File

@ -47,7 +47,7 @@ public class PaymentService {
* oficial (ApiMacSha256).
*/
@Transactional
public FormPayload createRedsysPayment(Long cartId, long amountCents, String currency, String method)
public FormPayload createRedsysPayment(Long cartId, Long dirFactId, Long amountCents, String currency, String method)
throws Exception {
Payment p = new Payment();
p.setOrderId(null);
@ -73,7 +73,7 @@ public class PaymentService {
payRepo.save(p);
RedsysService.PaymentRequest req = new RedsysService.PaymentRequest(dsOrder, amountCents,
"Compra en Imprimelibros", cartId);
"Compra en Imprimelibros", cartId, dirFactId);
if ("bizum".equalsIgnoreCase(method)) {
return redsysService.buildRedirectFormBizum(req);
@ -213,7 +213,10 @@ public class PaymentService {
}
if (authorized) {
processOrder(notif.cartId, locale);
Long orderId = processOrder(notif.cartId, notif.dirFactId, locale);
if (orderId != null) {
p.setOrderId(orderId);
}
}
payRepo.save(p);
@ -308,15 +311,13 @@ public class PaymentService {
}
@Transactional
public Payment createBankTransferPayment(Long cartId, long amountCents, String currency) {
public Payment createBankTransferPayment(Long cartId, Long dirFactId, long amountCents, String currency) {
Payment p = new Payment();
p.setOrderId(null);
Cart cart = this.cartService.findById(cartId);
if (cart != null && cart.getUserId() != null) {
p.setUserId(cart.getUserId());
// En el orderId de la transferencia pendiente guardamos el ID del carrito
p.setOrderId(cartId);
// Se bloquea el carrito para evitar modificaciones mientras se procesa el pago
this.cartService.lockCartById(cartId);
}
@ -334,6 +335,18 @@ public class PaymentService {
tx.setStatus(PaymentTransactionStatus.pending);
tx.setAmountCents(amountCents);
tx.setCurrency(currency);
String payload = "";
if (cartId != null) {
payload = "{\"cartId\":" + cartId + "}";
}
if (dirFactId != null) {
if (!payload.isEmpty()) {
payload = payload.substring(0, payload.length() - 1) + ",\"dirFactId\":" + dirFactId + "}";
} else {
payload = "{\"dirFactId\":" + dirFactId + "}";
}
}
tx.setResponsePayload(payload);
// tx.setProcessedAt(null); // la dejas nula hasta que se confirme
txRepo.save(tx);
@ -374,12 +387,33 @@ public class PaymentService {
p.setAmountCapturedCents(p.getAmountTotalCents());
p.setCapturedAt(LocalDateTime.now());
p.setStatus(PaymentStatus.captured);
payRepo.save(p);
// 4) Procesar el pedido asociado al carrito (si existe)
if (p.getOrderId() != null) {
processOrder(p.getOrderId(), locale);
Long cartId = null;
Long dirFactId = null;
try {
// Intentar extraer cartId del payload de la transacción
if (tx.getResponsePayload() != null && !tx.getResponsePayload().isBlank()) {
ObjectMapper om = new ObjectMapper();
var node = om.readTree(tx.getResponsePayload());
if (node.has("cartId")) {
cartId = node.get("cartId").asLong();
}
if (node.has("dirFactId")) {
dirFactId = node.get("dirFactId").asLong();
}
}
} catch (Exception e) {
// ignorar
}
// 4) Procesar el pedido asociado al carrito (si existe)
if (cartId != null) {
Long orderId = processOrder(cartId, dirFactId, locale);
if (orderId != null) {
p.setOrderId(orderId);
}
}
payRepo.save(p);
}
/**
@ -481,22 +515,23 @@ public class PaymentService {
*
*/
@Transactional
private Boolean processOrder(Long cartId, Locale locale) {
private Long processOrder(Long cartId, Long dirFactId, Locale locale) {
Cart cart = this.cartService.findById(cartId);
if (cart != null) {
// Bloqueamos el carrito
this.cartService.lockCartById(cart.getId());
// Creamos el pedido
Long orderId = this.cartService.crearPedido(cart.getId(), locale);
Long orderId = this.cartService.crearPedido(cart.getId(), dirFactId, locale);
if (orderId == null) {
return false;
return null;
} else {
// envio de correo de confirmacion de pedido podria ir aqui
return orderId;
}
}
return true;
return null;
}
}