working web based and intent
This commit is contained in:
@@ -1081,18 +1081,25 @@ public class BillerMapperHelper {
|
||||
}
|
||||
|
||||
public void savePaymentStartRecord(PaymentStartUserRequest request, PaymentStartApiResponse apiResponse,
|
||||
String txnId, String orderNo) {
|
||||
String txnId, String orderNo, String requestJson, String rawResponse, String responseType) {
|
||||
PaymentStartRecord record = new PaymentStartRecord();
|
||||
record.setOrderNo(orderNo);
|
||||
record.setRequestId(request.getReqId());
|
||||
record.setClientMid(request.getMid());
|
||||
record.setClientUserId(request.getClientUserId());
|
||||
record.setInternalTxnId(txnId);
|
||||
record.setAmount(apiResponse.getResponseData().getAmount());
|
||||
record.setMid(apiResponse.getResponseData().getMid());
|
||||
record.setTxnId(apiResponse.getResponseData().getTxnId());
|
||||
record.setQrString(apiResponse.getResponseData().getQrString());
|
||||
record.setCreatedOn(apiResponse.getResponseData().getCreatedOn());
|
||||
if (apiResponse != null && apiResponse.getResponseData() != null) {
|
||||
record.setAmount(apiResponse.getResponseData().getAmount());
|
||||
record.setMid(apiResponse.getResponseData().getMid());
|
||||
record.setTxnId(apiResponse.getResponseData().getTxnId());
|
||||
record.setQrString(apiResponse.getResponseData().getQrString());
|
||||
record.setCreatedOn(apiResponse.getResponseData().getCreatedOn());
|
||||
}
|
||||
|
||||
// store the original request body and raw response
|
||||
record.setRequestBody(requestJson);
|
||||
record.setResponseBody(rawResponse);
|
||||
record.setResponseType(responseType);
|
||||
|
||||
paymentStartRecordRepository.save(record);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
package com.billpaymentservice.bbps.util;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import java.util.zip.CRC32;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.spec.IvParameterSpec;
|
||||
@@ -18,12 +22,9 @@ public class EncryptionUtil {
|
||||
/**
|
||||
* Encrypts plaintext using AES/CBC/PKCS5Padding.
|
||||
* If the provided key length is not 16/24/32 bytes it will be adjusted to 16 bytes (padded/truncated).
|
||||
* NOTE: This follows the algorithm snippet provided by the user but pads/truncates the key to avoid
|
||||
* common runtime errors when key lengths are incorrect.
|
||||
*/
|
||||
public String encryptMData(String plaintext, String encryptKey) {
|
||||
try {
|
||||
// Ensure key length is 16 bytes (AES-128). If callers provide a longer key this will truncate.
|
||||
byte[] keyBytes = normalizeKey(encryptKey);
|
||||
SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");
|
||||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
||||
@@ -61,4 +62,56 @@ public class EncryptionUtil {
|
||||
}
|
||||
return normalized;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrypts data encrypted by encryptMData: Base64(iv + ciphertext) with AES/CBC/PKCS5Padding
|
||||
*/
|
||||
public String decryptMData(String base64Payload, String encryptKey) {
|
||||
if (base64Payload == null) return null;
|
||||
try {
|
||||
byte[] combined = Base64.getDecoder().decode(base64Payload);
|
||||
// IV is first 16 bytes for AES
|
||||
if (combined.length < 16) {
|
||||
LOGGER.warn("Encrypted payload too short to contain IV");
|
||||
return null;
|
||||
}
|
||||
byte[] iv = new byte[16];
|
||||
System.arraycopy(combined, 0, iv, 0, 16);
|
||||
byte[] cipherText = new byte[combined.length - 16];
|
||||
System.arraycopy(combined, 16, cipherText, 0, cipherText.length);
|
||||
|
||||
byte[] keyBytes = normalizeKey(encryptKey);
|
||||
SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");
|
||||
|
||||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
||||
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(iv));
|
||||
|
||||
byte[] plain = cipher.doFinal(cipherText);
|
||||
return new String(plain, StandardCharsets.UTF_8);
|
||||
} catch (Exception ex) {
|
||||
LOGGER.error("Error decrypting payload: {}", ex.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute CRC32 checksum value for the provided input string and return the numeric value as String
|
||||
*/
|
||||
public String getChecksumCRC32(String input) {
|
||||
if (input == null) input = "";
|
||||
try (InputStream is = new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8))) {
|
||||
CRC32 crc = new CRC32();
|
||||
byte[] buffer = new byte[1024];
|
||||
int len;
|
||||
while ((len = is.read(buffer)) != -1) {
|
||||
crc.update(buffer, 0, len);
|
||||
}
|
||||
long value = crc.getValue();
|
||||
LOGGER.debug("Computed CRC32: {} for input: {}", value, input);
|
||||
return String.valueOf(value);
|
||||
} catch (IOException e) {
|
||||
LOGGER.error("Error computing CRC32: {}", e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user