[ADD] BBVA CAMBIOS

parent 04a2dac9
...@@ -4,38 +4,26 @@ import org.json.JSONObject; ...@@ -4,38 +4,26 @@ import org.json.JSONObject;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import web.multitask.trismegistoservices.services.BBVA.bbvaService; import web.multitask.trismegistoservices.services.BBVA.bbvaService;
import web.multitask.trismegistoservices.singleton.bbvaSingleton;
@RestController @RestController
@CrossOrigin(origins = "*") @CrossOrigin(origins = "*")
@RequestMapping("bbva") @RequestMapping("bbva")
public class bbvaRest { public class bbvaRest {
private final bbvaSingleton singleton; @PostMapping("/private/generate/qr")
public ResponseEntity<?> generateQR(@RequestBody String json) {
public bbvaRest(bbvaSingleton singleton) { bbvaService service = new bbvaService();
this.singleton = singleton; JSONObject jsonRequest = new JSONObject(json);
JSONObject jsonResponse = service.generalService(jsonRequest);
return ResponseEntity.ok(jsonResponse.toMap());
} }
@PostMapping("/private/prepareRequest") @GetMapping("/private/status/qr/{id}")
public ResponseEntity<?> getHeader(@RequestBody String json) { public ResponseEntity<?> getStatus( @PathVariable String id) {
bbvaService service = new bbvaService(); bbvaService service = new bbvaService();
JSONObject response = service.getToken(json); JSONObject jsonResponse = service.getStatus(id);
String token = response.optString("access_token"); return ResponseEntity.ok(jsonResponse.toMap());
if(token.isEmpty()){
return ResponseEntity.internalServerError().body(response.put("status",false).put("token","").toMap());
}else{
singleton.setToken(token);
String digest = service.bodyIntoBase64AfterSHA512(json);
singleton.setDigest(digest);
String txt = service.txtToSign(json ,digest);
String signature = service.createSignature(txt);
singleton.setSignature(signature);
System.out.println("token = "+singleton.getToken());
System.out.println("digest = "+singleton.getDigest());
System.out.println("signature = "+singleton.getSignature());
return ResponseEntity.ok(service.peticionHTTP(singleton.getToken(),singleton.getDigest(),singleton.getSignature(),json).toMap());
}
} }
} }
package web.multitask.trismegistoservices.services.BBVA; package web.multitask.trismegistoservices.services.BBVA;
import okhttp3.*; import org.json.JSONArray;
import okio.Buffer;
import org.json.JSONObject; import org.json.JSONObject;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import web.multitask.trismegistoservices.utils.CommonUtils; import java.util.Objects;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.MessageDigest;
import java.security.PrivateKey;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;
import java.util.zip.GZIPInputStream;
@Service @Service
public class bbvaService { public class bbvaService {
String client_secret = "513375e5181e462fb3ec7a00dfc26dc7"; bbvaUtils utils = new bbvaUtils();
String client_id = "174895823298";
String grant_type = "client_credentials";
String globalDate = CommonUtils.getCurrentDateRFC1123();
public JSONObject getToken(String json) {
try {
JSONObject obj = new JSONObject(json);
obj.put("client_secret",client_secret);
obj.put("client_id",client_id);
obj.put("grant_type", grant_type);
obj.remove("body");
String host = obj.getString("host");
String formBody = CommonUtils.jsonToUrlEncoded(obj);
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://" + host + "/auth/oauth/v2/token")
.post(RequestBody.create(formBody, MediaType.parse("application/x-www-form-urlencoded")))
.build();
Call call = client.newCall(request);
Response response = call.execute();
assert response.body() != null;
String bodyResult = response.body().string();
response.close();
return new JSONObject(bodyResult);
} catch (Exception e) {
System.out.println(e.getMessage());
return new JSONObject().put("status", false).put("message", e.getMessage());
}
}
public String bodyIntoBase64AfterSHA512(String json) { public JSONObject generalService(JSONObject json){
try { JSONObject jsonBillResponse = queryBill(json);
JSONObject jsonObject = new JSONObject(json); if (jsonBillResponse.optBoolean("status",false)){
String body = jsonObject.optString("body", ""); return generateQR(jsonBillResponse);
byte[] bodyBytes = body.getBytes(StandardCharsets.UTF_8); }else{
MessageDigest digest = MessageDigest.getInstance("SHA-512"); return jsonBillResponse;
byte[] hashedBytes = digest.digest(bodyBytes);
return Base64.getEncoder().encodeToString(hashedBytes);
} catch (Exception e) {
System.out.println(e.getMessage());
return null;
} }
} }
public String txtToSign(String json, String digest) { private JSONObject queryBill(JSONObject json){
JSONObject obj = new JSONObject(json);
String host = obj.getString("host");
String endpoint = obj.getString("endpoint");
String method = obj.getString("method");
return "(request-target):" + method.toLowerCase() + " " + endpoint + "\n" JSONObject returnMessage = new JSONObject();
+ "host:" + host + "\n"
+ "date:" + globalDate + "\n"
+ "digest:SHA-512=" + digest;
}
public String createSignature(String txt) { int serviceID = json.optInt("service_id",0);
PrivateKey privateKey = loadPrivateKey("/tokens/key.p8");
return signContent(txt, privateKey);
}
public PrivateKey loadPrivateKey(String path) { String actualServiceID;
try (InputStream in = bbvaService.class.getResourceAsStream(path)) { switch (serviceID) {
assert in != null; case 1:
String keyString = CommonUtils.readInputStream(in); actualServiceID = "00110010017221100";
return loadPrivateKeyFromString(keyString); break;
} catch (Exception e) { case 2:
System.out.println(e.getMessage()); actualServiceID = "00110010017208000";
return null; break;
} case 3:
} actualServiceID = "00110010018991200";
break;
default:
actualServiceID = "0";
break;
}
String documentNumber = json.getString("document_number");
String endpoint = "/pe/bill-payments/v1/services/"+actualServiceID+"/bills";
String queryParamater = "?billReference.value="+documentNumber;
String method = "get";
JSONObject jsonRequest = new JSONObject();
jsonRequest.put("endpoint",endpoint);
jsonRequest.put("extra",queryParamater);
jsonRequest.put("method",method);
String token = utils.getToken();
String digest = utils.hashDigest(jsonRequest.toString());
String txt = utils.txtToSign(jsonRequest ,digest);
String signature = utils.createSignature(txt);
JSONObject jsonResponse = utils.peticionHTTP(token,digest,signature,jsonRequest);
JSONArray dataFound = jsonResponse.optJSONArray("data", new JSONArray());
JSONObject firstElement = dataFound.optJSONObject(0, new JSONObject());
if(firstElement.isEmpty()){
returnMessage.put("message", "No se encontró ninguna deuda");
returnMessage.put("status", false);
return returnMessage;
}
int month = json.optInt("month",0);
int year = json.optInt("year",0);
int amount = json.optInt("amount",0);
String monthString = utils.numberToMonth(month);
String numberElement = firstElement.optString("number","");
if(!(numberElement.contains(monthString) && numberElement.contains(String.valueOf(year)))){
returnMessage.put("message", "La deuda viegente no coincide con el "+monthString+" de "+year);
returnMessage.put("status", false);
return returnMessage;
}
String maturityDate = json.optString("maturity_date","");
String maturityDateElement = firstElement.optString("maturityDate","");
if(!Objects.equals(maturityDateElement, maturityDate)){
returnMessage.put("message", "La deuda no tiene la fecha de vencimiento correcta - "+maturityDateElement);
returnMessage.put("status", false);
return returnMessage;
}
JSONArray amounts = firstElement.optJSONArray("amounts", new JSONArray());
JSONObject firstAmount = amounts.optJSONObject(0, new JSONObject());
JSONObject amountValue = firstAmount.optJSONObject("amountValue", new JSONObject());
int amountElement = amountValue.optInt("amount",0);
if(amountElement != amount){
returnMessage.put("message", "El monto de la deuda no es correcto - "+amountElement);
returnMessage.put("status", false);
return returnMessage;
}
returnMessage.put("serviceId", actualServiceID);
returnMessage.put("documentNumber", documentNumber);
returnMessage.put("number", numberElement);
returnMessage.put("amount", amountElement);
returnMessage.put("maturityDate", maturityDateElement);
returnMessage.put("status", true);
return returnMessage;
}
public JSONObject generateQR(JSONObject json){
JSONObject returnMessage = new JSONObject();
String endpoint = "/pe/request-to-pay/v1/qr";
String method = "post";
JSONObject body = new JSONObject();
String serviceId = json.getString("serviceId");
String billId = json.getString("documentNumber");
String fullNumber = json.getString("number");
String billNumber = fullNumber.length() > 40 ? fullNumber.substring(0, 40) : fullNumber;
String billDocumentNumber = json.getString("documentNumber");
String billDocumentType = "DNI";
int amountsAmount = json.getInt("amount");
String amountsCurrency = "PEN";
String billMaturityDate = json.getString("maturityDate");
String originContractId = "00110661000100081541";
String productTypeId = "QR_CODE";
String additionalPaymentFieldId = "0";
String additionalPaymentFieldValue = json.getString("documentNumber");
String serviceProvider = "ASOCIACION SACO OLIVEROS";
String partnerId = "0007";
String partnerEmail = "sistemas@sacooliveros.edu.pe";
String partnerPhoneNumber = "993521358";
String partnerTypeId = "AGENT";
body.put("service", new JSONObject().put("id", serviceId));
body.put("bills", new JSONArray().put(new JSONObject()
.put("id", billId)
.put("number", billNumber)
.put("documentNumber", billDocumentNumber)
.put("documentType", billDocumentType)
.put("amounts", new JSONArray().put(new JSONObject().put("amount", amountsAmount).put("currency", amountsCurrency)))
.put("maturityDate", billMaturityDate)));
body.put("origin", new JSONObject()
.put("contractId", originContractId)
.put("productType", new JSONObject().put("id", productTypeId)));
body.put("additionalFields", new JSONArray().put(new JSONObject()
.put("additionalPaymentField", new JSONObject().put("id", additionalPaymentFieldId))
.put("value", additionalPaymentFieldValue)));
body.put("serviceProvider", serviceProvider);
body.put("partner", new JSONObject()
.put("id", partnerId)
.put("email", partnerEmail)
.put("phoneNumber", partnerPhoneNumber)
.put("partnerType", new JSONObject().put("id", partnerTypeId)));
private static PrivateKey loadPrivateKeyFromString(String keyString) throws Exception { JSONObject jsonRequest = new JSONObject()
keyString = keyString.replace("-----BEGIN RSA PRIVATE KEY-----", "") .put("body", body)
.replace("-----END RSA PRIVATE KEY-----", "") .put("method", method)
.trim(); .put("endpoint", endpoint);
byte[] keyBytes = Base64.getMimeDecoder().decode(keyString); String token = utils.getToken();
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes); String digest = utils.hashDigest(jsonRequest.toString());
KeyFactory keyFactory = KeyFactory.getInstance("RSA"); String txt = utils.txtToSign(jsonRequest ,digest);
return keyFactory.generatePrivate(spec); String signature = utils.createSignature(txt);
}
public String signContent(String content, PrivateKey privateKey) { JSONObject jsonResponse = utils.peticionHTTP(token, digest, signature, jsonRequest);
try {
Signature signature = Signature.getInstance("SHA512withRSA"); JSONObject qrCode = jsonResponse.optJSONObject("qrCode", new JSONObject());
signature.initSign(privateKey);
signature.update(content.getBytes()); if(!qrCode.isEmpty()){
byte[] signedBytes = signature.sign(); returnMessage.put("hashCode", qrCode.get("hashCode"));
return Base64.getEncoder().encodeToString(signedBytes); returnMessage.put("id", qrCode.get("id"));
} catch (Exception e) { returnMessage.put("message", "Well-generated QRCode");
System.out.println(e.getMessage()); returnMessage.put("status", true);
return null; }else{
} returnMessage.put("message", jsonResponse.get("message"));
returnMessage.put("status", false);
} }
return returnMessage;
}
public JSONObject peticionHTTP(String token,String digest,String signature,String json) { public JSONObject getStatus(String id){
try {
JSONObject jsonBody = new JSONObject(json); JSONObject returnMessage = new JSONObject();
String host = jsonBody.getString("host");
String endpoint = jsonBody.getString("endpoint");
String extra = jsonBody.optString("extra", "");
String method = jsonBody.optString("method", "get").toLowerCase();
OkHttpClient client = new OkHttpClient(); String endpoint = "/pe/request-to-pay/v1/qrs/"+id+"/status";
MediaType mediaType = MediaType.parse("application/json; charset=utf-8"); String method = "get";
JSONObject jsonRequest = new JSONObject()
.put("method", method)
.put("endpoint", endpoint);
RequestBody requestBody = null; String token = utils.getToken();
if ("post".equals(method)) { String digest = utils.hashDigest(jsonRequest.toString());
JSONObject body = jsonBody.optJSONObject("body", new JSONObject()); String txt = utils.txtToSign(jsonRequest ,digest);
requestBody = RequestBody.create(body.toString(), mediaType); String signature = utils.createSignature(txt);
}
Request.Builder builder = new Request.Builder() JSONObject jsonResponse = utils.peticionHTTP(token, digest, signature, jsonRequest);
.url("https://" + host + endpoint + extra)
.addHeader("Content-Type", "application/json")
.addHeader("Accept-Encoding", "gzip")
.addHeader("Authorization", "Bearer " + token)
.addHeader("Digest", "SHA-512=" + digest)
.addHeader("Signature", "algorithm=\"rsa-sha512\",headers=\"(request-target) host date digest\",signature=\"" + signature + "\"")
.addHeader("Host", host)
.addHeader("Date", globalDate);
Request request = "post".equals(method) ? builder.post(requestBody).build() : builder.get().build();
StringBuilder requestInfo = new StringBuilder();
requestInfo.append("URL: ").append(request.url()).append("\n");
requestInfo.append("Method: ").append(request.method()).append("\n");
requestInfo.append("Headers:\n");
for (String name : request.headers().names()) {
requestInfo.append(" ").append(name).append(": ").append(request.header(name)).append("\n");
}
if (request.body() != null) { JSONObject data = jsonResponse.optJSONObject("data", new JSONObject());
try {
Buffer buffer = new Buffer();
request.body().writeTo(buffer);
String bodyString = buffer.readUtf8();
requestInfo.append("Body:\n").append(bodyString).append("\n");
} catch (IOException e) {
requestInfo.append("Body: (error reading body)\n");
}
}
System.out.println(requestInfo); if(!data.isEmpty()){
try (Response response = client.newCall(request).execute()) {
System.out.println("x-request-id = " + response.header("x-request-id"));
String bodyResult;
if (response.code() == 204) {
bodyResult = "{}";
} else if (response.body() != null) {
String encoding = response.header("Content-Encoding", "");
bodyResult = "gzip".equalsIgnoreCase(encoding)
? decompressGzip(response.body().byteStream())
: response.body().string();
} else {
bodyResult = "{}";
}
System.out.println(bodyResult); JSONObject billFirstElement = data.optJSONArray("bills", new JSONArray()).getJSONObject(0);
return new JSONObject(bodyResult);
}
} catch (Exception e) { String billId = billFirstElement.getString("id");
System.out.println(e.getMessage()); JSONObject statusBill = billFirstElement.optJSONObject("status", new JSONObject());
return new JSONObject().put("status", false).put("message", e.getMessage()); String nameStatus = statusBill.getString("name");
} String idStatus = statusBill.getString("id");
returnMessage.put("id", id);
returnMessage.put("billId", billId);
returnMessage.put("statusName", nameStatus);
returnMessage.put("statusId", idStatus);
}else{
returnMessage.put("message", "id not found");
returnMessage.put("status", false);
}
return returnMessage;
} }
private String decompressGzip(InputStream compressed) throws IOException {
try (GZIPInputStream gis = new GZIPInputStream(compressed);
BufferedReader reader = new BufferedReader(new InputStreamReader(gis, StandardCharsets.UTF_8))) {
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
return sb.toString();
} catch (Exception e) {
System.out.println(e.getMessage());
return "";
}
}
} }
package web.multitask.trismegistoservices.services.BBVA;
import okhttp3.*;
import okio.Buffer;
import org.json.JSONObject;
import web.multitask.trismegistoservices.utils.CommonUtils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.MessageDigest;
import java.security.PrivateKey;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;
import java.util.zip.GZIPInputStream;
public class bbvaUtils {
String host = "apis.pe.bbvaapimarket.com";
String client_secret = "513375e5181e462fb3ec7a00dfc26dc7";
String client_id = "174895823298";
String grant_type = "client_credentials";
String globalDate = CommonUtils.getCurrentDateRFC1123();
public String getToken() {
final MediaType FORM_URLENCODED = MediaType.parse("application/x-www-form-urlencoded");
try {
JSONObject obj = new JSONObject();
obj.put("client_secret", client_secret);
obj.put("client_id", client_id);
obj.put("grant_type", grant_type);
String formBody = CommonUtils.jsonToUrlEncoded(obj);
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://" + host + "/auth/oauth/v2/token")
.post(RequestBody.create(formBody, FORM_URLENCODED))
.build();
try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) {
System.err.println("HTTP Error: " + response.code() + " - " + response.message());
return null;
}
String responseBody = response.body() != null ? response.body().string() : "";
JSONObject jsonObject = new JSONObject(responseBody);
return jsonObject.optString("access_token", null);
}
} catch (Exception e) {
System.err.println("Error getting token: " + e.getMessage());
return null;
}
}
public String hashDigest(String json) {
try {
JSONObject jsonObject = new JSONObject(json);
String body = jsonObject.optString("body", "");
byte[] bodyBytes = body.getBytes(StandardCharsets.UTF_8);
MessageDigest digest = MessageDigest.getInstance("SHA-512");
byte[] hashedBytes = digest.digest(bodyBytes);
return Base64.getEncoder().encodeToString(hashedBytes);
} catch (Exception e) {
System.err.println("Error generating digest: " + e.getMessage());
return null;
}
}
public String txtToSign(JSONObject json, String digest) {
try{
String endpoint = json.getString("endpoint");
String method = json.getString("method");
return "(request-target):" + method.toLowerCase() + " " + endpoint + "\n"
+ "host:" + host + "\n"
+ "date:" + globalDate + "\n"
+ "digest:SHA-512=" + digest;
}catch (Exception e){
System.err.println("Error generating txtToSign: " + e.getMessage());
return null;
}
}
public String createSignature(String txt) {
PrivateKey privateKey = loadPrivateKey("/tokens/key.p8");
return signContent(txt, privateKey);
}
public PrivateKey loadPrivateKey(String path) {
try (InputStream in = bbvaService.class.getResourceAsStream(path)) {
assert in != null;
String keyString = CommonUtils.readInputStream(in);
return loadPrivateKeyFromString(keyString);
} catch (Exception e) {
System.err.println("Error loading private key: " + e.getMessage());
return null;
}
}
private static PrivateKey loadPrivateKeyFromString(String keyString) throws Exception {
keyString = keyString.replace("-----BEGIN RSA PRIVATE KEY-----", "")
.replace("-----END RSA PRIVATE KEY-----", "")
.trim();
byte[] keyBytes = Base64.getMimeDecoder().decode(keyString);
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return keyFactory.generatePrivate(spec);
}
public String signContent(String content, PrivateKey privateKey) {
try {
Signature signature = Signature.getInstance("SHA512withRSA");
signature.initSign(privateKey);
signature.update(content.getBytes());
byte[] signedBytes = signature.sign();
return Base64.getEncoder().encodeToString(signedBytes);
} catch (Exception e) {
System.err.println("Error signing content: " + e.getMessage());
return null;
}
}
public JSONObject peticionHTTP(String token, String digest, String signature, JSONObject json) {
try {
String endpoint = json.getString("endpoint");
String extra = json.optString("extra", "");
String method = json.optString("method", "get").toLowerCase();
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
RequestBody requestBody = null;
if ("post".equals(method)) {
JSONObject body = json.optJSONObject("body", new JSONObject());
requestBody = RequestBody.create(body.toString(), mediaType);
}
Request.Builder builder = new Request.Builder()
.url("https://" + host + endpoint + extra)
.addHeader("Content-Type", "application/json")
.addHeader("Accept-Encoding", "gzip")
.addHeader("Authorization", "Bearer " + token)
.addHeader("Digest", "SHA-512=" + digest)
.addHeader("Signature", "algorithm=\"rsa-sha512\",headers=\"(request-target) host date digest\",signature=\"" + signature + "\"")
.addHeader("Host", host)
.addHeader("Date", globalDate);
Request request = "post".equals(method) ? builder.post(requestBody).build() : builder.get().build();
StringBuilder requestInfo = new StringBuilder();
requestInfo.append("URL: ").append(request.url()).append("\n");
requestInfo.append("Method: ").append(request.method()).append("\n");
requestInfo.append("Headers:\n");
for (String name : request.headers().names()) {
requestInfo.append(" ").append(name).append(": ").append(request.header(name)).append("\n");
}
if (request.body() != null) {
try {
Buffer buffer = new Buffer();
request.body().writeTo(buffer);
String bodyString = buffer.readUtf8();
requestInfo.append("Body:\n").append(bodyString).append("\n");
} catch (IOException e) {
requestInfo.append("Body: (error reading body)\n");
}
}
System.out.println(requestInfo);
try (Response response = client.newCall(request).execute()) {
System.out.println("x-request-id = " + response.header("x-request-id"));
String bodyResult;
if (response.code() == 204) {
bodyResult = "{}";
} else if (response.body() != null) {
String encoding = response.header("Content-Encoding", "");
bodyResult = "gzip".equalsIgnoreCase(encoding)
? decompressGzip(response.body().byteStream())
: response.body().string();
} else {
bodyResult = "{}";
}
System.out.println(bodyResult);
return new JSONObject(bodyResult);
}
} catch (Exception e) {
System.out.println(e.getMessage());
return new JSONObject().put("status", false).put("message", e.getMessage());
}
}
private String decompressGzip(InputStream compressed) throws IOException {
try (GZIPInputStream gis = new GZIPInputStream(compressed);
BufferedReader reader = new BufferedReader(new InputStreamReader(gis, StandardCharsets.UTF_8))) {
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
return sb.toString();
} catch (Exception e) {
System.out.println(e.getMessage());
return "";
}
}
public String numberToMonth(int number) {
String[] months = {"ENERO", "FEBRERO", "MARZO", "ABRIL", "MAYO", "JUNIO", "JULIO", "AGOSTO", "SETIEMBRE", "OCTUBRE", "NOVIEMBRE", "DICIEMBRE"};
return months[number - 1];
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment