private async Task <bool> AplicarVoucher(AdicionarPedidoCommand message, Pedido pedido) { if (!message.VoucherUtilizado) { return(true); } var voucher = await _voucherRepository.ObterVoucherPorCodigo(message.VoucherCodigo); if (voucher == null) { AdicionarErro("O voucher informado não existe!"); return(false); } var voucherValidation = new VoucherValidation().Validate(voucher); if (!voucherValidation.IsValid) { voucherValidation.Errors.ToList().ForEach(m => AdicionarErro(m.ErrorMessage)); return(false); } pedido.AtribuirVoucher(voucher); voucher.DebitarQuantidade(); _voucherRepository.Atualizar(voucher); return(true); }
private async Task <bool> ApplyVoucher(ExecuteOrderCommand message, OrderCustomer order) { if (!message.VoucherUsage) { return(true); } var voucher = await _voucherRepository.GetVoucherByCode(message.VoucherCode); if (voucher == null) { AddErrors("Voucher not found!"); return(false); } var voucherValidation = new VoucherValidation().Validate(voucher); if (!voucherValidation.IsValid) { voucherValidation.Errors.ToList().ForEach(m => AddErrors(m.ErrorMessage)); return(false); } order.AssociateVoucher(voucher); voucher.DebitStock(); _voucherRepository.Update(voucher); return(true); }
private bool ApplyVoucher(CreateOrderCommand message, Domain.Orders.Order order) { if (!message.HasVoucher) { return(true); } var voucher = new Voucher(message.Voucher.Code, message.Voucher.Discount, 1, (VoucherType)message.Voucher.DiscountType, DateTime.Now.AddMinutes(1), true, false); var voucherValidation = new VoucherValidation().Validate(voucher); if (!voucherValidation.IsValid) { voucherValidation.Errors.ToList().ForEach(m => AddError(m.ErrorMessage)); return(false); } order.ApplyVoucher(voucher); return(true); }
public async Task<VoucherValidation> VoucherValidation() { try { WriteLog("VoucherTranfer", "VoucherTranfer"); VoucherValidation model = new VoucherValidation(); string apiUrl = string.Empty; if (ConfigurationManager.AppSettings.AllKeys.Contains("ApiUrl")) // Key exists { apiUrl = ConfigurationManager.AppSettings["ApiUrl"].ToString(); WriteLog("VoucherTranfer apiUrl", apiUrl); } else//Error log { } string rechargeType = Request.Form["rechargeType"]; string serviceType = Request.Form["serviceType"]; string operatorName = Request.Form["operatorCode"]; string mobileNumber = Request.Form["mobileNumber"]; string amount = Request.Form["amount"]; string paymentID = Request.Form["paymentID"]; string status = Request.Form["result"]; string trackID = Request.Form["trackID"]; string tranID = Request.Form["tranID"]; string reference = Request.Form["ref"]; using (var client = new HttpClient()) { #region login and get token var logindata = string.Format("grant_type=password&username={0}&password={1}", USERNAME, PASSWORD);//LOGIN DATA var url = BASEADDRESS + "token"; client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url); request.Content = new StringContent(logindata, Encoding.UTF8, "application/x-www-form-urlencoded"); var resp = client.PostAsync(url, request.Content); Token token = new Token(); if (resp.Result.IsSuccessStatusCode) { token = await resp.Result.Content.ReadAsAsync<Token>(); } #endregion login and get token using (var httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(token.token_type, token.access_token); httpClient.BaseAddress = new Uri(BASEADDRESS); string data = string.Format("?OperatorName={0}&Denomination={1}&CardCount={2}",operatorName, amount, "1"); HttpResponseMessage response = await httpClient.GetAsync("api/Services/VoucherValidation" + data); if (response.IsSuccessStatusCode) { model = await response.Content.ReadAsAsync<VoucherValidation>(); } } } return model; } catch (Exception ex) { WriteLog("VoucherTranfer Exception ", ex.Message); throw ex; } }