public async Task <ActionResult> ReduceWallet(UserModel um)
        {
            try
            {
                var user = await _userrepo.GetById(um.U_Id);

                if (user == null)
                {
                    return(new JsonResult(ResponseModel.Error("کاربر یافت نشد")));
                }
                var doTransaction = await _walletrepo.DecreaseWallet(user.U_Id, um.U_Wallet);

                if (doTransaction.Status)
                {
                    if (user.U_Phone != null && user.U_Phone != "")
                    {
                        await _smsService.WalletAllert(user.U_Phone, 2, um.U_Wallet.ToString("#,#"));
                    }
                    return(new JsonResult(ResponseModel.Success("عملیات با موفقیت انجام گردید")));
                }
                else
                {
                    return(new JsonResult(doTransaction));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("Erro in user controller", ex);
                return(new JsonResult(ResponseModel.ServerInternalError(data: ex)));
            }
        }
Exemple #2
0
        public async Task <ResponseStructure> TestCheckout(HttpRequest httpRequest, HttpResponse httpResponse, int type = 1)
        {
            try
            {
                var authorize = Barayand.Common.Services.TokenService.AuthorizeUser(httpRequest);
                if (authorize < 1)
                {
                    return(ResponseModel.Error("User not logged in."));
                }
                var basket = await GetBasketItems(httpRequest);

                if (basket.CartItems.Count < 1)
                {
                    return(ResponseModel.Error("Basket is empty."));
                }
                UserModel userModel = await _userrepository.GetById(authorize);

                decimal      wll     = userModel.U_Wallet;
                decimal      Sum     = basket.BasketTotalAmount(_lang.GetLang(), CVRT);
                InvoiceModel invoice = new InvoiceModel();
                invoice.I_TotalAmount        = basket.BasketTotalAmount(_lang.GetLang(), CVRT);
                invoice.I_Id                 = UtilesService.RandomDigit(12);
                invoice.I_UserId             = authorize;
                invoice.I_ShippingCost       = basket.ShippingCost;
                invoice.I_RecipientInfo      = JsonConvert.SerializeObject(basket.RecipientInfo);
                invoice.I_CopponDiscount     = basket.SumDiscount();
                invoice.I_CopponId           = (basket.Coppon.Count > 0) ? basket.Coppon.FirstOrDefault().CP_Id : 0;
                invoice.Created_At           = DateTime.Now;
                invoice.I_PaymentDate        = DateTime.Now;
                invoice.I_TotalProductAmount = basket.BasketTotalProductPrice(_lang.GetLang(), CVRT);
                invoice.I_PaymentInfo        = "Test payment";
                invoice.I_PaymentType        = type;


                if (type == 2)
                {
                    decimal w = (_lang.GetLang() == "fa") ?userModel.U_Wallet : userModel.U_Wallet / CVRT;
                    if (w < basket.BasketTotalAmount(_lang.GetLang(), CVRT))
                    {
                        return(ResponseModel.Error("موجودی کیف پول شما کافی نمیباشد"));
                    }
                    else
                    {
                        w = w - basket.BasketTotalAmount(_lang.GetLang(), CVRT);
                        await _walletrepository.DecreaseWallet(userModel.U_Id, basket.BasketTotalAmount("fa", CVRT));

                        await _smsService.WalletAllert(userModel.U_Phone, 2, basket.BasketTotalAmount(_lang.GetLang(), CVRT).ToString("#,# تومان"));

                        invoice.I_Status = 2;
                    }
                }
                if (type == 3)
                {
                    decimal w = userModel.U_Coupon;
                    if (w < basket.BasketProductBinPriceTotal(BINPERC))
                    {
                        return(ResponseModel.Error("تعداد بن های شما برای پرداخت کافی نمیباشد"));
                    }
                    else
                    {
                        userModel.U_Coupon = userModel.U_Coupon - basket.BasketProductBinPriceTotal(BINPERC);
                        await _userrepository.Update(userModel);

                        invoice.I_Status = 2;
                    }
                }
                if (type == 4)
                {
                    invoice.I_Status = 1;
                }
                if (type == 1)
                {
                    invoice.I_Status = 2;
                }
                ResponseStructure res = await _invoicerepository.Insert(invoice);

                if (!res.Status)
                {
                    return(res);
                }
                int sumGiftBon = basket.BasketProductBinPriceTotal(BINPERC);
                foreach (var item in basket.CartItems)
                {
                    //sumGiftBon += (item.Product.P_GiftBin * item.Quantity);
                    var orderres = await _orderrepository.Insert(new OrderModel()
                    {
                        O_Discount     = (!item.PrintAble) ? basket.BasketProductPrice(item.Product.P_Id, _lang.GetLang(), false, false) : basket.BasketProductPrintablePrice(item.Product.P_Id, _lang.GetLang(), false, false),
                        O_DiscountType = item.Product.P_DiscountType,
                        O_Price        = (!item.PrintAble) ? basket.BasketProductPrice(item.Product.P_Id, _lang.GetLang(), true, false) : basket.BasketProductPrintablePrice(item.Product.P_Id, _lang.GetLang(), true, false),
                        O_Quantity     = item.Quantity,
                        O_ProductId    = item.Product.P_Id,
                        O_ReciptId     = invoice.I_Id,
                        Created_At     = DateTime.Now,
                        Lang           = _lang.GetLang(),
                        O_Version      = item.PrintAble ? 2 : 1
                    });
                }
                userModel.U_Coupon += sumGiftBon;
                await _userrepository.Update(userModel);

                await this.FreeUpCart(httpRequest, httpResponse);

                if (_lang.GetLang() == "fa")
                {
                    await _smsService.OrderAlert(userModel.U_Phone, invoice.I_Id, basket.BasketTotalAmount(_lang.GetLang(), CVRT).ToString("#,# تومان"));
                }

                return(ResponseModel.Success("سفارش شما با موفقیت ثبت گردید", new { invoiceid = invoice.I_Id }));
            }
            catch (Exception ex)
            {
                return(ResponseModel.ServerInternalError(data: ex));
            }
        }