Esempio n. 1
0
        public async Task <IActionResult> MoMoSuccess(string deliveryAddress, string orderId, string payType, string responseTime, string errorCode)
        {
            if (errorCode.Equals("0"))
            {
                var user = await _accountService.GetUserAsync(User);

                var userInclude = await _accountService.GetUserByUserIdAsync(user.Id);

                var carts = GetCartItems();

                using var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled);

                try
                {
                    var order = await _orderService.AddOrderFromCartsAsync(carts, userInclude, MOMO, HttpUtility.UrlDecode(deliveryAddress));

                    if (!(order is null))
                    {
                        int result = await _orderDetailService.AddOrderDetailAsync(order, carts);

                        if (result > 0)
                        {
                            result = await _momoService.AddMoMoPaymentAsync(order.Id, orderId, payType, responseTime);

                            if (result > 0)
                            {
                                transaction.Complete();

                                //var path = _hostingEnvironment.WebRootPath + Path.DirectorySeparatorChar.ToString()
                                //            + "EmailTemplate"
                                //            + Path.DirectorySeparatorChar.ToString()
                                //            + "EmailOrder.html";

                                string subject = "Order Confirmation #" + order.Id;

                                ViewBag.OrderId = order.Id;

                                var text = "";
                                foreach (var item in carts)
                                {
                                    text += item.ProductName + " ";
                                }

                                string body = $"<h3>Order confimartion ${order.Id}</h3><p>Ordered Date ${order.OrderDate}</p>" +
                                              $"<p>Your cart: ${text}</p>" +
                                              $"<p>Total cost: ${order.Total}</p>";

                                await _emailSender.SendEmailAsync(user.Email, subject, body);

                                HttpContext.Session.Remove(CARTKEY);
                                return(View());
                            }
                        }
                    }
                }
                catch
                {
                }
            }

            return(PartialView(ERROR_PAYMENT_PAGE));
        }