/// <summary>
 /// Fire up the repos and service context in the constructor.
 /// </summary>
 /// <param name="oAuthorization"></param>
 public SyncService(OAuthorizationdto oAuthorization)
 {
     dataserviceFactory = new DataserviceFactory(oAuthorization);
     dataService = dataserviceFactory.getDataService();
     syncObjects = new Syncdto();
     syncRepository = new SyncRepository();
 }
 /// <summary>
 /// Sequence:
 /// -->Retrieve the Request token
 /// -->Retrieve the value from query string
 /// -->Retrieve acces token
 /// -->Retrieve acces secret
 /// -->Redirect to close
 /// </summary>
 /// <returns></returns>
 public ActionResult Response()
 {
     oAuthorizationDB = new OAuthTokens();
     oAuthService = new OAuthService(oAuthorizationdto);
     oAuthorizationdto = oAuthService.GetRequestToken(this);
     if (Request.QueryString.HasKeys())
     {
         oAuthorizationdto.OauthVerifyer = Request.QueryString["oauth_verifier"].ToString();
         oAuthorizationDB.realmid = Convert.ToInt32(Request.QueryString["realmId"].ToString());
         oAuthorizationdto.Realmid = oAuthorizationDB.realmid;
         oAuthorizationDB.datasource = Request.QueryString["dataSource"].ToString();
         oAuthorizationdto.DataSource = oAuthorizationDB.datasource;
         oAuthorizationdto = oAuthService.GetAccessTokenFromServer(this,oAuthorizationdto);
         //encrypt the tokens
         oAuthorizationDB.access_secret = Utility.Encrypt(oAuthorizationdto.AccessTokenSecret, oAuthorizationdto.SecurityKey);
         oAuthorizationDB.access_token = Utility.Encrypt(oAuthorizationdto.AccessToken, oAuthorizationdto.SecurityKey);
         using (var oAuthorizationDBContext = new OAuthdataContext("DBContext"))
         {
             //store the encrypted tokens to DB.
             oAuthorizationDBContext.Tokens.Add(oAuthorizationDB);
             oAuthorizationDBContext.SaveChanges();
         }
     }
     return RedirectToAction("Close", "Home");
 }
 /// <summary>
 /// Action Result for Index, This flow will create OAuthConsumer Context using Consumer key and Consuler Secret key
 /// obtained when Application is added at intuit workspace. It creates OAuth Session out of OAuthConsumer and Calls 
 /// Intuit Workpsace endpoint for OAuth.
 /// </summary>
 /// <returns>Redirect Result.</returns>
 public RedirectResult Index()
 {
     oAuthorizationdto = new OAuthorizationdto();
     oAuthService = new OAuthService(oAuthorizationdto);
     oAuthorizationdto.CallBackUrl = Request.Url.GetLeftPart(UriPartial.Authority) + "/Oauth/Response";
     return Redirect(oAuthService.GrantUrl(this));
 }
 /// <summary>
 /// This function checks whether the token is present.
 /// </summary>
 /// <param name="oauthController"></param>
 /// <returns></returns>
 public OAuthorizationdto IsTokenAvailable(object oauthController)
 {
     var oAuthDetails = new OAuthorizationdto();
     using (var db = new OAuthdataContext("DBContext"))
     {
         foreach (var currentIndex in db.Tokens)
         {
             oAuthDetails.Realmid = currentIndex.realmid;
             string testAccesToken = Utility.Decrypt(currentIndex.access_token, oAuthDetails.SecurityKey);
             string testAccesTokenSecret = Utility.Decrypt(currentIndex.access_secret, oAuthDetails.SecurityKey);
             oAuthDetails.AccessToken = testAccesToken;
             oAuthDetails.AccessTokenSecret = testAccesTokenSecret;
             oAuthDetails.IsConnected = true;
             oAuthDetails.DataSource = currentIndex.datasource;
             oAuthRepository.Save(oauthController, oAuthDetails);
         }
     }
     return oAuthDetails;
 }
 /// <summary>
 /// allocate memory for service context objects
 /// </summary>
 /// <param name="oAuthorization"></param>
 public DataserviceFactory(OAuthorizationdto oAuthorization)
 {
     try
     {
         oAuthRequestValidator = new OAuthRequestValidator(
         oAuthorization.AccessToken, 
         oAuthorization.AccessTokenSecret, 
         oAuthorization.ConsumerKey, 
         oAuthorization.ConsumerSecret);
     intuitServicesType = oAuthorization.DataSource == "QBO" ? IntuitServicesType.QBO : IntuitServicesType.None;
     serviceContext = new ServiceContext(oAuthorization.Realmid.ToString(), intuitServicesType, oAuthRequestValidator);
     serviceContext.IppConfiguration.BaseUrl.Qbo = ConfigurationManager.AppSettings["ServiceContext.BaseUrl.Qbo"];
     serviceContext.IppConfiguration.Logger.RequestLog.EnableRequestResponseLogging = true;
     serviceContext.IppConfiguration.Logger.RequestLog.ServiceRequestLoggingLocation = ConfigurationManager.AppSettings["ServiceRequestLoggingLocation"];
     getServiceContext = serviceContext;
     dataService = new DataService(serviceContext);
     }
     catch (Intuit.Ipp.Exception.FaultException ex)
     {
         throw ex;
     }
 }
 /// <summary>
 /// Constructor instantiate the repository
 /// </summary>
 /// <param name="oAuthDto"></param>
 public OAuthService(OAuthorizationdto oAuthDto)
 {
    
     oAuthorizationdto = oAuthDto;
     oAuthRepository = new OAuthRepository();
 }
 /// <summary>
 /// Retrieve the access token from the server.
 /// </summary>
 /// <param name="oauthController"></param>
 /// <param name="oAuthorizationdto"></param>
 /// <returns></returns>
 internal OAuthorizationdto GetAccessTokenFromServer(object oauthController,OAuthorizationdto oAuthorizationdto)
 {
     try
     {
         IToken accessToken = oAuthorizationdto.OAuthSession.ExchangeRequestTokenForAccessToken(oAuthorizationdto.Token, oAuthorizationdto.OauthVerifyer);
         oAuthorizationdto.AccessToken = accessToken.Token;
         oAuthorizationdto.AccessTokenSecret = accessToken.TokenSecret;
         oAuthorizationdto.IsConnected = true;
         oAuthRepository.Save(oauthController, oAuthorizationdto);
         return oAuthorizationdto;
     }
     catch (Intuit.Ipp.Exception.FaultException ex)
     {
         throw ex;
     }
     catch (Intuit.Ipp.Exception.InvalidTokenException ex)
     {
         throw ex;
     }
     catch (Intuit.Ipp.Exception.SdkException ex)
     {
         throw ex;
     }
 }