Пример #1
0
 private Task GetBackgroundPaymentState(BackgroundPaymentParams _params, CancellationToken token)
 {
     return(Task.Factory.StartNew(() =>
     {
         while (true)
         {
             var result = ApiManager.Post <BackgroundPaymentParams, ApiRetrunResult <int> >(@"api/GetBackgroundPaymentState", _params);
             if (token.IsCancellationRequested)
             {
                 return;
             }
             if (result.Code == "200")
             {
                 if (result.Result == 1)
                 {
                     Application.Current.Dispatcher.Invoke(new Action(() =>
                     {
                         PosViewModel.Current.MultiPayItemViewModel = null;
                         IsSuccess = true;
                         EnableClose = false;
                         Message = string.Format("成功支付{0:N2}元!", Amount);
                         return;
                     }));
                 }
                 else if (result.Result == -1)
                 {
                     Application.Current.Dispatcher.Invoke(new Action(() =>
                     {
                         IsSuccess = false;
                         Message = "支付失败,请按F5重新发起支付!";
                         return;
                     }));
                 }
                 else
                 {
                     Thread.Sleep(1000);
                 }
             }
         }
     }));
 }
Пример #2
0
        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;
            }
        }