Ejemplo n.º 1
0
        public IActionResult OnPostPay(int paymentMethod, string address, string phoneNumber)
        {
            if (string.IsNullOrEmpty(address) || string.IsNullOrEmpty(phoneNumber))
            {
                long number;
                if (string.IsNullOrEmpty(address))
                {
                    Message = "آدرس خود را مشخص نمایید";
                }
                if (string.IsNullOrEmpty(phoneNumber) || !phoneNumber.StartsWith("09") || !long.TryParse(phoneNumber, out number) || phoneNumber.Length != 11)
                {
                    Message = "شماره موبایل خود را وارد نمایید";
                }
                return(RedirectToPage("Checkout", new { message = Message }));
            }

            Cart = _cartService.GetCart();

            if (Cart.CartItems.Any(c => !c.IsInStock))
            {
                return(RedirectToPage("Cart"));
            }
            Cart.SetPaymentMethod(paymentMethod);
            Cart.Address      = address;
            Cart.MobileNumber = phoneNumber;

            var user    = _userQuery.GetUserBy(User.Identity.Name);
            var orderId = _orderApplication.PlaceOrder(Cart);

            if (paymentMethod == PaymentMethod.Online)
            {
                var paymentResponse = _zarinPalFactory.CreatePaymentRequest(Cart.PayAmount.ToString(), user.Mobile, user.Email, "خرید محصول", orderId);
                return(Redirect(
                           $"https://{_zarinPalFactory.Prefix}.zarinpal.com/pg/StartPay/{paymentResponse.Authority}"));
            }

            else if (paymentMethod == PaymentMethod.Cash)
            {
                OperationResult result = new OperationResult();

                var issueTrackingNo = _orderApplication.CreateCashOrderOperation(orderId);
                result.Succeeded($"سفارش شما ثبت گردید ، جهت ارسال محصول ، همکاران ما با شما تماس خواهند گرفت!\nشماره پیگیری شما :{issueTrackingNo}");
                Response.Cookies.Delete(CookieName);

                return(RedirectToPage("PaymentResult", result));
            }

            return(RedirectToPage("Index"));
        }