예제 #1
0
        public void InitGalleryView(facebookPage fbpage, ITradelrRepository repository, bool liked)
        {
            hostname   = string.Format("{0}.{1}", fbpage.MASTERsubdomain.name, GeneralConstants.SUBDOMAIN_HOST);
            viewAllUrl = hostname.ToDomainUrl();
#if DEBUG
            canvasStoreUrl = string.Format("http://apps.facebook.com/tradelrtest/store/{0}", fbpage.MASTERsubdomain.name);
#else
            canvasStoreUrl = string.Format("http://apps.facebook.com/tradelr/store/{0}", fbpage.MASTERsubdomain.name);
#endif
            categories = repository.GetProductCategories(null, fbpage.subdomainid)
                         .Select(
                x =>
                new SelectListItem()
            {
                Text = x.MASTERproductCategory.name, Value = x.id.ToString()
            });
            if ((fbpage.MASTERsubdomain.flags & (int)SubdomainFlags.STORE_ENABLED) != 0)
            {
                products = repository.GetProducts(fbpage.subdomainid)
                           .IsActive()
                           .OrderByDescending(x => x.id)
                           .Take(21)
                           .ToFacebookModel(hostname);
            }

            // handle coupon message
            if (fbpage.MASTERsubdomain.facebookCoupon.HasValue)
            {
                if (liked)
                {
                    var    coupon   = fbpage.MASTERsubdomain.coupon;
                    var    currency = fbpage.MASTERsubdomain.currency.ToCurrency();
                    string description;
                    string minimumPurchase = " on all purchases.";
                    if (coupon.minimumPurchase.HasValue)
                    {
                        minimumPurchase = string.Format(" with a minimum purchase of {0}{1}.", currency.code,
                                                        coupon.minimumPurchase.Value.ToString("n" +
                                                                                              currency.decimalCount));
                    }
                    if (coupon.couponPercentage.HasValue)
                    {
                        description = string.Format("{0}% discount", coupon.couponPercentage.Value.ToString("n2"));
                    }
                    else
                    {
                        description = string.Format("{0}{1} off", currency.code,
                                                    coupon.couponValue.Value.ToString("n" + currency.decimalCount));
                    }
                    couponMessage = string.Format(
                        "<p><strong>Discount code:</strong> {0}</p><p>{1}{2}</p>", coupon.code, description,
                        minimumPurchase);
                }
                else
                {
                    couponMessage = string.Format("<p class='strong'><img src='{0}/Content/img/arrow_up.png' /> Click on the like button above to view the latest discount code.</p>",
                                                  GeneralConstants.FACEBOOK_HOST);
                }
            }
        }
예제 #2
0
 public void AddFacebookPage(facebookPage facebookPage)
 {
     db.facebookPages.InsertOnSubmit(facebookPage);
     db.SubmitChanges();
 }
예제 #3
0
 public void DeleteFacebookPage(facebookPage fbpage)
 {
     db.facebookPages.DeleteOnSubmit(fbpage);
     db.SubmitChanges();
 }
예제 #4
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"
            }));
        }
