Compare commits

...

2 Commits

Author SHA1 Message Date
c11c34011e trabajando en la tabla de transferencias 2025-11-05 21:46:54 +01:00
a4443763d8 trabajando en devoluciones 2025-11-05 15:09:26 +01:00
18 changed files with 1090 additions and 517 deletions

View File

@ -4,121 +4,281 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import org.springframework.context.MessageSource;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.imprimelibros.erp.payments.model.PaymentTransactionStatus.*;
import com.imprimelibros.erp.common.Utils;
import com.imprimelibros.erp.configuracion.margenes_presupuestos.MargenPresupuesto;
import com.imprimelibros.erp.datatables.DataTable;
import com.imprimelibros.erp.datatables.DataTablesParser;
import com.imprimelibros.erp.datatables.DataTablesRequest;
import com.imprimelibros.erp.datatables.DataTablesResponse;
import com.imprimelibros.erp.i18n.TranslationService;
import com.imprimelibros.erp.payments.model.Payment;
import com.imprimelibros.erp.payments.model.PaymentStatus;
import com.imprimelibros.erp.payments.model.PaymentTransaction;
import com.imprimelibros.erp.payments.model.PaymentTransactionStatus;
import com.imprimelibros.erp.payments.model.PaymentTransactionType;
import com.imprimelibros.erp.payments.repo.PaymentTransactionRepository;
import com.imprimelibros.erp.users.User;
import com.imprimelibros.erp.users.UserDao;
import jakarta.servlet.http.HttpServletRequest;
@Controller
@RequestMapping("/pagos")
@PreAuthorize("hasRole('SUPERADMIN')")
public class PaymentController {
protected final MessageSource messageSource;
protected final TranslationService translationService;
protected final PaymentTransactionRepository repoPaymentTransaction;
protected final UserDao repoUser;
public PaymentController(PaymentTransactionRepository repoPaymentTransaction, UserDao repoUser) {
public PaymentController(PaymentTransactionRepository repoPaymentTransaction, UserDao repoUser,
MessageSource messageSource, TranslationService translationService) {
this.repoPaymentTransaction = repoPaymentTransaction;
this.repoUser = repoUser;
this.messageSource = messageSource;
this.translationService = translationService;
}
@GetMapping()
public String index() {
public String index(Model model, Locale locale) {
List<String> keys = List.of(
"app.cancelar",
"app.aceptar",
"pagos.refund.title",
"pagos.refund.text",
"pagos.refund.success",
"pagos.refund.error.general",
"pagos.refund.error.invalid-number");
Map<String, String> translations = translationService.getTranslations(locale, keys);
model.addAttribute("languageBundle", translations);
return "imprimelibros/pagos/gestion-pagos";
}
@GetMapping(value = "datatable/redsys", produces = "application/json")
@ResponseBody
public DataTablesResponse<Map<String, Object>> getDatatableRedsys(HttpServletRequest request,Locale locale) {
public DataTablesResponse<Map<String, Object>> getDatatableRedsys(HttpServletRequest request, Locale locale) {
DataTablesRequest dt = DataTablesParser.from(request);
List<String> searchable = List.of(
);
"payment.gatewayOrderId",
"payment.orderId"
// "client" no, porque lo calculas a posteriori
);
// Campos ordenables
List<String> orderable = List.of(
);
"payment.gatewayOrderId",
"payment.orderId",
"amountCents",
"payment.amountRefundedCents",
"createdAt");
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));
String clientSearch = dt.getColumnSearch("client");
// 2) Si hay filtro, traducirlo a userIds y añadirlo al Specification
if (clientSearch != null) {
List<Long> userIds = repoUser.findIdsByFullNameLike(clientSearch.trim());
if (userIds.isEmpty()) {
// Ningún usuario coincide → forzamos 0 resultados
base = base.and((root, query, cb) -> cb.disjunction());
} else {
base = base.and((root, query, cb) -> root.join("payment").get("userId").in(userIds));
}
}
Long total = repoPaymentTransaction.count(base);
return DataTable
.of(repoPaymentTransaction, PaymentTransaction.class, dt, searchable) // 'searchable' en DataTable.java
// edita columnas "reales":
.of(repoPaymentTransaction, PaymentTransaction.class, dt, searchable)
.orderable(orderable)
.add("created_at", (pago) -> {
return Utils.formatDateTime(pago.getCreatedAt(), locale);
})
.add("client", (pago) -> {
.add("created_at", pago -> Utils.formatDateTime(pago.getCreatedAt(), locale))
.add("client", pago -> {
if (pago.getPayment() != null && pago.getPayment().getUserId() != null) {
Payment payment = pago.getPayment();
if(payment.getUserId() != null) {
Optional<User> user = repoUser.findById(payment.getUserId());
if (payment.getUserId() != null) {
Optional<User> user = repoUser.findById(payment.getUserId().longValue());
return user.map(User::getFullName).orElse("");
}
return "";
} else {
return "";
}
return "";
})
.add("gateway_order_id", (pago) -> {
.add("gateway_order_id", pago -> {
if (pago.getPayment() != null) {
return pago.getPayment().getGatewayOrderId();
} else {
return "";
}
})
.add("orderId", (pago) -> {
.add("orderId", pago -> {
if (pago.getPayment() != null && pago.getPayment().getOrderId() != null) {
return pago.getPayment().getOrderId().toString();
} else {
return "";
}
})
.add("amount_cents", (pago) -> {
return Utils.formatCurrency(pago.getAmountCents() / 100.0, locale);
.add("amount_cents", pago -> Utils.formatCurrency(pago.getAmountCents() / 100.0, locale))
.add("amount_cents_refund", pago -> {
Payment payment = pago.getPayment();
if (payment != null) {
return Utils.formatCurrency(payment.getAmountRefundedCents() / 100.0, locale);
}
return "";
})
.add("actions", (pago) -> {
return "<div class=\"hstack gap-3 flex-wrap\">\n" +
" <a href=\"javascript:void(0);\" data-id=\"" + pago.getId()
+ "\" class=\"link-success btn-edit-pago fs-15\"><i class=\"ri-edit-2-line\"></i></a>\n"
+ " <a href=\"javascript:void(0);\" data-id=\"" + pago.getId()
+ "\" class=\"link-danger btn-delete-pago fs-15\"><i class=\"ri-delete-bin-5-line\"></i></a>\n"
+ " </div>";
.add("actions", pago -> {
Payment p = pago.getPayment();
if (p != null) {
if (pago.getAmountCents() - p.getAmountRefundedCents() > 0) {
return "<span class=\'badge bg-secondary btn-refund-payment \' data-dsOrderId=\'"
+ p.getGatewayOrderId()
+ "\' data-transactionId=\'" + pago.getPayment().getId()
+ "\' data-amount=\'" + (pago.getAmountCents() - p.getAmountRefundedCents())
+ "\' style=\'cursor: pointer;\'>"
+ messageSource.getMessage("pagos.table.devuelto", null, locale) + "</span>";
}
return "";
} else {
return "";
}
})
.where(base)
// Filtros custom:
.toJson(total);
}
@GetMapping(value = "datatable/transferencias", produces = "application/json")
@ResponseBody
public DataTablesResponse<Map<String, Object>> getDatatableTransferencias(HttpServletRequest request,
Locale locale) {
DataTablesRequest dt = DataTablesParser.from(request);
List<String> searchable = List.of(
// "client" no, porque lo calculas a posteriori
);
// Campos ordenables
List<String> orderable = List.of(
"transferId",
"status",
"amountCents",
"payment.amountRefundedCents",
"createdAt", "updatedAt");
Specification<PaymentTransaction> base = Specification.allOf(
(root, query, cb) -> cb.equal(root.get("status"), PaymentTransactionStatus.pending));
base = base.and((root, query, cb) -> cb.equal(root.get("type"), PaymentTransactionType.CAPTURE));
base = base.and((root, query, cb) -> cb.equal(root.get("payment").get("gateway"), "bank_transfer"));
String clientSearch = dt.getColumnSearch("client");
// 2) Si hay filtro, traducirlo a userIds y añadirlo al Specification
if (clientSearch != null) {
List<Long> userIds = repoUser.findIdsByFullNameLike(clientSearch.trim());
if (userIds.isEmpty()) {
// Ningún usuario coincide → forzamos 0 resultados
base = base.and((root, query, cb) -> cb.disjunction());
} else {
base = base.and((root, query, cb) -> root.join("payment").get("userId").in(userIds));
}
}
Long total = repoPaymentTransaction.count(base);
return DataTable
.of(repoPaymentTransaction, PaymentTransaction.class, dt, searchable)
.orderable(orderable)
.add("created_at", pago -> Utils.formatDateTime(pago.getCreatedAt(), locale))
.add("processed_at", pago -> Utils.formatDateTime(pago.getProcessedAt(), locale))
.add("client", pago -> {
if (pago.getPayment() != null && pago.getPayment().getUserId() != null) {
Payment payment = pago.getPayment();
if (payment.getUserId() != null) {
Optional<User> user = repoUser.findById(payment.getUserId().longValue());
return user.map(User::getFullName).orElse("");
}
}
return "";
})
.add("transfer_id", pago -> {
if (pago.getPayment() != null) {
return "TRANSF-" + pago.getPayment().getGatewayOrderId();
} else {
return "";
}
})
.add("order_id", pago -> {
if (pago.getStatus() != PaymentTransactionStatus.pending) {
if (pago.getPayment() != null && pago.getPayment().getOrderId() != null) {
return pago.getPayment().getOrderId().toString();
} else {
return "";
}
}
return messageSource.getMessage("pagos.transferencia.no-pedido", null, "Pendiente", locale);
}).add("amount_cents", pago -> Utils.formatCurrency(pago.getAmountCents() / 100.0, locale))
.add("amount_cents_refund", pago ->
{
Payment payment = pago.getPayment();
if (payment != null) {
return Utils.formatCurrency(payment.getAmountRefundedCents() / 100.0, locale);
}
return "";
}).add("status", pago -> {
switch (pago.getStatus()) {
case PaymentTransactionStatus.pending:
return messageSource.getMessage("pagos.table.estado.pending", null, "Pendiente", locale);
case PaymentTransactionStatus.succeeded:
return messageSource.getMessage("pagos.table.estado.succeeded", null, "Completada", locale);
case PaymentTransactionStatus.failed:
return messageSource.getMessage("pagos.table.estado.failed", null, "Fallido", locale);
default:
return pago.getStatus().name();
}
}).add("actions", pago -> {
Payment p = pago.getPayment();
if (p != null) {
String actions = "";
if (pago.getStatus() != PaymentTransactionStatus.succeeded) {
actions += "<span class=\'badge bg-secondary btn-mark-as-completed \' data-dsOrderId=\'"
+ p.getGatewayOrderId()
+ "\' data-transactionId=\'" + pago.getPayment().getId()
+ "\' style=\'cursor: pointer;\'>"
+ messageSource.getMessage("pagos.table.finalizar", null, locale) + "</span> ";
}
if (pago.getAmountCents() - p.getAmountRefundedCents() > 0) {
actions += "<span class=\'badge bg-secondary btn-refund-payment \' data-dsOrderId=\'"
+ p.getGatewayOrderId()
+ "\' data-transactionId=\'" + pago.getPayment().getId()
+ "\' data-amount=\'" + (pago.getAmountCents() - p.getAmountRefundedCents())
+ "\' style=\'cursor: pointer;\'>"
+ messageSource.getMessage("pagos.table.devuelto", null, locale) + "</span>";
}
return actions;
} else {
return "";
}
}).where(base).toJson(total);
}
}

View File

@ -41,7 +41,6 @@ public class PaymentService {
this.webhookEventRepo = webhookEventRepo;
this.cartService = cartService;
}
/**
* Crea el Payment en BD y construye el formulario de Redsys usando la API
@ -54,7 +53,7 @@ public class PaymentService {
p.setOrderId(null);
Cart cart = this.cartService.findById(cartId);
if(cart != null && cart.getUserId() != null) {
if (cart != null && cart.getUserId() != null) {
p.setUserId(cart.getUserId());
}
p.setCurrency(currency);
@ -198,21 +197,11 @@ public class PaymentService {
p.setFailedAt(LocalDateTime.now());
}
if(authorized) {
// GENERAR PEDIDO A PARTIR DEL CARRITO
Cart cart = this.cartService.findById(notif.cartId);
if(cart != null) {
// Bloqueamos el carrito
this.cartService.lockCartById(cart.getId());
// order ID es generado dentro de createOrderFromCart donde se marcan los presupuestos como no editables
// Long orderId = this.cartService.pedidoService.createOrderFromCart(cart.getId(), p.getId());
// p.setOrderId(orderId);
}
if (authorized) {
processOrder(notif.cartId);
}
payRepo.save(p);
if (!authorized) {
ev.setLastError("Payment declined (Ds_Response=" + notif.response + ")");
@ -230,9 +219,8 @@ public class PaymentService {
}
}
// ---- refundViaRedsys y bank_transfer igual que antes, no tocan RedsysService
// ---- refundViaRedsys
// ----
@Transactional
public void refundViaRedsys(Long paymentId, long amountCents, String idempotencyKey) {
Payment p = payRepo.findById(paymentId)
@ -240,6 +228,7 @@ public class PaymentService {
if (amountCents <= 0)
throw new IllegalArgumentException("Importe inválido");
long maxRefundable = p.getAmountCapturedCents() - p.getAmountRefundedCents();
if (amountCents > maxRefundable)
throw new IllegalStateException("Importe de devolución supera lo capturado");
@ -256,8 +245,18 @@ public class PaymentService {
r.setRequestedAt(LocalDateTime.now());
r = refundRepo.save(r);
String gatewayRefundId = "REF-" + UUID.randomUUID(); // aquí iría el ID real si alguna vez llamas a un API de
// devoluciones
String gatewayRefundId;
try {
// ⚠️ Usa aquí el mismo valor que mandaste en Ds_Merchant_Order al cobrar
// por ejemplo, p.getGatewayOrderId() o similar
String originalOrder = p.getGatewayOrderId(); // ajusta al nombre real del campo
gatewayRefundId = redsysService.requestRefund(originalOrder, amountCents);
} catch (Exception e) {
r.setStatus(RefundStatus.failed);
r.setProcessedAt(LocalDateTime.now());
refundRepo.save(r);
throw new IllegalStateException("Error al solicitar la devolución a Redsys", e);
}
PaymentTransaction tx = new PaymentTransaction();
tx.setPayment(p);
@ -291,8 +290,12 @@ public class PaymentService {
p.setOrderId(null);
Cart cart = this.cartService.findById(cartId);
if(cart != null && cart.getUserId() != null) {
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);
}
p.setCurrency(currency);
@ -310,7 +313,7 @@ public class PaymentService {
tx.setCurrency(currency);
// tx.setProcessedAt(null); // la dejas nula hasta que se confirme
txRepo.save(tx);
return p;
}
@ -349,6 +352,11 @@ public class PaymentService {
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());
}
}
private boolean isRedsysAuthorized(RedsysService.RedsysNotification notif) {
@ -365,4 +373,20 @@ public class PaymentService {
return code >= 0 && code <= 99;
}
private Boolean processOrder(Long cartId) {
// GENERAR PEDIDO A PARTIR DEL CARRITO
Cart cart = this.cartService.findById(cartId);
if (cart != null) {
// Bloqueamos el carrito
this.cartService.lockCartById(cart.getId());
// order ID es generado dentro de createOrderFromCart donde se marcan los
// presupuestos como no editables
// Long orderId =
// this.cartService.pedidoService.createOrderFromCart(cart.getId(), p.getId());
// p.setOrderId(orderId);
}
return true;
}
}

View File

@ -163,9 +163,9 @@ public class RedsysController {
try {
String idem = "refund-" + paymentId + "-" + amountCents + "-" + UUID.randomUUID();
paymentService.refundViaRedsys(paymentId, amountCents, idem);
return ResponseEntity.ok("Refund solicitado");
return ResponseEntity.ok("{success:true}");
} catch (Exception e) {
return ResponseEntity.badRequest().body("Error refund: " + e.getMessage());
return ResponseEntity.badRequest().body("{success:false, error: '" + e.getMessage() + "'}");
}
}
}

View File

@ -8,6 +8,11 @@ import sis.redsys.api.ApiMacSha256;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.net.URI;
import java.net.URLEncoder;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.Base64;
@ -18,6 +23,10 @@ import java.util.Objects;
public class RedsysService {
// ---------- CONFIG ----------
@Value("${redsys.url}")
private String url;
@Value("${redsys.refund.url}")
private String urlRefund;
@Value("${redsys.merchant-code}")
private String merchantCode;
@Value("${redsys.terminal}")
@ -37,6 +46,8 @@ public class RedsysService {
@Value("${redsys.environment}")
private String env;
private final HttpClient httpClient = HttpClient.newHttpClient();
// ---------- RECORDS ----------
// Pedido a Redsys
public record PaymentRequest(String order, long amountCents, String description, Long cartId) {
@ -89,9 +100,11 @@ public class RedsysService {
String merchantParameters = api.createMerchantParameters();
String signature = api.createMerchantSignature(secretKeyBase64);
String action = "test".equalsIgnoreCase(env)
? "https://sis-t.redsys.es:25443/sis/realizarPago"
: "https://sis.redsys.es/sis/realizarPago";
String action = url;
/*
* ? "https://sis-t.redsys.es:25443/sis/realizarPago"
* : "https://sis.redsys.es/sis/realizarPago";
*/
return new FormPayload(action, "HMAC_SHA256_V1", merchantParameters, signature);
}
@ -231,4 +244,79 @@ public class RedsysService {
}
}
}
/**
* Solicita a Redsys una devolución (TransactionType = 3)
*
* @param order El mismo Ds_Merchant_Order que se usó en el cobro.
* @param amountCents Importe en céntimos a devolver.
* @return gatewayRefundId (p.ej. Ds_AuthorisationCode o Ds_Order)
*/
public String requestRefund(String order, long amountCents) throws Exception {
ApiMacSha256 api = new ApiMacSha256();
// Montar parámetros para el refund
api.setParameter("DS_MERCHANT_MERCHANTCODE", merchantCode);
api.setParameter("DS_MERCHANT_TERMINAL", terminal);
api.setParameter("DS_MERCHANT_ORDER", order);
api.setParameter("DS_MERCHANT_AMOUNT", String.valueOf(amountCents));
api.setParameter("DS_MERCHANT_CURRENCY", currency);
api.setParameter("DS_MERCHANT_TRANSACTIONTYPE", "3"); // 3 = devolución
api.setParameter("DS_MERCHANT_MERCHANTURL", "");
api.setParameter("DS_MERCHANT_URLOK", "");
api.setParameter("DS_MERCHANT_URLKO", "");
// Crear parámetros y firma (como en tu PHP)
String merchantParameters = api.createMerchantParameters();
String signature = api.createMerchantSignature(secretKeyBase64);
// Montar el JSON para Redsys REST
String json = """
{
"Ds_MerchantParameters": "%s",
"Ds_Signature": "%s",
"Ds_SignatureVersion": "HMAC_SHA256_V1"
}
""".formatted(merchantParameters, signature);
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(urlRefund))
.header("Content-Type", "application/json; charset=UTF-8")
.POST(HttpRequest.BodyPublishers.ofString(json))
.build();
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() / 100 != 2)
throw new IllegalStateException("HTTP error Redsys refund: " + response.statusCode());
if (response.body() == null || response.body().isBlank())
throw new IllegalStateException("Respuesta vacía de Redsys refund REST");
// Parsear la respuesta JSON
Map<String, Object> respMap = MAPPER.readValue(response.body(), new TypeReference<>() {
});
// Redsys puede devolver "Ds_MerchantParameters" o "errorCode"
if (respMap.containsKey("errorCode")) {
throw new IllegalStateException("Error Redsys refund: " + respMap.get("errorCode"));
}
String dsMerchantParametersResp = (String) respMap.get("Ds_MerchantParameters");
if (dsMerchantParametersResp == null) {
throw new IllegalStateException("Respuesta Redsys refund sin Ds_MerchantParameters");
}
// Decodificar MerchantParameters de la respuesta
Map<String, Object> decoded = decodeMerchantParametersToMap(dsMerchantParametersResp);
String dsResponse = String.valueOf(decoded.get("Ds_Response"));
if (!"0900".equals(dsResponse)) {
throw new IllegalStateException("Devolución rechazada, Ds_Response=" + dsResponse);
}
return String.valueOf(decoded.getOrDefault("Ds_AuthorisationCode", order));
}
}

View File

@ -19,60 +19,63 @@ import org.springframework.lang.Nullable;
@Repository
public interface UserDao extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> {
// Aplicamos EntityGraph a la versión con Specification+Pageable
@Override
@EntityGraph(attributePaths = { "rolesLink", "rolesLink.role" })
@NonNull
Page<User> findAll(@Nullable Specification<User> spec, @NonNull Pageable pageable);
// Aplicamos EntityGraph a la versión con Specification+Pageable
@Override
@EntityGraph(attributePaths = { "rolesLink", "rolesLink.role" })
@NonNull
Page<User> findAll(@Nullable Specification<User> spec, @NonNull Pageable pageable);
Optional<User> findByUserNameIgnoreCase(String userName);
Optional<User> findByUserNameIgnoreCase(String userName);
boolean existsByUserNameIgnoreCase(String userName);
boolean existsByUserNameIgnoreCase(String userName);
// Para comprobar si existe al hacer signup
@Query(value = """
SELECT id, deleted, enabled
FROM users
WHERE LOWER(username) = LOWER(:userName)
LIMIT 1
""", nativeQuery = true)
Optional<UserLite> findLiteByUserNameIgnoreCase(@Param("userName") String userName);
// Para comprobar si existe al hacer signup
@Query(value = """
SELECT id, deleted, enabled
FROM users
WHERE LOWER(username) = LOWER(:userName)
LIMIT 1
""", nativeQuery = true)
Optional<UserLite> findLiteByUserNameIgnoreCase(@Param("userName") String userName);
boolean existsByUserNameIgnoreCaseAndIdNot(String userName, Long id);
boolean existsByUserNameIgnoreCaseAndIdNot(String userName, Long id);
// Nuevo: para login/negocio "activo"
@EntityGraph(attributePaths = { "rolesLink", "rolesLink.role" })
Optional<User> findByUserNameIgnoreCaseAndEnabledTrueAndDeletedFalse(String userName);
// Nuevo: para login/negocio "activo"
@EntityGraph(attributePaths = { "rolesLink", "rolesLink.role" })
Optional<User> findByUserNameIgnoreCaseAndEnabledTrueAndDeletedFalse(String userName);
// Para poder restaurar, necesitas leer ignorando @Where (native):
@Query(value = "SELECT * FROM users WHERE id = :id", nativeQuery = true)
Optional<User> findByIdIncludingDeleted(@Param("id") Long id);
// Para poder restaurar, necesitas leer ignorando @Where (native):
@Query(value = "SELECT * FROM users WHERE id = :id", nativeQuery = true)
Optional<User> findByIdIncludingDeleted(@Param("id") Long id);
@Query(value = "SELECT * FROM users WHERE deleted = TRUE", nativeQuery = true)
List<User> findAllDeleted();
@Query(value = "SELECT * FROM users WHERE deleted = TRUE", nativeQuery = true)
List<User> findAllDeleted();
@Query("select u.id from User u where lower(u.userName) = lower(:userName)")
Optional<Long> findIdByUserNameIgnoreCase(@Param("userName") String userName);
@Query("select u.id from User u where lower(u.userName) = lower(:userName)")
Optional<Long> findIdByUserNameIgnoreCase(@Param("userName") String userName);
@Query(value = """
SELECT DISTINCT u
FROM User u
JOIN u.rolesLink rl
JOIN rl.role r
WHERE (:role IS NULL OR r.name = :role)
AND (:q IS NULL OR LOWER(u.fullName) LIKE LOWER(CONCAT('%', :q, '%'))
OR LOWER(u.userName) LIKE LOWER(CONCAT('%', :q, '%')))
""", countQuery = """
SELECT COUNT(DISTINCT u.id)
FROM User u
JOIN u.rolesLink rl
JOIN rl.role r
WHERE (:role IS NULL OR r.name = :role)
AND (:q IS NULL OR LOWER(u.fullName) LIKE LOWER(CONCAT('%', :q, '%'))
OR LOWER(u.userName) LIKE LOWER(CONCAT('%', :q, '%')))
""")
Page<User> searchUsers(@Param("role") String role,
@Param("q") String q,
Pageable pageable);
@Query(value = """
SELECT DISTINCT u
FROM User u
JOIN u.rolesLink rl
JOIN rl.role r
WHERE (:role IS NULL OR r.name = :role)
AND (:q IS NULL OR LOWER(u.fullName) LIKE LOWER(CONCAT('%', :q, '%'))
OR LOWER(u.userName) LIKE LOWER(CONCAT('%', :q, '%')))
""", countQuery = """
SELECT COUNT(DISTINCT u.id)
FROM User u
JOIN u.rolesLink rl
JOIN rl.role r
WHERE (:role IS NULL OR r.name = :role)
AND (:q IS NULL OR LOWER(u.fullName) LIKE LOWER(CONCAT('%', :q, '%'))
OR LOWER(u.userName) LIKE LOWER(CONCAT('%', :q, '%')))
""")
Page<User> searchUsers(@Param("role") String role,
@Param("q") String q,
Pageable pageable);
@Query("select u.id from User u where lower(u.fullName) like lower(concat('%', :name, '%'))")
List<Long> findIdsByFullNameLike(@Param("name") String name);
}

View File

@ -20,6 +20,8 @@ safekat.api.password=Safekat2024
# Configuración Redsys
redsys.environment=test
redsys.url=https://sis-t.redsys.es:25443/sis/realizarPago
redsys.refund.url=https://sis-t.redsys.es:25443/sis/rest/trataPeticionREST
redsys.urls.ok=http://localhost:8080/pagos/redsys/ok
redsys.urls.ko=http://localhost:8080/pagos/redsys/ko
redsys.urls.notify=https://orological-sacrilegiously-lucille.ngrok-free.dev/pagos/redsys/notify

View File

@ -20,6 +20,8 @@ safekat.api.password=Safekat2024
# Configuración Redsys
redsys.environment=test
redsys.url=https://sis-t.redsys.es:25443/sis/realizarPago
redsys.refund.url=https://sis-t.redsys.es:25443/sis/rest/trataPeticionREST
redsys.urls.ok=https://imprimelibros.jjimenez.eu/pagos/redsys/ok
redsys.urls.ko=https://imprimelibros.jjimenez.eu/pagos/redsys/ko
redsys.urls.notify=https://imprimelibros.jjimenez.eu/pagos/redsys/notify

View File

@ -1,403 +1,418 @@
databaseChangeLog:
- changeSet:
id: 0007-payments-core
author: jjo
changes:
# 2) payments
- createTable:
tableName: payments
columns:
- column:
name: id
type: BIGINT AUTO_INCREMENT
constraints:
primaryKey: true
nullable: false
- column:
name: order_id
type: BIGINT
- column:
name: user_id
type: BIGINT
- column:
name: currency
type: CHAR(3)
constraints:
nullable: false
- column:
name: amount_total_cents
type: BIGINT
constraints:
nullable: false
- column:
name: amount_captured_cents
type: BIGINT
defaultValueNumeric: 0
constraints:
nullable: false
- column:
name: amount_refunded_cents
type: BIGINT
defaultValueNumeric: 0
constraints:
nullable: false
- column:
name: status
type: "ENUM('requires_payment_method','requires_action','authorized','captured','partially_refunded','refunded','canceled','failed')"
defaultValue: "requires_payment_method"
constraints:
nullable: false
- column:
name: capture_method
type: "ENUM('automatic','manual')"
defaultValue: "automatic"
constraints:
nullable: false
- column:
name: gateway
type: VARCHAR(32)
constraints:
nullable: false
- column:
name: gateway_payment_id
type: VARCHAR(128)
- column:
name: gateway_order_id
type: VARCHAR(12)
- column:
name: authorization_code
type: VARCHAR(32)
- column:
name: three_ds_status
type: "ENUM('not_applicable','attempted','challenge','succeeded','failed')"
defaultValue: "not_applicable"
constraints:
nullable: false
- column:
name: descriptor
type: VARCHAR(22)
- column:
name: client_ip
type: VARBINARY(16)
- column:
name: authorized_at
type: DATETIME
- column:
name: captured_at
type: DATETIME
- column:
name: canceled_at
type: DATETIME
- column:
name: failed_at
type: DATETIME
- column:
name: metadata
type: JSON
- column:
name: created_at
type: DATETIME
defaultValueComputed: CURRENT_TIMESTAMP
constraints:
nullable: false
- column:
name: updated_at
type: DATETIME
defaultValueComputed: "CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"
constraints:
nullable: false
id: 0007-payments-core
author: jjo
changes:
# 2) payments
- createTable:
tableName: payments
columns:
- column:
name: id
type: BIGINT AUTO_INCREMENT
constraints:
primaryKey: true
nullable: false
- column:
name: order_id
type: BIGINT
- column:
name: user_id
type: BIGINT
- column:
name: currency
type: CHAR(3)
constraints:
nullable: false
- column:
name: amount_total_cents
type: BIGINT
constraints:
nullable: false
- column:
name: amount_captured_cents
type: BIGINT
defaultValueNumeric: 0
constraints:
nullable: false
- column:
name: amount_refunded_cents
type: BIGINT
defaultValueNumeric: 0
constraints:
nullable: false
- column:
name: status
type: "ENUM('requires_payment_method','requires_action','authorized','captured','partially_refunded','refunded','canceled','failed')"
defaultValue: "requires_payment_method"
constraints:
nullable: false
- column:
name: capture_method
type: "ENUM('automatic','manual')"
defaultValue: "automatic"
constraints:
nullable: false
- column:
name: gateway
type: VARCHAR(32)
constraints:
nullable: false
- column:
name: gateway_payment_id
type: VARCHAR(128)
- column:
name: gateway_order_id
type: VARCHAR(12)
- column:
name: authorization_code
type: VARCHAR(32)
- column:
name: three_ds_status
type: "ENUM('not_applicable','attempted','challenge','succeeded','failed')"
defaultValue: "not_applicable"
constraints:
nullable: false
- column:
name: descriptor
type: VARCHAR(22)
- column:
name: client_ip
type: VARBINARY(16)
- column:
name: authorized_at
type: DATETIME
- column:
name: captured_at
type: DATETIME
- column:
name: canceled_at
type: DATETIME
- column:
name: failed_at
type: DATETIME
- column:
name: metadata
type: JSON
- column:
name: created_at
type: DATETIME
defaultValueComputed: CURRENT_TIMESTAMP
constraints:
nullable: false
- column:
name: updated_at
type: DATETIME
defaultValueComputed: "CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"
constraints:
nullable: false
- createIndex:
tableName: payments
indexName: idx_payments_order
columns:
- column:
name: order_id
- createIndex:
tableName: payments
indexName: idx_payments_order
columns:
- column:
name: order_id
- createIndex:
tableName: payments
indexName: idx_payments_gateway
columns:
- column:
name: gateway
- column:
name: gateway_payment_id
- createIndex:
tableName: payments
indexName: idx_payments_gateway
columns:
- column:
name: gateway
- column:
name: gateway_payment_id
- createIndex:
tableName: payments
indexName: idx_payments_status
columns:
- column:
name: status
- createIndex:
tableName: payments
indexName: idx_payments_status
columns:
- column:
name: status
- addUniqueConstraint:
tableName: payments
columnNames: gateway, gateway_order_id
constraintName: uq_payments_gateway_order
- addUniqueConstraint:
tableName: payments
columnNames: gateway, gateway_order_id
constraintName: uq_payments_gateway_order
# 3) payment_transactions
- createTable:
tableName: payment_transactions
columns:
- column:
name: id
type: BIGINT AUTO_INCREMENT
constraints:
primaryKey: true
nullable: false
- column:
name: payment_id
type: BIGINT
constraints:
nullable: false
- column:
name: type
type: "ENUM('AUTH','CAPTURE','REFUND','VOID')"
constraints:
nullable: false
- column:
name: status
type: "ENUM('pending','succeeded','failed')"
constraints:
nullable: false
- column:
name: amount_cents
type: BIGINT
constraints:
nullable: false
- column:
name: currency
type: CHAR(3)
constraints:
nullable: false
- column:
name: gateway_transaction_id
type: VARCHAR(128)
- column:
name: gateway_response_code
type: VARCHAR(64)
- column:
name: avs_result
type: VARCHAR(8)
- column:
name: cvv_result
type: VARCHAR(8)
- column:
name: three_ds_version
type: VARCHAR(16)
- column:
name: idempotency_key
type: VARCHAR(128)
- column:
name: request_payload
type: JSON
- column:
name: response_payload
type: JSON
- column:
name: processed_at
type: DATETIME
- column:
name: created_at
type: DATETIME
defaultValueComputed: CURRENT_TIMESTAMP
constraints:
nullable: false
# 3) payment_transactions
- createTable:
tableName: payment_transactions
columns:
- column:
name: id
type: BIGINT AUTO_INCREMENT
constraints:
primaryKey: true
nullable: false
- column:
name: payment_id
type: BIGINT
constraints:
nullable: false
- column:
name: type
type: "ENUM('AUTH','CAPTURE','REFUND','VOID')"
constraints:
nullable: false
- column:
name: status
type: "ENUM('pending','succeeded','failed')"
constraints:
nullable: false
- column:
name: amount_cents
type: BIGINT
constraints:
nullable: false
- column:
name: currency
type: CHAR(3)
constraints:
nullable: false
- column:
name: gateway_transaction_id
type: VARCHAR(128)
- column:
name: gateway_response_code
type: VARCHAR(64)
- column:
name: avs_result
type: VARCHAR(8)
- column:
name: cvv_result
type: VARCHAR(8)
- column:
name: three_ds_version
type: VARCHAR(16)
- column:
name: idempotency_key
type: VARCHAR(128)
- column:
name: request_payload
type: JSON
- column:
name: response_payload
type: JSON
- column:
name: processed_at
type: DATETIME
- column:
name: created_at
type: DATETIME
defaultValueComputed: CURRENT_TIMESTAMP
constraints:
nullable: false
- addForeignKeyConstraint:
baseTableName: payment_transactions
baseColumnNames: payment_id
referencedTableName: payments
referencedColumnNames: id
constraintName: fk_tx_payment
onDelete: CASCADE
- addForeignKeyConstraint:
baseTableName: payment_transactions
baseColumnNames: payment_id
referencedTableName: payments
referencedColumnNames: id
constraintName: fk_tx_payment
onDelete: CASCADE
- addUniqueConstraint:
tableName: payment_transactions
columnNames: gateway_transaction_id
constraintName: uq_tx_gateway_txid
- addUniqueConstraint:
tableName: payment_transactions
columnNames: gateway_transaction_id
constraintName: uq_tx_gateway_txid
- createIndex:
tableName: payment_transactions
indexName: idx_tx_pay
columns:
- column:
name: payment_id
- createIndex:
tableName: payment_transactions
indexName: idx_tx_pay
columns:
- column:
name: payment_id
- createIndex:
tableName: payment_transactions
indexName: idx_tx_type_status
columns:
- column:
name: type
- column:
name: status
- createIndex:
tableName: payment_transactions
indexName: idx_tx_type_status
columns:
- column:
name: type
- column:
name: status
- createIndex:
tableName: payment_transactions
indexName: idx_tx_idem
columns:
- column:
name: idempotency_key
- createIndex:
tableName: payment_transactions
indexName: idx_tx_idem
columns:
- column:
name: idempotency_key
# 4) refunds
- createTable:
tableName: refunds
columns:
- column:
name: id
type: BIGINT AUTO_INCREMENT
constraints:
primaryKey: true
nullable: false
- column:
name: payment_id
type: BIGINT
constraints:
nullable: false
- column:
name: transaction_id
type: BIGINT
- column:
name: amount_cents
type: BIGINT
constraints:
nullable: false
- column:
name: reason
type: "ENUM('customer_request','partial_return','pricing_adjustment','duplicate','fraud','other')"
defaultValue: "customer_request"
constraints:
nullable: false
- column:
name: status
type: "ENUM('pending','succeeded','failed','canceled')"
defaultValue: "pending"
constraints:
nullable: false
- column:
name: requested_by_user_id
type: BIGINT
- column:
name: requested_at
type: DATETIME
defaultValueComputed: CURRENT_TIMESTAMP
constraints:
nullable: false
- column:
name: processed_at
type: DATETIME
- column:
name: gateway_refund_id
type: VARCHAR(128)
- column:
name: notes
type: VARCHAR(500)
- column:
name: metadata
type: JSON
# 4) refunds
- createTable:
tableName: refunds
columns:
- column:
name: id
type: BIGINT AUTO_INCREMENT
constraints:
primaryKey: true
nullable: false
- column:
name: payment_id
type: BIGINT
constraints:
nullable: false
- column:
name: transaction_id
type: BIGINT
- column:
name: amount_cents
type: BIGINT
constraints:
nullable: false
- column:
name: reason
type: "ENUM('customer_request','partial_return','pricing_adjustment','duplicate','fraud','other')"
defaultValue: "customer_request"
constraints:
nullable: false
- column:
name: status
type: "ENUM('pending','succeeded','failed','canceled')"
defaultValue: "pending"
constraints:
nullable: false
- column:
name: requested_by_user_id
type: BIGINT
- column:
name: requested_at
type: DATETIME
defaultValueComputed: CURRENT_TIMESTAMP
constraints:
nullable: false
- column:
name: processed_at
type: DATETIME
- column:
name: gateway_refund_id
type: VARCHAR(128)
- column:
name: notes
type: VARCHAR(500)
- column:
name: metadata
type: JSON
- addForeignKeyConstraint:
baseTableName: refunds
baseColumnNames: payment_id
referencedTableName: payments
referencedColumnNames: id
constraintName: fk_ref_payment
onDelete: CASCADE
- addForeignKeyConstraint:
baseTableName: refunds
baseColumnNames: payment_id
referencedTableName: payments
referencedColumnNames: id
constraintName: fk_ref_payment
onDelete: CASCADE
- addForeignKeyConstraint:
baseTableName: refunds
baseColumnNames: transaction_id
referencedTableName: payment_transactions
referencedColumnNames: id
constraintName: fk_ref_tx
onDelete: SET NULL
- addForeignKeyConstraint:
baseTableName: refunds
baseColumnNames: transaction_id
referencedTableName: payment_transactions
referencedColumnNames: id
constraintName: fk_ref_tx
onDelete: SET NULL
- addUniqueConstraint:
tableName: refunds
columnNames: gateway_refund_id
constraintName: uq_refund_gateway_id
- addUniqueConstraint:
tableName: refunds
columnNames: gateway_refund_id
constraintName: uq_refund_gateway_id
- createIndex:
tableName: refunds
indexName: idx_ref_pay
columns:
- column:
name: payment_id
- createIndex:
tableName: refunds
indexName: idx_ref_pay
columns:
- column:
name: payment_id
- createIndex:
tableName: refunds
indexName: idx_ref_status
columns:
- column:
name: status
- createIndex:
tableName: refunds
indexName: idx_ref_status
columns:
- column:
name: status
# 5) webhook_events
- createTable:
tableName: webhook_events
columns:
- column:
name: id
type: BIGINT AUTO_INCREMENT
constraints:
primaryKey: true
nullable: false
- column:
name: provider
type: VARCHAR(32)
constraints:
nullable: false
- column:
name: event_type
type: VARCHAR(64)
constraints:
nullable: false
- column:
name: event_id
type: VARCHAR(128)
- column:
name: signature
type: VARCHAR(512)
- column:
name: payload
type: JSON
constraints:
nullable: false
- column:
name: processed
type: TINYINT(1)
defaultValueNumeric: 0
constraints:
nullable: false
- column:
name: processed_at
type: DATETIME
- column:
name: attempts
type: INT
defaultValueNumeric: 0
constraints:
nullable: false
- column:
name: last_error
type: VARCHAR(500)
- column:
name: created_at
type: DATETIME
defaultValueComputed: CURRENT_TIMESTAMP
constraints:
nullable: false
# 5) webhook_events
- createTable:
tableName: webhook_events
columns:
- column:
name: id
type: BIGINT AUTO_INCREMENT
constraints:
primaryKey: true
nullable: false
- column:
name: provider
type: VARCHAR(32)
constraints:
nullable: false
- column:
name: event_type
type: VARCHAR(64)
constraints:
nullable: false
- column:
name: event_id
type: VARCHAR(128)
- column:
name: signature
type: VARCHAR(512)
- column:
name: payload
type: JSON
constraints:
nullable: false
- column:
name: processed
type: TINYINT(1)
defaultValueNumeric: 0
constraints:
nullable: false
- column:
name: processed_at
type: DATETIME
- column:
name: attempts
type: INT
defaultValueNumeric: 0
constraints:
nullable: false
- column:
name: last_error
type: VARCHAR(500)
- column:
name: created_at
type: DATETIME
defaultValueComputed: CURRENT_TIMESTAMP
constraints:
nullable: false
- addUniqueConstraint:
tableName: webhook_events
columnNames: provider, event_id
constraintName: uq_webhook_provider_event
- addUniqueConstraint:
tableName: webhook_events
columnNames: provider, event_id
constraintName: uq_webhook_provider_event
- createIndex:
tableName: webhook_events
indexName: idx_webhook_processed
columns:
- column:
name: processed
- createIndex:
tableName: webhook_events
indexName: idx_webhook_processed
columns:
- column:
name: processed
rollback:
# Se borran las tablas en orden inverso de dependencias
- dropTable:
tableName: webhook_events
- dropTable:
tableName: refunds
- dropTable:
tableName: payment_transactions
- dropTable:
tableName: payments

View File

@ -26,3 +26,22 @@ databaseChangeLog:
sql: |
CREATE UNIQUE INDEX uq_carts_user_active
ON carts (user_id, active_flag);
rollback:
# 🔙 1) Eliminar el índice nuevo basado en active_flag
- sql:
sql: |
ALTER TABLE carts
DROP INDEX uq_carts_user_active;
# 🔙 2) Eliminar la columna generada active_flag
- sql:
sql: |
ALTER TABLE carts
DROP COLUMN active_flag;
# 🔙 3) Restaurar el índice único original (user_id, status)
- sql:
sql: |
CREATE UNIQUE INDEX uq_carts_user_active
ON carts (user_id, status);

View File

@ -0,0 +1,29 @@
databaseChangeLog:
- changeSet:
id: 0009-drop-unique-refund-gateway-id
author: JJO
changes:
# 1⃣ Eliminar la UNIQUE constraint sobre gateway_refund_id
- dropUniqueConstraint:
constraintName: uq_refund_gateway_id
tableName: refunds
# 2⃣ Crear un índice normal (no único) para acelerar búsquedas por gateway_refund_id
- createIndex:
tableName: refunds
indexName: idx_refunds_gateway_refund_id
columns:
- column:
name: gateway_refund_id
rollback:
# 🔙 1) Eliminar el índice normal creado en este changeSet
- dropIndex:
indexName: idx_refunds_gateway_refund_id
tableName: refunds
# 🔙 2) Restaurar la UNIQUE constraint original
- addUniqueConstraint:
tableName: refunds
columnNames: gateway_refund_id
constraintName: uq_refund_gateway_id

View File

@ -0,0 +1,30 @@
databaseChangeLog:
- changeSet:
id: 0010-drop-unique-tx-gateway
author: JJO
changes:
# 1⃣ Eliminar la UNIQUE constraint sobre (gateway_transaction_id, type)
- dropUniqueConstraint:
constraintName: uq_tx_gateway_txid_type
tableName: payment_transactions
# 2⃣ Crear un índice normal (no único) sobre gateway_transaction_id
# para poder seguir buscando rápido por este campo
- createIndex:
tableName: payment_transactions
indexName: idx_payment_tx_gateway_txid
columns:
- column:
name: gateway_transaction_id
rollback:
# 🔙 1) Eliminar el índice normal creado en este changeSet
- dropIndex:
indexName: idx_payment_tx_gateway_txid
tableName: payment_transactions
# 🔙 2) Restaurar la UNIQUE constraint original
- addUniqueConstraint:
tableName: payment_transactions
columnNames: gateway_transaction_id, type
constraintName: uq_tx_gateway_txid_type

View File

@ -14,4 +14,8 @@ databaseChangeLog:
- include:
file: db/changelog/changesets/0007-payments-core.yml
- include:
file: db/changelog/changesets/0008-update-cart-status-constraint.yml
file: db/changelog/changesets/0008-update-cart-status-constraint.yml
- include:
file: db/changelog/changesets/0009-add-composite-unique-txid-type.yml
- include:
file: db/changelog/changesets/0010-drop-unique-tx-gateway.yml

View File

@ -7,6 +7,25 @@ pagos.table.cliente.nombre=Nombre Cliente
pagos.table.redsys.id=Cod. Redsys
pagos.table.pedido.id=Pedido
pagos.table.cantidad=Cantidad
pagos.table.devuelto=Devolución
pagos.table.fecha=Fecha
pagos.table.estado=Estado
pagos.table.acciones=Acciones
pagos.table.concepto-transferencia=Concepto
pagos.table.estado-transferencia=Estado
pagos.table.fecha-created=Fecha creación
pagos.table.fecha-procesed=Fecha procesada
pagos.table.estado.pending=Pendiente
pagos.table.estado.succeeded=Completada
pagos.table.estado.failed=Fallido
pagos.table.finalizar=Finalizar
pagos.transferencia.no-pedido=No disponible
pagos.refund.title=Devolución de Pago Redsys
pagos.refund.text=Introduce la cantidad a devolver (en euros):
pagos.refund.success=Devolución solicitada con éxito. Si no se refleja inmediatamente, espere unos minutos y actualiza la página.
pagos.refund.error.general=Error al procesar la devolución
pagos.refund.error.invalid-number=Cantidad inválida para la devolución

View File

@ -23,9 +23,10 @@ $(() => {
serverSide: true,
orderCellsTop: true,
pageLength: 50,
lengthMenu: [10, 25, 50, 100, 500],
language: { url: '/assets/libs/datatables/i18n/' + language + '.json' },
responsive: true,
dom: 'lrBtip',
dom: 'lBrtip',
buttons: {
dom: {
button: {
@ -45,16 +46,160 @@ $(() => {
url: '/pagos/datatable/redsys',
method: 'GET',
},
order: [[4, 'asc']], // Ordena por fecha por defecto
order: [[5, 'desc']], // Ordena por fecha por defecto
columns: [
{ data: 'client', name: 'user.fullName', orderable: true },
{ data: 'gateway_order_id', name: 'payments.gateway_order_id', orderable: true },
{ data: 'orderId', name: 'order.id', orderable: true },
{ data: 'amount_cents', name: 'amount_cents', orderable: true },
{ data: 'created_at', name: 'created_at', orderable: true },
{ data: 'actions', name: 'actions' }
{ data: 'client', name: 'client', orderable: true },
{ data: 'gateway_order_id', name: 'payment.gatewayOrderId', orderable: true },
{ data: 'orderId', name: 'payment.orderId', orderable: true },
{ data: 'amount_cents', name: 'amountCents', orderable: true },
{ data: 'amount_cents_refund', name: 'amountCentsRefund', orderable: true },
{ data: 'created_at', name: 'createdAt', orderable: true },
{ data: 'actions', name: 'actions', orderable: false, searchable: false }
],
columnDefs: [{ targets: -1, orderable: false, searchable: false }]
});
})
// Fila de filtros = segunda fila del thead (index 1)
$('#pagos-redsys-datatable thead tr:eq(1) th').each(function (colIdx) {
const input = $(this).find('input');
if (input.length === 0) return; // columnas sin filtro
input.on('keyup change', function () {
const value = this.value;
if (table.column(colIdx).search() !== value) {
table.column(colIdx).search(value).draw();
}
});
});
$(document).on('click', '.btn-refund-payment', function () {
const dsOrderId = $(this).data('dsorderid');
const transactionId = $(this).data('transactionid');
const maxAmountCents = $(this).data('amount');
// show swal confirmation with input for amount to refund
Swal.fire({
showCancelButton: true,
buttonsStyling: false,
title: window.languageBundle['pagos.refund.title'],
text: window.languageBundle['pagos.refund.text'],
input: 'number',
confirmButtonText: window.languageBundle['app.aceptar'] || 'Seleccionar',
cancelButtonText: window.languageBundle['app.cancelar'] || 'Cancelar',
customClass: {
confirmButton: 'btn btn-secondary me-2',
cancelButton: 'btn btn-light',
},
inputAttributes: {
min: 0,
}
}).then((result) => {
if (result.isConfirmed) {
const amountToRefund = parseFloat(result.value);
if (isNaN(amountToRefund) || amountToRefund <= 0) {
showSwal('Error', window.languageBundle['pagos.refund.error.invalid-number'], 'error');
return;
}
if (amountToRefund*100 > maxAmountCents) {
showSwal('Error', window.languageBundle['pagos.refund.error.invalid-number'], 'error');
return;
}
if (amountToRefund*100 > maxAmountCents) {
showSwal('Error', window.languageBundle['pagos.refund.error.invalid-number'], 'error');
return;
}
$.ajax({
url: '/pagos/redsys/refund/' + transactionId,
method: 'POST',
data: {
amountCents: amountToRefund*100
}
}).then((response) => {
response = typeof response === 'string' ? JSON.parse(response) : response;
if (response.success) {
showSwal('Éxito', window.languageBundle['pagos.refund.success'], 'success');
$('#pagos-redsys-datatable').DataTable().draw();
} else {
showSwal('Error', window.languageBundle['pagos.refund.error.general'], 'error');
$('#pagos-redsys-datatable').DataTable().draw();
}
}).fail(() => {
showSwal('Error', window.languageBundle['pagos.refund.error.general'], 'error');
});
}
});
});
const tableT = new DataTable('#pagos-transferencias-datatable', {
processing: true,
serverSide: true,
orderCellsTop: true,
pageLength: 50,
lengthMenu: [10, 25, 50, 100, 500],
language: { url: '/assets/libs/datatables/i18n/' + language + '.json' },
responsive: true,
dom: 'lBrtip',
buttons: {
dom: {
button: {
className: 'btn btn-sm btn-outline-primary me-1'
},
buttons: [
{ extend: 'copy' },
{ extend: 'csv' },
{ extend: 'excel' },
{ extend: 'pdf' },
{ extend: 'print' },
{ extend: 'colvis' }
],
}
},
ajax: {
url: '/pagos/datatable/transferencias',
method: 'GET',
},
order: [[7, 'desc']], // Ordena por fecha por defecto
columns: [
{ data: 'client', name: 'client', orderable: true },
{ data: 'transfer_id', name: 'transfer_id', orderable: true },
{ data: 'status', name: 'status', orderable: true },
{ data: 'order_id', name: 'payment.orderId', orderable: true },
{ data: 'amount_cents', name: 'amountCents', orderable: true },
{ data: 'amount_cents_refund', name: 'amountCentsRefund', orderable: true },
{ data: 'created_at', name: 'createdAt', orderable: true },
{ data: 'processed_at', name: 'processedAt', orderable: true },
{ data: 'actions', name: 'actions', orderable: false, searchable: false }
],
columnDefs: [{ targets: -1, orderable: false, searchable: false }]
});
// Fila de filtros = segunda fila del thead (index 1)
$('#pagos-redsys-datatable thead tr:eq(1) th').each(function (colIdx) {
const input = $(this).find('input');
if (input.length === 0) return; // columnas sin filtro
input.on('keyup change', function () {
const value = this.value;
if (table.column(colIdx).search() !== value) {
table.column(colIdx).search(value).draw();
}
});
});
function showSwal(title, text, icon) {
Swal.fire({
title: title,
text: text,
icon: icon,
buttonsStyling: false,
confirmButtonText: window.languageBundle['app.aceptar'] || 'Aceptar',
customClass: {
confirmButton: 'btn btn-secondary',
}
});
}
});

View File

@ -8,8 +8,6 @@
<th:block layout:fragment="pagecss">
<link th:href="@{/assets/libs/datatables/dataTables.bootstrap5.min.css}" rel="stylesheet" />
</th:block>
<th:block layout:fragment="pagecss">
</th:block>
</head>
<body>
@ -68,11 +66,12 @@
</div>
<div class="tab-pane" id="arrow-transferencias" role="tabpanel">
<!---
<div></div>
<div
th:insert="~{imprimelibros/presupuestos/presupuesto-list-items/tabla-anonimos :: tabla-anonimos}">
th:insert="~{imprimelibros/pagos/tabla-transferencias :: tabla-transferencias}">
</div>
-->
</div>
</div>

View File

@ -6,13 +6,15 @@
<th scope="col" th:text="#{pagos.table.redsys.id}">Cliente</th>
<th scope="col" th:text="#{pagos.table.pedido.id}">Pedido</th>
<th scope="col" th:text="#{pagos.table.cantidad}">Cantidad</th>
<th scope="col" th:text="#{pagos.table.devuelto}">Devolución</th>
<th scope="col" th:text="#{pagos.table.fecha}">Fecha</th>
<th scope="col" th:text="#{pagos.table.acciones}">Acciones</th>
</tr>
<tr>
<th><input type="text" class="form-control form-control-sm presupuesto-filter" data-col="user.fullName" /></th>
<th><input type="text" class="form-control form-control-sm presupuesto-filter" data-col="user.fullName" /></th>
<th><input type="text" class="form-control form-control-sm presupuesto-filter" data-col="payments.gateway_order_id" /></th>
<th><input type="text" class="form-control form-control-sm redsys-filter" /></th>
<th><input type="text" class="form-control form-control-sm redsys-filter" /></th>
<th><input type="text" class="form-control form-control-sm redsys-filter" /></th>
<th></th>
<th></th>
<th></th>
<th></th> <!-- Acciones (sin filtro) -->

View File

@ -0,0 +1,30 @@
<div th:fragment="tabla-transferencias">
<table id="pagos-transferencias-datatable" class="table table-striped table-nowrap responsive w-100">
<thead>
<tr>
<th scope="col" th:text="#{pagos.table.cliente.nombre}"></th>
<th scope="col" th:text="#{pagos.table.concepto-transferencia}"></th>
<th scope="col" th:text="#{pagos.table.estado-transferencia}"></th>
<th scope="col" th:text="#{pagos.table.pedido.id}"></th>
<th scope="col" th:text="#{pagos.table.cantidad}"></th>
<th scope="col" th:text="#{pagos.table.devuelto}"></th>
<th scope="col" th:text="#{pagos.table.fecha-created}"></th>
<th scope="col" th:text="#{pagos.table.fecha-procesed}"></th>
<th scope="col" th:text="#{pagos.table.acciones}">Acciones</th>
</tr>
<tr>
<th><input type="text" class="form-control form-control-sm redsys-filter" /></th>
<th><input type="text" class="form-control form-control-sm redsys-filter" /></th>
<th><input type="text" class="form-control form-control-sm redsys-filter" /></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th> <!-- Acciones (sin filtro) -->
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>

View File

@ -17,7 +17,8 @@
<th scope="col" th:text="#{presupuesto.tabla.updated-at}">Actualizado el</th>
<th scope="col" th:text="#{presupuesto.tabla.acciones}">Acciones</th>
</tr>
<th><input type="text" class="form-control form-control-sm presupuesto-filter" data-col="id" /></th>
<tr>
<th><input type="text" class="form-control form-control-sm presupuesto-filter" data-col="id" /></th>
<th><input type="text" class="form-control form-control-sm presupuesto-filter" data-col="titulo" /></th>
</th>
<th>
@ -48,7 +49,8 @@
<option value="colorhq" th:text="#{presupuesto.color-premium}">Color HQ</option>
</select>
</th>
<th><input type="text" class="form-control form-control-sm presupuesto-filter" data-col="selectedTirada" /></th>
<th><input type="text" class="form-control form-control-sm presupuesto-filter"
data-col="selectedTirada" /></th>
<th><input type="text" class="form-control form-control-sm presupuesto-filter" data-col="paginas" />
</th>
<th>
@ -68,7 +70,7 @@
</th>
<th></th> <!-- Actualizado el (sin filtro) -->
<th></th> <!-- Acciones (sin filtro) -->
</tr>
</tr>
</thead>
<tbody>
</tbody>