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

            try
            {
                var service = new com.zarinpal.www.PaymentGatewayImplementationService();

                service.PaymentRequestCompleted += new PaymentRequestCompletedEventHandler((object sender, PaymentRequestCompletedEventArgs args) =>
                {
                    if (args.Error != null)
                    {
                        tcs.TrySetException(args.Error);
                    }
                    else
                    if (!args.Cancelled)
                    {
                        try
                        {
                            var result          = new PaymentProviderBeginPaymentResponse <ZarinPalBankTranStatus>(Now);
                            result.ProviderType = this.ProviderType;
                            result.Code         = SafeClrConvert.ToLong(args.Authority);
                            result.Status       = args.Result;
                            result.Succeeded    = (args.Result == 100);
                            result.GatewayUrl   = Config.GatewayUrl + $"/{result.Code}";
                            result.SendMethod   = Config.GatewayMethod;
                            result.StrongStatus = args.Result.ToEnum <ZarinPalBankTranStatus>();

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

                    service.Dispose();
                });

                var amount = SafeClrConvert.ToInt32(request.Amount);

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

                service.PaymentRequestAsync(Config.Credentials.MerchantId, amount, request.Info, "", "", (string.IsNullOrEmpty(request.ReturnUrl) ? Config.CallbackUrl : request.ReturnUrl));
            }
            catch (Exception e)
            {
                tcs.SetException(e);
            }

            return(tcs.Task);
        }
Esempio n. 2
0
        protected override Task <PaymentProviderEndPaymentResponse> endPaymentAsync(IDictionary <string, string> request, string code, string status, CancellationToken cancellation)
        {
            var tcs = new TaskCompletionSource <PaymentProviderEndPaymentResponse>();

            try
            {
                var service = new com.zarinpal.www.PaymentGatewayImplementationService();

                service.PaymentVerificationCompleted += new PaymentVerificationCompletedEventHandler((object sender, PaymentVerificationCompletedEventArgs args) =>
                {
                    if (args.Error != null)
                    {
                        tcs.TrySetException(args.Error);
                    }
                    else
                    if (!args.Cancelled)
                    {
                        try
                        {
                            var result = new PaymentProviderEndPaymentResponse <ZarinPalBankTranStatus>(Now);

                            result.Query        = code;
                            result.Code         = args.RefID;
                            result.Status       = args.Result;
                            result.Succeeded    = result.Status.ToString() == "100";
                            result.StrongStatus = args.Result.ToEnum <ZarinPalBankTranStatus>();
                            result.ProviderType = this.ProviderType;

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

                    service.Dispose();
                });

                var authority = code;
                var amount    = SafeClrConvert.ToInt32(request["Amount"]);

                if (!EndPaymentSucceeded(status))
                {
                    var result = new PaymentProviderEndPaymentResponse <ZarinPalBankTranStatus>();

                    result.Query        = authority;
                    result.Status       = status;
                    result.Succeeded    = false;
                    result.ProviderType = this.ProviderType;

                    tcs.SetResult(result);
                }
                else
                {
                    cancellation.Register(() => service.CancelAsync(authority));

                    service.PaymentVerificationAsync(Config.Credentials.MerchantId, authority, amount, authority);
                }
            }
            catch (Exception e)
            {
                tcs.SetException(e);
            }

            return(tcs.Task);
        }