double currentSourceBalance = 0.0;
int userId = externalUserTransaction.getUserId();
ExternalAccount sourceExternalAccount = externalAccountBO.findUserByid(userId);
if(sourceExternalAccount != null)
{
currentSourceBalance = sourceExternalAccount.getCurrentBalance();
}
// Check the current Balance if not return error
if(currentSourceBalance<amountToTransfer)
{
return "redirect:/IndividualUser/IndividualUserLessFunds";
}
//else process the transaction.
else
{
// Find the account from database where money needs to be transfered.
ExternalAccount destinationExternalAccount = individualUserBO.getAccountByUsername(merchantUserName);
if(destinationExternalAccount == null)
{
return "redirect:/IndividualUser/MerchantUserNameIncorrect";
}
else if(sourceExternalAccount.getUsername().equals(destinationExternalAccount.getUsername()))
{
return "redirect:/IndividualUser/MerchantUserNameIncorrect";
}
// Update Source Account Balance
sourceExternalAccount.setCurrentBalance(currentSourceBalance-amountToTransfer);
externalAccountBO.update(sourceExternalAccount);
// Create Transaction For Source Account
externalUserTransaction.setTransDetail("Transfer To Merchant " + merchantUserName);
externalUserTransaction.setAmountInvolved(amountToTransfer*-1);
externalTransactionBO.save(externalUserTransaction);
//Create Transaction for Destination Balance
/* externalUserTransaction.setUserId(destinationExternalAccount.getUserid());
externalUserTransaction.setTransType("debitFromUser");
externalUserTransaction.setTransDetail("Transfer From Individual " + sourceExternalAccount.getUsername());
externalUserTransaction.setAmountInvolved(amountToTransfer);
externalTransactionBO.save(externalUserTransaction);*/
long randomNumber = (long) Math.floor(Math.random() * 9000000) + 1000000L;
// GENERATE CERTIFICATE FOR THE TRANSACTION
KeyPair keyPair = null;
try {
keyPair = generateCertificate(principal.getName(),randomNumber);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Pki pki = new Pki();
pki.setUserName(principal.getName());
pki.setTransId(Long.toString(randomNumber));
pki.setMerchant(merchantUserName);
pki.setAmountInvolved(amountToTransfer);
pki.setTransDetail("Transfer From Individual " + sourceExternalAccount.getUsername());
//get a blob out of the keypair
Blob blob = pkiBO.createKeyBlob(keyPair);
//UPDATING THE ENTRY IN DATABASE
pki.setCert(blob);
pkiBO.save(pki);
//EMAILING THE MERCHANT
sendMail(merchantUserName,principal.getName(),"Transfer From Individual " + sourceExternalAccount.getUsername(),Long.toString(randomNumber),amountToTransfer,destinationExternalAccount.getEmail());
return "redirect:/IndividualUser/MerchantUserTransferSuccess";
}