public HttpResponseMessage CreateOrder(ViewModels.OrderItems items) { HttpResponseMessage ret = null; List <Models.Db.OrderItems> orderItems = new List <OrderItems>(); var temp = ConvertToResponseItems(items.Items); try { using (var ten = _shopContext.Database.BeginTransaction()) { var order = new Orders() { ClientId = items.ClientID, Phone = items.Phone, Price = temp.Total, Payment = false }; _shopContext.Orders.Add(order); _shopContext.SaveChanges(); foreach (var x in temp.Items) { orderItems.Add(new OrderItems() { Count = x.Count, IdSize = _shopContext.Size.Where(c => c.Name == x.Size).FirstOrDefault().Id, IdIce = _shopContext.Ice.Where(c => c.Name == x.Ice).FirstOrDefault().Id, IdItem = _shopContext.Items.Where(c => c.Name == x.Name).FirstOrDefault().Id, IdSweetness = _shopContext.Sweetness.Where(c => c.Name == x.Sweetness).FirstOrDefault().Id, Price = x.Price, IdOrder = order.Id }); } ; _shopContext.OrderItems.AddRange(orderItems); _shopContext.SaveChanges(); ten.Commit(); var payRoot = $"{this.Request.Scheme}://{this.Request.Host}{this.Request.PathBase}" + "/Pay"; var orderStr = EncodeTo64((order.Id + 1000000000).ToString()); ViewModels.ResponseCreateOrder response = new ViewModels.ResponseCreateOrder() { OrderUrl = $"{payRoot}/GetOrder?order={orderStr.Replace("==", "")}", PayUrl = $"{payRoot}?order={orderStr.Replace("==", "")}", Price = order.Price }; ret = CreateResponse(response); } } catch (Exception ex) { ret = CreateErrorResponse("null", ex.Message); } return(ret); }
/// <summary> /// 測試用金流 /// </summary> /// <param name="order"></param> /// <returns></returns> public IActionResult TestPay([FromQuery] string order) { var order1 = order; order = DecodeFrom64(order); int orderId = int.Parse(order) - 1000000000; ViewBag.order = orderId; var orderDao = _shopContext .Orders .Where(x => x.Id == orderId) .First(); if (orderDao.Payment) { return(View("Paid")); } orderDao.Payment = true; _shopContext.SaveChanges(); try { var payRoot = $"{this.Request.Scheme}://{this.Request.Host}{this.Request.PathBase}" + "/Pay"; var data = new { id = orderDao.ClientId, text = $"付款完成!!\r\n訂單網址:{payRoot}/GetOrder?order={order1.Replace("==", "")}" }; WebClient client = new WebClient(); client.UploadData(_configuration.GetValue <string>("PaySuccessUrl"), "POST", Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data))); } catch (Exception ex) { ViewBag.ex = ex.ToString(); } return(View()); }