public async Task <IActionResult> Post([FromBody] Order value)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var config = _smsapiRepository.Query().LastOrDefault(a => a.Status.ToLower().Contains("active") && a.Default == true);
            var sms    = new Sms();

            sms.UserId  = value.UserId; sms.Date = DateTime.UtcNow; sms.Mobile = value.Mobile;
            sms.Message = $"Welcome to {config.Name} {value.FullName}, your OrderNo: {value.OrderNo}, Amount: {value.Price}, VAT: {value.Vat}, Total: {value.Total}. We are Glad to serve you.";

            try
            {
                var           httpClient = new HttpClient();
                StringBuilder sb         = new StringBuilder();
                sb.Append(config.Url).Append("?&username="******"&password="******"&source=").Append(config.SenderId)
                .Append("&destination=").Append(sms.Mobile).Append("&message=").Append(sms.Message);
                var json = await httpClient.GetStringAsync(sb.ToString());

                var smsresponse = JsonConvert.DeserializeObject <SmsResponse>(json);
                sms.Code  = smsresponse.Code; sms.Response = smsresponse.Message;
                sms.MDate = DateTime.UtcNow;

                await _smsRepository.InsertAsync(sms);
            }
            catch (Exception e)
            {
                sms.Code = 400; sms.Response = "Unable to send sms";
                await _smsRepository.InsertAsync(sms);

                BackgroundJob.Schedule(() => RetrybyId(sms.SmsId), TimeSpan.FromMinutes(3));

                return(null);
            }
            return(Created($"sms/{sms.SmsId}", sms));
        }
예제 #2
0
        public async Task <IActionResult> Post([FromBody] Sms sms)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var config = _smsapiRepository.Query().LastOrDefault(a => a.Status.ToLower().Contains("active") && a.Default == true);

            try
            {
                var           httpClient = new HttpClient();
                StringBuilder sb         = new StringBuilder();
                sb.Append(config.Url).Append("?cmd=sendquickmsg&[email protected]&subacct=")
                .Append(config.Username).Append("&subacctpwd=").Append(config.Password)
                .Append("&message=").Append(sms.Message).Append("&sender=").Append(config.SenderId)
                .Append("&sendto=").Append(sms.Mobile).Append("&msgtype=0");
                var json = await httpClient.GetStringAsync(sb.ToString());

                // var smsresponse = JsonConvert.DeserializeObject<SmsResponse>(json);
                sms.Code  = 0; sms.Response = json;
                sms.MDate = DateTime.UtcNow;

                await _smsRepository.InsertAsync(sms);
            }
            catch (Exception e)
            {
                sms.Code = 400; sms.Response = "Unable to send sms";
                await _smsRepository.InsertAsync(sms);

                BackgroundJob.Schedule(() => RetrybyId(sms.SmsId), TimeSpan.FromMinutes(3));

                return(null);
            }
            return(Created($"sms/{sms.SmsId}", sms));
        }