Esempio n. 1
0
        public async Task <IActionResult> RequestByOrderPayment(int id)
        {
            try
            {
                //_logRepository.Add(new Log() { Text = "1" });
                //_logRepository.Add(new Log() { Text = "1.=>"+ _bankConfig.TerminalId });
                //_logRepository.Add(new Log() { Text = "2.=>"+ _bankConfig.PurchasePage });
                // MFile.append("mohammad.txt", "1");
                if (!User.Identity.IsAuthenticated)
                {
                    return(RedirectToAction("Login", "Account"));
                }

                var factorInfo = await _shopOrderPaymentRepository
                                 .GetByConditionAsync(a => !a.IsSuccess && a.Id == id, isTracked : true);

                #region BankDependency

                if (factorInfo == null)
                {
                    TempData.AddResult(SweetAlertExtenstion.Error("اطلاعات پرداختی با این عنوان یافت نشد"));
                    return(RedirectToAction("Index", "ShopProductController"));
                }

                var resultAmount = factorInfo.PaymentAmount;


                _logRepository.Add(new Log()
                {
                    Text = "2=>" + resultAmount.ToString()
                });
                // شماره خرید
                var OrderId = new Random().Next(1000, int.MaxValue).ToString();
                //_logRepository.Add(new Log() { Text = "2.1" + _bankConfig.PurchasePage });
                // رمز گذاری اطلاعات
                var dataBytes = Encoding.UTF8.GetBytes(string.Format("{0};{1};{2}", _bankConfig.TerminalId, OrderId, resultAmount.CastTomanToRial()));
                //_logRepository.Add(new Log() { Text = "2.2" + _bankConfig.PurchasePage });
                var symmetric = SymmetricAlgorithm.Create("TripleDes");
                //_logRepository.Add(new Log() { Text = "2.3" + _bankConfig.PurchasePage });
                symmetric.Mode = CipherMode.ECB;
                //_logRepository.Add(new Log() { Text = "2.4" + _bankConfig.PurchasePage });
                symmetric.Padding = PaddingMode.PKCS7;
                //_logRepository.Add(new Log() { Text = "2.5" + _bankConfig.PurchasePage });

                // رمز گذاری گلید پایانه
                var encryptor = symmetric.CreateEncryptor(Convert.FromBase64String(_bankConfig.MerchantKey), new byte[8]);
                //_logRepository.Add(new Log() { Text = "2.6" + _bankConfig.PurchasePage });
                var SignData = Convert.ToBase64String(encryptor.TransformFinalBlock(dataBytes, 0, dataBytes.Length));
                //_logRepository.Add(new Log() { Text = "2.7" + _bankConfig.PurchasePage });
                // ادرس بازگشت از درگاه
                var ReturnUrl = string.Format(_bankConfig.SecondReturnUrl);
                //_logRepository.Add(new Log() { Text = "2.8" + _bankConfig.PurchasePage });

                // ادرس وب سرویس درگاه
                var ipgUri = string.Format("{0}/api/v0/Request/PaymentRequest", _bankConfig.PurchasePage);
                //_logRepository.Add(new Log() { Text = "2.9" + _bankConfig.PurchasePage });
                #endregion

                #region Informations

                // آماده سازی اطلاعات برای ا
                var data = new
                {
                    _bankConfig.TerminalId,
                    _bankConfig.MerchantId,
                    Amount = resultAmount.CastTomanToRial(),
                    SignData,
                    ReturnUrl     = _bankConfig.SecondReturnUrl,
                    LocalDateTime = DateTime.Now,
                    OrderId,
                    //MultiplexingData = request.MultiplexingData
                };


                _logRepository.Add(new Log()
                {
                    Text = "Data=>" + JsonConvert.SerializeObject(data)
                });
                //_logRepository.Add(new Log() { Text = "ipgUri=>" + ipgUri });

                #endregion


                #region RequestBuild

                var res = ManageBankService.CallApi <BankResultViewModel>(ipgUri, data);
                res.Wait();
                _logRepository.Add(new Log()
                {
                    Text = "3Status=>" + res.Status
                });
                //_logRepository.Add(new Log() { Text = "3ResCode=>" + res.Result?.ResCode });
                #endregion

                #region Request Result

                if (res != null && res.Result != null)
                {
                    _logRepository.Add(new Log()
                    {
                        Text = "3=>" + res.Result.ResCode
                    });
                    if (res.Result.ResCode == "0")
                    {
                        factorInfo.OrderId = OrderId;

                        await _shopProductRepository.UpdateCreateDate(factorInfo.ShopOrderId);

                        await _shopOrderRepository.UpdateCreateDate(factorInfo.ShopOrderId);



                        await _usersPaymentRepository.MapAddAsync(SetValue(res.Result.Token));

                        //await _shopOrderRepository.UpdateAsync(factorInfo);
                        await _shopOrderPaymentRepository.UpdateAsync(factorInfo);

                        return(Redirect(string.Format("{0}/Purchase/Index?token={1}", _bankConfig.PurchasePage, res.Result.Token)));
                    }
                    //TempData["Result"] = res.Result.Description + " + " + string.Format("{0}/Purchase/Index?token={1}", _bankConfig.PurchasePage, res.Result.Token);
                    //MFile.append("mohammad.txt", "4=>" + res.Result.Description + " + " + string.Format("{0}/Purchase/Index?token={1}", _bankConfig.PurchasePage, res.Result.Token));
                    //_logRepository.Add(new Log() { Text = "4=>" + res.Result.Description });
                    return(RedirectToAction("BankMessage"));
                }
                #endregion

                TempData["Result"] = res.Result?.Description;

                return(RedirectToAction("BankMessage"));

                #region LocalMethods

                AddUserPaymentViewModel SetValue(string token)
                {
                    return(new AddUserPaymentViewModel()
                    {
                        Amount = data.Amount,
                        DateTime = data.LocalDateTime,
                        OrderId = data.OrderId,
                        Token = token,
                        UserId = UserId,
                        ShopOrderId = factorInfo.ShopOrderId,
                        PaymentId = id,
                    });
                }
                #endregion
            }
            catch (Exception e)
            {
                _logRepository.Add(new Log()
                {
                    Text = "Exception=>" + e.Message + e.TargetSite
                });
                throw;
            }
        }
