package my.com.kfh.enterprise.services.ib117_jomPay;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;

import my.com.kfh.enterprise.services.IBEnterpriseServicesWorkspace;
import my.com.kfh.enterprise.services.IBUtils;
import my.com.kfh.enterprise.services.common.CommonConstants;
import my.com.kfh.enterprise.services.ib101_access.AccessConstants;
import my.com.kfh.enterprise.services.ib112_si.IBGenericSIServices;
import my.com.kfh.enterprise.services.ib112_si.SIConstants;
import my.com.kfh.enterprise.services.ib112_si.SI_PAYMENT_MODE;
import my.com.kfh.enterprise.services.ib114_online_contest.ContestContants;
import my.com.kfh.enterprise.services.integration.ESBIntegrationConfig;
import my.com.kfh.error.exception.IBBusinessException;
import my.com.kfh.error.exception.IBGenericException;
import my.com.kfh.error.exception.IBServiceException;
import my.com.kfh.error.handler.CommonErrorCode;
import my.com.kfh.error.handler.IBErrorHandler;
import my.com.kfh.esb.response.ESBAccountEnquiryTC100Bean;
import my.com.kfh.esb.response.ESBCompanyProfileBean;
import my.com.kfh.hibernate.tables.IBJomPay;
import my.com.kfh.hibernate.utils.HibernateJomPayUtils;
import my.com.kfh.sms.services.SMSConstants;
import my.com.kfh.sms.services.SMSServices;
import my.com.kfh.web.utils.BVI_DateTime;
import my.com.kfh.web.utils.DataValidator;
import my.com.kfh.web.utils.DateTimeUtils;
import my.com.kfh.web.utils.FormatterUtils;
import my.com.kfh.web.utils.IBDynaBean;
import my.com.kfh.web.utils.IBLogger;
import my.com.kfh.web.utils.IBSessionObjects;
import my.com.kfh.web.utils.IBTimer;
import my.com.kfh.web.utils.ResourcesUtils;
import my.com.kfh.web.utils.SettingsUtils;
import my.com.kfh.web.utils.Constant.ServiceInfoExtend;
import my.com.kfh.web.utils.constant.ServiceInfo;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.kfh.nbps.webservice.NBPSWS1;
import com.kfh.nbps.webservice.NBPSWS1Service;
import com.kfh.nbps.webservice.NBPSWS1ServiceLocator;

public class IBOpenJomPayResultServices extends IBEnterpriseServicesWorkspace {
	
	private static Class c_oClass = IBOpenJomPayResultServices.class;

	//Xml Response
	String xmlResponse = null;
	String respStatus = null;
	String respErrCode = null;
	String respErrMsg = null;
	String respJomPayRefNo = null;
	String respDateTime = null;
	String respChannelRefNo = null;
	String respChargeReferenceNo = null;
	String respChargeRespCode = null;
	String respFundReferenceNo = null;
	String respFundRespCode = null;
	
	public IBOpenJomPayResultServices(IBSessionObjects m_oIBSessionObjects) {
		super(m_oIBSessionObjects);
	}

	public IBOpenJomPayResultServices(IBSessionObjects m_oIBSessionObjects, String p_sUsername, String p_sPassword) {
		super(m_oIBSessionObjects, p_sUsername, p_sPassword);
	}

