Ejemplo n.º 1
0
        private void GetSavedPayMethodInfo(string paymentNonce, out string customerId, out string token, out string type) {
            using(var ctx = new Data.FitathonDataEntities()) {
                var user = Common.GetUserFromEmail(ctx, Context.User.Identity.Name);
                var sponsor = user.sponsors.SingleOrDefault();
                var evt = sponsor.participant.fitevent;

                var gateway = new Braintree.BraintreeGateway {
                    Environment = Braintree.Environment.SANDBOX,
                    MerchantId = evt.braintreeClientID,
                    PublicKey = evt.braintreePublicKey,
                    PrivateKey = evt.braintreePrivateKey
                };

                //create request with just the payment nonce (empty customer)
                var custRequest = new Braintree.CustomerRequest {
                    PaymentMethodNonce = paymentNonce
                };
                Braintree.Result<Braintree.Customer> custResult = gateway.Customer.Create(custRequest);

                if(custResult.IsSuccess()) {
                    Braintree.Customer customer = custResult.Target;
                    customerId = customer.Id;
                    token = customer.PaymentMethods[0].Token;
                    type = GetPMType(customer.PaymentMethods[0]);
                    return;
                }
            }
            customerId = null;
            token = null;
            type = null;
        }
        protected void btnSubmit_Click(object sender, EventArgs e) {

            try {
                using(var ctx = new Data.FitathonDataEntities()) {
                    var user = Common.GetUserFromEmail(ctx, Context.User.Identity.Name);
                    var part = user.participants.SingleOrDefault();
                    var evt = part.fitevent;

                    if(user == null || part == null) {
                        Response.Redirect("~", false);
                        return;
                    }

                    evt.braintreeClientID = txtClientId.Text;
                    evt.braintreePublicKey = txtPublicKey.Text;
                    evt.braintreePrivateKey = txtPrivateKey.Text;

                    ctx.SaveChanges();

                    if(part.fitbitAccessToken != null)
                        Response.Redirect("ViewEvent.aspx", false);
                    else
                        Response.Redirect("FitbitSetup.aspx", false);
                }

            } catch(Exception ex) {
                Response.Write("ERROR writing to database: " + ex.ToString());
            }
        }
Ejemplo n.º 3
0
        protected void btnSubmit_Click(object sender, EventArgs e) {

            decimal amount = 0;
            decimal max = 0;
            int perSteps = 0;

            decimal.TryParse(txtPledge.Text, out amount);
            decimal.TryParse(txtMaxDonation.Text, out max);
            int.TryParse(txtPerSteps.Text, out perSteps);

            try {
                using(var ctx = new Data.FitathonDataEntities()) {
                    var user = Common.GetUserFromEmail(ctx, Context.User.Identity.Name);

                    var sponsor = user.sponsors.SingleOrDefault();

                    if(user == null || sponsor == null) {
                        Response.Redirect("~", false);
                        return;
                    }

                    sponsor.pledgeAmount = amount;
                    sponsor.donationMax = max;
                    sponsor.pledgePerSteps = perSteps;

                    ctx.SaveChanges();
                    Response.Redirect("PayMethod.aspx", false);
                }
            } catch(Exception ex) {
                Response.Write("ERROR writing to database: " + ex.ToString());
            }

        }
Ejemplo n.º 4
0
        internal static string GetFitbitClientSecret() {
            string secret = null;
            using(var ctx = new Data.FitathonDataEntities()) {

                try {
                    secret = (from ap in ctx.appsettings
                              where ap.key == "fitbitClientSecret"
                              select ap.value).SingleOrDefault();
                } catch { }
            }
            return secret;
        }
Ejemplo n.º 5
0
        protected void lnkLinkFitbit_Click(object sender, EventArgs e) {
            using(var ctx = new Data.FitathonDataEntities()) {
                var user = Common.GetUserFromEmail(ctx, Context.User.Identity.Name);
                var part = user.participants.SingleOrDefault();

                if(user == null || part == null) {
                    Response.Redirect("~", false);
                    return;
                }

                Common.RedirectToFitbitAuthFlow(part.uniqueID);
            }
        }
