[Authorize] [HttpPost] [Route("ApplyVoucher")] public Apac.Api.Models.ApplyVoucherResponse ApplyVoucher(Apac.Api.Models.ApplyVoucherRequest applyVoucherRequest) { Apac.Api.Models.ApplyVoucherResponse applyVoucherResponse = new Apac.Api.Models.ApplyVoucherResponse(); applyVoucherResponse.ResponseStatus.Code = "200"; applyVoucherResponse.ResponseStatus.Message = "SUCCESS"; try { string username = ((ClaimsIdentity)RequestContext.Principal.Identity).Claims.FirstOrDefault(x => x.Type == "username").Value; string token = ((ClaimsIdentity)RequestContext.Principal.Identity).Claims.FirstOrDefault(x => x.Type == "membersontoken").Value; Dynamicweb.Security.UserManagement.User user = Dynamicweb.Security.UserManagement.User.GetUserByUserName(username); var commandBuilder = new Dynamicweb.Data.CommandBuilder(); commandBuilder.Add("SELECT AccessUserCartId FROM AccessUser WHERE AccessUserId={0}", user.ID); string cartId = Dynamicweb.Data.Database.ExecuteScalar(commandBuilder).ToString(); string customerNumber = username; if (applyVoucherRequest.OrderId != cartId) { applyVoucherResponse.ResponseStatus.Code = "000"; applyVoucherResponse.ResponseStatus.Message = "ERROR: Requested OrderId and current customer's OrderId does not match."; return applyVoucherResponse; } Dynamicweb.Ecommerce.Orders.OrderService orderService = new Dynamicweb.Ecommerce.Orders.OrderService(); Dynamicweb.Ecommerce.Orders.Order dwOrder = new Dynamicweb.Ecommerce.Orders.Order(); dwOrder = orderService.GetOrder(cartId); if (dwOrder == null) { applyVoucherResponse.ResponseStatus.Code = "000"; applyVoucherResponse.ResponseStatus.Message = "Customer doesn't have current shopping cart."; return applyVoucherResponse; } Apac.Api.Models.OrderDTO orderDTO = new Models.OrderDTO(); orderDTO = new Apac.Api.Controllers.BraintreeMobilePaymentController().GetOrderObj_PageControl(dwOrder.Id); string rewardName = ""; double rewardDiscount = 0; double promotionWithProduct = 0; double promotion = 0; foreach (Apac.Api.Models.OrderLineDTO orderLineDTO in orderDTO.OrderLines) { if (orderLineDTO.Type == "3" || orderLineDTO.Type == "1") { string rewardOrDiscount = ""; if (orderLineDTO.DiscountId.Contains("SALESDISCNT")) { var sqlCommand = "SELECT DiscountDescription FROM EcomDiscount WHERE DiscountId='" + orderLineDTO.DiscountId + "'"; using (System.Data.IDataReader reader = Dynamicweb.Data.Database.CreateDataReader(sqlCommand)) { while (reader.Read()) { rewardOrDiscount = reader["DiscountDescription"].ToString(); } } } if (rewardOrDiscount == "Reward") { rewardDiscount = orderLineDTO.Price.PriceWithoutVAT; rewardName = orderLineDTO.ProductName; } if (rewardOrDiscount == "Promotion") { promotionWithProduct += orderLineDTO.Price.PriceWithVAT; if (orderLineDTO.Type == "1") { promotion = orderLineDTO.Price.PriceWithVAT; } } } } if (rewardName.ToUpper() == "REWARD" && applyVoucherRequest.ApplyVoucherType.ToUpper() != "REWARD") { applyVoucherResponse.Message = "Reward Code Applied."; return applyVoucherResponse; } else if (promotion != 0.00 || promotionWithProduct != 0.00) { applyVoucherResponse.Message = "Promotion Code Applied."; return applyVoucherResponse; } //List voucherList = new List(); //voucherList = GetPromotion_Helper(customerNumber); applyVoucherResponse.ResponseStatus = ApplyVoucher_PageControl(applyVoucherRequest); } catch (Exception ex) { applyVoucherResponse.ResponseStatus.Code = "000"; applyVoucherResponse.ResponseStatus.Message = "ERROR: " + ex.Message.ToString(); } return applyVoucherResponse; } public Apac.Api.Models.ResponseStatus ApplyVoucher_PageControl(Apac.Api.Models.ApplyVoucherRequest applyVoucherRequest) { Apac.Api.Models.ResponseStatus responseStatus = new Apac.Api.Models.ResponseStatus(); string pageResult = "{}"; string myDomain = System.Web.HttpContext.Current.Request.Url.Scheme + "://" + System.Web.HttpContext.Current.Request.Url.Host; string bodyContent = ""; bodyContent = new JavaScriptSerializer().Serialize(applyVoucherRequest); string url = myDomain + "/api/set-voucher-to-order"; HttpContent httpContent = new StringContent(bodyContent); using (HttpClient client = new HttpClient()) { client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage response = client.PostAsync(url, httpContent).Result; pageResult = response.Content.ReadAsStringAsync().Result; } JObject jObjResult = JObject.Parse(pageResult); responseStatus.Code = jObjResult["Code"].ToString(); responseStatus.Message = jObjResult["Message"].ToString(); return responseStatus; }