public JsonResult OrderPay(int payType, int OrderId, int payMoney) { MotaiDataEntities dbContext = new MotaiDataEntities(); tOrderPay pay = dbContext.tOrderPays.OrderByDescending(op => op.oOrderInstallment).Where(op => op.oOrderId.Equals(OrderId)).FirstOrDefault(); if (pay == null) { tOrderPay orderPay = new tOrderPay(); orderPay.oOrderId = OrderId; orderPay.oOrderInstallment = 1; orderPay.oPayType = payType; orderPay.oPayment = payMoney; orderPay.oPayDate = DateTime.Now; dbContext.tOrderPays.Add(orderPay); dbContext.SaveChanges(); } else { tOrderPay orderPay = new tOrderPay(); orderPay.oOrderId = OrderId; orderPay.oOrderInstallment = pay.oOrderInstallment++; orderPay.oPayType = payType; orderPay.oPayment = payMoney; orderPay.oPayDate = DateTime.Now; dbContext.tOrderPays.Add(orderPay); dbContext.SaveChanges(); } return(Json(new { msg = "結帳完成", url = Url.Action("實體店新增訂單", "Order") })); }
//網購寫入訂單 public string webOrder(WebPay payData) { if (Session[CSession關鍵字.SK_LOGINED_CUSTOMER] != null) { tCustomer cust = Session[CSession關鍵字.SK_LOGINED_CUSTOMER] as tCustomer; MotaiDataEntities dbContext = new MotaiDataEntities(); List <tStatu> StatuList = dbContext.tStatus.Where(s => s.sCustomerId.Equals(cust.CustomerId)).ToList(); string szHtml = String.Empty; string szErrorMessage = String.Empty; List <string> enErrors = new List <string>(); try { using (AllInOne oPayment = new AllInOne()) { /* 服務參數 */ oPayment.ServiceMethod = AllPay.Payment.Integration.HttpMethod.HttpPOST; oPayment.ServiceURL = "https://payment-stage.opay.tw/Cashier/AioCheckOut/V5"; oPayment.HashKey = "5294y06JbISpM5x9"; oPayment.HashIV = "v77hoKGq4kWxNNIS"; oPayment.MerchantID = "2000132"; /* 基本參數 */ string baseURI = Request.Url.Scheme + "://" + Request.Url.Authority; if (payData.payType == 2) { oPayment.Send.ReturnURL = baseURI + Url.Action("orderCredit", "Order"); } else { oPayment.Send.ReturnURL = baseURI + Url.Action("ATMpayOff", "Order"); } oPayment.Send.ClientBackURL = baseURI; //oPayment.Send.OrderResultURL = baseURI; int number = (DateTime.Now.Hour * 3600 + DateTime.Now.Minute * 60 + DateTime.Now.Second) * 3; oPayment.Send.MerchantTradeNo = "MD" + DateTime.Now.Date.ToString("yyyyMMdd") + number.ToString("000000"); oPayment.Send.MerchantTradeDate = DateTime.Now; oPayment.Send.TotalAmount = Decimal.Parse(payData.totalPay); oPayment.Send.TradeDesc = "感謝購買墨台商品"; if (payData.payType == 1) { oPayment.Send.ChoosePayment = PaymentMethod.ATM; oPayment.SendExtend.ExpireDate = Int32.Parse("3"); } else { oPayment.Send.ChoosePayment = PaymentMethod.Credit; } //oPayment.Send.ChoosePayment = PaymentMethod.ALL; oPayment.Send.Remark = "饒了我吧"; oPayment.Send.ChooseSubPayment = PaymentMethodItem.None; oPayment.Send.NeedExtraPaidInfo = ExtraPaymentInfo.No; oPayment.Send.HoldTrade = HoldTradeType.No; oPayment.Send.DeviceSource = DeviceType.PC; oPayment.Send.UseRedeem = UseRedeemFlag.No; //購物金/紅包折抵 oPayment.Send.IgnorePayment = ""; // 例如財付通:Tenpay // 加入選購商品資料。 foreach (var item in StatuList) { tProduct product = dbContext.tProducts.Where(p => p.ProductId.Equals(item.sProductId)).FirstOrDefault(); //var chg = JObject.Parse(item.Value.ToString()); oPayment.Send.Items.Add(new Item() { Name = product.pName, Price = product.pPrice, Currency = "NTD", Quantity = item.sProductQty, URL = "<< 產品說明位址 >>" }); } // 當付款方式為 ALL 時,建議增加的參數。 if (payData.payType == 1) { oPayment.SendExtend.PaymentInfoURL = baseURI + Url.Action("orderATM", "Order"); } ////攜帶參數 //WebOrderModel order = new WebOrderModel(); //order.boughtList = StatuList; //order.webpay = payData; //order.customer = cust; //order.payDate = oPayment.Send.MerchantTradeDate; //http.Session[oPayment.Send.MerchantTradeNo] = order; //database產生訂單 tOrder newOrder = new tOrder(); newOrder.oCustomerId = cust.CustomerId; newOrder.oEmployeeId = 19; newOrder.oDate = DateTime.Now; newOrder.oAddress = payData.shipAddress; newOrder.oWarehouseName = 1; newOrder.cNote = oPayment.Send.MerchantTradeNo; dbContext.tOrders.Add(newOrder); dbContext.SaveChanges(); tOrder CreateOrder = dbContext.tOrders.OrderByDescending(o => o.OrderId).FirstOrDefault(); tOrderPay pay = new tOrderPay(); pay.oOrderId = CreateOrder.OrderId; pay.oOrderInstallment = 1; pay.oPayType = payData.payType; pay.oPayment = Convert.ToInt32(payData.totalPay); pay.oPayDate = DateTime.Now; dbContext.tOrderPays.Add(pay); foreach (var item in StatuList) { tOrderDetail orderDetail = new tOrderDetail(); orderDetail.oOrderId = CreateOrder.OrderId; orderDetail.oProductId = item.sProductId; orderDetail.oProductQty = item.sProductQty; dbContext.tOrderDetails.Add(orderDetail); dbContext.tStatus.Remove(item); dbContext.SaveChanges(); } dbContext.SaveChanges(); /* 產生訂單 */ enErrors.AddRange(oPayment.CheckOut()); /* 產生產生訂單 Html Code 的方法 */ //string szHtml = String.Empty; enErrors.AddRange(oPayment.CheckOutString(ref szHtml)); } } catch (Exception ex) { // 例外錯誤處理。 enErrors.Add(ex.Message); } finally { // 顯示錯誤訊息。 if (enErrors.Count() > 0) { szErrorMessage = String.Join("\\r\\n", enErrors); } } if (enErrors.Count() == 0) { return(szHtml); } else { return(szErrorMessage); } } return(String.Empty); }