Ejemplo n.º 1
0
        public ActionResult Tab(string code, string signed_request)
        {
            var client = new OAuthFacebook(GeneralConstants.FACEBOOK_APP_ID, GeneralConstants.FACEBOOK_API_SECRET,
                                           GeneralConstants.HTTP_SECURE + "/fbapp/tab", "email");

            if (!string.IsNullOrEmpty(code))
            {
                client.AccessTokenGet(code);

                var access_token = client.token;

                // TODO: save token for use later?

                return(Redirect(string.Format("http://www.facebook.com/add.php?api_key={0}&pages=1", GeneralConstants.FACEBOOK_APP_ID)));
            }

            if (!string.IsNullOrEmpty(signed_request))
            {
                if (!client.ValidateSignedRequest(signed_request))
                {
                    throw new NotImplementedException();
                }
                var payload = client.ParseSignedRequest(signed_request);

                var fbpage = repository.GetFacebookPage(payload.page.id).SingleOrDefault();
                if (fbpage != null)
                {
                    var viewModel = new FacebookGalleryViewModel();
                    viewModel.InitGalleryView(fbpage, repository, payload.page.liked);

                    var owner = fbpage.MASTERsubdomain.organisation.users.FirstOrDefault();
                    if (owner != null && owner.FBID == payload.user_id)
                    {
                        viewModel.isOwner = true;
                        viewModel.token   = payload.oauth_token;
                    }

                    return(View("Gallery", viewModel));
                }

                return(View("Configure", new FacebookConfigureViewModel()
                {
                    pageID = payload.page.id,
                    profileID = payload.user_id,
                    signed_request = signed_request
                }));
            }

            throw new NotImplementedException();
        }
Ejemplo n.º 2
0
        public ActionResult Configure(string pageid, string address, string affiliate, string profileid, string token)
        {
            bool isError = (string.IsNullOrEmpty(pageid) && string.IsNullOrEmpty(profileid)) ||
                           string.IsNullOrEmpty(address);

            Uri storeAddress = null;

            try
            {
                if (address.IndexOf('.') == -1)
                {
                    // user only enter store name
                    address = string.Format("{0}.tradelr.com", address);
                }
                if (!address.StartsWith("http"))
                {
                    // user did not enter http
                    address = string.Format("http://{0}", address);
                }
                storeAddress = new Uri(address);
            }
            catch
            {
                Syslog.Write(new Exception("FB: Unable to parse " + address));
                isError = true;
            }

            if (!isError)
            {
                string subdomain = "";
                if (storeAddress.Host.Split('.').Length > 2)
                {
                    int lastIndex = storeAddress.Host.LastIndexOf(".");
                    int index     = storeAddress.Host.LastIndexOf(".", lastIndex - 1);
                    subdomain = storeAddress.Host.Substring(0, index);
                }
                else
                {
                    return(View("Error", new FacebookPageViewModel
                    {
                        errorMessage = "This is not a valid store address",
                        pageID = pageid
                    }));
                }

                var mastersd = repository.GetSubDomains().SingleOrDefault(x => x.name == subdomain);

                if (mastersd == null)
                {
                    Syslog.Write("New FB subdomain:" + subdomain);
                    // TODO: create new account
                    var client = new OAuthFacebook(GeneralConstants.FACEBOOK_APP_ID, GeneralConstants.FACEBOOK_API_SECRET,
                                                   GeneralConstants.HTTP_SECURE + "/fbapp/tab", "email");

                    if (!client.ValidateSignedRequest(token))
                    {
                        return(View("Error", new FacebookPageViewModel
                        {
                            errorMessage = "There was an error processing your request",
                            pageID = pageid
                        }));
                    }
                    var payload = client.ParseSignedRequest(token);

                    var facebook = new FacebookService(payload.oauth_token);
                    var fb_usr   = facebook.People.GetUser("me");

                    try
                    {
                        var account     = new Account(repository, fb_usr.email, subdomain, AccountPlanType.BASIC, affiliate);
                        var errorString = account.CreateAccountWithFacebookLogin(fb_usr);
                        if (!string.IsNullOrEmpty(errorString))
                        {
                            return(View("Error", new FacebookPageViewModel
                            {
                                errorMessage = errorString,
                                pageID = pageid
                            }));
                        }
                        mastersd = account.mastersubdomain;
                    }
                    catch (Exception ex)
                    {
                        Syslog.Write(ex);
                        return(View("Error", new FacebookPageViewModel
                        {
                            errorMessage = "There was an error processing your request. Your store was not created. Please try again.",
                            pageID = pageid
                        }));
                    }
                }

                // check if there's already an entry, we ignore if there's already an entry
                var existing =
                    repository.GetFacebookPage(pageid).SingleOrDefault(x => x.subdomainid == mastersd.id);

                if (existing == null)
                {
                    existing = new facebookPage {
                        subdomainid = mastersd.id, pageid = pageid
                    };
                    repository.AddFacebookPage(existing);
                }

                var viewmodel = new FacebookGalleryViewModel();
                viewmodel.InitGalleryView(existing, repository, false);

                return(View("Gallery", viewmodel));
            }

            return(View("Error",
                        new FacebookPageViewModel {
                pageID = pageid, errorMessage = "This is not a valid store address"
            }));
        }