Esempio n. 1
0
        public ActionResult Create(CreateReferral model)
        {
            try
            {
                if (model.Referrals[1].EmailAddress == null)
                {
                    _model              = new CreateReferral();
                    _model.AccountId    = model.AccountId;
                    _model.Referrals    = new Referral[1];
                    _model.Referrals[0] = model.Referrals[0];
                }
                else
                {
                    _model = model;
                }

                if (_accessToken == null)
                {
                    getAccessToken();
                }

                if (ModelState.IsValid)
                {
                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                    var request = JsonConvert.SerializeObject(_model);

                    using (WebClient client = new WebClient())
                    {
                        client.Headers[HttpRequestHeader.Accept]      = "application/json";
                        client.Headers[HttpRequestHeader.ContentType] = "application/json";
                        //                        client.Headers[ HttpRequestHeader.ContentType ] = "application/json; charset=utf-8";
                        client.Headers[HttpRequestHeader.Authorization] = "Bearer " + _accessToken;

                        //                        client.Encoding = System.Text.Encoding.UTF8;

                        var jsonResponse = client.UploadString(postURL, request);

                        return(Content(jsonResponse, "application/json"));

                        //throw new NotImplementedException();
                    }
                }
            }
            catch (System.Net.WebException e)
            {
                if (((HttpWebResponse)e.Response).StatusCode == HttpStatusCode.Unauthorized)
                {
                    // get token and retry
                    getAccessToken();

                    //sendJourney( _accessToken, _model );
                    Create(model);

                    return(Content(e.Message, "application/json"));
                }
                return(Content(e.Message, "application/json"));
            }

            return(View("Index", model));
        }
Esempio n. 2
0
        public ActionResult TrackReferral(CreateReferral model)
        {
            try
            {
                var accountId = model.AccountId;

                if (_accessToken == null)
                {
                    getAccessToken();
                }

                //      if ( ModelState.IsValid )
                {
                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                    //var request = JsonConvert.SerializeObject( _model );

                    using (WebClient client = new WebClient())
                    {
                        client.Headers[HttpRequestHeader.Accept]      = "application/json";
                        client.Headers[HttpRequestHeader.ContentType] = "application/json";
                        //                        client.Headers[ HttpRequestHeader.ContentType ] = "application/json; charset=utf-8";
                        client.Headers[HttpRequestHeader.Authorization] = "Bearer " + _accessToken;

                        //                        client.Encoding = System.Text.Encoding.UTF8;

                        var jsonResponse = client.DownloadString("https://nbty--UAT.cs64.my.salesforce.com/services/apexrest/TrackReferrals?AccountId=" + accountId);

                        //return Content( jsonResponse, "application/json" );
                        return(Json(jsonResponse, JsonRequestBehavior.AllowGet));

                        //throw new NotImplementedException();
                    }
                }
            }
            catch (System.Net.WebException e)
            {
                if (((HttpWebResponse)e.Response).StatusCode == HttpStatusCode.Unauthorized)
                {
                    // get token and retry
                    getAccessToken();

                    //sendJourney( _accessToken, _model );
                    TrackReferral(model);

                    return(Content(e.Message, "application/json"));
                }
                return(Content(e.Message, "application/json"));
            }

            return(View("TrackReferral", model));
        }