Ejemplo n.º 1
0
        public IActionResult VerifyPayment(string Authority, string Status)
        {
            try
            {
                var orderpeymnt = _repository.CustomerOrderPayment.FindByCondition(c => c.TraceNo == Authority)
                                  .FirstOrDefault();
                var CustomerOrderId = orderpeymnt.CustomerOrderId;
                if (orderpeymnt == null)
                {
                    return(NotFound());
                }

                ZarinPalVerifyRequest zarinPalVerifyRequest = new ZarinPalVerifyRequest();
                zarinPalVerifyRequest.authority = Authority;
                zarinPalVerifyRequest.amount    = (int)orderpeymnt.TransactionPrice.Value;

                Tools.ZarinPal.ZarinPal zarinPal = new Tools.ZarinPal.ZarinPal();
                var result = zarinPal.VerifyPayment(zarinPalVerifyRequest);
                if (result.code == 100 || result.code == 101)
                {
                    orderpeymnt.FinalStatusId   = 100;
                    orderpeymnt.RefNum          = result.ref_id.ToString();
                    orderpeymnt.TransactionDate = timeTick;
                    orderpeymnt.CardPan         = result.card_pan;
                    _repository.CustomerOrderPayment.Update(orderpeymnt);
                    _repository.Save();
                    SendSMS sendSMS = new SendSMS();

                    var mobileNo = User.Claims.Where(c => c.Type == "mobile").Select(x => x.Value).SingleOrDefault();
                    sendSMS.SendSuccessOrderPayment(mobileNo, orderpeymnt.OrderNo, CustomerOrderId.Value);
                    SendEmail sendEmail = new SendEmail();
                    var       email     = User.Claims.Where(c => c.Type == ClaimTypes.Name).Select(x => x.Value).SingleOrDefault();
                    sendEmail.SendSuccessOrderPayment(email, orderpeymnt.OrderNo, CustomerOrderId.Value);
                    return(Ok("success"));
                }
                else
                {
                    orderpeymnt.FinalStatusId   = result.code;
                    orderpeymnt.TransactionDate = timeTick;
                    _repository.CustomerOrderPayment.Update(orderpeymnt);
                    _repository.Save();
                    return(Ok("error"));
                }
            }
            catch (Exception e)
            {
                _logger.LogError($"Something went wrong inside GetCustomerOrderListByCustomerId: {e.Message}");
                return(BadRequest("Internal server error"));
            }
        }
Ejemplo n.º 2
0
        public SingleResult <string> VerifyPayment(string authority, string status)
        {
            try
            {
                var orderpeymnt = _repository.CustomerOrderPayment.FindByCondition(c => c.TraceNo == authority)
                                  .FirstOrDefault();
                if (orderpeymnt == null)
                {
                    throw new BusinessException(XError.BusinessErrors.PaymentInfoNotFound());
                }


                var customerOrderId = orderpeymnt.CustomerOrderId;
                var customer        = _repository.CustomerOrder.FindByCondition(c => c.Id == customerOrderId)
                                      .Include(c => c.Customer).Select(c => c.Customer).First();

                var zarinPalVerifyRequest = new ZarinPalVerifyRequest
                {
                    authority = authority,
                    amount    = (int)orderpeymnt.TransactionPrice.Value * 10
                };

                var zarinPal = new ZarinPal();
                var result   = zarinPal.VerifyPayment(zarinPalVerifyRequest);
                if (result.code == 100 || result.code == 101)
                {
                    orderpeymnt.FinalStatusId   = 24;
                    orderpeymnt.RefNum          = result.ref_id.ToString();
                    orderpeymnt.TransactionDate = DateTime.Now.Ticks;
                    orderpeymnt.CardPan         = result.card_pan;
                    _repository.CustomerOrderPayment.Update(orderpeymnt);

                    var sendSms = new SendSMS();
                    sendSms.SendSuccessOrderPayment(customer.Mobile.Value, orderpeymnt.OrderNo, orderpeymnt.PaymentPrice.Value);

                    var sendEmail = new SendEmail();
                    var email     = customer.Email;
                    sendEmail.SendSuccessOrderPayment(email, orderpeymnt.OrderNo, customerOrderId.Value);



                    var productist = _repository.CustomerOrderProduct.FindByCondition(c => c.CustomerOrderId == customerOrderId).Select(c => c.Product).ToList();
                    productist.ForEach(c =>
                    {
                        c.Count = c.Count--;
                        _repository.Product.Update(c);
                    });


                    var sellerList = _repository.CustomerOrderProduct.FindByCondition(c => c.CustomerOrderId == customerOrderId).Select(c => c.Seller.Mobile).ToList();

                    sellerList.ForEach(c =>
                    {
                        if (c == null)
                        {
                            return;
                        }
                        var sendSms = new SendSMS();
                        sendSms.SendOrderSmsForSeller(c.Value);
                    });


                    _repository.Save();

                    var finalres = SingleResult <string> .GetSuccessfulResult("عملیات پرداخت با موفقیت انجام شد.");

                    _logger.LogData(MethodBase.GetCurrentMethod(), finalres, null, customerOrderId);
                    return(finalres);
                }
                else
                {
                    orderpeymnt.FinalStatusId   = 25;
                    orderpeymnt.TransactionDate = DateTime.Now.Ticks;
                    _repository.CustomerOrderPayment.Update(orderpeymnt);
                    _repository.Save();

                    throw new BusinessException(XError.BusinessErrors.FailedPayment());
                }
            }
            catch (Exception e)
            {
                _logger.LogError(e, MethodBase.GetCurrentMethod(), authority, status);

                return(SingleResult <string> .GetFailResult("خطا در سامانه"));
            }
        }
