public async Task <IActionResult> Confirm(int id) { var claimsIdentity = (ClaimsIdentity)User.Identity; var claim = claimsIdentity.FindFirst(ClaimTypes.NameIdentifier); OrderHeader OrderHeader = await _db.OrderHeader.Include(o => o.ApplicationUser).FirstOrDefaultAsync(o => o.Id == id && o.UserId == claim.Value); string PayUId = OrderHeader.PayUId; string jsonTokenString = await PayU.GetAccessToken(); JToken jsonToken = JObject.Parse(jsonTokenString); string accessToken = jsonToken.Value <string>("access_token"); string tokenType = jsonToken.Value <string>("token_type"); var baseAddress = new Uri("https://secure.snd.payu.com/"); using (var httpClient = new HttpClient { BaseAddress = baseAddress }) { httpClient.DefaultRequestHeaders.TryAddWithoutValidation("authorization", tokenType + " " + accessToken); using (var response = await httpClient.GetAsync("api/v2_1/orders/" + PayUId + "")) { string responseData = await response.Content.ReadAsStringAsync(); dynamic obj = JsonConvert.DeserializeObject <dynamic>(responseData); string status = obj.status.statusCode; if (status != "SUCCESS") { OrderHeader.PaymentStatus = SD.PaymentStatusRejected; } else { await _emailSender.SendEmailAsync(_db.Users.Where(u => u.Id == claim.Value).FirstOrDefault().Email, "Florist - zamówienie nr" + OrderHeader.Id.ToString() + " przyjęte ", "Zamówienie zostało opłacone oraz przyjęte do realizacji."); OrderHeader.PaymentStatus = SD.PaymentStatusApproved; OrderHeader.Status = SD.StatusSubmitted; } await _db.SaveChangesAsync(); } } OrderDetailsViewModel orderDetailsViewModel = new OrderDetailsViewModel() { OrderHeader = await _db.OrderHeader.Include(o => o.ApplicationUser).FirstOrDefaultAsync(o => o.Id == id && o.UserId == claim.Value), OrderDetails = await _db.OrderDetails.Where(o => o.OrderId == id).ToListAsync() }; return(View(orderDetailsViewModel)); }
public async Task <IActionResult> SummaryPOST() { var ip = _accessor.ActionContext.HttpContext.Connection.RemoteIpAddress.ToString(); var claimsIdentity = (ClaimsIdentity)User.Identity; var claim = claimsIdentity.FindFirst(ClaimTypes.NameIdentifier); detailsCard.listCart = await _db.ShoppingCart.Where(c => c.ApplicationUserId == claim.Value).ToListAsync(); detailsCard.OrderHeader.PaymentStatus = SD.PaymentStatusPending; detailsCard.OrderHeader.OrderDate = DateTime.Now; detailsCard.OrderHeader.UserId = claim.Value; detailsCard.OrderHeader.Status = SD.PaymentStatusPending; detailsCard.OrderHeader.PickupTime = Convert.ToDateTime(detailsCard.OrderHeader.PickupDate.ToShortDateString() + " " + detailsCard.OrderHeader.PickupTime.ToShortTimeString()); detailsCard.OrderHeader.ApplicationUser = _db.ApplicationUser.Where(m => m.Id == claim.Value).FirstOrDefault(); List <OrderDetails> orderDetailsList = new List <OrderDetails>(); _db.OrderHeader.Add(detailsCard.OrderHeader); await _db.SaveChangesAsync(); detailsCard.OrderHeader.OrderTotalOriginal = 0; foreach (var item in detailsCard.listCart) { item.Flower = await _db.Flower.FirstOrDefaultAsync(m => m.Id == item.FlowerId); OrderDetails orderDetails = new OrderDetails { FlowerId = item.FlowerId, OrderId = detailsCard.OrderHeader.Id, Description = item.Flower.Description, Name = item.Flower.Name, Price = item.Flower.Price, Count = item.Count }; detailsCard.OrderHeader.OrderTotalOriginal += orderDetails.Count * orderDetails.Price; _db.OrderDetails.Add(orderDetails); } if (HttpContext.Session.GetString(SD.ssCouponCode) != null) { detailsCard.OrderHeader.CouponCode = HttpContext.Session.GetString(SD.ssCouponCode); var couponFromDb = await _db.Coupon.Where(c => c.Name.ToLower() == detailsCard.OrderHeader.CouponCode.ToLower()).FirstOrDefaultAsync(); detailsCard.OrderHeader.OrderTotal = SD.DiscountedPrice(couponFromDb, detailsCard.OrderHeader.OrderTotalOriginal); } else { detailsCard.OrderHeader.OrderTotal = detailsCard.OrderHeader.OrderTotalOriginal; } detailsCard.OrderHeader.CouponCodeDiscount = detailsCard.OrderHeader.OrderTotalOriginal - detailsCard.OrderHeader.OrderTotal; await _db.SaveChangesAsync(); _db.ShoppingCart.RemoveRange(detailsCard.listCart); HttpContext.Session.SetInt32(SD.ssShoppingCartCount, 0); await _db.SaveChangesAsync(); string jsonTokenString = await PayU.GetAccessToken(); JToken jsonToken = JObject.Parse(jsonTokenString); string accessToken = jsonToken.Value <string>("access_token"); string tokenType = jsonToken.Value <string>("token_type"); string jsonUrlString = await PayU.CreateNewOrder(accessToken, tokenType, detailsCard, ip); string[] array = jsonUrlString.Split('?', '&', '='); string PayUId = array[2]; detailsCard.OrderHeader.PayUId = PayUId; await _db.SaveChangesAsync(); return(Redirect(jsonUrlString)); }