public async Task <IActionResult> PutCustomerOrderPayment(UpdateOutstandingBalance_View outstandingbalnceView)
        {
            int payment = outstandingbalnceView.orderPaymentList[0].AmountPaid;

            foreach (CustomerOrderPayment orderPayment in outstandingbalnceView.orderPaymentList)
            {
                CustomerOrderPayment orderPaymentBeingUpdate = _context.CustomerOrderPayment.Where(obj => obj.AgentOrderStatusId == orderPayment.AgentOrderStatusId).FirstOrDefault();

                int remainingBalancetoPay = comparewithRemainingBalance(orderPayment.AgentOrderStatusId);
                if (remainingBalancetoPay >= payment)
                {
                    orderPaymentBeingUpdate.AmountPaid += payment;
                    _context.CustomerOrderPayment.Update(orderPaymentBeingUpdate);
                    _context.SaveChanges();
                    break;
                }
                else
                {
                    orderPaymentBeingUpdate.AmountPaid += remainingBalancetoPay;
                    payment -= remainingBalancetoPay;
                    _context.CustomerOrderPayment.Update(orderPaymentBeingUpdate);
                    _context.SaveChanges();
                }
            }

            return(Ok(new { CustomerOrderOutstandingBalance = "updateOutstandingBalanceSuccess" }));
        }
        public SingleResult <string> MakePaymentByOrderId(long customerOrderId)
        {
            try
            {
                var userId        = ClaimPrincipalFactory.GetUserId(User);
                var customerId    = _repository.Customer.FindByCondition(c => c.UserId == userId).Select(c => c.Id).FirstOrDefault();
                var customerOrder = _repository.CustomerOrder.FindByCondition(c => c.Id == customerOrderId && c.CustomerId == customerId).FirstOrDefault();

                if (customerOrder == null)
                {
                    throw new BusinessException(XError.BusinessErrors.InvalidOrder());
                }

                var request = new ZarinPallRequest
                {
                    amount      = (int)((customerOrder.FinalPrice.Value + customerOrder.PostServicePrice) * 10),
                    description = "order NO: " + customerOrder.OrderNo
                };
                var zarinPal = new ZarinPal();
                var res      = zarinPal.Request(request);

                var customerOrderPayment = new CustomerOrderPayment
                {
                    OrderNo          = customerOrder.OrderNo.ToString(),
                    TraceNo          = res.authority,
                    TransactionPrice = customerOrder.FinalPrice,
                    TransactionDate  = DateTime.Now.Ticks,
                    Cdate            = DateTime.Now.Ticks,
                    CuserId          = userId,
                    PaymentPrice     = customerOrder.FinalPrice,
                    FinalStatusId    = 26
                };
                _repository.CustomerOrderPayment.Create(customerOrderPayment);
                _repository.Save();
                var finalres = SingleResult <string> .GetSuccessfulResult("https://www.zarinpal.com/pg/StartPay/" + res.authority);

                _logger.LogData(MethodBase.GetCurrentMethod(), finalres, null, customerOrderId);
                return(finalres);
            }
            catch (Exception e)
            {
                _logger.LogError(e, MethodBase.GetCurrentMethod(), customerOrderId);

                return(SingleResult <string> .GetFailResult(e.Message));
            }
        }
 [HttpPost]  //order payment of specific order after delivery
 public async Task <ActionResult <CustomerOrderPayment> > PostCustomerOrderPayment(CustomerOrderPayment customerOrderPayment)
 {
     _context.CustomerOrderPayment.Add(customerOrderPayment);
     try
     {
         await _context.SaveChangesAsync();
     }
     catch (DbUpdateException)
     {
         if (CustomerOrderPaymentExists(customerOrderPayment.AgentOrderStatusId))
         {
             return(Conflict());
         }
         else
         {
             throw;
         }
     }
     return(Ok(new { CustomerOrderPayment = "PaymentSuccess" }));
     //return CreatedAtAction("GetCustomerOrderPayment", new { id = customerOrderPayment.AgentOrderStatusId }, customerOrderPayment);
 }
        public SingleResult <InsertOrderResultDto> GetProductByIdList_UI(OrderModel order)
        {
            try
            {
                var userId          = ClaimPrincipalFactory.GetUserId(User);
                var cc              = _repository.Customer.FindByCondition(c => c.UserId == userId).FirstOrDefault();
                var customerId      = cc.Id;
                var today           = DateTime.Now.AddDays(-1).Ticks;
                var orerProductList = new List <CustomerOrderProduct>();


                var orderNo = customerId.ToString() + DateTimeFunc.TimeTickToShamsi(DateTime.Now.Ticks).Replace("/", "") +
                              (_repository.CustomerOrder.FindByCondition(c => c.CustomerId == customerId && today > c.Cdate)
                               .Count() + 1).ToString().PadLeft(3, '0');


                order.ProductList.ForEach(c =>
                {
                    var product     = _repository.Product.FindByCondition(x => x.Id == c.ProductId).First();
                    var ofer        = _repository.Offer.FindByCondition(x => x.Id == c.OfferId).FirstOrDefault();
                    var packingType = _repository.ProductPackingType.FindByCondition(x => x.Id == c.PackingTypeId)
                                      .FirstOrDefault();
                    var statusId     = _repository.Status.GetSatusId("CustomerOrderProduct", 2);
                    var orderproduct = new CustomerOrderProduct
                    {
                        OrderCount           = c.Count,
                        ProductId            = c.ProductId,
                        Cdate                = DateTime.Now.Ticks,
                        CuserId              = userId,
                        OrderType            = 1,
                        ProductCode          = product.Coding,
                        ProductIncreasePrice = null,
                        ProductName          = product.Name,
                        ProductPrice         = product.Price,
                        ProductOfferId       = c.OfferId,
                        ProductOfferCode     = ofer?.OfferCode,
                        ProductOfferPrice    = (long?)(ofer != null ? (ofer.Value / 100 * product.Price) : 0),
                        ProductOfferValue    = ofer?.Value,
                        PackingTypeId        = packingType?.PackinggTypeId,
                        PackingWeight        = packingType == null ? 0 : packingType.Weight,
                        PackingPrice         = packingType == null ? 0 : packingType.Price,
                        SellerId             = product.SellerId,
                        Weight               = c.Count * product.Weight,
                        FinalWeight          = (c.Count * product.Weight) + (c.Count * (packingType == null ? 0 : packingType.Weight)),
                        FinalStatusId        = statusId
                    };
                    orerProductList.Add(orderproduct);
                });

                var offer         = _repository.Offer.FindByCondition(c => c.Id == order.OfferId).FirstOrDefault();
                var paking        = _repository.PackingType.FindByCondition(c => c.Id == order.PaymentTypeId).FirstOrDefault();
                var customerOrder = new CustomerOrder
                {
                    Cdate               = DateTime.Now.Ticks,
                    CuserId             = userId,
                    CustomerAddressId   = order.CustomerAddressId,
                    CustomerDescription = order.CustomerDescription,
                    CustomerId          = customerId,
                    FinalStatusId       = _repository.Status.GetSatusId("CustomerOrder", 1),
                    OfferId             = order.OfferId,
                    OrderPrice          = orerProductList.Sum(x =>
                                                              ((x.PackingPrice + x.ProductPrice - x.ProductOfferPrice) * x.OrderCount))
                };

                customerOrder.OrderPrice = orerProductList.Sum(c =>
                                                               (c.ProductPrice + c.PackingPrice - c.ProductOfferPrice) * c.OrderCount);
                customerOrder.OfferPrice =
                    (long?)(customerOrder.OrderPrice * (offer == null ? 0 : offer.Value / 100));
                customerOrder.OfferValue       = (int?)offer?.Value;
                customerOrder.OrderDate        = DateTime.Now.Ticks;
                customerOrder.FinalWeight      = orerProductList.Sum(x => x.FinalWeight);
                customerOrder.OrderNo          = Convert.ToInt64(orderNo);
                customerOrder.OrderProduceTime = 0;
                customerOrder.OrderType        = 1;
                customerOrder.OrderWeight      = customerOrder.FinalWeight;
                customerOrder.PackingPrice     = 0;
                customerOrder.PackingWeight    = 0;
                customerOrder.PaymentTypeId    = order.PaymentTypeId;
                customerOrder.PostServicePrice = 0;
                customerOrder.PostTypeId       = order.PostTypeId;

                //customerOrder.TaxPrice = (long?)((customerOrder.OrderPrice - customerOrder.OfferPrice) * 0.09);
                //customerOrder.TaxValue = 9;
                customerOrder.TaxPrice = 0;
                customerOrder.TaxValue = 9;


                customerOrder.CustomerOrderProduct = orerProductList;
                var toCityId = _repository.CustomerAddress.FindByCondition(c => c.Id == order.CustomerAddressId).Include(c => c.City).Select(c => c.City.PostCode).FirstOrDefault();


                var postType = _repository.PostType.FindByCondition(c => c.Id == order.PostTypeId).FirstOrDefault();
                var payType  = _repository.PaymentType.FindByCondition(c => c.Id == order.PaymentTypeId)
                               .FirstOrDefault();

                if (postType.IsFree.Value)
                {
                    customerOrder.PostServicePrice = 0;
                }
                else
                {
                    var post           = new PostServiceProvider();
                    var postpriceparam = new PostGetDeliveryPriceParam
                    {
                        Price       = (int)customerOrder.OrderPrice.Value,
                        Weight      = (int)customerOrder.FinalWeight.Value,
                        ServiceType = postType?.Rkey ?? 2,// (int)customerOrder.PostTypeId,
                        ToCityId    = (int)toCityId,
                        PayType     = (int)(payType?.Rkey ?? 88)
                    };
                    var postresult = post.GetDeliveryPrice(postpriceparam).Result;
                    if (postresult.ErrorCode != 0)
                    {
                        throw new BusinessException(XError.IncomingSerivceErrors.PostSeerivcError());
                    }
                    customerOrder.PostServicePrice = (postresult.PostDeliveryPrice + postresult.VatTax) / 10;
                }


                customerOrder.FinalPrice = customerOrder.OrderPrice + customerOrder.TaxPrice + customerOrder.PostServicePrice;
                _repository.CustomerOrder.Create(customerOrder);



                var request = new ZarinPallRequest
                {
                    //  amount = (int)((customerOrder.FinalPrice.Value + customerOrder.PostServicePrice) * 10),
                    amount      = (int)((customerOrder.FinalPrice.Value) * 10),
                    description = "order NO: " + customerOrder.OrderNo,
                    metadata    = new ZarinPalRequestMetaData
                    {
                        mobile = "0" + cc.Mobile.ToString(),
                        email  = cc.Email
                    }
                };
                var zarinPal = new ZarinPal();
                var res      = zarinPal.Request(request);

                var customerOrderPayment = new CustomerOrderPayment
                {
                    OrderNo          = customerOrder.OrderNo.ToString(),
                    TraceNo          = res.authority,
                    TransactionPrice = customerOrder.FinalPrice,
                    TransactionDate  = DateTime.Now.Ticks,
                    Cdate            = DateTime.Now.Ticks,
                    CuserId          = userId,
                    PaymentPrice     = customerOrder.FinalPrice,
                    FinalStatusId    = 26
                };
                customerOrder.CustomerOrderPayment.Add(customerOrderPayment);
                _repository.Save();

                var result = new InsertOrderResultDto
                {
                    OrderNo         = customerOrder.OrderNo,
                    CustomerOrderId = customerOrder.Id,
                    BankUrl         = "https://www.zarinpal.com/pg/StartPay/" + res.authority,
                    RedirectToBank  = true,
                    PostPrice       = customerOrder.PostServicePrice
                };
                var finalres = SingleResult <InsertOrderResultDto> .GetSuccessfulResult(result);

                _logger.LogData(MethodBase.GetCurrentMethod(), finalres, null, order);
                return(finalres);
            }
            catch (Exception e)
            {
                _logger.LogError(e, MethodBase.GetCurrentMethod(), order);
                return(SingleResult <InsertOrderResultDto> .GetFailResult(e.Message));
            }
        }
