public bool DoPay(CancellationToken token, POSDevicePayRequest request, out POSDevicePayResponse response, out string msg) { msg = ""; response = null; try { // return CallDevice(token, request, out response, out msg); SerialPort connection = NewLandPOSDeviceConnectTool.OpenSerialPort(token, new SerialPortRequest() { ComPort = Global.MachineSettings.DevicesSettingsConfiguration.POSCOM, BaudRate = Global.MachineSettings.DevicesSettingsConfiguration.POSCOMRate }); var cmd = NewLandPOSDeviceConnectTool.FormatDeviceCommand(request); NewLandPOSDeviceConnectTool.SendCommandToSerialPortConnection(token, connection, cmd); response = NewLandPOSDeviceConnectTool.ListenToSerialPortConnection(token, connection); msg = string.Format("成功支付{0}元!", request.Amount); return(true); } catch (DeviceException ex) { msg = ex.Message; return(false); } catch (Exception ex) { msg = "连接设备失败!" + ex.Message; return(false); } }
public bool ReadCard(CancellationToken token, decimal amount, out Models.PosModels.StoreValueCardInfomactions info, out string msg) { info = null; msg = ""; var request = new POSDevicePayRequest() { MachineSn = Global.MachineSettings.MachineInformations.MachineSn, Amount = amount, CashierId = PosViewModel.Current.UserCode, OldTransactionCode = "", OrderSn = PosViewModel.Current.OrderSn, Type = TransactionType.ReadCard }; POSDevicePayResponse response = null; try { // return CallDevice(token, request, out response, out msg); SerialPort connection = NewLandPOSDeviceConnectTool.OpenSerialPort(token, new SerialPortRequest() { ComPort = Global.MachineSettings.DevicesSettingsConfiguration.POSCOM, BaudRate = Global.MachineSettings.DevicesSettingsConfiguration.POSCOMRate }); var cmd = NewLandPOSDeviceConnectTool.FormatDeviceCommand(request); NewLandPOSDeviceConnectTool.SendCommandToSerialPortConnection(token, connection, cmd); response = NewLandPOSDeviceConnectTool.ListenToSerialPortConnection(token, connection); info = new StoreValueCardInfomactions() { CardNo = response.CardNo.Trim(), Password = response.CardPin.Trim() }; return(true); } catch (DeviceException ex) { msg = ex.Message; return(false); } catch (Exception ex) { msg = "连接设备是失败!失败信息:" + ex.Message; return(false); } }
public void DoPay() { IsRequestEnd = false; this.OnPropertyChanged(o => o.IsRequestEnd); switch (PayItem.Mode) { case PayMode.StoredValueCard: { Task.Factory.StartNew(() => { bool isSuccess; var password = string.Empty; if (string.IsNullOrEmpty(CardNo) || isRetry) //将来设备完整后要去掉 { if (source != null) { source.Cancel(); Thread.Sleep(200); } source = new CancellationTokenSource(); token = source.Token; Application.Current.Dispatcher.Invoke(new Action(() => { Message = "请操作读卡设备进行收款!【如果设备未响应,请检查设备连接并按“F5”重试】"; IsSuccess = null; IsRunning = true; })); var device = StoredValueCardDeviceFactory.Create(); StoreValueCardInfomactions info; string msg; isSuccess = device.ReadCard(token, Amount, out info, out msg); source = null; Application.Current.Dispatcher.Invoke(new Action(() => { IsSuccess = isSuccess; if (!isSuccess) { isRetry = false; Message = msg + "【按F5重新发起支付】"; } else { Message = msg + "正在等待后台回应!"; } if (info != null) { CardNo = info.CardNo; password = info.Password; } else { CardNo = string.Empty; password = string.Empty; } IsRunning = false; IsRequestEnd = true; this.OnPropertyChanged(o => o.IsRequestEnd); })); } else { isSuccess = true; } if (isSuccess) { Application.Current.Dispatcher.Invoke(new Action(() => { MachineInformations _machineInfo = Global.MachineSettings.MachineInformations; BackgroundPaymentParams _params = new BackgroundPaymentParams() { StoreId = _machineInfo.StoreId, MachineSn = _machineInfo.MachineSn, CID = _machineInfo.CompanyId, Amount = Amount, CardNo = _CardNo, CardPassword = password, Mode = PayMode.StoredValueCard }; var result = ApiManager.Post <BackgroundPaymentParams, ApiRetrunResult <StoredValueCardInfo, StoredValueCardInfo> >(@"api/BackgroundPayment", _params); if (result.Code == "200") { HasOperat = false; Balance = result.Result.Balance; User = result.Result.User; PayAmount = result.Result.PayAmount; this.OnPropertyChanged(o => o.HasOperat); this.OnPropertyChanged(o => o.Balance); this.OnPropertyChanged(o => o.PayAmount); this.OnPropertyChanged(o => o.User); this.OnPropertyChanged(o => o.CardNo); isRetry = false; EnableClose = false; Message = string.Format("成功支付{0:N}元!", Amount); } else { if (result.ErrorInfo != null) { Balance = result.ErrorInfo.Balance; User = result.ErrorInfo.User; PayAmount = result.ErrorInfo.PayAmount; this.OnPropertyChanged(o => o.Balance); this.OnPropertyChanged(o => o.PayAmount); this.OnPropertyChanged(o => o.CardNo); this.OnPropertyChanged(o => o.User); } isRetry = false; IsSuccess = false; Message = result.Message + "【按F5重新发起支付】"; } IsRequestEnd = true; this.OnPropertyChanged(o => o.IsRequestEnd); })); } }); } break; case PayMode.UnionPayCTPOSM: { Task.Factory.StartNew(() => { if (source != null) { source.Cancel(); Thread.Sleep(500); } source = new CancellationTokenSource(); token = source.Token; Application.Current.Dispatcher.Invoke(new Action(() => { Message = "请操作POS银联机进行收款!【如果设备未响应,请检查设备连接并按“F5”重试】"; IsSuccess = null; IsRunning = true; })); var posDevice = POSDeviceFactory.Create(); string msg; var request = new POSDevicePayRequest() { Amount = Amount, CashierId = PosViewModel.Current.UserCode, MachineSn = Global.MachineSettings.MachineInformations.MachineSn, OldTransactionCode = "", OrderSn = PosViewModel.Current.OrderSn, Type = Amount >= 0 ? TransactionType.Consumption : TransactionType.Refund }; POSDevicePayResponse response; var isSuccess = posDevice.DoPay(token, request, out response, out msg); source = null; Application.Current.Dispatcher.Invoke(new Action(() => { IsSuccess = isSuccess; EnableClose = !isSuccess; if (!isSuccess) { msg += "【按F5重新发起支付】"; } else { HasOperat = false; } Message = msg; if (response != null) { CardNo = response.CardNo; CardName = response.CardName; } IsRunning = false; IsRequestEnd = true; this.OnPropertyChanged(o => o.IsRequestEnd); })); }); } break; case PayMode.RongHeDynamicQRCodePay: IsSuccess = null; Message = "正在请求支付,请稍后!"; Task.Factory.StartNew(() => { if (source != null) { source.Cancel(); Thread.Sleep(500); } source = new CancellationTokenSource(); token = source.Token; MachineInformations _machineInfo = Global.MachineSettings.MachineInformations; BackgroundPaymentParams _params = new BackgroundPaymentParams() { StoreId = _machineInfo.StoreId, MachineSn = _machineInfo.MachineSn, CID = _machineInfo.CompanyId, Amount = Amount, CardNo = _CardNo, CardPassword = string.Empty, Mode = PayMode.RongHeDynamicQRCodePay }; var result = ApiManager.Post <BackgroundPaymentParams, ApiRetrunResult <JObject> >(@"api/BackgroundPayment", _params); Application.Current.Dispatcher.Invoke(new Action(() => { if (result.Code == "200") { HasOperat = false; var qrCodeEncoder = new QrEncoder(); var qrCode = qrCodeEncoder.Encode(result.Result.Property("PayToken").ToString()); var renderer = new GraphicsRenderer(new FixedModuleSize(60, QuietZoneModules.Zero), Brushes.Black, Brushes.White); PayOrderSn = result.Result.Property("PayOrderSn").ToString(); using (var stream = new MemoryStream()) { renderer.WriteToStream(qrCode.Matrix, ImageFormat.Bmp, stream); BitmapImage bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = stream; bitmapImage.CacheOption = BitmapCacheOption.OnLoad; bitmapImage.EndInit(); bitmapImage.Freeze(); PayItem.RongHeDynamicQRCode = bitmapImage; } PayItem.QrContent = result.Result.Property("PayToken").ToString(); IsSuccess = null; Message = "请使用微信或者支付宝扫描此二维码进行支付!"; PosViewModel.Current.MultiPayItemViewModel = this; GetBackgroundPaymentState(_params, token); } else { IsSuccess = false; Message = result.Message; } IsRequestEnd = true; this.OnPropertyChanged(o => o.IsRequestEnd); })); }); break; case PayMode.RongHeCustomerDynamicQRCodePay: IsSuccess = null; Message = "正在请求支付,请稍后!"; Task.Factory.StartNew(() => { if (source != null) { source.Cancel(); Thread.Sleep(500); } source = new CancellationTokenSource(); token = source.Token; MachineInformations _machineInfo = Global.MachineSettings.MachineInformations; BackgroundPaymentParams _params = new BackgroundPaymentParams() { StoreId = _machineInfo.StoreId, MachineSn = _machineInfo.MachineSn, CID = _machineInfo.CompanyId, Amount = Amount, CardNo = _CardNo, CardPassword = string.Empty, Mode = PayMode.RongHeCustomerDynamicQRCodePay }; var result = ApiManager.Post <BackgroundPaymentParams, ApiRetrunResult <string> >(@"api/BackgroundPayment", _params); Application.Current.Dispatcher.Invoke(new Action(() => { if (result.Code == "200") { HasOperat = false; EnableClose = false; IsSuccess = true; Message = string.Format("成功支付{0:N2}元!", Amount); PayOrderSn = result.Result; PosViewModel.Current.MultiPayItemViewModel = null; } else { IsSuccess = false; Message = result.Message; } IsRequestEnd = true; this.OnPropertyChanged(o => o.IsRequestEnd); })); }); break; } }