package com.saslpay.datacapture; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.saslpay.config.AppPropertiesConfig; import com.saslpay.config.BeanUtil; import com.saslpay.entities.T24DataEntity; import com.saslpay.entities.TransactionDataEntity; import com.saslpay.impl.T24DataServiceImpl; import com.saslpay.impl.TransactionDataServiceImpl; import org.springframework.beans.factory.annotation.Autowired; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import java.io.FileWriter; import java.io.IOException; import java.util.List; import static org.aspectj.util.LangUtil.isEmpty; public class ExtReversalCapture { @PersistenceContext EntityManager entityManager; @Autowired TransactionDataServiceImpl transactionDataServiceImpl; @Autowired T24DataServiceImpl t24DataServiceImpl; TransactionDataEntity transactionDataEntitySave = new TransactionDataEntity(); T24DataEntity t24DataEntitySave = new T24DataEntity(); List transactionDataEntity; List t24DataEntity; AppPropertiesConfig configClass = BeanUtil.getBean(AppPropertiesConfig.class); String t24DataTable = configClass.getT24DataTable(); String transactionDataTable = configClass.getTransactionDataTable(); String outFolder = configClass.getOutFolder(); String sqlStatement; String errorMessage; String txnCode; String hold; String bankWallet; String customerName; String customerBranch; String drAccount; String crAccount; String txnCurrency; String txnAmount; String txnCharge; String txnNarration; String api; String requestType; String txnRef; String ftRef; String errorMsg; String statusMsg; String txnStatus = ""; String account; Float sumBalance = 0.0F; Float newBalance = 0.0F; Float acBalance = 0.0F; Float fullAmt; // @PostMapping(value ="/extreversal" ,produces = MediaType.APPLICATION_JSON_VALUE) public String extReversal(String request) throws IOException { JsonElement jsonElementAuth = JsonParser.parseString(request); JsonObject jsonObjectAuth = jsonElementAuth.getAsJsonObject(); transactionDataServiceImpl = BeanUtil.getBean(TransactionDataServiceImpl.class); t24DataServiceImpl = BeanUtil.getBean(T24DataServiceImpl.class); entityManager = BeanUtil.getBean(EntityManager.class); try { txnCode = jsonObjectAuth.get("txnCode").getAsString(); api = jsonObjectAuth.get("api").getAsString(); requestType = jsonObjectAuth.get("requestType").getAsString(); ftRef = jsonObjectAuth.get("ourTxnRef").getAsString(); txnRef = jsonObjectAuth.get("otherTxnRef").getAsString(); hold = jsonObjectAuth.get("hold").getAsString(); bankWallet = jsonObjectAuth.get("bankWallet").getAsString(); customerName = jsonObjectAuth.get("customerName").getAsString(); customerBranch = jsonObjectAuth.get("customerBranch").getAsString(); drAccount = jsonObjectAuth.get("drAccount").getAsString(); crAccount = jsonObjectAuth.get("crAccount").getAsString(); txnCurrency = jsonObjectAuth.get("txnCurrency").getAsString(); txnAmount = jsonObjectAuth.get("txnAmount").getAsString(); txnCharge = jsonObjectAuth.get("txnCharge").getAsString(); txnNarration = jsonObjectAuth.get("txnNarration").getAsString(); } catch (NullPointerException e) { errorMessage = e.getLocalizedMessage(); } if (txnRef.isEmpty()){ errorMsg = "Payment Reference Empty"; statusMsg = "99"; return outValue(api, txnCode, requestType, drAccount,crAccount,txnRef,errorMsg,statusMsg); } String transactionDataQuery = "SELECT * FROM " + transactionDataTable + " WHERE txnRef='" + txnRef + "'"; transactionDataEntity = entityManager.createNativeQuery(transactionDataQuery, TransactionDataEntity.class).getResultList(); boolean isTxnEmpty = isEmpty(transactionDataEntity); if (isTxnEmpty){ errorMsg = "Payment Reference Invalid - " + txnRef; statusMsg = "99"; return outValue(api, txnCode, requestType, drAccount,crAccount,txnRef,errorMsg,statusMsg); } crAccount = transactionDataEntity.get(0).getCrAccount(); drAccount = transactionDataEntity.get(0).getDrAccount(); txnCode = transactionDataEntity.get(0).getTxnCode(); api = transactionDataEntity.get(0).getApi(); requestType = transactionDataEntity.get(0).getRequestType(); switch (txnCode){ case "extDebTrf": account = drAccount; sqlStatement = "SELECT * FROM " + t24DataTable +" WHERE accountId='" + account + "'"; t24DataEntity = entityManager.createNativeQuery(sqlStatement,T24DataEntity.class).getResultList(); boolean isDebEmpty = isEmpty(t24DataEntity); if (isDebEmpty){ errorMsg = "Debit Account Number - " + drAccount + " Does Not Exist"; statusMsg = "99"; return outValue(api, txnCode, requestType, drAccount,crAccount,txnRef,errorMsg,statusMsg); } fullAmt = transactionDataEntity.get(0).getTxnAmount() + transactionDataEntity.get(0).getTxnCharge(); acBalance = t24DataEntity.get(0).getAccountBalance(); newBalance = fullAmt + acBalance ; break; case "extCredTrf": account = crAccount; sqlStatement = "SELECT * FROM " + t24DataTable +" WHERE accountId='" + account + "'"; t24DataEntity = entityManager.createNativeQuery(sqlStatement,T24DataEntity.class).getResultList(); boolean isCreEmpty = isEmpty(t24DataEntity); if (isCreEmpty){ errorMsg = "Credit Account Number - " + crAccount + " Does Not Exist"; statusMsg = "99"; return outValue(api, txnCode, requestType, drAccount,crAccount,txnRef,errorMsg,statusMsg); } fullAmt = transactionDataEntity.get(0).getTxnAmount() + transactionDataEntity.get(0).getTxnCharge(); acBalance = t24DataEntity.get(0).getAccountBalance(); newBalance = acBalance - fullAmt; break; } txnStatus = transactionDataEntity.get(0).getTxnStatus(); switch (hold){ case "F": if(txnStatus.equals("hold")){ errorMsg = "Cannot Reverse Transaction in hold - " + txnRef; statusMsg = "99"; return outValue(api, txnCode, requestType, drAccount,crAccount,txnRef,errorMsg,statusMsg); } if(txnStatus.equals("del")){ errorMsg = "Transaction Deleted - " + txnRef; statusMsg = "99"; return outValue(api, txnCode, requestType, drAccount,crAccount,txnRef,errorMsg,statusMsg); } if(txnStatus.equals("rev")){ errorMsg = "Transaction Already Reversed! - " + txnRef; statusMsg = "99"; return outValue(api, txnCode, requestType, drAccount,crAccount,txnRef,errorMsg,statusMsg); } if(txnStatus.equals("revch")){ errorMsg = "Charge Already Reversed, Can Only reverse Amount- " + txnRef; statusMsg = "99"; return outValue(api, txnCode, requestType, drAccount,crAccount,txnRef,errorMsg,statusMsg); } if(txnStatus.equals("revamt")){ errorMsg = "Amount Already Reversed, Can Only reverse Charge - " + txnRef; statusMsg = "99"; return outValue(api, txnCode, requestType, drAccount,crAccount,txnRef,errorMsg,statusMsg); } transactionDataEntitySave.setTxnRef(txnRef); transactionDataEntitySave.setApi(transactionDataEntity.get(0).getApi()); transactionDataEntitySave.setHold(hold); transactionDataEntitySave.setRequestType(transactionDataEntity.get(0).getRequestType()); transactionDataEntitySave.setFtId(transactionDataEntity.get(0).getFtId()); transactionDataEntitySave.setTxnCode(transactionDataEntity.get(0).getTxnCode()); transactionDataEntitySave.setBankWallet(transactionDataEntity.get(0).getBankWallet()); transactionDataEntitySave.setCustomerBranch(transactionDataEntity.get(0).getCustomerBranch()); transactionDataEntitySave.setDrAccount(transactionDataEntity.get(0).getDrAccount()); transactionDataEntitySave.setCrAccount(transactionDataEntity.get(0).getCrAccount()); transactionDataEntitySave.setTxnAmount(transactionDataEntity.get(0).getTxnAmount()); transactionDataEntitySave.setTxnCharge(transactionDataEntity.get(0).getTxnCharge()); transactionDataEntitySave.setTxnCurrency(transactionDataEntity.get(0).getTxnCurrency()); transactionDataEntitySave.setTxnNarration(transactionDataEntity.get(0).getTxnNarration()); transactionDataEntitySave.setTxnStatus("rev"); transactionDataEntitySave.setProcessFlag("Y"); transactionDataEntitySave.setElevyCharge(transactionDataEntity.get(0).getElevyCharge()); transactionDataServiceImpl.save(transactionDataEntitySave); t24DataEntitySave.setAccountId(account); t24DataEntitySave.setAccountBalance(newBalance); t24DataEntitySave.setCategory(t24DataEntity.get(0).getCategory()); t24DataEntitySave.setCustomerNo(t24DataEntity.get(0).getCustomerNo()); t24DataEntitySave.setCustomerName(t24DataEntity.get(0).getCustomerName()); t24DataEntitySave.setCustomerType(t24DataEntity.get(0).getCustomerType()); t24DataEntitySave.setCustomerId(t24DataEntity.get(0).getCustomerId()); t24DataEntitySave.setPhoneNumber(t24DataEntity.get(0).getPhoneNumber()); t24DataEntitySave.setBranch(t24DataEntity.get(0).getBranch()); t24DataEntitySave.setBranchName(t24DataEntity.get(0).getBranchName()); t24DataEntitySave.setDob(t24DataEntity.get(0).getDob()); t24DataEntitySave.setEmailAddress(t24DataEntity.get(0).getEmailAddress()); t24DataEntitySave.setGender(t24DataEntity.get(0).getGender()); t24DataEntitySave.setAcStatus(t24DataEntity.get(0).getAcStatus()); t24DataEntitySave.setMaxDebitBalance(t24DataEntity.get(0).getMaxDebitBalance()); t24DataEntitySave.setMaxCreditBalance(t24DataEntity.get(0).getMaxCreditBalance()); t24DataEntitySave.setDebitAllowed(t24DataEntity.get(0).getDebitAllowed()); t24DataEntitySave.setCreditAllowed(t24DataEntity.get(0).getCreditAllowed()); t24DataEntitySave.setUssdSubscribed(t24DataEntity.get(0).getUssdSubscribed()); t24DataServiceImpl.save(t24DataEntitySave); break; case "A": if(txnStatus.equals("hold")){ errorMsg = "Cannot Reverse Transaction in hold - " + txnRef; statusMsg = "99"; return outValue(api, txnCode, requestType, drAccount,crAccount,txnRef,errorMsg,statusMsg); } if(txnStatus.equals("del")){ errorMsg = "Transaction Deleted - " + txnRef; statusMsg = "99"; return outValue(api, txnCode, requestType, drAccount,crAccount,txnRef,errorMsg,statusMsg); } if(txnStatus.equals("rev")){ errorMsg = "Transaction Already Reversed! - " + txnRef; statusMsg = "99"; return outValue(api, txnCode, requestType, drAccount,crAccount,txnRef,errorMsg,statusMsg); } if(txnStatus.equals("revamt")){ errorMsg = "Amount Already Reversed, Can Only reverse Charge - " + txnRef; statusMsg = "99"; return outValue(api, txnCode, requestType, drAccount,crAccount,txnRef,errorMsg,statusMsg); } transactionDataEntitySave.setTxnRef(txnRef); transactionDataEntitySave.setApi(transactionDataEntity.get(0).getApi()); transactionDataEntitySave.setHold(hold); transactionDataEntitySave.setRequestType(transactionDataEntity.get(0).getRequestType()); transactionDataEntitySave.setFtId(transactionDataEntity.get(0).getFtId()); transactionDataEntitySave.setTxnCode(transactionDataEntity.get(0).getTxnCode()); transactionDataEntitySave.setBankWallet(transactionDataEntity.get(0).getBankWallet()); transactionDataEntitySave.setCustomerName(transactionDataEntity.get(0).getCustomerName()); transactionDataEntitySave.setCustomerBranch(transactionDataEntity.get(0).getCustomerBranch()); transactionDataEntitySave.setDrAccount(transactionDataEntity.get(0).getDrAccount()); transactionDataEntitySave.setCrAccount(transactionDataEntity.get(0).getCrAccount()); transactionDataEntitySave.setTxnAmount(transactionDataEntity.get(0).getTxnAmount()); transactionDataEntitySave.setTxnCharge(transactionDataEntity.get(0).getTxnCharge()); transactionDataEntitySave.setTxnCurrency(transactionDataEntity.get(0).getTxnCurrency()); transactionDataEntitySave.setTxnNarration(transactionDataEntity.get(0).getTxnNarration()); transactionDataEntitySave.setTxnStatus("revamt"); transactionDataEntitySave.setProcessFlag("Y"); transactionDataEntitySave.setElevyCharge(transactionDataEntity.get(0).getElevyCharge()); transactionDataServiceImpl.save(transactionDataEntitySave); t24DataEntitySave.setAccountId(account); t24DataEntitySave.setAccountBalance(newBalance); t24DataEntitySave.setCategory(t24DataEntity.get(0).getCategory()); t24DataEntitySave.setCustomerNo(t24DataEntity.get(0).getCustomerNo()); t24DataEntitySave.setCustomerName(t24DataEntity.get(0).getCustomerName()); t24DataEntitySave.setCustomerType(t24DataEntity.get(0).getCustomerType()); t24DataEntitySave.setCustomerId(t24DataEntity.get(0).getCustomerId()); t24DataEntitySave.setPhoneNumber(t24DataEntity.get(0).getPhoneNumber()); t24DataEntitySave.setBranch(t24DataEntity.get(0).getBranch()); t24DataEntitySave.setBranchName(t24DataEntity.get(0).getBranchName()); t24DataEntitySave.setDob(t24DataEntity.get(0).getDob()); t24DataEntitySave.setEmailAddress(t24DataEntity.get(0).getEmailAddress()); t24DataEntitySave.setGender(t24DataEntity.get(0).getGender()); t24DataEntitySave.setAcStatus(t24DataEntity.get(0).getAcStatus()); t24DataEntitySave.setMaxDebitBalance(t24DataEntity.get(0).getMaxDebitBalance()); t24DataEntitySave.setMaxCreditBalance(t24DataEntity.get(0).getMaxCreditBalance()); t24DataEntitySave.setDebitAllowed(t24DataEntity.get(0).getDebitAllowed()); t24DataEntitySave.setCreditAllowed(t24DataEntity.get(0).getCreditAllowed()); t24DataEntitySave.setUssdSubscribed(t24DataEntity.get(0).getUssdSubscribed()); t24DataServiceImpl.save(t24DataEntitySave); break; case "C": if(txnStatus.equals("hold")){ errorMsg = "Cannot Reverse Transaction in hold - " + txnRef; statusMsg = "99"; return outValue(api, txnCode, requestType, drAccount,crAccount,txnRef,errorMsg,statusMsg); } if(txnStatus.equals("del")){ errorMsg = "Transaction Deleted - " + txnRef; statusMsg = "99"; return outValue(api, txnCode, requestType, drAccount,crAccount,txnRef,errorMsg,statusMsg); } if(txnStatus.equals("rev")){ errorMsg = "Transaction Already Reversed! - " + txnRef; statusMsg = "99"; return outValue(api, txnCode, requestType, drAccount,crAccount,txnRef,errorMsg,statusMsg); } if(txnStatus.equals("revchg")){ errorMsg = "Charge Already Reversed, Can Only reverse Amount- " + txnRef; statusMsg = "99"; return outValue(api, txnCode, requestType, drAccount,crAccount,txnRef,errorMsg,statusMsg); } transactionDataEntitySave.setTxnRef(txnRef); transactionDataEntitySave.setApi(transactionDataEntity.get(0).getApi()); transactionDataEntitySave.setHold(hold); transactionDataEntitySave.setRequestType(transactionDataEntity.get(0).getRequestType()); transactionDataEntitySave.setFtId(ftRef); transactionDataEntitySave.setTxnCode(transactionDataEntity.get(0).getTxnCode()); transactionDataEntitySave.setBankWallet(transactionDataEntity.get(0).getBankWallet()); transactionDataEntitySave.setCustomerName(transactionDataEntity.get(0).getCustomerName()); transactionDataEntitySave.setCustomerBranch(transactionDataEntity.get(0).getCustomerBranch()); transactionDataEntitySave.setDrAccount(transactionDataEntity.get(0).getDrAccount()); transactionDataEntitySave.setCrAccount(transactionDataEntity.get(0).getCrAccount()); transactionDataEntitySave.setTxnAmount(transactionDataEntity.get(0).getTxnAmount()); transactionDataEntitySave.setTxnCharge(transactionDataEntity.get(0).getTxnCharge()); transactionDataEntitySave.setTxnCurrency(transactionDataEntity.get(0).getTxnCurrency()); transactionDataEntitySave.setTxnNarration(transactionDataEntity.get(0).getTxnNarration()); transactionDataEntitySave.setTxnStatus("revchg"); transactionDataEntitySave.setProcessFlag("Y"); transactionDataEntitySave.setElevyCharge(transactionDataEntity.get(0).getElevyCharge()); transactionDataServiceImpl.save(transactionDataEntitySave); t24DataEntitySave.setAccountId(account); t24DataEntitySave.setAccountBalance(acBalance); t24DataEntitySave.setCategory(t24DataEntity.get(0).getCategory()); t24DataEntitySave.setCustomerNo(t24DataEntity.get(0).getCustomerNo()); t24DataEntitySave.setCustomerName(t24DataEntity.get(0).getCustomerName()); t24DataEntitySave.setCustomerType(t24DataEntity.get(0).getCustomerType()); t24DataEntitySave.setCustomerId(t24DataEntity.get(0).getCustomerId()); t24DataEntitySave.setPhoneNumber(t24DataEntity.get(0).getPhoneNumber()); t24DataEntitySave.setBranch(t24DataEntity.get(0).getBranch()); t24DataEntitySave.setBranchName(t24DataEntity.get(0).getBranchName()); t24DataEntitySave.setDob(t24DataEntity.get(0).getDob()); t24DataEntitySave.setEmailAddress(t24DataEntity.get(0).getEmailAddress()); t24DataEntitySave.setGender(t24DataEntity.get(0).getGender()); t24DataEntitySave.setAcStatus(t24DataEntity.get(0).getAcStatus()); t24DataEntitySave.setMaxDebitBalance(t24DataEntity.get(0).getMaxDebitBalance()); t24DataEntitySave.setMaxCreditBalance(t24DataEntity.get(0).getMaxCreditBalance()); t24DataEntitySave.setDebitAllowed(t24DataEntity.get(0).getDebitAllowed()); t24DataEntitySave.setCreditAllowed(t24DataEntity.get(0).getCreditAllowed()); t24DataEntitySave.setUssdSubscribed(t24DataEntity.get(0).getUssdSubscribed()); t24DataServiceImpl.save(t24DataEntitySave); break; default: errorMsg = "Invalid Option"; statusMsg = "99"; return outValue(api, txnCode, requestType, drAccount,crAccount,txnRef,errorMsg,statusMsg); } errorMsg = ""; statusMsg = "00"; return outValue(api, txnCode, requestType, drAccount,crAccount,txnRef,errorMsg,statusMsg); } // public void txnWriter(String outRequest) throws IOException { // FileWriter myWriter = new FileWriter(outFolder + "/"+ txnRef + ".xml"); // myWriter.write(outRequest); // myWriter.close(); // } public String outValue(String api, String txnCode, String requestType, String drAccount, String crAccount, String txnRef, String error, String status){ return "{\"txnCode\" :\"" + txnCode + "\",\"" + "api\" :\"" + api + "\",\"" + "requestType\" :\"" + requestType + "\",\"" + "otherTxnRef\" :\"" + txnRef + "\",\"" + "drAccount\" :\"" + drAccount + "\",\"" + "crAccount\" :\"" + crAccount + "\",\"" + "ourTxnRef\" :\"" + "" + "\",\"" + "errorMsg\" :\"" + error + "\",\"" + "status\" :\"" + status + "\"" + "}"; } }