Esempio n. 5
0
        public IActionResult FinalOrderInsert(long customerOrderId, long postTypeId, long paymentTypeId, string customerDescription, string offerCode)
        {
            try
            {
                var custumerOrderProduct =
                    _repository.CustomerOrderProduct.GetCustomerOrderProductFullInfoByCustomerOrderId(customerOrderId);


                custumerOrderProduct.ForEach(c =>
                {
                    c.ProductPrice = c.Product.Price;

                    c.ProductOfferValue = c.Product.ProductOffer
                                          .Where(x => x.FromDate <= timeTick && timeTick <= x.ToDate).Select(x => x.Value)
                                          .DefaultIfEmpty(0).FirstOrDefault();


                    c.ProductOfferCode = c.Product.ProductOffer
                                         .Where(x => x.FromDate <= timeTick && timeTick <= x.ToDate).Select(x => x.OfferCode)
                                         .FirstOrDefault();


                    c.ProductOfferPrice = Convert.ToInt64((c.ProductOfferValue / 100) * c.Product.Price);


                    c.Weight      = c.Product.Weight;
                    c.ProductCode = c.Product.Coding;
                });

                var custumerOrder = _repository.CustomerOrder.FindByCondition(c => c.Id.Equals(customerOrderId)).FirstOrDefault();
                var costomerId    = custumerOrder.CustomerId.Value;
                custumerOrder.PaymentTypeId        = paymentTypeId;
                custumerOrder.PostTypeId           = postTypeId;
                custumerOrder.CustomerDescription  = customerDescription;
                custumerOrder.CustomerOrderProduct = custumerOrderProduct;

                custumerOrder.Weight     = custumerOrderProduct.Sum(c => (c.Weight * c.OrderCount));
                custumerOrder.TaxValue   = 9;
                custumerOrder.OrderPrice = custumerOrderProduct.Where(c => c.Ddate.Equals(null))
                                           .Sum(c => (c.OrderCount * (c.ProductPrice - c.ProductOfferPrice)));



                var customerOfferRecord = _repository.CustomerOffer
                                          .FindByCondition(c => c.CustomerId == costomerId && c.FromDate <= timeTick && timeTick <= c.ToDate && c.OfferCode == offerCode &&
                                                           string.IsNullOrWhiteSpace(c.DuserId) && string.IsNullOrWhiteSpace(c.DaUserId))
                                          .FirstOrDefault();
                custumerOrder.OfferValue = customerOfferRecord != null ? (int?)customerOfferRecord.Value / 100 : 0;
                custumerOrder.OfferPrice = (custumerOrder.OrderPrice) * (custumerOrder.OfferValue / 100);
                custumerOrder.TaxPrice   = (long?)((custumerOrder.OrderPrice - custumerOrder.OfferPrice) * 0.09);


                var postTypeRecord = _repository.PostType.FindByCondition(c => c.Rkey.Equals(postTypeId)).FirstOrDefault();
                custumerOrder.PostPrice = postTypeRecord != null ? postTypeRecord.Price : 0;

                custumerOrder.FinalPrice =
                    ((custumerOrder.OrderPrice - custumerOrder.OfferPrice) + custumerOrder.TaxPrice + custumerOrder.PostPrice);

                _repository.CustomerOrder.Update(custumerOrder);
                if (customerOfferRecord != null)
                {
                    var userid = User.Claims.Where(c => c.Type == ClaimTypes.NameIdentifier).Select(x => x.Value).SingleOrDefault();
                    customerOfferRecord.DaUserId = userid;
                    customerOfferRecord.DaDate   = timeTick;
                    _repository.CustomerOffer.Update(customerOfferRecord);
                }



                if (custumerOrder.PaymentTypeId == 2)
                {
                    ZarinPallRequest request = new ZarinPallRequest();
                    request.amount      = ((int)custumerOrder.FinalPrice.Value) * 10;
                    request.description = "order NO: " + custumerOrder.OrderNo;
                    Tools.ZarinPal.ZarinPal zarinPal = new Tools.ZarinPal.ZarinPal();
                    var res = zarinPal.Request(request);

                    CustomerOrderPayment customerOrderPayment = new CustomerOrderPayment();
                    customerOrderPayment.CustomerOrderId  = customerOrderId;
                    customerOrderPayment.OrderNo          = custumerOrder.OrderNo.ToString();
                    customerOrderPayment.TraceNo          = res.authority;
                    customerOrderPayment.TransactionPrice = custumerOrder.FinalPrice;
                    customerOrderPayment.Cdate            = timeTick;
                    customerOrderPayment.CuserId          = User.Claims.Where(c => c.Type == ClaimTypes.NameIdentifier).Select(x => x.Value).SingleOrDefault();
                    _repository.CustomerOrderPayment.Create(customerOrderPayment);


                    _repository.Save();
                    return(Ok("https://www.zarinpal.com/pg/StartPay/" + res.authority));
                }
                _repository.Save();
                return(Ok(""));
            }
            catch (Exception e)
            {
                _logger.LogError($"Something went wrong inside FinalOrderInsert  To database: {e.Message}");
                return(BadRequest("Internal server error"));
            }
        }