public static OrderLst insOrderListTemp(ProductModel prod, PkgDetailModel pkg, confirmPkgInfo confirm, DataModel dataModel, B2dAccount UserData, string priceType, int lstSeqno, int?cusSeqno, int prodQty, DiscountRuleModel discRule) { OrderLst lstTemp = new OrderLst(); //lstTemp.lst_seqno = lstSeqno; //lstTemp.cus_seqno = cusSeqno; lstTemp.prod_no = prod.prod_no.ToString(); lstTemp.prod_amt = Convert.ToDouble(priceType == "price1" ? pkg.price1 : priceType == "price2" ? pkg.price2 : priceType == "price3" ? pkg.price3 : pkg.price4); lstTemp.prod_name = prod.prod_name; lstTemp.prod_b2c_amt = Convert.ToDouble(priceType == "price1" ? pkg.price1_b2c : priceType == "price2" ? pkg.price2_b2c : priceType == "price3" ? pkg.price3_b2c : pkg.price4_b2c); lstTemp.prod_currency = UserData.CURRENCY; lstTemp.prod_cond1 = priceType; lstTemp.prod_cond2 = pkg.unit; lstTemp.events = confirm.pkgEvent; lstTemp.pkg_date = confirm.selDate; //lstTemp.discount_xid = 0; lstTemp.pkg_no = pkg.pkg_no; lstTemp.pkg_name = pkg.pkg_name; lstTemp.prod_qty = prodQty; OrderDiscountRule rule = new OrderDiscountRule(); if (discRule.isRule == true) { double discAmt = 0; if (priceType == "price1") { discAmt = pkg.price1_org - pkg.price1; } if (priceType == "price2") { discAmt = pkg.price2_org - pkg.price2; } if (priceType == "price3") { discAmt = Convert.ToDouble(pkg.price3_org - pkg.price3); } if (priceType == "price4") { discAmt = Convert.ToDouble(pkg.price4_org - pkg.price4); } rule.disc_amt = discAmt; rule.disc_currency = UserData.CURRENCY; rule.disc_name = discRule.disc_name; rule.disc_note = ""; //rule.lst_seqno = lstSeqno; lstTemp.order_discount_rule = rule; } else { lstTemp.order_discount_rule = rule; } return(lstTemp); }
//成立b2d 訂單 public static string insB2dOrder(ProdTitleModel title, ProductModel prod, PkgDetailModel pkg, confirmPkgInfo confirm, DataModel dataModel, B2dAccount UserData, DiscountRuleModel discRule) { try { B2dOrderModel order = new B2dOrderModel(); order.connect_mail = dataModel.contactEmail; order.order_date = DateTime.Now; order.order_type = "B2D"; order.order_status = "NW"; order.order_amt = Convert.ToDouble(dataModel.currPriceTotal); order.order_b2c_amt = Convert.ToDouble(dataModel.currPriceTotal); //要重算 order.connect_name = dataModel.asiaMileMemberLastName + " " + dataModel.contactFirstname; order.connect_tel = dataModel.contactTel; order.order_note = dataModel.note; Source source = new Source(); source.booking_type = "WEB"; source.company_xid = UserData.COMPANY_XID; source.channel_oid = UserData.KKDAY_CHANNEL_OID; source.connect_tel = dataModel.contactTel; source.connect_mail = dataModel.contactEmail; source.connect_name = dataModel.asiaMileMemberLastName + " " + dataModel.contactFirstname; source.order_note = dataModel.note; source.client_ip = "127.0.0.1"; source.crt_time = DateTime.Now; order.source = source; List <OrderCus> cusList = new List <OrderCus>(); List <OrderLst> lstList = new List <OrderLst>(); //NORANK 且 (只有一個代表人 或 不要代表人) ->只塞一筆order_lst //NORANK 且要填所有旅客資料->只塞1~*筆order_lst //RANK 且 (只有一個代表人 或 不要代表人) ->只塞1~*筆order_lst //RANK 且要填所有旅客資料 ->只塞1~*筆order_lst int?cusSeqno = 1; int lstSeqno = 1; string priceType = ""; int ii = 0; //滿足cus foreach (CusDataInfo cus in dataModel.travelerData) { if (ii < confirm.price1Qty) { priceType = "price1"; } else if (ii < (confirm.price1Qty + confirm.price2Qty)) { priceType = "price2"; } else if (ii < (confirm.price1Qty + confirm.price2Qty + confirm.price3Qty)) { priceType = "price3"; } else if (ii < (confirm.price1Qty + confirm.price2Qty + confirm.price3Qty + confirm.price4Qty)) { priceType = "price4"; } OrderCus cusTemp = new OrderCus(); //cusTemp.cus_seqno = Convert.ToInt32(cusSeqno); cusTemp.cus_type = priceType; cusTemp.cus_mail = ""; cusTemp.cus_name_e_first = cus.englishName.firstName; cusTemp.cus_name_e_last = cus.englishName.lastName; cusTemp.cus_sex = cus.gender; cusTemp.cus_tel = ""; cusList.Add(cusTemp); cusSeqno = cusSeqno + 1; ii = ii + 1; } if (dataModel.travelerData.Count == 0) { cusSeqno = null; } else { cusSeqno = 1; } if (dataModel.travelerData.Count == 1) { //依priceTeype寫入 if (confirm.price1Qty > 0) { lstList.Add(insOrderListTemp(prod, pkg, confirm, dataModel, UserData, "price1", lstSeqno, 1, Convert.ToInt32(confirm.price1Qty), discRule)); } lstSeqno = lstSeqno + 1; if (confirm.price2Qty > 0) { lstList.Add(insOrderListTemp(prod, pkg, confirm, dataModel, UserData, "price2", lstSeqno, 1, Convert.ToInt32(confirm.price2Qty), discRule)); } lstSeqno = lstSeqno + 1; if (confirm.price3Qty > 0) { lstList.Add(insOrderListTemp(prod, pkg, confirm, dataModel, UserData, "price3", lstSeqno, 1, Convert.ToInt32(confirm.price3Qty), discRule)); } lstSeqno = lstSeqno + 1; if (confirm.price4Qty > 0) { lstList.Add(insOrderListTemp(prod, pkg, confirm, dataModel, UserData, "price4", lstSeqno, 1, Convert.ToInt32(confirm.price4Qty), discRule)); } } else { //依每一個row寫入 for (ii = 0; ii < dataModel.travelerData.Count; ii++) { if (ii < confirm.price1Qty) { priceType = "price1"; } else if (ii < (confirm.price1Qty + confirm.price2Qty)) { priceType = "price2"; } else if (ii < (confirm.price1Qty + confirm.price2Qty + confirm.price3Qty)) { priceType = "price3"; } else if (ii < (confirm.price1Qty + confirm.price2Qty + confirm.price3Qty + confirm.price4Qty)) { priceType = "price4"; } lstList.Add(insOrderListTemp(prod, pkg, confirm, dataModel, UserData, priceType, lstSeqno, cusSeqno, 1, discRule)); lstSeqno = lstSeqno + 1; cusSeqno = cusSeqno + 1; } } order.order_cus = cusList; order.order_lst = lstList; Website.Instance.logger.Debug($"bookingStep1_insB2dOrder:{ JsonConvert.SerializeObject(order)}"); insB2dOrderResult result = ApiHelper.insB2dOrder(order, title); if (result.result == "0000") { Website.Instance.logger.Debug($"bookingStep1_insB2dOrderResult:{ JsonConvert.SerializeObject(result)}"); return(result.order_no); } else { Website.Instance.logger.Debug($"bookingStep1_insB2dOrderResult:{ JsonConvert.SerializeObject(result)}"); throw new Exception(result.result_msg); } } catch (Exception ex) { Website.Instance.logger.Debug($"bookingStep1_insB2dOrderErr:{ JsonConvert.SerializeObject(ex.ToString())}"); throw new Exception(ex.Message.ToString()); } }
public IActionResult bookingStep1([FromBody] DataModel data) { try { //B2d分銷商資料 var aesUserData = User.Identities.SelectMany(i => i.Claims.Where(c => c.Type == ClaimTypes.UserData).Select(c => c.Value)).FirstOrDefault(); var UserData = JsonConvert.DeserializeObject <B2dAccount>(AesCryptHelper.aesDecryptBase64(aesUserData, Website.Instance.AesCryptKey)); string ip = httpContextAccessor.HttpContext.Request.HttpContext.Connection.RemoteIpAddress.ToString().Replace("::1", "127.0.0.1"); data = BookingRepostory.setCardEncrypt(data); //log時把卡號移除 DataModel dataTemp = data.Clone(); dataTemp.card = null; Website.Instance.logger.Debug($"bookingStep1_inputdata:{ JsonConvert.SerializeObject(dataTemp)}"); ApiSetting api = new ApiSetting(); api.apiKey = "kkdayapi"; api.userOid = "1"; api.ver = "1.0.1"; api.locale = UserData.LOCALE; api.currency = UserData.CURRENCY; api.ipaddress = ip; string prodStr = TempData["prod_" + data.guidNo] as string; if (string.IsNullOrEmpty(prodStr)) { throw new Exception("資料錯誤,請重新讀取頁"); } ProductModel prod = JsonConvert.DeserializeObject <ProductModel>(prodStr); string moduleStr = TempData["module_" + data.guidNo] as string; if (string.IsNullOrEmpty(moduleStr)) { throw new Exception("資料錯誤,請重新讀取頁"); } ProductModuleModel module = JsonConvert.DeserializeObject <ProductModuleModel>(moduleStr); string pkgStr = TempData["pkg_" + data.guidNo] as string; if (string.IsNullOrEmpty(pkgStr)) { throw new Exception("資料錯誤,請重新讀取頁"); } PkgDetailModel pkg = JsonConvert.DeserializeObject <PkgDetailModel>(pkgStr); string pkgConfirmStr = TempData["confirm_" + data.guidNo] as string; if (string.IsNullOrEmpty(moduleStr)) { throw new Exception("資料錯誤,請重新讀取頁"); } confirmPkgInfo confirm = JsonConvert.DeserializeObject <confirmPkgInfo>(pkgConfirmStr); string titleStr = TempData["ProdTitleKeep_" + data.guidNo] as string; if (string.IsNullOrEmpty(moduleStr)) { throw new Exception("資料錯誤,請重新讀取頁"); } ProdTitleModel title = JsonConvert.DeserializeObject <ProdTitleModel>(titleStr); string discRuleStr = TempData["pkgsDiscRule_" + data.guidNo] as string; if (string.IsNullOrEmpty(discRuleStr)) { throw new Exception("資料錯誤,請重新讀取頁"); } DiscountRuleModel rule = JsonConvert.DeserializeObject <DiscountRuleModel>(discRuleStr); string pmgwStr = TempData["pmgw_" + data.guidNo] as string; if (string.IsNullOrEmpty(pmgwStr)) { throw new Exception("資料錯誤,請重新讀取頁"); } Pmgw pmgw = JsonConvert.DeserializeObject <Pmgw>(pmgwStr); string showStr = TempData["prodShow_" + data.guidNo] as string; if (string.IsNullOrEmpty(showStr)) { throw new Exception("資料錯誤,請重新讀取頁"); } //BookingShowProdModel show = JsonConvert.DeserializeObject<BookingShowProdModel>(showStr); TempData.Keep(); //排除餐食 data = BookingRepostory.exculdeFood(prod, data, module); string b2bOrder = BookingRepostory.insB2dOrder(title, prod, pkg, confirm, data, UserData, rule); //轉 ordermodel //OrderRepostory res = new OrderRepostory(); //OrderModel ord = res.setOrderModel(data,pmgw,title); //api.json = ord; //string orderModelStr = JsonConvert.SerializeObject(api); //Website.Instance.logger.Debug($"bookingStep1_ordernewdata:{ JsonConvert.SerializeObject(orderModelStr)}"); //KKapiHelper kk = new KKapiHelper(); //JObject order =kk.crtOrder(api); JObject order = ApiHelper.orderNew(data, title); string orderMid = ""; string orderOid = ""; returnStatus status = new returnStatus(); Website.Instance.logger.Debug($"bookingStep1_ordernewresponse:" + JsonConvert.SerializeObject(order));//要改 //要先判斷是不是result='0000' if (order["content"]["result"].ToString() == "0000") { string memUuid = "051794b8-db2a-4fe7-939f-31ab1ee2c719"; orderMid = order["content"]["orderMid"].ToString(); orderOid = order["content"]["orderOid"].ToString(); status.pmchSslRequest = BookingRepostory.setPaymentInfo2(prod, data, orderMid, UserData, pmgw, memUuid); status.status = "OK"; status.url = Website.Instance.Configuration["kkUrl:pmchUrl"].ToString() + pmgw.pmchPayURL; //pmchUrl //要把BookingShowProdModel 帶到訂購final頁 RedisHelper.SetRedis(showStr, "b2d:ec:order:final:prodShow:" + orderMid, 60); RedisHelper.SetRedis(JsonConvert.SerializeObject(data), "b2d:ec:order:final:orderData:" + orderMid, 60); //要存redis 付款主要資訊,最後訂單 upd時要使用,可和下面整合存一個就 BookingRepostory.setPayDtltoRedis(data, orderMid, memUuid, RedisHelper); //要存redis 因為付款後要從這個redis內容再進行payment驗證,可和上面整合存一個就好 //CallJsonPay rdsJson = (CallJsonPay)status.pmchSslRequest.json; CallJsonPay2 rdsJson = (CallJsonPay2)status.pmchSslRequest.json; string callPmchReq = JsonConvert.SerializeObject(status.pmchSslRequest.json); RedisHelper.SetRedis(callPmchReq, "b2d:ec:pmchSslRequest:" + orderMid, 60); } else { status.status = "Error"; status.msgErr = order["content"]["result"].ToString() + order["content"]["msg"].ToString();//要改 } return(Json(status)); //v1/channel/citi/auth //https://pmch.sit.kkday.com/citi/payment/auth //https://payment.kkday.com/v1/channel/adyen/auth } catch (Exception ex) { //error Website.Instance.logger.Debug($"bookingStep1_err_ordernew失敗:{ex.Message.ToString()}"); returnStatus status = new returnStatus(); status.status = "Error"; status.msgErr = ex.Message.ToString(); return(Json(status)); } }
public IActionResult bookingStep1([FromBody] DataModel data) { try { string memUuid = Website.Instance.Configuration["kkdayKey:uuid"]; string userAgent = Request.Headers["User-Agent"].ToString(); UserAgent ua = new UserAgent(userAgent); //B2d分銷商資料 var aesUserData = User.Identities.SelectMany(i => i.Claims.Where(c => c.Type == ClaimTypes.UserData).Select(c => c.Value)).FirstOrDefault(); var UserData = JsonConvert.DeserializeObject <B2dAccount>(AesCryptHelper.aesDecryptBase64(aesUserData, Website.Instance.AesCryptKey)); string ip = httpContextAccessor.HttpContext.Request.HttpContext.Connection.RemoteIpAddress.ToString().Replace("::1", "127.0.0.1"); data = BookingRepostory.setCardEncrypt(data); //log時把卡號移除 DataModel dataTemp = data.Clone(); dataTemp.card = null; Website.Instance.logger.Debug($"bookingStep1_inputdata:{ JsonConvert.SerializeObject(dataTemp)}"); string prodStr = TempData["prod_" + data.guidNo] as string; if (string.IsNullOrEmpty(prodStr)) { throw new Exception("資料錯誤,請重新讀取頁"); } ProductModel prod = JsonConvert.DeserializeObject <ProductModel>(prodStr); string moduleStr = TempData["module_" + data.guidNo] as string; if (string.IsNullOrEmpty(moduleStr)) { throw new Exception("資料錯誤,請重新讀取頁"); } ProductModuleModel module = JsonConvert.DeserializeObject <ProductModuleModel>(moduleStr); string pkgStr = TempData["pkg_" + data.guidNo] as string; if (string.IsNullOrEmpty(pkgStr)) { throw new Exception("資料錯誤,請重新讀取頁"); } PkgDetailModel pkg = JsonConvert.DeserializeObject <PkgDetailModel>(pkgStr); string pkgConfirmStr = TempData["confirm_" + data.guidNo] as string; if (string.IsNullOrEmpty(moduleStr)) { throw new Exception("資料錯誤,請重新讀取頁"); } confirmPkgInfo confirm = JsonConvert.DeserializeObject <confirmPkgInfo>(pkgConfirmStr); string titleStr = TempData["ProdTitleKeep_" + data.guidNo] as string; if (string.IsNullOrEmpty(moduleStr)) { throw new Exception("資料錯誤,請重新讀取頁"); } ProdTitleModel title = JsonConvert.DeserializeObject <ProdTitleModel>(titleStr); string discRuleStr = TempData["pkgsDiscRule_" + data.guidNo] as string; if (string.IsNullOrEmpty(discRuleStr)) { throw new Exception("資料錯誤,請重新讀取頁"); } DiscountRuleModel rule = JsonConvert.DeserializeObject <DiscountRuleModel>(discRuleStr); string pmgwStr = TempData["pmgw_" + data.guidNo] as string; if (string.IsNullOrEmpty(pmgwStr)) { throw new Exception("資料錯誤,請重新讀取頁"); } Pmgw pmgw = JsonConvert.DeserializeObject <Pmgw>(pmgwStr); string showStr = TempData["prodShow_" + data.guidNo] as string; if (string.IsNullOrEmpty(showStr)) { throw new Exception("資料錯誤,請重新讀取頁"); } //BookingShowProdModel show = JsonConvert.DeserializeObject<BookingShowProdModel>(showStr); TempData.Keep(); data = BookingRepostory.setDefaultBookingInfo(memUuid, ua, data, prod, pkg, confirm, UserData, pmgw); //排除餐食 data = BookingRepostory.exculdeFood(prod, data, module); data.company_xid = UserData.COMPANY_XID.ToString(); data.channel_oid = UserData.KKDAY_CHANNEL_OID; data.locale = UserData.LOCALE; data.ip = ip; JObject order = ApiHelper.orderNew(data, title); string orderMid = ""; string orderOid = ""; returnStatus status = new returnStatus(); Website.Instance.logger.Debug($"bookingStep1_ordernewresponse:" + JsonConvert.SerializeObject(order));//要改 //要先判斷是不是result='0000' if (order["result"].ToString() == "0000") { orderMid = order["order_mid"].ToString(); orderOid = order["order_oid"].ToString(); //upd B2bOrder //BookingRepostory.updB2dOrder(UserData.COMPANY_XID, orderOid, orderMid, b2bOrder, title); status.pmchSslRequest = BookingRepostory.setPaymentInfo2(prod, data, orderMid, UserData, pmgw, memUuid, ip); status.status = "OK"; status.url = Website.Instance.Configuration["kkUrl:pmchUrl"].ToString() + pmgw.pmchPayURL; //pmchUrl //要把BookingShowProdModel 帶到訂購final頁 RedisHelper.SetRedis(showStr, "b2d:ec:order:final:prodShow:" + orderMid, 60); RedisHelper.SetRedis(JsonConvert.SerializeObject(data), "b2d:ec:order:final:orderData:" + orderMid, 60); //要存redis 付款主要資訊,最後訂單 upd時要使用,可和下面整合存一個就 BookingRepostory.setPayDtltoRedis(data, orderMid, UserData.UUID, RedisHelper); //要存redis 因為付款後要從這個redis內容再進行payment驗證,可和上面整合存一個就好 //CallJsonPay rdsJson = (CallJsonPay)status.pmchSslRequest.json; CallJsonPay2 rdsJson = (CallJsonPay2)status.pmchSslRequest.json; string callPmchReq = JsonConvert.SerializeObject(status.pmchSslRequest.json); RedisHelper.SetRedis(callPmchReq, "b2d:ec:pmchSslRequest:" + orderMid, 60); } else { status.status = "Error"; status.msgErr = order["content"]["result"].ToString() + order["content"]["msg"].ToString();//要改 } return(Json(status)); //v1/channel/citi/auth //https://pmch.sit.kkday.com/citi/payment/auth //https://payment.kkday.com/v1/channel/adyen/auth } catch (Exception ex) { Website.Instance.logger.Debug($"bookingStep1_err_ordernew失敗:{ex.Message.ToString()}"); ViewData["errMsg"] = ex.Message.ToString(); Website.Instance.logger.Debug($"booking_index_err:{ex.Message.ToString()}"); //導到錯誤頁 return(RedirectToAction("Index", "Error", new ErrorViewModel { ErrorType = ErrorType.Order_Fail, ErrorMessage = ex.Message.ToString() })); } }
//2. 套價規則 public static double GetCompanyDiscPrice(Int64 company_xid, double b2d_price, string prod_no, string prod_type, ref DiscountRuleModel disc) { var objRules = new JObject(); List <DataModel.Discount.Rule> ruList = new List <DataModel.Discount.Rule>(); //prod_no = "2246"; //prod_type = "M06"; try { DataModel.Discount.Rule rule = null; //此分銷商的折扣規則 objRules = DiscountDAL.GetDiscRuleList(company_xid); //當初最原始牌價 rule = new DataModel.Discount.Rule(); rule.disc_price = b2d_price; rule.mst_xid = null; rule.disc_name = null; ruList.Add(rule); if (objRules["Table"] != null) { //找出不限的規則 var all_list = objRules["Table"].Where(y => y["rule_status"].ToString() == "00"); foreach (var item in all_list) { rule = new DataModel.Discount.Rule(); rule.mst_xid = (string)item["xid"]; rule.disc_percent = (double)item["disc_percent"]; rule.amt = (double)item["amt"]; rule.disc_price = System.Math.Round((b2d_price * (1 + rule.disc_percent / 100)) + rule.amt, MidpointRounding.AwayFromZero); rule.disc_name = (string)item["disc_name"]; ruList.Add(rule); } //找出有規定黑白名單條件的規則 var rules = objRules["Table"].Where(y => y["rule_status"].ToString() == "01" && (y["main_cat_wb"].ToString().Contains(prod_type + "^0") || (y["main_cat_wb"].ToString().Contains("^1") && !y["main_cat_wb"].ToString().Contains(prod_type + "^1"))) && //(符合白名單)|| (確定是黑名單&&不符合黑名單) 也算白名單 (y["prod_no_wb"].ToString().Contains(prod_no + "^0") || (y["prod_no_wb"].ToString().Contains("^1") && !y["prod_no_wb"].ToString().Contains(prod_no + "^1"))) //符合白名單 不符合黑名單 也算白名單 //!y["main_cat_wb"].ToString().Contains(prod_type + "^1") && !y["prod_no_wb"].ToString().Contains(prod_no + "^1")//符合黑名單 不符合白名單 也算黑名單 ); foreach (var item in rules) { rule = new DataModel.Discount.Rule(); rule.mst_xid = (string)item["xid"]; rule.disc_percent = (double)item["disc_percent"]; rule.amt = (double)item["amt"]; rule.disc_price = System.Math.Round((b2d_price * (1 + rule.disc_percent / 100)) + rule.amt, MidpointRounding.AwayFromZero); rule.disc_name = (string)item["disc_name"]; rule.currency = (string)item["currency"]; rule.disc_dtl_xid = (string)item["disc_dtl_xid"]; ruList.Add(rule); } } //套價規則排序 取最低價 ruList = ruList.OrderBy(x => x.disc_price).ToList(); if (disc == null) // 第一次進入才需要紀錄 折扣資訊 避免 price1 price2 price3 price4 記錄了4次 { disc = new DiscountRuleModel(); if (ruList[0].mst_xid == null) { disc.isRule = false; disc.disc_xid = null; disc.disc_name = null; disc.disc_percent = null; disc.amt = null; disc.currency = null; disc.disc_dtl_xid = null; } else { disc.isRule = true; disc.disc_xid = ruList[0].mst_xid; disc.disc_name = ruList[0].disc_name; disc.disc_percent = ruList[0].disc_percent; disc.amt = ruList[0].amt; disc.currency = ruList[0].currency; disc.disc_dtl_xid = ruList[0].disc_dtl_xid; } } Website.Instance.logger.Info($"B2D套價規則 COMPANY_XID:{company_xid},PROD_NO:,{prod_no},XID:{ruList[0].mst_xid},DISC_PERCENT:{ruList[0].disc_percent},DISC_AMT:{ruList[0].amt}"); } catch (Exception ex) { Website.Instance.logger.FatalFormat($"getDiscPrice Error :{ex.Message},{ex.StackTrace}"); throw ex; } return(ruList[0].disc_price); }