Ejemplo n.º 6
0
        public static bool LoadTokensForParticipant(int participantId, bool refresh) {
            try {
                using(var ctx = new Data.FitathonDataEntities()) {
                    var part = (from parts in ctx.participants
                                where parts.id == participantId
                                select parts).SingleOrDefault();

                    if(part == null || string.IsNullOrEmpty(part.fitbitAuthCode)) {
                        HttpContext.Current.Response.Redirect("~", false);
                        return false;
                    }

                    string clientId = GetFitbitClientId();
                    string clientSecret = GetFitbitClientSecret();

                    var authHeaderVal = GetBase64(string.Format("{0}:{1}", clientId, clientSecret));
                    string url;
                    if(!refresh)
                        url = string.Format("{0}?client_id={1}&grant_type=authorization_code&redirect_uri={2}&code={3}", FITBIT_TOKEN_URI, clientId, HttpUtility.UrlEncode(FITBIT_CALLBACK_URI), part.fitbitAuthCode);
                    else
                        url = string.Format("{0}?grant_type=refresh_token&refresh_token={1}", FITBIT_TOKEN_URI, part.fitbitRefreshToken);
                    var cli = new WebClient();
                    var req = WebRequest.Create(url);
                    req.Method = "POST";
                    req.Headers.Add("Authorization", string.Format("Basic {0}", authHeaderVal));
                    req.ContentType = "application/x-www-form-urlencoded";

                    var resp = req.GetResponse();
                    string responseText;
                    using(var s = resp.GetResponseStream()) {
                        using(var sr = new StreamReader(s)) {
                            responseText = sr.ReadToEnd();
                        }
                    }

                    var ser = new JavaScriptSerializer();
                    var tokenResponse = ser.Deserialize<FitbitTokenResponse>(responseText);

                    part.fitbitAccessToken = tokenResponse.access_token;
                    part.fitbitRefreshToken = tokenResponse.refresh_token;
                    //part.fitbitTokenExpires = DateTime.Now.AddSeconds(tokenResponse.expires_in);
                    //part.fitbitUserId = tokenResponse.user_id;

                    ctx.SaveChanges();
                }
            } catch {
                return false;
            }
            return true;

        }
Ejemplo n.º 7
0
        protected void btnSubmit_Click(object sender, EventArgs e) {
            if(Session["role"] != null && !string.IsNullOrEmpty(Session["role"].ToString())) {
                if(Session["role"].ToString() == "sponsor") {

                    var part = Common.GetParticipantFromCode(txtParticipantCode.Text);
                    if(part != null) {
                        try {
                            using(var ctx = new Data.FitathonDataEntities()) {

                                var user = new Data.user {
                                    name = txtName.Text,
                                    email = txtEmail.Text,
                                    passwordHash = txtPassword.Text,
                                    passwordSalt = txtPassword.Text
                                };
                                //TODO actually hash password

                                ctx.users.Add(user);
                                ctx.SaveChanges();

                                var sponsor = new Data.sponsor {
                                    userID = user.ID,
                                    participantID = part.id
                                };
                                ctx.sponsors.Add(sponsor);
                                ctx.SaveChanges();
                            }
                            FormsAuthentication.SetAuthCookie(txtEmail.Text, false);
                            Response.Redirect("Pledge.aspx", false);
                        } catch(Exception ex) {
                            Response.Write("ERROR writing to database: " + ex.ToString());
                        }
                    } else {
                        Response.Write("ERROR: Participant code not found");
                        return;
                    }

                } else if(Session["role"].ToString() == "solowalker") {
                    //store data in session to put in db after registerevent
                    Session["regName"] = txtName.Text;
                    Session["regEmail"] = txtEmail.Text;
                    Session["regPassword"] = txtPassword.Text;

                    FormsAuthentication.SetAuthCookie(txtEmail.Text, false);
                    Response.Redirect("RegisterEvent.aspx", false);
                }
            } else {
                Response.Redirect("~", false);
                return;
            }
        }
