public ActionResult Checkout(PaymentModel payment) { CPaymentItem cPaymentItem = new CPaymentItem(); cPaymentItem.merchantName = MerchantName; cPaymentItem.amount = (int)(Convert.ToDouble(payment.TotalPrice) * 100); cPaymentItem.cardType = payment.CardType.Key; cPaymentItem.lang = "lv"; cPaymentItem.description = "any description you want"; cPaymentItem.hashCode = GetMD5HashCode(AuthKey + MerchantName + payment.CardType.Key + cPaymentItem.amount + cPaymentItem.description); var jsonValues = JsonConvert.SerializeObject(cPaymentItem); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(RequestToServerURLGetPaymentKey); request.ContentType = "application/json; charset=utf-8"; request.Method = "POST"; request.Accept = "application/json"; using (StreamWriter streamWriter = new StreamWriter(request.GetRequestStream())) { streamWriter.Write(jsonValues); streamWriter.Flush(); } string responseData = string.Empty; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (StreamReader reader = new StreamReader(response.GetResponseStream())) { responseData = reader.ReadToEnd(); } CRespGetPaymentKey cRespPymnt = (CRespGetPaymentKey)JsonConvert.DeserializeObject(responseData, typeof(CRespGetPaymentKey)); if (cRespPymnt.status.code != 1) { throw new Exception("Error while getting paymentKey, code=" + cRespPymnt.status.code + ", message=" + cRespPymnt.status.message); } return(Redirect(RequestToServerURLPayPage + cRespPymnt.paymentKey)); }
public async Task <IActionResult> Checkout(string cardType, string Zip, string City, string Address) { string user_id = HttpContext.Session.GetString("user_id"); if (user_id != null) { List <Card> cards = HttpContext.Session.GetObjectFromJson <Card>("Card") as List <Card>; if (cardType != null && Zip != null && City != null && Address != null) { #region Make CPaymentItem For API decimal amount = Convert.ToDecimal(HttpContext.Session.GetString("Total")); decimal shippingPrice = amount - cards.Sum(c => c.Quantity * c.Price); CPaymentItem cPaymentItem = new CPaymentItem(); cPaymentItem.merchantName = configuration["PaymentInformation:MerchantName"]; cPaymentItem.amount = (int)(amount * 100); cPaymentItem.cardType = cardType; cPaymentItem.lang = "lv"; cPaymentItem.description = "Product"; cPaymentItem.hashCode = GetMD5HashCode(configuration["PaymentInformation:AuthKey"] + configuration["PaymentInformation:MerchantName"] + cPaymentItem.cardType + cPaymentItem.amount + cPaymentItem.description); #endregion #region Create Payment List <Payment> userOrders = new List <Payment>(); for (var i = 0; i < cards.Count; i++) { Payment currentPayment = new Payment(); //City currentPayment.City = City; //Postal Code currentPayment.PostalCode = Zip; //Address currentPayment.Address = Address; currentPayment.ProductDiscount = cards[i].DiscountPercent; //User string userId = HttpContext.Session.GetString("user_id"); User user = db.Users.Where(u => u.Id == userId).FirstOrDefault(); currentPayment.UserName = user.Name; currentPayment.UserSurname = user.Surname; currentPayment.UserEmail = user.Email; currentPayment.UserId = user.Id; //If user address was null on register then on payment we take address and append to it user.Address = user.Address == null ? Address : user.Address; //Cardtype CardType type = await db.CardTypes.Where(c => c.Key == cardType).FirstOrDefaultAsync(); currentPayment.CardType = type; //Product Product orderedProduct = await db.Products.Where(p => p.Id == cards[i].Id).FirstOrDefaultAsync(); ProductLanguage productLanguage = await db.ProductLanguages .Include(l => l.Language) .Where(p => p.Language.Key == "az" && p.ProductId == orderedProduct.Id) .FirstOrDefaultAsync(); currentPayment.ProductName = productLanguage.Name; currentPayment.ProductPrice = orderedProduct.Price; currentPayment.ProductId = orderedProduct.Id; //Datetime currentPayment.Date = DateTime.Now; //Shipping Price currentPayment.ShippingPrice = shippingPrice; //ProductSize currentPayment.ProductSize = cards[i].SizeName; //Quantity currentPayment.Count = cards[i].Quantity; //Total Price if (currentPayment.ProductDiscount != 0 || currentPayment.ProductDiscount != null) { currentPayment.TotalPrice = (decimal)(cards[i].Price * currentPayment.ProductDiscount / 100) * cards[i].Quantity; } else { currentPayment.TotalPrice = cards[i].Price * cards[i].Quantity; } //Status currentPayment.Status = 2; //TransactionId currentPayment.TransactionId = cPaymentItem.hashCode; userOrders.Add(currentPayment); } await db.Payments.AddRangeAsync(userOrders); await db.SaveChangesAsync(); #endregion #region Send Web Request var jsonValues = JsonConvert.SerializeObject(cPaymentItem); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(configuration["PaymentInformation:RequestToServerUrlGetPaymentKey"]); request.ContentType = "application/json; charset=utf-8"; request.Method = "POST"; request.Accept = "application/json"; using (StreamWriter streamWriter = new StreamWriter(request.GetRequestStream())) { streamWriter.Write(jsonValues); streamWriter.Flush(); } #endregion #region Get Web Response string responseData = string.Empty; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (StreamReader reader = new StreamReader(response.GetResponseStream())) { responseData = reader.ReadToEnd(); } CRespGetPaymentKey cRespPymnt = (CRespGetPaymentKey)JsonConvert.DeserializeObject(responseData, typeof(CRespGetPaymentKey)); #endregion if (cRespPymnt.status.code != 1) { throw new Exception("Error while getting paymentKey, code=" + cRespPymnt.status.code + ", message=" + cRespPymnt.status.message); } return(Redirect(configuration["PaymentInformation:ReuqestToServerUrlPayPage"] + cRespPymnt.paymentKey)); } else { return(RedirectToAction("Error", "Payment")); } } else { return(RedirectToAction("Login", "Account")); } }