Beispiel #1
0
        private void Sender(ServiceCode service, string from, string to, string subject, string message)
        {
            var messageRequest = new MessageRequestModel()
            {
                From    = from,
                To      = to,
                Message = message,
                Subject = subject,
                Brand   = _xnotifiSettings.Value.Brand
            };

            var gateway = GetGateway(service);

            gateway.Password = _generator.Decrypt(gateway.Password).Result;
            var application = GetCreateApplication(_xnotifiSettings.Value.Brand, _xnotifiSettings.Value.ClientId);

            var messager = (_xnotifiSettings.Value.Development)? "Local Dev." : _messageAdapter.Send(messageRequest, gateway);

            var requestLog = new RequestModel()
            {
                ApplicationId = application.ApplicationId,
                GatewayId     = gateway.GatewayId,
                Service       = ServiceCode.Email,
                Recipients    = 1,
                Cost          = 0.00m,
                Response      = messager,
                DateCreated   = DateTime.UtcNow
            };

            _requestService.Create(requestLog);
        }
Beispiel #2
0
        public IActionResult Edit(string id)
        {
            SelectList();

            var request = _gatewayService.Get(id);

            //var response = (request.Successful) ? request. : null;
            request.Password = _generator.Decrypt(request.Password).Result;
            return(View(request));
        }
        private MessageResponseModel Messager(MessageRequestModel model)
        {
            if (!ModelState.IsValid)
            {
                return(new MessageResponseModel()
                {
                    Error = Data.Enums.MessageStatus.InvalidFields,
                    Message = "Required parameter(s) are missing"
                });
            }

            if (!model.Service.Equals(ServiceCode.Sms) && !model.Service.Equals(ServiceCode.Email))
            {
                return(new MessageResponseModel()
                {
                    Error = Data.Enums.MessageStatus.InvalidService,
                    Message = "Invalid service request"
                });
            }

            var gateway = _gatewayService.List()
                          .FirstOrDefault(x => x.ServiceCode == model.Service && x.IsActive == true);

            if (gateway == null)
            {
                return(new MessageResponseModel()
                {
                    Error = Data.Enums.MessageStatus.InactiveGateway,
                    Message = "No active gateway available"
                });
            }

            var client = _clientService.List().ToList().FirstOrDefault(x =>
                                                                       x.ClientId.Equals(model.ClientId) && x.AccessKey.Equals(model.AccesssKey) && x.IsActive == true);

            if (client == null)
            {
                return(new MessageResponseModel()
                {
                    Error = Data.Enums.MessageStatus.InvalidApplication,
                    Message = "No client record is found"
                });
            }

            var existingApplication = _applicationService.List()
                                      .FirstOrDefault(x => x.ClientId == model.ClientId && x.Name.ToLower().Equals(model.AppName.ToLower()) && x.IsActive == true);

            string applicationId;
            string applicationName;

            if (existingApplication == null)
            {
                var applicationModel = new ApplicationModel()
                {
                    ClientId    = model.ClientId,
                    Name        = model.AppName,
                    DateCreated = DateTime.UtcNow
                };
                var newApplication = _applicationService.Create(applicationModel).Result;
                applicationId   = newApplication.ApplicationId;
                applicationName = newApplication.Name;
            }
            else
            {
                applicationId   = existingApplication.ApplicationId;
                applicationName = existingApplication.Name;
            }

            var price        = (model.Service == ServiceCode.Sms) ? _priceSettings.Value.SmsPrice : _priceSettings.Value.EmailPrice;
            var destinations = model.To.Split(',');
            var cost         = destinations.Length * price;



            if (client.Balance < cost)
            {
                return(new MessageResponseModel()
                {
                    Error = Data.Enums.MessageStatus.InsufficientCredit,
                    Message = "Insufficient credit"
                });
            }

            gateway.Password = _generator.Decrypt(gateway.Password).Result;

            try
            {
                //var gatewayResponse = "local testing";

                var gatewayResponse = _messageAdapter.Send(model, gateway);


                client.Balance = client.Balance - cost;
                _clientService.Update(client);

                var request = new RequestModel()
                {
                    ApplicationId = applicationId,
                    GatewayId     = gateway.GatewayId,
                    Recipients    = destinations.Length,
                    Cost          = cost,
                    Response      = gatewayResponse,
                    DateCreated   = DateTime.UtcNow
                };

                var lastRequest = _requestService.Create(request).Result;

                return(new MessageResponseModel()
                {
                    Error = Data.Enums.MessageStatus.MessageSubmitted,
                    Message = "Message(s) submited successfully",
                    Reference = lastRequest.RequestId
                });
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                //throw;
            }



            var response = new MessageResponseModel()
            {
                Error   = Data.Enums.MessageStatus.InternalServerError,
                Message = "Internal server error"
            };

            return(response);
        }