mirror of
https://git.imnavajas.es/jjimenez/erp-imprimelibros.git
synced 2026-01-13 08:58:48 +00:00
intentando meter los ficheros redsys
This commit is contained in:
@ -0,0 +1,89 @@
|
||||
package com.imprimelibros.erp.redsys;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class RedsysService {
|
||||
|
||||
@Value("${redsys.merchant-code}") private String merchantCode;
|
||||
@Value("${redsys.terminal}") private String terminal;
|
||||
@Value("${redsys.currency}") private String currency;
|
||||
@Value("${redsys.transaction-type}") private String txType;
|
||||
@Value("${redsys.secret-key}") private String secretKey;
|
||||
@Value("${redsys.urls.ok}") private String urlOk;
|
||||
@Value("${redsys.urls.ko}") private String urlKo;
|
||||
@Value("${redsys.urls.notify}") private String urlNotify;
|
||||
@Value("${redsys.environment}") private String env;
|
||||
|
||||
public record PaymentRequest(String order, long amountCents, String description) {}
|
||||
|
||||
public record FormPayload(String action, String signatureVersion, String merchantParameters, String signature) {}
|
||||
|
||||
public FormPayload buildRedirectForm(PaymentRequest req) {
|
||||
// RedsysAPI proviene del JAR oficial
|
||||
com.redsys.api.RedsysAPI api = new com.redsys.api.RedsysAPI();
|
||||
|
||||
Map<String, String> mp = new HashMap<>();
|
||||
mp.put("DS_MERCHANT_AMOUNT", String.valueOf(req.amountCents()));
|
||||
mp.put("DS_MERCHANT_ORDER", req.order());
|
||||
mp.put("DS_MERCHANT_MERCHANTCODE", merchantCode);
|
||||
mp.put("DS_MERCHANT_CURRENCY", currency);
|
||||
mp.put("DS_MERCHANT_TRANSACTIONTYPE", txType);
|
||||
mp.put("DS_MERCHANT_TERMINAL", terminal);
|
||||
mp.put("DS_MERCHANT_MERCHANTNAME", "Tu Comercio");
|
||||
mp.put("DS_MERCHANT_PRODUCTDESCRIPTION", req.description());
|
||||
mp.put("DS_MERCHANT_URLOK", urlOk);
|
||||
mp.put("DS_MERCHANT_URLKO", urlKo);
|
||||
mp.put("DS_MERCHANT_MERCHANTURL", urlNotify);
|
||||
|
||||
String merchantParameters = api.createMerchantParameters(mp);
|
||||
String signature = api.createMerchantSignature(secretKey);
|
||||
|
||||
String action = "test".equalsIgnoreCase(env)
|
||||
? "https://sis-t.redsys.es:25443/sis/realizarPago"
|
||||
: "https://sis.redsys.es/sis/realizarPago";
|
||||
|
||||
return new FormPayload(action, "HMAC_SHA256_V1", merchantParameters, signature);
|
||||
}
|
||||
|
||||
// Validación de la notificación on-line (webhook).
|
||||
public RedsysNotification validateAndParse(String dsSignature, String dsSignatureVersion, String dsMerchantParametersB64) {
|
||||
com.redsys.api.RedsysAPI api = new com.redsys.api.RedsysAPI();
|
||||
|
||||
// 1) Validar firma
|
||||
String calc = api.createMerchantSignatureNotif(secretKey, dsMerchantParametersB64);
|
||||
if (!Objects.equals(calc, dsSignature)) {
|
||||
throw new IllegalArgumentException("Firma Redsys no válida");
|
||||
}
|
||||
|
||||
// 2) Decodificar parámetros
|
||||
String json = api.decodeMerchantParameters(dsMerchantParametersB64);
|
||||
Map<String, Object> params = new com.fasterxml.jackson.databind.ObjectMapper()
|
||||
.readValue(json, new com.fasterxml.jackson.core.type.TypeReference<>() {});
|
||||
// Campos típicos: Ds_Order, Ds_Amount, Ds_Currency, Ds_Response, etc.
|
||||
return RedsysNotification.from(params);
|
||||
}
|
||||
|
||||
public static record RedsysNotification(String order, String dsResponse, long amountCents, String currency) {
|
||||
static RedsysNotification from(Map<String, Object> p) {
|
||||
String order = (String) p.get("Ds_Order");
|
||||
String resp = String.valueOf(p.get("Ds_Response"));
|
||||
long amount = Long.parseLong((String) p.get("Ds_Amount"));
|
||||
String curr = String.valueOf(p.get("Ds_Currency"));
|
||||
return new RedsysNotification(order, resp, amount, curr);
|
||||
}
|
||||
// Éxito si 0–99.
|
||||
public boolean authorized() {
|
||||
try {
|
||||
int r = Integer.parseInt(dsResponse);
|
||||
return r >= 0 && r <= 99;
|
||||
} catch (Exception e) { return false; }
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user