This commit is contained in:
Mohd-Arham
2025-11-04 09:53:52 +05:30
parent 89a131fa84
commit 3de3f8f60c
19 changed files with 537 additions and 134 deletions
@@ -4,24 +4,24 @@ import java.util.function.Function;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.billpaymentservice.bbps.dto.ResponseModel;
import com.billpaymentservice.bbps.dto.request.MerchantRequestModel;
import com.billpaymentservice.bbps.entites.BBPSMerchantValidation;
import com.billpaymentservice.bbps.entites.BBPSMastersData;
import com.billpaymentservice.bbps.exception.CustomException;
import com.billpaymentservice.bbps.repository.MerchantRepository;
import com.billpaymentservice.bbps.repository.BBPSMasterDataRepo;
@Component
public class Helper {
private static final Logger LOGGER = LoggerFactory.getLogger(Helper.class);
@Autowired
private MerchantRepository merchantRegisterRepo;
private final BBPSMasterDataRepo bbpsMasterDataRepo;
// Constructor Injection
public Helper(BBPSMasterDataRepo bbpsMasterDataRepo) {
this.bbpsMasterDataRepo = bbpsMasterDataRepo;
}
/**
* Validates whether the merchant exists and is active.
@@ -33,82 +33,119 @@ public class Helper {
*
*
*/
public <T> void startPolling(
T requestObject, // Request object (any type)
Function<T, ResponseModel> apiCallFunction, // Function to call API
int intervalSeconds, // gap between two polls
int maxDurationSeconds // total polling time
public <T> void startPolling(T requestObject, // Request object (any type)
Function<T, ResponseModel> apiCallFunction, // Function to call API
int intervalSeconds, // gap between two polls
int maxDurationSeconds // total polling time
) {
new Thread(() -> {
try {
int maxAttempts = maxDurationSeconds / intervalSeconds;
new Thread(() -> {
try {
int maxAttempts = maxDurationSeconds / intervalSeconds;
for (int i = 0; i < maxAttempts; i++) {
LOGGER.info("⏳ Polling attempt {}...", i + 1);
for (int i = 0; i < maxAttempts; i++) {
LOGGER.info("⏳ Polling attempt {}...", i + 1);
ResponseModel response = apiCallFunction.apply(requestObject);
LOGGER.info("📥 Poll response: {}", response);
ResponseModel response = apiCallFunction.apply(requestObject);
LOGGER.info("📥 Poll response: {}", response);
if ("SUCCESS".equalsIgnoreCase(response.getStatus())) {
LOGGER.info("✅ Polling successful, stopping early.");
break;
}
if ("SUCCESS".equalsIgnoreCase(response.getStatus())) {
LOGGER.info("✅ Polling successful, stopping early.");
break;
}
Thread.sleep(intervalSeconds * 1000L);
}
} catch (Exception e) {
LOGGER.error("❌ Error in polling thread: {}", e.getMessage(), e);
}
}).start();
Thread.sleep(intervalSeconds * 1000L);
}
} catch (Exception e) {
LOGGER.error("❌ Error in polling thread: {}", e.getMessage(), e);
}
}).start();
}
public boolean isMerchantValidAndActive(MerchantRequestModel requestModel) {
if (requestModel == null || requestModel.getMid() == null) {
// public boolean isMerchantValidAndActive(MerchantRequestModel requestModel) {
//
// if (requestModel == null) {
// LOGGER.warn("MerchantRequestModel or MID is null");
// throw new CustomException("Invalid request: MID is required");
// }
//
// // Fetch merchant details
// BBPSMastersData merchantValidation = bbpsMasterDataRepo.findByMid(requestModel.getMid());
//
// if (merchantValidation == null) {
// LOGGER.warn("Merchant not found for MID: {}", requestModel.getMid());
// throw new CustomException("Merchant not found");
// }
//
// if (merchantValidation.getApiKey() == null && merchantValidation.getSecreteKey() == null) {
//
// throw new CustomException("Merchant inactive");
// }
//
// if (!Boolean.TRUE.equals(merchantValidation.getIsActive())) {
// LOGGER.warn("Merchant inactive for MID: {}", requestModel.getMid());
// throw new CustomException("Merchant inactive");
// }
//
// LOGGER.info("Merchant is valid and active for MID: {}", requestModel.getMid());
// return true;
// }
// public String validateMerchant(String mid) {
//
// if (mid == null) {
// LOGGER.warn("MerchantRequestModel or MID is null");
// return "Mid is required";
// }
//
// // Fetch merchant details
// BBPSMastersData merchantValidation = bbpsMasterDataRepo.findByMid(mid);
//
// if (merchantValidation == null) {
// LOGGER.warn("Merchant not found for MID: {}", mid);
// return "Merchant not found by id";
// }
//
// if (merchantValidation.getApiKey() == null && merchantValidation.getSecreteKey() == null) {
// LOGGER.warn("Merchant BBPS service is not active MID: {}", mid);
// return "Merchant BBPS Service is not active by mid";
// }
//
// if (!Boolean.TRUE.equals(merchantValidation.getIsActive())) {
// LOGGER.warn("Merchant inactive for MID: {}", mid);
// return "Merchant is inactive";
// }
//
// LOGGER.info("Merchant is valid and active for MID: {}", mid);
// return "Valid"; // Success message
// }
public void validateMerchant(String mid) {
if (mid == null || mid == "") {
LOGGER.warn("MerchantRequestModel or MID is null");
throw new CustomException("Invalid request: MID is required");
throw new CustomException("Mid is required", "FAILED", BBPSConstant.BADREQUEST, null);
}
// Fetch merchant details
BBPSMerchantValidation merchantValidation = merchantRegisterRepo.findByMid(requestModel.getMid());
BBPSMastersData merchantValidation = bbpsMasterDataRepo.findByMid(mid);
if (merchantValidation == null) {
LOGGER.warn("Merchant not found for MID: {}", requestModel.getMid());
throw new CustomException("Merchant not found");
LOGGER.warn("Merchant not found for MID: {}", mid);
throw new CustomException("Merchant not found by id", "FAILED", BBPSConstant.BADREQUEST, null);
}
if (!Boolean.TRUE.equals(merchantValidation.isActive())) {
LOGGER.warn("Merchant inactive for MID: {}", requestModel.getMid());
throw new CustomException("Merchant inactive");
if (merchantValidation.getApiKey() == null && merchantValidation.getSecreteKey() == null) {
LOGGER.warn("Merchant BBPS service is not active MID: {}", mid);
throw new CustomException("Merchant BBPS Service is not active by mid", "FAILED", BBPSConstant.BADREQUEST,
null);
}
LOGGER.info("Merchant is valid and active for MID: {}", requestModel.getMid());
return true;
}
public String validateMerchant(Object requestModel) {
if (!Boolean.TRUE.equals(merchantValidation.getIsActive())) {
LOGGER.warn("Merchant inactive for MID: {}", mid);
throw new CustomException("Merchant is inactive", "FAILED", BBPSConstant.BADREQUEST, null);
}
// if (requestModel == null || requestModel.getMid() == null) {
// LOGGER.warn("MerchantRequestModel or MID is null");
// return "Invalid request: MID is required";
// }
//
// // Fetch merchant details
// BBPSMerchantValidation merchant = merchantRegisterRepo.findByMid(requestModel.getMid());
//
// if (merchant == null) {
// LOGGER.warn("Merchant not found for MID: {}", requestModel.getMid());
// return "Merchant not found";
// }
//
// if (!Boolean.TRUE.equals(merchant.isActive())) {
// LOGGER.warn("Merchant inactive for MID: {}", requestModel.getMid());
// return "Merchant inactive";
// }
//
// LOGGER.info("Merchant is valid and active for MID: {}", requestModel.getMid());
return "Valid"; // Success message
LOGGER.info("Merchant is valid and active for MID: {}", mid);
// Agar exception nahi throw hua, merchant valid hai
}
}