	public void execute() throws Exception {
		
		String m_sMethodName = "executeServices";
		IBTimer m_oTimer = IBLogger.logBSFNMethodEnteringMessage (c_oClass, m_sMethodName);
		
		Date dt = new Date();
		BVI_DateTime m_oCurrentBVIDateTime = new BVI_DateTime();
		
		Map<String, IBDynaBean> accountMap = new LinkedHashMap();
		Map<String, String> m_oReceiptMap = new LinkedHashMap<String, String> ();
		
		IBDynaBean viewBean = null;
		IBDynaBean m_oAccountFrom = null;
		IBDynaBean m_oServiceBean = null;
		
		double m_dAvailableBal = 0.0;
		double m_dTotalTransferAmount = 0.0;
		boolean m_boIsFundSufficient = true;
		boolean m_boIsResultSuccessful = false;
		boolean m_bContestFlag = false;
		
		String m_sTrnxTypeAll = null;
		String m_sTransStatusMsg 	= null;
		String m_sReason = null;
		String m_sDate = null;
		String m_sTime = null;
		String m_sReceiptLink = null;
		String m_sSIServiceCode = "";
		
		// Value from confirm's page
		String m_oJomPayConfirmAccount = null;
		String m_oJomPayConfirmAmount = null;
		
		// DB
		Long m_sTrnxId 	= null;
		String m_sTrxnType = null;
		Date m_sTrxnDate = null;
		Long m_sUserId = null;
		String m_sBillerCode = null;
		String m_sBillerName = null;
		String m_sBillRefNo1 = null;
		String m_sBillRefNo2 = null;
		BigDecimal m_sAmount = null;
		String m_sStatus = null;
		String m_sFromAccNo = null;
		String m_sFromCurrency = null;
		String m_sMerchantAccNo = null;
		String m_sToCurrency = null;
		String m_sPhxReferenceNo = null;
		String m_sPhxrefRevNo = null;
		String m_sRespCode = null;
		String m_sRespMsg = null;
		String m_sServiceCode = null;
		String m_sInfo2 = null;
		String m_sInfo3 = null;
		String m_sContactNo = null;
		String m_sMerchantEmail = null;
		String m_sSiIndicator = null;
		String m_sRemoteIp = null;
		String m_sOther1 = null;
		String m_sOther2 = null;
		String m_sEPayReference = null;
		String m_sRevertReference = null;
		StringBuffer m_sbDesc = new StringBuffer ();
		
		//WebService
		String channel				=	null;
		String subChannel			=	null;
		String fromAccountNo 		=	null;			 	
		String toAccountNo  		=	null;
		String amount  				=	null;
		String chargeAmount  		=	null;
		String toName  				=	null;
		String fromName  			=	null;
		String rimNo  				=	null;
		String paymentDescription  	=	null;
		String phnxChargesDesc  	=	null;
		String phnxPrincipleDesc  	=	null;
		String channelRefNo			=	null;
		String paymentType  		=	null;
		String accountType  		=	null;
		String recipientRefNo   	=	null;
		String recipientRefNo2   	=	null;
		String billerCode   		=	null;
		String paymentChannel    	=	null;
		String payerType    		=	null;
		String chargesColAccount	=	null;
		String jomPayRefNo			=	null;
		
		// si
		boolean m_boSIIndicator = false;
		Date m_oEffectiveDate = null;
		String m_sEffectiveDate = null;
		String m_sPayFrequency = null;
		String m_sPaymentFrequencyDisplay = null;
		String m_sTerminationDate = null;
		String m_sPaymentMode = null;
		String m_sPaymentModeDisplay = null;
		String m_sEffectiveDateDisplay = null;
		String m_sTerminationDateDisplay = null;
		
		boolean m_boDeleteSIIndicator = false;
		
		String m_sAccNewBal="";
		try 
		{
			viewBean = (IBDynaBean) getIBDynaBean ().get (JomPayContants.jomPayOpenView);
			m_oServiceBean = (IBDynaBean) viewBean.get(JomPayContants.jomPayServiceInfo);
			
			if(m_oServiceBean != null)
			{
				if (((Boolean) m_oServiceBean.get (ServiceInfo.BEAN_ATT_TAC_REQUIRED)).booleanValue ())
				{
					getIBAuthenticationServices ().userVerifyTac ((String) getIBDynaBean ().get (JomPayContants.jomPayOpenFormTacValue),getIBSessionObjects().getIBSessionId());
				}
			}
			
			m_sServiceCode = JomPayContants.jomPayOpen;
			m_sPaymentMode = (String)getIBDynaBean ().get (SIConstants.PAY_MODE);
			m_sTrnxId 	= new Long(viewBean.get(JomPayContants.jomPayOpenViewTransID).toString());
			m_sTrnxTypeAll = "JomPAY Open Payment";
			m_sDate = DateTimeUtils.formatDayMonthYear (dt);
			m_sTime = (DateTimeUtils.convertDateTime (dt)).getTime ();
			
			m_oJomPayConfirmAccount = String.valueOf(getIBDynaBean().get(JomPayContants.jomPayOpenFormAccountIndex));
			m_oJomPayConfirmAmount = String.valueOf(viewBean.get(JomPayContants.jomPayOpenViewAmount));
			accountMap = (Map) viewBean.get(JomPayContants.jomPayViewAccountMap);
			m_oAccountFrom = (IBDynaBean)accountMap.get(m_oJomPayConfirmAccount);
			
			m_sbDesc.append ((String) m_oServiceBean.get (ServiceInfo.BEAN_ATT_SERVICE_NAME));
			m_sbDesc.append (" - ");
			m_sbDesc.append(viewBean.get(JomPayContants.jomPayOpenViewBillerCodeName).toString());
			
			//Set before calling to host
			fromAccountNo		= 	m_oAccountFrom.get(JomPayContants.jomPayViewAccountNo).toString();
			
			ESBAccountEnquiryTC100Bean m_oTCEnquiryBean = getESBServices ().getAccountDetailsTC100 (m_oJomPayConfirmAccount);
			ESBCompanyProfileBean profile = getESBServices().getProfileByAccRim(fromAccountNo, null);
			
			//Setting input for WebService
			channel 			= 	JomPayContants.jomPayWSChannel;//HD
			subChannel 			= 	JomPayContants.jomPayWSSubChannel;//HD
			toAccountNo			=	viewBean.get(JomPayContants.jomPayOpenBillerAccount).toString();
			amount				= 	m_oJomPayConfirmAmount;
			chargeAmount		= 	"0";
			toName				= 	viewBean.get(JomPayContants.jomPayOpenViewBillerCodeName).toString();
			fromName			= 	profile.getAccountName();//m_oAccountFrom.get(JomPayContants.jomPayViewAccountNickName).toString();
			rimNo				= 	(String)getIBSessionObjects ().getHttpSessionAttrObject (AccessConstants.RIM_NO);
			paymentDescription	=	"";
			phnxPrincipleDesc  	=	"IB JP:";
			phnxChargesDesc  	=	"";
			channelRefNo		=	String.valueOf(m_sTrnxId);
			paymentType			=	"01";
			accountType 		= 	m_oAccountFrom.get(JomPayContants.jomPayViewAccountApplType).toString().equalsIgnoreCase("SV")?"1":"2";
			recipientRefNo		=	viewBean.get(JomPayContants.jomPayOpenViewRef1).toString();
			recipientRefNo2		=	viewBean.get(JomPayContants.jomPayOpenViewRef2).toString();
			billerCode			=	viewBean.get(JomPayContants.jomPayOpenViewBillerCode).toString();
			paymentChannel		=	JomPayContants.jomPayWSPaymentChannel;//HD//1.InternetBanking|2.ATM|3.MobileBanking|4.MyClearUSSD|5.OverTheCounter
			payerType			=	JomPayContants.jomPayWSPayerType;//HD
			chargesColAccount	=	null;
			jomPayRefNo			=	viewBean.get(JomPayContants.jomPayOpenViewJomPayRef).toString();
			
			m_sTrxnType = m_sTrnxTypeAll;
			m_sTrxnDate = dt;
			m_sUserId = this.getIBSessionObjects ().getVisitor ().getId ();
			m_sBillerCode = viewBean.get(JomPayContants.jomPayOpenViewBillerCode).toString();
			m_sBillerName = viewBean.get(JomPayContants.jomPayOpenViewBillerCodeName).toString();
			m_sBillRefNo1 = viewBean.get(JomPayContants.jomPayOpenViewRef1).toString();
			m_sBillRefNo2 = viewBean.get(JomPayContants.jomPayOpenViewRef2).toString();
			m_sAmount = new BigDecimal(viewBean.get(JomPayContants.jomPayOpenViewAmount).toString());
			m_sFromAccNo = m_oAccountFrom.get(JomPayContants.jomPayViewAccountNo).toString();
			m_sFromCurrency = "MYR";
			m_sMerchantAccNo = viewBean.get(JomPayContants.jomPayOpenBillerAccount).toString();
			m_sToCurrency = "MYR";
			m_sPhxReferenceNo = respFundReferenceNo;
			m_sPhxrefRevNo = "";
			m_sRespCode = respErrCode;
			m_sRespMsg = respErrMsg;
			m_sServiceCode = (String) m_oServiceBean.get (ServiceInfoExtend.BEAN_ATT_SERVICE_CODE);
			m_sInfo2 = "";
			m_sInfo3 = "";
			m_sContactNo = "";
			m_sMerchantEmail = null;
			m_sSiIndicator = "0";
			m_sRemoteIp = (String)getIBSessionObjects ().getHttpSessionAttrObject (AccessConstants.COL_REMOTE_IP);
			m_sOther1 = "";
			m_sOther2 = "";
			
			if (SI_PAYMENT_MODE.PAY_NOW.getCode ().equals (m_sPaymentMode))
			{
				phnxPrincipleDesc = phnxPrincipleDesc + (toName.length() > 20?toName.substring(0, 20):toName) + "," + recipientRefNo + "," + jomPayRefNo;
				
				m_oEffectiveDate = m_oCurrentBVIDateTime.getJavaDate ();
				
				if (!DataValidator.isEmptyString (m_oTCEnquiryBean.getAvailable_Bal ()))
				{
					m_dAvailableBal = Double.parseDouble (m_oTCEnquiryBean.getAvailable_Bal ());
				}
	
				m_dTotalTransferAmount = Double.parseDouble (m_oJomPayConfirmAmount);
				
				if(m_dAvailableBal < m_dTotalTransferAmount)
				{
					m_boIsFundSufficient = false;
					m_boIsResultSuccessful = false;
					m_sStatus = ResourcesUtils.getUnsuccessful ();
					m_sTransStatusMsg = ResourcesUtils.getApplicationResourceMessage (SIConstants.RESOURCES_INSUFFICIENT_FUND);
					m_sReason = m_sTransStatusMsg;
					respStatus = "0";
				}
	
				if (m_boIsFundSufficient)
				{
					//NBPS WebService Start
					xmlResponse = AddIBGNBPSEntry(channel,subChannel,fromAccountNo,toAccountNo,amount,chargeAmount,toName,fromName,rimNo,paymentDescription,phnxChargesDesc,phnxPrincipleDesc,channelRefNo,paymentType,accountType,recipientRefNo,recipientRefNo2,billerCode,paymentChannel,payerType,chargesColAccount,jomPayRefNo);
					readXmlResponse(xmlResponse);
					//NBPS WebService End
					
					if(respStatus.equalsIgnoreCase("0"))
					{
						m_sStatus = "Unsuccessful";
						viewBean.set(JomPayContants.jomPayTransErrMsg, ResourcesUtils.getApplicationResourceMessage("jomPay.host.error.message." + respErrCode));
					}
					else if(respStatus.equalsIgnoreCase("1"))
					{
						m_sStatus = "Successful";
						m_boIsResultSuccessful = true;
						ESBAccountEnquiryTC100Bean m_oTCEnquiryBeanNew = getESBServices ().getAccountDetailsTC100 (m_oJomPayConfirmAccount);
						m_sAccNewBal = m_oTCEnquiryBeanNew.getAvailable_Bal();
					}
				}
				
				//Checking Contest module
	            String m_sContestAmount = SettingsUtils.getSetting(ContestContants.ONLINE_CONTEST_AMOUNT);
				if(m_boIsResultSuccessful)
				{
					if(Double.parseDouble (m_oJomPayConfirmAmount)>Double.parseDouble (m_sContestAmount) || Double.parseDouble (m_oJomPayConfirmAmount)== Double.parseDouble (m_sContestAmount))
					{
						//check whether Contest is ON/OFF
						String m_sContestFlag = SettingsUtils.getSetting(ContestContants.ONLINE_CONTEST_FLAG);
						
						if(m_sContestFlag.equalsIgnoreCase(ContestContants.ONLINE_CONTEST_ON))
						{
							//Check Date start & end date
							Date m_oTodayDate = new Date();
							String m_sStartDate = SettingsUtils.getSetting(ContestContants.ONLINE_CONTEST_START_DATE);
							String m_sEndDate = SettingsUtils.getSetting(ContestContants.ONLINE_CONTEST_END_DATE);
							
							SimpleDateFormat m_oSDF = new SimpleDateFormat("dd/MM/yyyy");
							
							Date m_oStartDate = m_oSDF.parse(m_sStartDate);
							Date m_EndDate = m_oSDF.parse(m_sEndDate);
							
							if(m_oTodayDate.compareTo(m_oStartDate) > 0 && m_oTodayDate.compareTo(m_EndDate) < 0)
							{
								m_bContestFlag = true;
							}
						}
					}
				}
				viewBean.set (ContestContants.IS_CONTEST_ON, m_bContestFlag);
			}
			// SI
			else
			{
				// stored SI Service code into SI JomPAY
				m_sSIServiceCode = JomPayContants.siJomPayOpen;
				m_sServiceCode = m_sSIServiceCode;

				m_sTrnxTypeAll = IBGenericSIServices.getSITransType (m_sTrnxTypeAll, m_sPaymentMode);
				m_boSIIndicator = true;

				m_sEffectiveDate = (String)viewBean.get (SIConstants.EFFECTIVE_DATE_FORMAT);
				BVI_DateTime m_oBVEffectiveDate = new BVI_DateTime (m_sEffectiveDate);
				m_oEffectiveDate = m_oBVEffectiveDate.getJavaDate ();
				if (SI_PAYMENT_MODE.RECURRING_PAYMENT.getCode ().equals (m_sPaymentMode))
				{
					m_sPayFrequency = (String)getIBDynaBean ().get (SIConstants.PAY_FREQUENCY);
					m_sPaymentFrequencyDisplay = (String)viewBean.get (SIConstants.PAY_FREQUENCY_DISPLAY);
					m_sTerminationDateDisplay = (String)viewBean.get (SIConstants.TERMINATION_DATE_DISPLAY);
					m_sTerminationDate = (String)viewBean.get (SIConstants.TERMINATION_DATE_FORMAT);
				}

				Map m_oSILimitMap = IBGenericSIServices.verifySIDailyLimitByBank (this, getIBDynaBean (), getIBSessionObjects (),
						m_sSIServiceCode, String.valueOf(m_sAmount), m_sEffectiveDate, false);

				if (!((Boolean) m_oSILimitMap.get (SIConstants.TRANS_STATUS)).booleanValue ())
				{
					m_boIsResultSuccessful = false;
					m_sStatus = ResourcesUtils.getUnsuccessful ();
					m_sReason = ResourcesUtils.getBusinessExceptionErrorMessage (IBBusinessException.SI,
							SIConstants.ERR_BANK_MAX_LIMIT,
							new String [] { FormatterUtils.formatMoneyDisplay ( (String)m_oSILimitMap.get (SIConstants.MAX_LIMIT),
									null, true) });
					respStatus = "0";
					viewBean.set(JomPayContants.jomPayTransErrMsg, m_sReason);
				}
				else
				{
					m_boIsResultSuccessful = true;
					respStatus = "1";
					m_sStatus 	= ResourcesUtils.getAccepted ();
				}
			}
			
			viewBean.set(JomPayContants.jomPayOpenViewPaymentType, m_sPaymentMode);
			viewBean.set(JomPayContants.jomPayTransRefNo, respFundReferenceNo);
			viewBean.set(JomPayContants.jomPayTransStatus, respStatus);
			viewBean.set(JomPayContants.jomPayTransStatusDisplay, m_sStatus);
			viewBean.set(JomPayContants.jomPayTransIbRefNo, channelRefNo);
			
			//Set transaction's Date&Time
			viewBean.set(JomPayContants.jomPayTransDate, m_sDate);
			viewBean.set(JomPayContants.jomPayTransTime, m_sTime);
			
		   // Start Create Receipt
			if(m_boIsResultSuccessful)
			{
				m_oReceiptMap.put(ResourcesUtils.getApplicationResourceMessage ("receipt.transaction.type"), m_sTrnxTypeAll);
				m_oReceiptMap.put(ResourcesUtils.getApplicationResourceMessage ("ib117.account.from"), m_oAccountFrom.get(JomPayContants.jomPayViewAccountNo).toString());
				m_oReceiptMap.put(ResourcesUtils.getApplicationResourceMessage ("ib117.account.amount"), viewBean.get(JomPayContants.jomPayOpenViewAmountDisplay).toString());
				m_oReceiptMap.put(ResourcesUtils.getApplicationResourceMessage ("ib117.account.biller.code"), viewBean.get(JomPayContants.jomPayOpenViewBillerCodeNameDisplay).toString());
				m_oReceiptMap.put(ResourcesUtils.getApplicationResourceMessage ("ib117.account.ref1"), viewBean.get(JomPayContants.jomPayOpenViewRef1Display).toString());
				m_oReceiptMap.put(ResourcesUtils.getApplicationResourceMessage ("ib117.account.ref2"), viewBean.get(JomPayContants.jomPayOpenViewRef2).toString());
				m_oReceiptMap.put(ResourcesUtils.getApplicationResourceMessage ("ib117.result.jomPay.reference"), viewBean.get(JomPayContants.jomPayOpenViewJomPayRef).toString());
				m_oReceiptMap.put(ResourcesUtils.getApplicationResourceMessage ("ib112.si.payment.mode"), viewBean.get(JomPayContants.jomPayOpenViewPayModeDisplay).toString());
				m_oReceiptMap.put(ResourcesUtils.getApplicationResourceMessage ("ib117.pay.start.date"), viewBean.get(JomPayContants.jomPayOpenViewEffectiveDateDisplay).toString());
				m_oReceiptMap.put(ResourcesUtils.getApplicationResourceMessage ("ib117.result.status"), viewBean.get(JomPayContants.jomPayTransStatusDisplay).toString());
				m_oReceiptMap.put(ResourcesUtils.getApplicationResourceMessage ("ib117.result.ib.reference"), viewBean.get(JomPayContants.jomPayTransIbRefNo).toString());
				m_oReceiptMap.put(ResourcesUtils.getApplicationResourceMessage ("ib117.result.transaction.date"), viewBean.get(JomPayContants.jomPayTransDate).toString());
				m_oReceiptMap.put(ResourcesUtils.getApplicationResourceMessage ("ib117.result.transaction.time"), viewBean.get(JomPayContants.jomPayTransTime).toString());
								
				IBUtils.loopIntoReceiptDetails (this, m_oReceiptMap, DateTimeUtils.formatIBDateTimeToYMD(dt), m_sTrnxId.toString());
				viewBean.set (CommonConstants.RECEIPT_MAP, m_oReceiptMap);
				m_sReceiptLink = IBUtils.getReceiptLink (JomPayContants.jomPayOpenForm, JomPayContants.jomPayOpenView);
				viewBean.set (CommonConstants.RECEIPT_LINK, m_sReceiptLink);
			}
			


			
//			 SMS Notification START
			String m_sCustomerSMSMessage = "-";
//			String m_sBeneficiarySMSMessage = "-";
			
			String m_sCustomerAmount = SMSServices.formatAmountValue(String.valueOf(m_sAmount), null);
//			String m_sBeneficiaryAmount = SMSServices.formatAmountValue(String.valueOf(m_sAmount), null);
			
			String m_sTransactionTypeForSMSMessage = ResourcesUtils.getApplicationResourceMessage(SMSConstants.RESOURCE_SMS_TEXT_MESSAGE_SERVICE_JOMPAY);
			
			//compose sms
			m_sCustomerSMSMessage = SMSServices.generateCustomerSMSMessage(
					m_sTransactionTypeForSMSMessage, 
					m_sCustomerAmount,
					m_sBillerName.length()>15?m_sBillerName.substring(0, 15):m_sBillerName,
					m_sStatus, 
					new Date(),
					FormatterUtils.formatMoneyDisplay (m_sAccNewBal, "", true));
			
			//send sms
			
			SMSServices.sendCustomerSMS(this, 
					viewBean, 
					m_sFromAccNo, 
					m_sStatus, 
					getIBDynaBean().get(SIConstants.PAY_MODE).toString(), 
					m_sTrnxTypeAll, 
					String.valueOf(m_sTrnxId),
					String.valueOf(m_sAmount), 
					getIBSessionObjects().getVisitor().getId(), 
					m_sCustomerSMSMessage, 
					m_oCurrentBVIDateTime,
					SMSConstants.DEBIT_IB_TYPE);
			
//			m_sBeneficiarySMSMessage = SMSServices.generateBeneficiaryOtherSMSMessage(
//					viewBean, 
//					m_sTransactionTypeForSMSMessage, 
//					"", 
//					m_sBeneficiaryAmount, 
//					m_oCurrentBVIDateTime);
//			
//			SMSServices.sendBeneficiarySMS(this, 
//					getIBDynaBean(),
//					viewBean, 
//					m_sFromAccNo, 
//					m_sStatus, 
//					getIBDynaBean().get(SIConstants.PAY_MODE).toString(), 
//					m_sTrnxTypeAll, 
//					String.valueOf(m_sTrnxId),
//					getIBSessionObjects().getVisitor().getId(),  
//					m_sBeneficiarySMSMessage, 
//					m_oCurrentBVIDateTime,
//					"");
			
			viewBean.set (JomPayContants.jomPayTransNewBalance,
					FormatterUtils.formatMoneyDisplay (m_sAccNewBal, null, true));
			
//			m_oReceiptMap.put(ResourcesUtils.getApplicationResourceMessage ("general.new.balance"), (String)m_oBillPaymentViewBean.get(BillPaymentContants.VBEAN_FROM_ACCOUNT_BALANCE));
			
			// UAT START
//			viewBean.set(SMSConstants.VBEAN_SMS_UAT_IS_BENE_SMS_SENT, SMSServices.isSMSLogged(this, viewBean.get(SMSConstants.VBEAN_SMS_BENE_MOBILE_NO).toString(), SMSConstants.SMS_NOTIFICATION_TYPE, m_oCurrentBVIDateTime.getDateDDMMMYYYYHMS()));
			viewBean.set(SMSConstants.VBEAN_SMS_UAT_IS_CUSTOMER_SMS_SENT, SMSServices.isSMSLogged(this, viewBean.get(SMSConstants.VBEAN_SMS_CUSTOMER_MOBILE_NO).toString(), SMSConstants.SMS_NOTIFICATION_TYPE, m_oCurrentBVIDateTime.getDateDDMMMYYYYHMS()));
//			viewBean.set(SMSConstants.VBEAN_SMS_UAT_BENE_SMS_MESSAGE, m_sBeneficiarySMSMessage);
			viewBean.set(SMSConstants.VBEAN_SMS_UAT_CUSTOMER_SMS_MESSAGE, m_sCustomerSMSMessage);
			// UAT END

			// SMS Notification END
			
			IBJomPay ibJomPay = new IBJomPay();

			IBLogger.logInetTranHistory(
					this, 
					String.valueOf(m_sTrnxId), //(String)m_oServiceInfoBean.get(ServiceInfo.BEAN_ATT_SERVICE_NAME),
					m_sTrnxTypeAll, 
					m_sFromAccNo, 
					"", 
					"", 
					"", 
					String.valueOf(m_sAmount), 
					m_sbDesc.toString (), 
					m_sStatus, 
					m_sMerchantAccNo, 
					"", 
					String.valueOf(m_oEffectiveDate), 
					getIBSessionObjects ().getRemoteIPAddress (), 
					"", 
					m_boIsResultSuccessful, 
					respFundReferenceNo, 
					"", // Other1
					"", // Other2
					m_sEPayReference, 
					getIBSessionObjects ().getIBSessionId (), 
					m_sRevertReference, 
					"", // PhxChargeRefNo
					"", // PhxChargeRevertRefNo
					"", // FromAccCreateDate
					"", // ToAccCreateDate
					m_sReason,
					m_sPaymentMode,
					m_sPayFrequency,
					m_sTerminationDate,"",""
					);
			
			
						
			ibJomPay.setTrxnId(m_sTrnxId);
			ibJomPay.setTrnxType(m_sTrnxTypeAll);
			ibJomPay.setTrnxDate(m_sTrxnDate);
			ibJomPay.setUserId(m_sUserId);
			ibJomPay.setEffectiveDate(m_oEffectiveDate);
			ibJomPay.setBillerCode(m_sBillerCode);
			ibJomPay.setBillerName(m_sBillerName);
			ibJomPay.setBillRefNo1(m_sBillRefNo1);
			ibJomPay.setBillRefNo2(m_sBillRefNo2);
			ibJomPay.setAmount(m_sAmount);
			ibJomPay.setStatus(m_sStatus);
			ibJomPay.setFromAccNo(m_sFromAccNo);
			ibJomPay.setFromCurrency(m_sFromCurrency);
			ibJomPay.setMerchantAccNo(m_sMerchantAccNo);
			ibJomPay.setToCurrency(m_sToCurrency);
			ibJomPay.setPhxReferenceNo(m_sPhxReferenceNo);
			ibJomPay.setPhxrevRefNo(m_sPhxrefRevNo);
			ibJomPay.setRespCode(m_sRespCode);
			ibJomPay.setRespMsg(m_sRespMsg);
			ibJomPay.setServiceCode(m_sServiceCode);
			ibJomPay.setInfo2(m_sInfo2);
			ibJomPay.setInfo3(m_sInfo3);
			ibJomPay.setContactNo(m_sContactNo);
			ibJomPay.setMerchantEmail(m_sMerchantEmail);
			ibJomPay.setSiIndicator(m_sSiIndicator);
			ibJomPay.setRemoteIp(m_sRemoteIp);
			ibJomPay.setOther1(m_sOther1);
			ibJomPay.setOther2(m_sOther2);
			ibJomPay.setJomPayRefNo(jomPayRefNo);
			
			HibernateJomPayUtils.insertJomPay(ibJomPay);
			
			
			// SI
			if (m_boSIIndicator && m_boIsResultSuccessful)
			{
				IBLogger.logSIDetail (
						this,
						String.valueOf(m_sTrnxId),
						String.valueOf(m_sTrnxId),
						"jomPay",//SIConstants.TRANSCODE_BILL_PAYMENT,
						m_sTrnxTypeAll,
						m_sPaymentMode,
						m_sPayFrequency,
						m_sEffectiveDate,
						m_sTerminationDate,
						m_sSIServiceCode,
						m_boDeleteSIIndicator
						);
			}
			
			getIBDynaBean().set(JomPayContants.jomPayOpenView, viewBean);
		} catch (Exception e) {
			e.printStackTrace();
			if (e instanceof IBGenericException)
			{
				throw (IBGenericException) e;
			}

			throw IBErrorHandler.createServiceException(
					IBServiceException.GENERAL_SERVICE_ERROR, true,
					new String[] {CommonErrorCode.GENERIC_CODE, "", "", "", IBErrorHandler.generateErrorMessage(e)}, c_oClass);
		}
		IBLogger.logBSFNMethodLeavingMessage (c_oClass, m_sMethodName, m_oTimer);
	}
	