Ejemplo n.º 8
0
        static internal Data.user GetUserFromEmail(string email) {
            Data.user u = null;
            using(var ctx = new Data.FitathonDataEntities()) {

                try {
                    u = (from users in ctx.users
                         where users.email == email
                         select users).SingleOrDefault();

                } catch { }
            }
            return u;

        }
Ejemplo n.º 9
0
        protected void Page_Load(object sender, EventArgs e) {
            bool isParticipant, isSponsor;
            EventState eventState;
            Data.sponsor sponsor = null;
            Data.sponsor[] sponsors = null;

            var email = Context.User.Identity.Name;
            using(var ctx = new Data.FitathonDataEntities()) {
                var user = (from u in ctx.users
                            where u.email == email
                            select u).SingleOrDefault();

                if(user != null) {
                    isSponsor = (user.sponsors != null && user.sponsors.Count > 0);
                    isParticipant = !isSponsor; //(user.participants != null && user.participants.Count > 0);

                    if(isSponsor) {
                        //make sure has paymethod and pledge
                        sponsor = user.sponsors.SingleOrDefault();
                        if(sponsor.pledgeAmount == null || sponsor.pledgeAmount.Value == 0) {
                            Response.Redirect("Pledge.aspx", false);
                            return;
                        } else if(sponsor.payMethodToken == null) {
                            Response.Redirect("PayMethod.aspx", false);
                            return;
                        }
                    }

                    eventState = GetEventState(user);
                    Data.participant part;
                    Data.fitevent evt;
                    if(isParticipant) {
                        part = user.participants.SingleOrDefault();
                    } else {
                        sponsor = user.sponsors.SingleOrDefault();
                        part = sponsor.participant;
                    }
                    sponsors = part.sponsors != null ? part.sponsors.ToArray() : null;
                    evt = part.fitevent;
                    UpdateDisplay(user, part, evt, sponsor, sponsors, isParticipant, eventState);
                    litUser.Text = user.name;
                }
            }
        }
Ejemplo n.º 10
0
        protected void Page_Load(object sender, EventArgs e) {
            //handle form-submission which includes paymethod nonce
            if(Request.Form["payment_method_nonce"] != null) {
                Submission(Request.Form["payment_method_nonce"]);
                return;
            }

            using(var ctx = new Data.FitathonDataEntities()) {
                var user = Common.GetUserFromEmail(ctx, Context.User.Identity.Name);
                var sponsor = user.sponsors.SingleOrDefault();
                var evt = sponsor.participant.fitevent;
                
                var gateway = new Braintree.BraintreeGateway {
                    Environment = Braintree.Environment.SANDBOX,
                    MerchantId = evt.braintreeClientID,  
                    PublicKey = evt.braintreePublicKey,
                    PrivateKey = evt.braintreePrivateKey
                };

                var btToken = gateway.ClientToken.generate();
                litBTClientToken.Text = string.Format("<script type='text/javascript'>var clientToken = '{0}';</script>", btToken);
            }
        }
