using System; using System.Collections.Generic; using System.ComponentModel; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; using System.Windows.Threading; using SPC.Kiosk.Base; using SPC.Kiosk.Common; using SPC.Kiosk.Popup.Model; using SPC.Kiosk.Payments; namespace SPC.Kiosk.Popup.ViewModel { /// /// HappyPointCertify.xaml에 대한 ViewModel /// public class VmPaymentProcess : PopupViewModelBase { #region [ Members ] private string titileImage; /// /// Read Barcode Formated Text /// public string TitileImage { get { return titileImage; } set { titileImage = value; PropertyChange("TitileImage"); } } private string processingGIF; /// /// Processing GIF /// public string ProcessingGIF { get { return processingGIF; } set { processingGIF = value; PropertyChange("ProcessingGIF"); } } private string errorImage; /// /// Error Icon /// public string ErrorImage { get { return errorImage; } set { errorImage = value; PropertyChange("ErrorImage"); } } private string successImage; /// /// Sucess Icon /// public string SuccessImage { get { return successImage; } set { successImage = value; PropertyChange("SuccessImage"); } } private List headerText; /// /// Top Header Text /// public List HeaderText { get { return headerText; } set { headerText = value; PropertyChange("HeaderText"); } } private List scanGuidText; /// /// Second Sub Header Text /// public List ScanGuidText { get { return scanGuidText; } set { scanGuidText = value; PropertyChange("ScanGuidText"); } } private List processingMessageText; /// /// Number Pad Input Guide Text /// public List ProcessingMessageText { get { return processingMessageText; } set { processingMessageText = value; PropertyChange("ProcessingMessageText"); } } private List processingGuide; /// /// Processing Guide Text /// public List ProcessingGuide { get { return processingGuide; } set { processingGuide = value; PropertyChange("ProcessingGuide"); } } private List errorGuide; /// /// Error Guide Text /// public List ErrorGuide { get { return errorGuide; } set { errorGuide = value; PropertyChange("ErrorGuide"); } } private List infoGuid1Text; /// /// Bootom first infomation text /// public List InfoGuid1Text { get { return infoGuid1Text; } set { infoGuid1Text = value; PropertyChange("InfoGuid1Text"); } } private TextAlignment infoGuid1TextAlignment = TextAlignment.Left; public TextAlignment InfoGuid1TextAlignment { get { return infoGuid1TextAlignment; } set { infoGuid1TextAlignment = value; PropertyChange("InfoGuid1TextAlignment"); } } private List infoGuid2Text; /// /// Bootom Second infomation text /// public List InfoGuid2Text { get { return infoGuid2Text; } set { infoGuid2Text = value; PropertyChange("InfoGuid2Text"); } } private List leftInfomationText; /// /// Bootom Second infomation text /// public List LeftInfomationText { get { return leftInfomationText; } set { leftInfomationText = value; PropertyChange("LeftInfomationText"); } } private bool paymentStartVisible = false; /// /// ReadCardOrBarCode Window Is Visible /// public bool PaymentStartVisible { get { return paymentStartVisible; } set { paymentStartVisible = value; PropertyChange("PaymentStartVisible"); } } private bool readSignVisible = false; /// /// PaymentProcessing Window Is Visible /// public bool ReadSignVisible { get { return readSignVisible; } set { readSignVisible = value; PropertyChange("ReadSignVisible"); } } private bool paymentProcessingVisible = false; /// /// PaymentProcessing Window Is Visible /// public bool PaymentProcessingVisible { get { return paymentProcessingVisible; } set { paymentProcessingVisible = value; PropertyChange("PaymentProcessingVisible"); } } private bool processingErrorVisible = false; /// /// ProcessingError Window Is Visible /// public bool ProcessingErrorVisible { get { return processingErrorVisible; } set { processingErrorVisible = value; PropertyChange("ProcessingErrorVisible"); } } private bool paymentSuccessVisible = false; /// /// Payment Success Window Is Visible /// public bool PaymentSuccessVisible { get { return paymentSuccessVisible; } set { paymentSuccessVisible = value; PropertyChange("PaymentSuccessVisible"); } } private List errorText; /// /// Process Error Text /// public List ErrorText { get { return errorText; } set { errorText = value; PropertyChange("ErrorText"); } } private bool isErrorBlink = false; /// /// Is Error Text Blink /// public bool IsErrorBlink { get { return isErrorBlink; } set { isErrorBlink = value; PropertyChange("IsErrorBlink"); } } private string formatedBarCode; /// /// Read Barcode Formated Text /// public string FormatedBarCode { get { return formatedBarCode; } set { formatedBarCode = value; PropertyChange("FormatedBarCode"); } } private string readBarCode; /// /// Read Barcode /// public string ReadBarCode { get { return readBarCode; } set { readBarCode = value; PropertyChange("ReadBarCode"); } } private double payments; /// /// Total Payments /// public double Payments { get { return payments; } set { payments = value; PropertyChange("Payments"); } } private string showPayment; /// /// Total Payments /// public string ShowPayment { get { return showPayment; } set { showPayment = value; PropertyChange("ShowPayment"); } } private double popupWidth = 1300d; /// /// Popup Whidth (For Calculate OK Button Width) /// public double PopupWidth { get { return popupWidth; } set { popupWidth = value; PropertyChange("PopupWidth"); } } private List cancelButtonText; /// /// Cancel Button Text /// public List CancelButtonText { get { return cancelButtonText; } set { cancelButtonText = value; PropertyChange("CancelButtonText"); } } private double cancelButtonWidth = 650d; /// /// Cancel Button Width /// public double CancelButtonWidth { get { return cancelButtonWidth; } set { cancelButtonWidth = value; PropertyChange("CancelButtonWidth"); } } private List okButtonText; /// /// OK Button Text /// public List OkButtonText { get { return okButtonText; } set { okButtonText = value; PropertyChange("OkButtonText"); } } private bool oKButtonEnabled = false; /// /// OK Button Enabled /// public bool OKButtonEnabled { get { return oKButtonEnabled; } set { oKButtonEnabled = value; PropertyChange("OKButtonEnabled"); } } private double okButtonWidth = 650d; /// /// OK Button Width /// public double OKButtonWidth { get { return okButtonWidth; } set { okButtonWidth = value; PropertyChange("OKButtonWidth"); } } private string signData; /// /// SignData /// public string SignData { get { return signData; } set { signData = value; PropertyChange("SignData"); } } private string signFile; /// /// SignFile /// public string SignFile { get { return signFile; } set { signFile = value; PropertyChange("SignFile"); } } private bool useableSign; /// /// Useable Sign /// public bool UseableSign { get { return useableSign; } set { useableSign = value; PropertyChange("UseableSign"); } } private bool signEnd; /// /// Sign End /// public bool SignEnd { get { return signEnd; } set { signEnd = value; PropertyChange("SignEnd"); } } private bool signReset; /// /// Sign Reset /// public bool SignReset { get { return signReset; } set { signReset = value; PropertyChange("SignReset"); } } /// /// 결제 처리 정보 /// public M_PaymentReturn PaymentReturn { get; set; } /// /// OK Click Command /// public ICommand OkCommand { get; protected set; } /// /// Cancel Click Command /// public ICommand CancelCommand { get; protected set; } private LastPaymentsType paymentType = LastPaymentsType.None; public LastPaymentsType PaymentType { get { return paymentType; } set { paymentType = value; PropertyChange("PaymentType"); } } private PaymentWindowType paymentWindow = PaymentWindowType.None; /// /// Current View Window /// public PaymentWindowType PaymentWindow { get { return paymentWindow; } set { paymentWindow = value; PropertyChange("PaymentWindow"); } } public bool ImmediatelyClose { get; protected set; } = false; private bool ProcessStart = false; private posLastPayments PosLastPayments = null; private Task ProcessTask = null; private Task ProcessTask2 = null; private CancellationTokenSource TokenSource; private string certifyKey = string.Empty; #endregion #region [ Ctor ] /// /// Ctor /// public VmPaymentProcess() { OkCommand = new Command(OkCommandHandler); CancelCommand = new Command(CancelCommandHandler); this.PropertyChanged += VmHappyPointCertify_PropertyChanged; ProcessingGIF = ResourceManager.GetNximagePathAdd("ic_alert_ing2.gif", CommonValue.PBdesignImagesPath); SuccessImage = ResourceManager.GetNximagePathAdd("ic_alert_check.png", CommonValue.PBdesignImagesPath); ErrorImage = ResourceManager.GetNximagePathAdd("ic_alert_error.png", CommonValue.PBdesignImagesPath); HeaderText = Languages.GetMessages("LBL0026"); ProcessingMessageText = Languages.GetMessages("LBL0031"); ProcessingGuide = Languages.GetMessages("LBL0140"); ErrorGuide = Languages.GetMessages("LBL0028"); ErrorText = new List(); ShowLanguageType = CommonValue.CommonLanguageType; PaymentWindow = PaymentWindowType.PaymentStart; PosLastPayments = new posLastPayments(); } /// /// Dispose() /// public new void Dispose() { this.TitileImage = string.Empty; this.ProcessingGIF = string.Empty; this.ErrorImage = string.Empty; this.PropertyChanged -= VmHappyPointCertify_PropertyChanged; if (TokenSource != null) { TokenSource.Cancel(); } if (PosLastPayments != null) { PaymentProcessRemoveEvents(); PosLastPayments.Dispose(); PosLastPayments = null; } if (ProcessTask != null) { //ProcessTask.Dispose(); ProcessTask = null; } base.Dispose(); } #endregion Ctor #region [ Methods ] private void PaymentProcessCreateEvents() { if (PosLastPayments != null) { PosLastPayments.ReadStart += posLastPayments_ReadStart; PosLastPayments.ReadEnd += posLastPayments_ReadEnd; PosLastPayments.SignStartEvent += posLastPayments_SignStartEvent; PosLastPayments.SignEndEvent += PosLastPayments_SignEndEvent; PosLastPayments.ErrorEvent += posLastPayments_ErrorEvent; PosLastPayments.ErrorMessageEvent += posLastPayments_ErrorMessageEvent; } } private void posLastPayments_SignStartEvent(object sender) { PaymentWindow = PaymentWindowType.ReadSign; } private void PosLastPayments_SignEndEvent(object sender) { PaymentWindow = PaymentWindowType.PaymentProcessing; } private void PaymentProcessRemoveEvents() { if (PosLastPayments != null) { PosLastPayments.ReadStart -= posLastPayments_ReadStart; PosLastPayments.ReadEnd -= posLastPayments_ReadEnd; PosLastPayments.SignStartEvent -= posLastPayments_SignStartEvent; PosLastPayments.SignEndEvent -= PosLastPayments_SignEndEvent; PosLastPayments.ErrorEvent -= posLastPayments_ErrorEvent; PosLastPayments.ErrorMessageEvent -= posLastPayments_ErrorMessageEvent; } } private void DoPaymentProcess() { try { #region 결제 처리 Task 처리 PosLastPayments.NoSignLimitAmount = CommonValue.NoSignAmountMaxLimit; if (PosLastPayments != null) { TimerEnabled = false; SignReset = false; SignEnd = false; PaymentProcessCreateEvents(); ProcessTask2 = new Task(() => PosLastPayments.StartCertifyProcessing(PaymentType, certifyKey, Payments)); TokenSource = new CancellationTokenSource(); ProcessTask2.Start(); ProcessTask2.Wait(); PaymentProcessRemoveEvents(); PaymentWindow = PosLastPayments.ProcessOK ? PaymentWindowType.PaymentSuccess : PaymentWindowType.ProcessingError; TokenSource.Dispose(); ProcessTask2.Dispose(); ProcessTask2 = null; TokenSource = null; } #endregion } catch (Exception ex) { CommonLog.ErrorLogWrite(this, "DoPaymentProcess()", "Fail !!", string.Format("{0}\n{1}", ex.Message, ex.StackTrace)); } finally { TimerEnabled = true; } } #endregion Methods #region [ Event Handlers ] private void CancelCommandHandler(object obj) { switch (PaymentWindow) { case PaymentWindowType.ProcessingError: case PaymentWindowType.PaymentStart: TimerEnabled = false; ReturnValue = new M_PopupReturn { OKAnswer = false, TimeOut = false, ReturnLanguage = ShowLanguageType, PopupArgs = null }; CanWindowClose = true; break; case PaymentWindowType.ReadSign: SignReset = true; break; } } private void OkCommandHandler(object obj) { switch (PaymentWindow) { case PaymentWindowType.ProcessingError: PaymentWindow = PaymentWindowType.PaymentStart; break; case PaymentWindowType.PaymentSuccess: if (ProcessTask != null) ProcessTask.Wait(); CanWindowClose = true; break; case PaymentWindowType.ReadSign: SignEnd = true; break; } } private void posLastPayments_ErrorMessageEvent(string ErrorString) { ErrorText = new List { new M_Language { Type = SupportLanguageType.ko, LanguageData = ErrorString.Replace("\n"," ").Replace("\r","") }, }; IsErrorBlink = true; } private void posLastPayments_ErrorEvent(posLastPayments.ErrorCode _errorCode) { //TODO: 공통에 에러 메세지 등록 필요 //ErrorText = Languages.GetMessages(_errorCode.ToString()); var errTxt = string.Empty; switch (_errorCode) { case posLastPayments.ErrorCode.LastPaymentsCardNumberError: errTxt = "카드를 리딩에 실패했습니다. \n다시 결제 버튼을 눌러 다시 시도하세요."; break; case posLastPayments.ErrorCode.LastPaymentsCardReadingError: errTxt = "카드를 리딩에 실패했습니다. \n다시 결제 버튼을 눌러 다시 시도하세요."; break; case posLastPayments.ErrorCode.LastPaymentsEncrtipError: errTxt = "암호화 에러가 발생했습니다."; break; case posLastPayments.ErrorCode.LastPaymentsNotInitailzedError: errTxt = "초기화 에러가 발생했습니다."; break; case posLastPayments.ErrorCode.LastPaymentsProcessingTimeOut: errTxt = "처리시간이 초과되었습니다. \n재결제 버튼을 눌러 다시 시도하세요."; break; case posLastPayments.ErrorCode.LastPaymentsUnknownError: errTxt = "에러가 발생했습니다."; break; default: break; } ErrorText = new List { new M_Language { Type = SupportLanguageType.ko, //LanguageData = _errorCode.ToString() LanguageData = errTxt }, }; IsErrorBlink = true; } private void posLastPayments_ReadStart(object sender) { TimerEnabled = false; ProcessStart = true; switch (PaymentType) { case LastPaymentsType.IcCard: case LastPaymentsType.SsLgPay: case LastPaymentsType.CashBee: case LastPaymentsType.HappyGift: case LastPaymentsType.Tmoney: case LastPaymentsType.SpcEmployee: case LastPaymentsType.SmilePay: PaymentWindow = PaymentWindowType.PaymentProcessing; break; } } private void posLastPayments_ReadEnd(object sender) { ProcessStart = false; try { if (sender is posLastPayments getposLastPayments) { if (getposLastPayments.ProcessOK) { PaymentReturn = new M_PaymentReturn { IsProcessOK = true, PaymentsTotal = getposLastPayments.PaymentsValue, PaySEQ = getposLastPayments.PaySEQ, }; ReturnValue = new M_PopupReturn { OKAnswer = true, TimeOut = false, ReturnLanguage = ShowLanguageType, PopupArgs = PaymentReturn }; } } } catch (Exception ex) { CommonLog.ErrorLogWrite(this, "posLastPayments_ReadEnd()", "Fail", string.Format("{0}\n{1}", ex.Message, ex.StackTrace)); } } private void VmHappyPointCertify_PropertyChanged(object sender, PropertyChangedEventArgs e) { try { switch (e.PropertyName) { case "IsTimeout": if (IsTimeout) { #region 입력시간 초과시 switch (PaymentWindow) { case PaymentWindowType.PaymentStart: case PaymentWindowType.ProcessingError: ReturnValue = new M_PopupReturn { OKAnswer = false, TimeOut = true, ReturnLanguage = ShowLanguageType, PopupArgs = null }; break; } if (!ProcessStart) CanWindowClose = true; #endregion } break; case "PaymentWindow": #region 창 모두 안보이게 하기 PaymentStartVisible = false; ReadSignVisible = false; PaymentProcessingVisible = false; ProcessingErrorVisible = false; PaymentSuccessVisible = false; #endregion #region 선택창 보이기 switch (PaymentWindow) { case PaymentWindowType.PaymentStart: PaymentStartVisible = true; break; case PaymentWindowType.ReadSign: ReadSignVisible = true; break; case PaymentWindowType.PaymentProcessing: PaymentProcessingVisible = true; break; case PaymentWindowType.ProcessingError: ProcessingErrorVisible = true; break; case PaymentWindowType.PaymentSuccess: PaymentSuccessVisible = true; break; } #endregion break; case "PaymentType": switch (PaymentType) { case LastPaymentsType.IcCard: TitileImage = ResourceManager.GetNximagePathAdd("img_gif_card.gif", CommonValue.PBdesignImagesPath); ScanGuidText = Languages.GetMessages("LBL0084"); break; case LastPaymentsType.SsLgPay: TitileImage = ResourceManager.GetNximagePathAdd("img_gif_pay.gif", CommonValue.PBdesignImagesPath); ScanGuidText = Languages.GetMessages("LBL0083"); break; case LastPaymentsType.SmilePay: TitileImage = ResourceManager.GetNximagePathAdd("img_gif_barcode.gif", CommonValue.PBdesignImagesPath); ScanGuidText = Languages.GetMessages("LBL0082"); break; } break; case "PaymentStartVisible": if (PaymentStartVisible) { #region 카드 또는 신용카드 리딩 처리 InfoGuid1Text = new List(); InfoGuid2Text = new List(); InfoGuid1TextAlignment = TextAlignment.Left; CancelButtonText = Languages.GetMessages("BTN0033"); CancelButtonWidth = PopupWidth; OKButtonWidth = 0; TimeOutSeconds = CommonValue.TimeOutSeconds; switch (PaymentType) { case LastPaymentsType.IcCard: case LastPaymentsType.SsLgPay: case LastPaymentsType.CashBee: case LastPaymentsType.HappyGift: case LastPaymentsType.Tmoney: case LastPaymentsType.SpcEmployee: if (ProcessTask != null) ProcessTask.Wait(); ProcessTask = new Task(DoPaymentProcess); ProcessTask.Start(); break; } #endregion } break; case "ReadSignVisible": if (ReadSignVisible) { ScanGuidText = Languages.GetMessages("LBL0072"); InfoGuid1Text = Languages.GetMessages("LBL0066"); InfoGuid2Text = new List { new M_Language { Type = SupportLanguageType.ko, LanguageData = "* 싸인이 너무 짧거나 길면 확인 버튼이 활성화 되지 않습니다." }, new M_Language { Type = SupportLanguageType.en, LanguageData = "en* * * 싸인이 너무 짧거나 길면 확인 버튼이 활성화 되지 않습니다." }, }; CancelButtonText = Languages.GetMessages("BTN0007"); CancelButtonWidth = PopupWidth / 2; OkButtonText = Languages.GetMessages("BTN0024"); OKButtonWidth = PopupWidth / 2; OKButtonEnabled = false; } break; case "PaymentProcessingVisible": if (PaymentProcessingVisible) { if (PaymentType == LastPaymentsType.SmilePay) { //ScanGuidText = Languages.GetMessages("LBL0087"); ProcessingMessageText = Languages.GetMessages("LBL0087"); } else { ScanGuidText = Languages.GetMessages("LBL0032"); } InfoGuid1Text = new List(); InfoGuid2Text = new List(); OKButtonWidth = 0d; CancelButtonWidth = 0d; if (!SignEnd) { switch (PaymentType) { case LastPaymentsType.SmilePay: case LastPaymentsType.AliPay: case LastPaymentsType.ZeroPay: if (ProcessTask != null) ProcessTask.Wait(); ProcessTask = new Task(DoPaymentProcess); ProcessTask.Start(); break; } } } break; case "ProcessingErrorVisible": if (ProcessingErrorVisible) { #region 처리 실패 오류 창 ScanGuidText = new List(); InfoGuid1Text = new List(); InfoGuid2Text = new List(); InfoGuid1TextAlignment = TextAlignment.Left; CancelButtonText = Languages.GetMessages("BTN0003"); CancelButtonWidth = PopupWidth / 2; OkButtonText = Languages.GetMessages("BTN0006"); OKButtonWidth = PopupWidth / 2; OKButtonEnabled = true; TimeOutSeconds = 10d; #endregion } break; case "PaymentSuccessVisible": if (PaymentSuccessVisible) { if (PaymentType == LastPaymentsType.SmilePay) { ScanGuidText = Languages.GetMessages("LBL0087"); } else { ScanGuidText = Languages.GetMessages("LBL0029"); } InfoGuid1Text = new List(); InfoGuid2Text = new List(); OKButtonWidth = 0d; CancelButtonWidth = 0d; ImmediatelyClose = true; TimeOutSeconds = 0d; } break; case "ReadBarCode": if (!ProcessStart) { switch (PaymentType) { case LastPaymentsType.SmilePay: case LastPaymentsType.AliPay: case LastPaymentsType.ZeroPay: switch (ReadBarCode.Length) { case 16: case 17: certifyKey = ReadBarCode.Substring(0, 16); FormatedBarCode = ReadBarCode.GetEncriptCardNo(); PaymentWindow = PaymentWindowType.PaymentProcessing; break; default: FormatedBarCode = string.Empty; break; } break; } } break; case "UseableSign": OKButtonEnabled = UseableSign; break; case "SignData": if (PosLastPayments != null && !string.IsNullOrEmpty(SignData)) { PosLastPayments.SignData = SignData; PosLastPayments.SignEndStatus = true; } break; } } catch (Exception ex) { CommonLog.ErrorLogWrite(this, "VmHappyPointCertify_PropertyChanged()", "Fail", string.Format("{0}\n{1}", ex.Message, ex.StackTrace)); } } #endregion Event Handlers } }