예제 #5
0
        public ActionResult Configure(string pageid, string address, PageType?pagetype, string profileid, string fb_sig_user)
        {
            bool   isError    = false;
            string identifier = "";

            if ((string.IsNullOrEmpty(pageid) && string.IsNullOrEmpty(profileid)) ||
                string.IsNullOrEmpty(address))
            {
                isError = true;
            }
            if (!string.IsNullOrEmpty(pageid))
            {
                identifier = pageid;
            }
            else if (!string.IsNullOrEmpty(profileid))
            {
                identifier = profileid;
            }
            else
            {
                isError = true;
                Syslog.Write(new Exception("NULL FB identifier"));
            }

            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 (Exception ex)
            {
                Syslog.Write(new Exception("FB: Unable to parse " + address));
                isError = true;
            }

            // pagetype can be empty if from a profile
            if (!pagetype.HasValue)
            {
                pagetype = PageType.PROFILE;
            }

            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
                {
                    isError = true;
                }

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

                if (mastersd == null)
                {
                    Syslog.Write(ErrorLevel.INFORMATION, "New FB subdomain:" + subdomain);
                    // create new account
                    return(RedirectToAction("Redirect", new
                    {
                        url =
                            string.Format(
                                "{0}/newaccount?id={1}&identifier={2}&pagetype={3}",
                                GeneralConstants.FACEBOOK_APP_URL, subdomain, identifier,
                                pagetype.Value)
                    }));
                }

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

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

            string returnUrl = pagetype.Value.ToReturnUrl(identifier);
            var    viewdata  = new FacebookViewData
            {
                pageUrl = returnUrl, errorMessage = "This is not a valid store address"
            };

            if (isError)
            {
                return(View("Error", viewdata));
            }

            return(RedirectToAction("Redirect", new { url = returnUrl }));
        }