Esempio n. 2
0
        public async Task <IActionResult> RequestBuilder(int id)
        {
            if (!User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("Login", "Account"));
            }

            var factorInfo = await _shopOrderRepository.GetByIdAsync(id);

            #region BankDependency

            if (factorInfo == null)
            {
                TempData.AddResult(SweetAlertExtenstion.Error("اطلاعات سبد خریدی با این عنوان یافت نشد"));
                return(RedirectToAction("Index", "ShopProductController"));
            }

            var resultAmount = factorInfo.Amount + (factorInfo.TransferProductPrice ?? 0);

            // شماره خرید
            var OrderId = new Random().Next(1000, int.MaxValue).ToString();

            // رمز گذاری اطلاعات
            var dataBytes = Encoding.UTF8.GetBytes(string.Format("{0};{1};{2}", _bankConfig.TerminalId, OrderId, resultAmount.CastTomanToRial()));

            var symmetric = SymmetricAlgorithm.Create("TripleDes");
            symmetric.Mode    = CipherMode.ECB;
            symmetric.Padding = PaddingMode.PKCS7;

            // رمز گذاری گلید پایانه
            var encryptor = symmetric.CreateEncryptor(Convert.FromBase64String(_bankConfig.MerchantKey), new byte[8]);
            var SignData  = Convert.ToBase64String(encryptor.TransformFinalBlock(dataBytes, 0, dataBytes.Length));

            // ادرس بازگشت از درگاه
            var ReturnUrl = string.Format(_bankConfig.ReturnUrl);

            // ادرس وب سرویس درگاه
            var ipgUri = string.Format("{0}/api/v0/Request/PaymentRequest", _bankConfig.PurchasePage);

            #endregion

            #region Informations

            // آماده سازی اطلاعات برای ا
            var data = new
            {
                _bankConfig.TerminalId,
                _bankConfig.MerchantId,
                Amount = resultAmount.CastTomanToRial(),
                SignData,
                _bankConfig.ReturnUrl,
                LocalDateTime = DateTime.Now,
                OrderId,
                //MultiplexingData = request.MultiplexingData
            };

            #endregion

            #region RequestBuild

            var res = ManageBankService.CallApi <BankResultViewModel>(ipgUri, data);
            res.Wait();

            #endregion

            #region Request Result

            if (res != null && res.Result != null)
            {
                if (res.Result.ResCode == "0")
                {
                    factorInfo.OrderId = OrderId;

                    await _usersPaymentRepository.MapAddAsync(SetValue(res.Result.Token));

                    await _shopOrderRepository.UpdateAsync(factorInfo);

                    return(Redirect(string.Format("{0}/Purchase/Index?token={1}", _bankConfig.PurchasePage, res.Result.Token)));
                }
                TempData["Result"] = res.Result.Description + " + " + string.Format("{0}/Purchase/Index?token={1}", _bankConfig.PurchasePage, res.Result.Token);

                return(RedirectToAction("BankMessage"));
            }

            #endregion

            TempData["Result"] = res.Result.Description;

            return(RedirectToAction("BankMessage"));

            #region LocalMethods

            AddUserPaymentViewModel SetValue(string token)
            {
                return(new AddUserPaymentViewModel()
                {
                    Amount = data.Amount,
                    DateTime = data.LocalDateTime,
                    OrderId = data.OrderId,
                    Token = token,
                    UserId = UserId,
                    ShopOrderId = id
                });
            }

            #endregion
        }