Ejemplo n.º 3
0
        public SingleResult <InsertOrderResultDto> InsertCustomerOrder_UI(OrderModel order)
        {
            try
            {
                var userId          = ClaimPrincipalFactory.GetUserId(User);
                var time            = DateTime.Now.Ticks;
                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.ProductOffer.FindByCondition(x => x.ProductId == c.ProductId && x.FromDate <= time && time <= x.ToDate && x.DaDate == null && x.Ddate == null).Include(c => c.Offer).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       = ofer?.Id,
                        ProductOfferCode     = ofer?.Offer.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))
                };

                if (offer != null)
                {
                    if (offer.Value == 0 || offer.Value == null)
                    {
                        customerOrder.OfferPrice = offer.MaximumPrice > customerOrder.OrderPrice
                            ? customerOrder.OrderPrice
                            : offer.MaximumPrice;
                        customerOrder.OfferValue = null;
                    }
                    else
                    {
                        customerOrder.OfferPrice = (long?)(customerOrder.OrderPrice * (offer.Value / 100));
                        customerOrder.OfferValue = (int?)offer.Value.Value;
                    }
                }
                else
                {
                    customerOrder.OfferPrice = 0;
                    customerOrder.OfferValue = null;
                }


                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.OfferPrice + customerOrder.TaxPrice + customerOrder.PostServicePrice;
                _repository.CustomerOrder.Create(customerOrder);

                if (customerOrder.FinalPrice > 0)
                {
                    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);
                }
                else
                {
                    var customerOrderPayment = new CustomerOrderPayment
                    {
                        OrderNo          = customerOrder.OrderNo.ToString(),
                        TraceNo          = "پرداخت رایگان",
                        TransactionPrice = customerOrder.FinalPrice,
                        TransactionDate  = DateTime.Now.Ticks,
                        Cdate            = DateTime.Now.Ticks,
                        CuserId          = userId,
                        PaymentPrice     = customerOrder.FinalPrice,
                        FinalStatusId    = 24
                    };
                    customerOrder.CustomerOrderPayment.Add(customerOrderPayment);


                    var sendSms = new SendSMS();
                    sendSms.SendSuccessOrderPayment(cc.Mobile.Value, customerOrderPayment.OrderNo, customerOrderPayment.PaymentPrice.Value);

                    var sendEmail = new SendEmail();
                    var email     = cc.Email;
                    sendEmail.SendSuccessOrderPayment(email, customerOrderPayment.OrderNo, customerOrderPayment.PaymentPrice.Value);



                    var productist = _repository.CustomerOrderProduct.FindByCondition(c => c.CustomerOrderId == customerOrder.Id).Select(c => c.Product).ToList();
                    productist.ForEach(c =>
                    {
                        c.Count = c.Count--;
                        _repository.Product.Update(c);
                    });


                    var sellerList = _repository.CustomerOrderProduct.FindByCondition(c => c.CustomerOrderId == customerOrder.Id).Select(c => c.Seller.Mobile).ToList();

                    sellerList.ForEach(c =>
                    {
                        if (c == null)
                        {
                            return;
                        }
                        var sendSms = new SendSMS();
                        sendSms.SendOrderSmsForSeller(c.Value);
                    });
                    _repository.Save();

                    var result = new InsertOrderResultDto
                    {
                        OrderNo         = customerOrder.OrderNo,
                        CustomerOrderId = customerOrder.Id,
                        BankUrl         = "",
                        RedirectToBank  = false,
                        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));
            }
        }