예제 #6
0
        public ActionResult NewAccount(string code, string id, string identifier, PageType pagetype)
        {
            var client = new OAuthFacebook(GeneralConstants.FACEBOOK_API_KEY, GeneralConstants.FACEBOOK_API_SECRET,
                                           HttpUtility.UrlEncode(
                                               string.Format(
                                                   "{0}/newaccount/{1}?identifier={2}&pagetype={3}",
                                                   GeneralConstants.FACEBOOK_APP_URL, id, identifier, pagetype)),
                                           "read_stream,email,publish_stream,offline_access,manage_pages");

            // starting our authorisation process
            if (string.IsNullOrEmpty(code))
            {
                return(RedirectToAction("Redirect", new{ url = client.AuthorizationLinkGet() }));
            }

            if (!client.AccessTokenGet(code))
            {
                return(View("Error", new FacebookViewData {
                    errorMessage = "Unable to obtain permission", pageUrl = pagetype.ToReturnUrl(identifier)
                }));
            }

            // check subdomain is valid
            id = id.ToLower();

            // also check special domain list
            if (GeneralConstants.SUBDOMAIN_RESTRICTED.Contains(id))
            {
                return(View("Error", new FacebookViewData {
                    errorMessage = "Store address is not available", pageUrl = pagetype.ToReturnUrl(identifier)
                }));
            }

            var mastersubdomain = repository.GetSubDomains().Where(x => x.name == id).SingleOrDefault();

            if (mastersubdomain != null)
            {
                return(View("Error", new FacebookViewData {
                    errorMessage = "Store address is not available", pageUrl = pagetype.ToReturnUrl(identifier)
                }));
            }

            var facebook = new FacebookService(client.token);
            var fb_usr   = facebook.People.GetUser("me");

            if (fb_usr == null)
            {
                return(View("Error", new FacebookViewData {
                    errorMessage = "Unable to create account", pageUrl = pagetype.ToReturnUrl(identifier)
                }));
            }

            // verify that email has not been used to register another account
            if (repository.GetUserByEmail(fb_usr.email).Where(x => (x.role & (int)UserRole.CREATOR) != 0).SingleOrDefault() != null)
            {
                Syslog.Write(ErrorLevel.INFORMATION, "Facebook email address in use: " + fb_usr.email);
                return(View("Error", new FacebookViewData {
                    errorMessage = "Email address is already registered", pageUrl = pagetype.ToReturnUrl(identifier)
                }));
            }

            var usr = new user
            {
                role                 = (int)UserRole.ADMIN,
                viewid               = Crypto.Utility.GetRandomString(),
                permissions          = (int)UserPermission.ADMIN,
                FBID                 = fb_usr.id,
                email                = fb_usr.email ?? "",
                externalProfileUrl   = fb_usr.link,
                firstName            = fb_usr.first_name,
                lastName             = fb_usr.last_name,
                gender               = fb_usr.gender,
                externalProfilePhoto = string.Format("https://graph.facebook.com/{0}/picture?type=large", fb_usr.id)
            };

            // create subdomain entry
            mastersubdomain = new MASTERsubdomain
            {
                flags                   = 0,
                name                    = id,
                total_outofstock        = 0,
                total_contacts_public   = 0,
                total_contacts_private  = 0,
                total_contacts_staff    = 0,
                total_invoices_sent     = 0,
                total_invoices_received = 0,
                total_orders_sent       = 0,
                total_orders_received   = 0,
                total_products_mine     = 0,
                accountType             = AccountPlanType.ULTIMATE.ToString()
            };

            repository.AddMasterSubdomain(mastersubdomain);

            // create organisation first
            var org = new organisation
            {
                subdomain = mastersubdomain.id,
                name      = fb_usr.name
            };

            repository.AddOrganisation(org);
            usr.organisation = org.id;

            // CREATE DEFAULT STRUCTURES
            // add default inventory location
            var loc = new inventoryLocation
            {
                name       = GeneralConstants.INVENTORY_LOCATION_DEFAULT,
                subdomain  = mastersubdomain.id,
                lastUpdate = DateTime.UtcNow
            };

            repository.AddInventoryLocation(loc, mastersubdomain.id);

            // add default shipping profile
            var shippingProfile = new shippingProfile()
            {
                title       = "Default",
                type        = ShippingProfileType.FLATRATE.ToString(),
                subdomainid = mastersubdomain.id
            };

            repository.AddShippingProfile(shippingProfile);

            // update subdomain entry
            mastersubdomain.creator = org.id;

            // create facebookpage to link to subdomain
            var newEntry = new facebookPage {
                subdomainid = mastersubdomain.id, pageid = identifier
            };

            repository.AddFacebookPage(newEntry);


            try
            {
                // if user exist then we still need to verify email
                Random rnd = RandomNumberGenerator.Instance;
                usr.confirmationCode = rnd.Next();
                repository.AddUser(usr);

                // generate photo
                new Thread(() => usr.externalProfilePhoto.ReadAndSaveFromUrl(mastersubdomain.id, usr.id, usr.id, PhotoType.PROFILE)).Start();

                // add access token
                var oauthdb = new oauth_token
                {
                    token_key    = client.token,
                    token_secret = "",
                    type         = OAuthTokenType.FACEBOOK.ToString(),
                    subdomainid  = mastersubdomain.id,
                    appid        = usr.id.ToString(),
                    authorised   = true
                };
                repository.AddOAuthToken(oauthdb);

                // obtain any other account tokens
                var accounts = facebook.Account.GetAccountTokens("me");
                if (accounts != null && accounts.data != null)
                {
                    foreach (var account in accounts.data)
                    {
                        if (account.name != null)
                        {
                            var ftoken = new facebook_token
                            {
                                pageid      = account.id,
                                subdomainid = mastersubdomain.id,
                                accesstoken = account.access_token,
                                name        = account.name
                            };
                            repository.AddUpdateFacebookToken(ftoken);
                        }
                    }
                }
                repository.Save();

                // send confirmation email
                var viewdata = new ViewDataDictionary()
                {
                    { "host", id.ToSubdomainUrl() },
                    { "confirmCode", usr.confirmationCode },
                    { "email", usr.email }
                };
                EmailHelper.SendEmailNow(EmailViewType.ACCOUNT_CONFIRMATION, viewdata, "New Account Details and Email Verification Link",
                                         usr.email, usr.ToFullName(), usr.id);
            }
            catch (Exception ex)
            {
                Syslog.Write(ex);
                return(View("Error", new FacebookViewData {
                    errorMessage = "Unable to create account", pageUrl = pagetype.ToReturnUrl(identifier)
                }));
            }

            return(RedirectToAction("Redirect", new { url = pagetype.ToReturnUrl(identifier) }));
        }