Ejemplo n.º 11
0
        protected void btnSubmit_Click(object sender, EventArgs e) {
            using(var ctx = new Data.FitathonDataEntities()) {
                var user = Common.GetUserFromEmail(ctx, Context.User.Identity.Name);
                var part = user.participants.SingleOrDefault();

                if(user == null || part == null) {
                    Response.Redirect("~", false);
                    return;
                }

                //split textbox by commas and newlines
                var emails = txtInvite.Text.Split(new char[] { ',', '\n', '\r' });
                foreach(var em in emails) {
                    try {
                        Common.SendEmail("*****@*****.**", em.Trim(), string.Format("Fitathon.org: {0} wants to invite you!", user.name), string.Format(
                            @"<p>You have been invited by {0} to sponsor them in a Fitathon event.</p>
                            <p>Fitathon.org is a digital walkathon platform, which enables people to raise funds by asking people like you to sponsor them. <a href='{1}'>Click here</a> to learn more</p>
                            <p><a href='{2}'>Sponsor {0}</a></p>", user.name, LEARN_MORE_URL, string.Format(SPONSOR_URL_FMT, part.uniqueID)));
                        } catch { }
                }
                
                Response.Redirect("ViewEvent.aspx", false);
            }
        }
Ejemplo n.º 12
0
        protected void Submission(string paymentNonce) {
            try {
                using(var ctx = new Data.FitathonDataEntities()) {
                    var user = Common.GetUserFromEmail(ctx, Context.User.Identity.Name);
                    var sponsor = user.sponsors.SingleOrDefault();

                    if(user == null || sponsor == null) {
                        Response.Redirect("~", false);
                        return;
                    }
                    string pmToken, pmCustId, pmType;
                    GetSavedPayMethodInfo(paymentNonce, out pmCustId, out pmToken, out pmType);
                    
                    sponsor.payMethodCustomerId = pmCustId;
                    sponsor.payMethodToken = pmToken;
                    sponsor.payMethodType = pmType;
                    
                    ctx.SaveChanges();
                    Response.Redirect("ViewEvent.aspx", false);
                }
            } catch(Exception ex) {
                Response.Write("ERROR writing to database: " + ex.ToString());
            }
        }
Ejemplo n.º 13
0
        internal static Data.user GetUserFromEmail(FitathonDataEntities ctx, string email) {
            Data.user u = null;
            try {
                u = (from users in ctx.users
                     where users.email == email
                     select users).SingleOrDefault();

            } catch { }
            return u;
        }
Ejemplo n.º 14
0
        static internal Data.participant GetParticipantFromCode(string code) {
            Data.participant p = null;
            using(var ctx = new Data.FitathonDataEntities()) {

                try {
                    p = (from parts in ctx.participants
                         where parts.uniqueID == code
                         select parts).SingleOrDefault();

                } catch { }
            }
            return p;
        }
Ejemplo n.º 15
0
        internal static Data.user GetUserFromLogin(string email, string password) {
            Data.user u = null;
            using(var ctx = new Data.FitathonDataEntities()) {
                try {
                    u = (from users in ctx.users
                         where users.email == email
                         && users.passwordHash == password
                         select users).SingleOrDefault();

                } catch { }
            }
            return u;
        }
Ejemplo n.º 16
0
        internal static Data.participant GetParticipantFromCode(FitathonDataEntities ctx, string code) {
            Data.participant p = null;
            try {
                p = (from parts in ctx.participants
                     where parts.uniqueID == code
                     select parts).SingleOrDefault();

            } catch { }
            return p;
        }
Ejemplo n.º 17
0
        internal static int GetTodaysSteps(int participantId) {

            try {
                using(var ctx = new Data.FitathonDataEntities()) {
                    var part = (from parts in ctx.participants
                                where parts.id == participantId
                                select parts).SingleOrDefault();

                    if(part == null || string.IsNullOrEmpty(part.fitbitAuthCode)) {
                        return -1;
                    }
                    LoadTokensForParticipant(part.id, true); //refresh access token
                }

                //dispose of context to reload participant data, post token refresh

                using(var ctx = new Data.FitathonDataEntities()) {
                    var part = (from parts in ctx.participants
                                where parts.id == participantId
                                select parts).SingleOrDefault();
                    var url = "https://api.fitbit.com/1/user/-/activities/steps/date/today/1d.json";

                    var cli = new WebClient();
                    var req = WebRequest.Create(url);
                    req.Method = "GET";
                    req.Headers.Add("Authorization", string.Format("Bearer {0}", part.fitbitAccessToken));
                    req.ContentType = "application/x-www-form-urlencoded";

                    var resp = req.GetResponse();
                    using(var s = resp.GetResponseStream()) {
                        ActivitiesStepsCollection collection;
                        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(ActivitiesStepsCollection));
                        collection = (ActivitiesStepsCollection)serializer.ReadObject(s);
                        var steps = collection.Steps.SingleOrDefault();
                        if(steps != null)
                            return steps.Value;
                    }

                    return -1;
                }
            } catch (Exception ex) {
                //HttpContext.Current.Response.Write(ex.ToString());
                return -1;
            }
        }