public HttpResponseMessage PostSmsNotification(PGNotificationSMS notification)
 {
     var sms = new SDPSMSRequest();
     ServiceProvider sp = _resolveMessageService.GetServiceProvider(notification.HubId);
     if (sp == null)
     {
         return Request.CreateResponse(HttpStatusCode.OK, "This service provider is not registered.");
     }
     sms = new SDPSMSRequest
     {
         applicationId = sp.SdpAppId,
         password = sp.SdpPassword,
         destinationAddresses = notification.Recipitents.Distinct().Select(n => "tel:" + n).ToList(),
         deliveryStatusRequest = 1,
         encoding = SDPSmsEncoding.Text,
         message = notification.SmsBody,
         sourceAddress = "20358",
         binaryHeader = "",
         chargingAmount = 0,
         version = "1.0",
         
         
     };
     SDPSMSResponse sdpResponse = HSenidSendNotification(sms);
     return Request.CreateResponse(HttpStatusCode.OK, sdpResponse.statusCode +" "+ sdpResponse.statusDetail);
 }
        public HttpResponseMessage SmsNotificationSend()
        {
            var sms = new SDPSMSRequest();
            ServiceProvider sp = _resolveMessageService.GetServiceProvider(new Guid("ae19c448-47a4-4044-843c-102085e0157f"));
            if (sp == null)
            {
                return Request.CreateResponse(HttpStatusCode.OK, "This service provider is not registered.");
            }
            sms = new SDPSMSRequest
            {
                applicationId = sp.SdpAppId,
                password = sp.SdpPassword,
                destinationAddresses = new List<string>(){"254722557538"}.Distinct().Select(n => "tel:" + n).ToList(),
                deliveryStatusRequest = 1,
                encoding = SDPSmsEncoding.Text,
                message = "Test",
                sourceAddress = "20358",
                binaryHeader = "",
                chargingAmount = 0,
                version = "1.0",


            };
            SDPSMSResponse sdpResponse = HSenidSendNotification(sms);
            return Request.CreateResponse(HttpStatusCode.OK, sdpResponse.statusCode + " " + sdpResponse.statusDetail);
            return Request.CreateResponse(HttpStatusCode.OK, "ok");
        }
        SDPSMSResponse HSenidSend(SDPSMSRequest sdpSms, DocSMS clientSms)
        {
            SDPSMSResponse response;
            string jsonRequest = JsonConvert.SerializeObject(sdpSms);
            WebClient wc = new WebClient();
            wc.Encoding = Encoding.UTF8;
            string hsenidUrl = "";// Request.Url.AbsoluteUri.Substring(0, (Request.Url.AbsoluteUri.Length - 1));
            hsenidUrl = SdpHost.GetSdpsmsUri();
            Uri uri = new Uri(hsenidUrl , UriKind.Absolute);

            _auditLogRepository.AddLog(clientSms.DistributorCostCenterId, "SMS Request", "To HSenid", string.Format("Json SMS Request: {0}", jsonRequest));
            string jsonResponse = wc.UploadString(uri, "POST", jsonRequest);
            _messageValidation.CanDeserializeMessage(jsonResponse, out response);
            _auditLogRepository.AddLog(clientSms.DistributorCostCenterId,"SMS Response","From HSenid", string.Format("Json SMS Response: {0}", jsonResponse));
            
            return response;
        }
        SDPSMSResponse HSenidSendNotification(SDPSMSRequest sdpSms)
        {
            SDPSMSResponse response;
            string jsonRequest = JsonConvert.SerializeObject(sdpSms);
            WebClient wc = new WebClient();
            wc.Encoding = Encoding.UTF8;
            string hsenidUrl = "";// Request.Url.AbsoluteUri.Substring(0, (Request.Url.AbsoluteUri.Length - 1));
            hsenidUrl = SdpHost.GetSdpsmsUri();
            Uri uri = new Uri(hsenidUrl, UriKind.Absolute);

           
            string jsonResponse = wc.UploadString(uri, "POST", jsonRequest);
            _messageValidation.CanDeserializeMessage(jsonResponse, out response);
           return response;
        }
        public HttpResponseMessage SmsQuery(string code, string clientKeyWord, string actionKeyWord, string phoneNumber)
        {

            string msg = "";
                
            var sms = new SDPSMSRequest();
            var client = _clientRepository.GetByCode(clientKeyWord);
            if (client == null)
            {
                msg= "This Client is not registered.";
            }
            else
            {
                msg = _smsQueryResolver.SmsQuery(new SmsQuery
                {
                    Code = code,
                    ActionKeyWord = actionKeyWord,
                    ClientKeyWord = clientKeyWord,
                    PhoneNumber = phoneNumber
                });
            }
          
            sms = new SDPSMSRequest
            {
                applicationId = client.ApplicationId,
                password = client.ApplicationPassword,
                destinationAddresses = new List<string>{"tel:" +phoneNumber},
                deliveryStatusRequest = 1,
                encoding = SDPSmsEncoding.Text,
                message = msg,
                sourceAddress = "20358",
                binaryHeader = "",
                chargingAmount = 0,
                version = "1.0",
            };
            SDPSMSResponse sdpResponse = HSenidSendNotification(sms);

            return Request.CreateResponse(HttpStatusCode.OK, "Sent");
        }
        public void ProcessClientRequest(ClientRequestResponseBase crrMessage, ServiceProvider serviceProvider, out ServerRequestBase serverReqBase)
        {
            if (crrMessage == null)
            {
                throw new Exception("Message null");
            }

            serverReqBase = new ServerRequestBase();
            string userName = "";
            string password = "******";
            string subscriberId = "tel:254701234563";
            string applicationId = "APP_000007";
            bool allowOverPayment = true, allowPartialPayment = true;
            string version = "1.0";
            string sourcesAddress = "hewani";
            string binaryHeader = "Content-Type:application/json";

            if (serviceProvider != null)
            {
                applicationId = serviceProvider.SdpAppId;
                subscriberId = serviceProvider.SubscriberId;
                password = serviceProvider.SdpPassword;
                allowOverPayment = serviceProvider.AllowOverPayment;
                allowPartialPayment = serviceProvider.AllowPartialPayment;
            }
            else
            {
                throw new Exception("This service provider is not registered.");
            }
            //
            if (crrMessage is PaymentInstrumentRequest)
            {
                PaymentInstrumentRequest pir = crrMessage as PaymentInstrumentRequest;
                SDPPaymentInstrumentRequest paymentIstReq = new SDPPaymentInstrumentRequest
                                                                {
                                                                    applicationId     = applicationId,
                                                                    password          = password,
                                                                    type              = pir.paymentInstrumentType,
                                                                    //subscriberId    = subscriberId
                                                                    subscriberId      = pir.SubscriberId
                                                                };

                serverReqBase = paymentIstReq;
            }
            if (crrMessage is PaymentRequest)
            {
                PaymentRequest apr         = crrMessage as PaymentRequest;
                SDPPaymentRequest sdpapr   = new SDPPaymentRequest();
                sdpapr.accountId                       = apr.AccountId.ToString();
                sdpapr.allowOverPayments = allowOverPayment ? AllOverPayment.Allow.ToString() : AllOverPayment.Disallow.ToString();
                sdpapr.allowPartialPayments = allowPartialPayment ? AllowPartialPayments.Allow.ToString() : AllowPartialPayments.Disallow.ToString();
                sdpapr.amount                          = apr.Amount.ToString();
                sdpapr.applicationId                   = applicationId;
                sdpapr.currency                        = apr.Currency;
                sdpapr.externalTrxId                   = apr.TransactionRefId.Replace("-","");
                sdpapr.extra = apr.Extra; //new Dictionary<string, string>();// {new string("tilNo","66363" )};
               // sdpapr.extra.Add("tillNo","66361"); 
                sdpapr.invoiceNo                       = apr.InvoiceNumber;
                sdpapr.orderNo                         = apr.OrderNumber;
                sdpapr.password                        = password;
                sdpapr.paymentInstrumentName           = apr.PaymentInstrumentName;
                //sdpapr.subscriberId                  = subscriberId;
                sdpapr.subscriberId                    = apr.SubscriberId;
                sdpapr.smsDescription                  = apr.smsDescription;
                
                serverReqBase = sdpapr;
            }
            if (crrMessage is PaymentQueryRequest)
            {
                PaymentQueryRequest apq = crrMessage as PaymentQueryRequest;
                SDPPaymentQueryRequest sdpapq = new SDPPaymentQueryRequest();

                sdpapq.applicationId = applicationId;
                sdpapq.internalTrxId = apq.TransactionRefId.ToString();
                sdpapq.password      = password;

                serverReqBase = sdpapq;
            }
            if(crrMessage is DocSMS)
            {
                DocSMS sms = crrMessage as DocSMS;
                SDPSMSRequest reqSMS = new SDPSMSRequest
                                        {
                                            applicationId = applicationId,
                                            password = password,
                                            destinationAddresses = sms.Recipitents.Select(n => "tel:" + n).ToList(),
                                            deliveryStatusRequest = 1,
                                            encoding = SDPSmsEncoding.Text,
                                            message = sms.SmsBody,
                                        };
            }
        }