	public static String AddIBGNBPSEntry(String channel,String subChannel,String fromAccountNo,String toAccountNo,String amount,
										 String chargeAmount,String toName,String fromName,String rimNo,String paymentDescription,
										 String phnxChargesDesc,String phnxPrincipleDesc,String channelRefNo,String paymentType,String accountType,
										 String recipientRefNo,String recipientRefNo2,String billerCode,String paymentChannel,String payerType,
										 String chargeCollectionAccount,String jomPayRefNo) throws Exception
	{	
		StringBuffer XMLEntry = new StringBuffer();
		
		XMLEntry.append("<Msg>");
			XMLEntry.append("<channel>").append(channel).append("</channel>");
			XMLEntry.append("<subChannel>").append(subChannel).append("</subChannel>");
			XMLEntry.append("<fromAccountNo>")		.append(fromAccountNo)		.append("</fromAccountNo>");
			XMLEntry.append("<toAccountNo>")		.append(toAccountNo)		.append("</toAccountNo>");
			XMLEntry.append("<amount>")				.append(amount)				.append("</amount>");
			XMLEntry.append("<chargeAmount>")		.append(chargeAmount)		.append("</chargeAmount>");
			XMLEntry.append("<toName><![CDATA[")	.append(toName)				.append("]]></toName>");
			XMLEntry.append("<fromName><![CDATA[")	.append(fromName)			.append("]]></fromName>");
			XMLEntry.append("<rimNo>")				.append(rimNo)				.append("</rimNo>");
			XMLEntry.append("<paymentDescription><![CDATA[").append(paymentDescription)	.append("]]></paymentDescription>");
			XMLEntry.append("<phnxChargesDesc><![CDATA[")	.append(phnxChargesDesc)	.append("]]></phnxChargesDesc>");
			XMLEntry.append("<phnxPrincipleDesc><![CDATA[")	.append(phnxPrincipleDesc)	.append("]]></phnxPrincipleDesc>");
			XMLEntry.append("<channelRefNo>")		.append(channelRefNo)			.append("</channelRefNo>");
			XMLEntry.append("<paymentType>")		.append(paymentType)		.append("</paymentType>");
			XMLEntry.append("<accountType>")		.append(accountType)		.append("</accountType>");
			XMLEntry.append("<recipientRefNo>")		.append(recipientRefNo)		.append("</recipientRefNo>");
			XMLEntry.append("<recipientRefNo2>")	.append(recipientRefNo2)	.append("</recipientRefNo2>");
			XMLEntry.append("<billerCode>")			.append(billerCode)			.append("</billerCode>");
			XMLEntry.append("<paymentChannel>")		.append(paymentChannel)		.append("</paymentChannel>");
			XMLEntry.append("<payerType>")			.append(payerType)			.append("</payerType>");
			XMLEntry.append("<chargeCollectionAccount>").append(chargeCollectionAccount).append("</chargeCollectionAccount>");
			XMLEntry.append("<jomPayRefNo>")		.append(jomPayRefNo).append("</jomPayRefNo>");
		XMLEntry.append("</Msg>");
			
		Properties m_oProp = ESBIntegrationConfig.getInstance ().getprop ();
		NBPSWS1ServiceLocator m_oLocator = new NBPSWS1ServiceLocator();
		m_oLocator.setkfhwsEndpointAddress(m_oProp.getProperty ("esb.nbps.ws.path"));
		
		NBPSWS1Service m_oService = m_oLocator;
		NBPSWS1 m_oIBG = m_oService.getkfhws();
		String response = m_oIBG.channelToNBPS(XMLEntry.toString());
		
		System.out.println("request :" + XMLEntry.toString());
		System.out.println("response : "+response);
		return response;
	}
	
