public override Task <PaymentProviderBeginPaymentResponse> BeginPaymentAsync(BeginPaymentRequest request, System.Threading.CancellationToken cancellation)
        {
            var tcs = new TaskCompletionSource <PaymentProviderBeginPaymentResponse>();

            try
            {
                var pec = new ir.shaparak.pec.EShopService();

                pec.PinPaymentRequestCompleted += new PinPaymentRequestCompletedEventHandler((sender, args) =>
                {
                    if (args.Error != null)
                    {
                        tcs.TrySetException(args.Error);
                    }
                    else
                    if (!args.Cancelled)
                    {
                        try
                        {
                            var result = new PaymentProviderBeginPaymentResponse <AyandehBankTranStatus>(Now);

                            result.ProviderType = this.ProviderType;
                            result.Code         = args.authority;
                            result.SendData.Add(BankCodeArgName, args.authority.ToString());
                            result.Status     = args.status;
                            result.Succeeded  = (args.status == 0 && args.authority != -1);
                            result.GatewayUrl = Config.GatewayUrl + $"?{BankCodeArgName}={args.authority}";
                            result.SendMethod = Config.GatewayMethod;

                            tcs.SetResult(result);
                        }
                        catch (Exception ex)
                        {
                            tcs.TrySetException(ex);
                        }
                    }
                    else
                    {
                        tcs.SetCanceled();
                    }

                    pec.Dispose();
                });

                var  orderid   = SafeClrConvert.ToInt32(request.PaymentCode);
                var  pay       = SafeClrConvert.ToInt32(request.Amount);
                byte pecStatus = 0;
                long auth      = 0;

                cancellation.Register(() => pec.CancelAsync(request.PaymentCode));

                pec.PinPaymentRequestAsync(Config.Credentials.Pin, pay, orderid, (string.IsNullOrEmpty(request.ReturnUrl) ? Config.CallbackUrl : request.ReturnUrl), auth, pecStatus, request.PaymentCode);
            }
            catch (Exception e)
            {
                tcs.SetException(e);
            }

            return(tcs.Task);
        }
        public override Task <ShetabPaymentStep1> BeginPaymentAsync(string paymentcode, decimal amount, string returnUrl, System.Threading.CancellationToken cancellation)
        {
            var tcs = new TaskCompletionSource <ShetabPaymentStep1>();

            try
            {
                var pec = new ir.shaparak.pec.EShopService();

                pec.PinPaymentRequestCompleted += new PinPaymentRequestCompletedEventHandler((sender, args) =>
                {
                    if (args.Error != null)
                    {
                        tcs.TrySetException(args.Error);
                    }
                    else
                    if (!args.Cancelled)
                    {
                        try
                        {
                            var result      = new ShetabPaymentStep1 <ParsianBankTranStatus>();
                            result.BankType = this.BankType;
                            result.Code     = args.authority;
                            result.SendData.Add("au", args.authority.ToString());
                            result.Status     = args.status;
                            result.Succeeded  = (args.status == 0 && args.authority != -1);
                            result.GatewayUrl = Config.GatewayUrl + "?au=" + args.authority.ToString();
                            result.SendMethod = Config.GatewayMethod;

                            tcs.SetResult(result);
                        }
                        catch (Exception ex)
                        {
                            tcs.TrySetException(ex);
                        }
                    }
                    else
                    {
                        tcs.SetCanceled();
                    }

                    pec.Dispose();
                });

                var  orderid   = SafeClrConvert.ToInt32(paymentcode);
                var  pay       = SafeClrConvert.ToInt32(amount);
                byte pecStatus = 0;
                long auth      = 0;

                cancellation.Register(() => pec.CancelAsync(paymentcode));

                pec.PinPaymentRequestAsync(Config.Credentials.Pin, pay, orderid, returnUrl, auth, pecStatus, paymentcode);
            }
            catch (Exception e)
            {
                tcs.SetException(e);
            }

            return(tcs.Task);
        }