	public void readXmlResponse(String xmlResponseDoc)
	{
		Document document;
		try
		{
			document = DocumentHelper.parseText (xmlResponseDoc);
			Element root = document.getRootElement ();
			
			respStatus 				= IBJomPayUtils.replaceNull(root.elementText(JomPayContants.jomPayWSRespStatus));
			respErrCode 			= IBJomPayUtils.replaceNull(root.elementText(JomPayContants.jomPayWSRespErrCode));
			respErrMsg 				= IBJomPayUtils.replaceNull(root.elementText(JomPayContants.jomPayWSRespErrMsg));
			respJomPayRefNo 		= IBJomPayUtils.replaceNull(root.elementText(JomPayContants.jomPayWSRespJomPayRefNo));
			respDateTime 			= IBJomPayUtils.replaceNull(root.elementText(JomPayContants.jomPayWSRespDateTime));
			respChannelRefNo 		= IBJomPayUtils.replaceNull(root.elementText(JomPayContants.jomPayWSRespChannelRefNo));
			respChargeReferenceNo 	= IBJomPayUtils.replaceNull(root.elementText(JomPayContants.jomPayWSRespChargeRefNo));
			respChargeRespCode 		= IBJomPayUtils.replaceNull(root.elementText(JomPayContants.jomPayWSRespChargeRespCode));
			respFundReferenceNo 	= IBJomPayUtils.replaceNull(root.elementText(JomPayContants.jomPayWSRespFundRefNo));
			respFundRespCode 		= IBJomPayUtils.replaceNull(root.elementText(JomPayContants.jomPayWSRespFundRespCode));
		}
		catch (DocumentException e) {
			e.printStackTrace();
		}
	}
}
