Esempio n. 1
0
        /// <summary>
        /// Create a new user in Firebase
        /// </summary>
        /// <param name="userFirstName"></param>
        /// <param name="userLastName"></param>
        /// <param name="userEmail"></param>
        /// <param name="userPassword"></param>
        /// <returns></returns>
        private User CreateCustomer_Firebase(string userFirstName, string userLastName, string userEmail, string userPassword)
        {
            var newUser = Global.db.SignUp(string.Format("{0} {1}", userFirstName, userLastName), userEmail, userPassword);

            if (newUser != null)
            {
                //Generate new API Key
                var APIKey = Guid.NewGuid().ToString().ToLower();
                Session["userKey"] = APIKey;

                var payload = new Dictionary <string, object>
                {
                    { "firstName", userFirstName },
                    { "lastName", userLastName },
                    { "email", userEmail },
                    { "emailValid", false },
                    { "key", APIKey }
                };

                Global.db.WriteToDB("Users/" + newUser.userID + "/", payload);

                //Authenticate with admin user to access firebase DB
                var firebaseInstance = new FireBaseDB(Global.appID, Global.databaseURL, Global.appKey);
                _ = firebaseInstance.Authenticate("*****@*****.**", "W_e7&c':Nc`scc(S");

                var usagePayload = new
                {
                    current = 0,
                    max     = 1500,
                    plan    = "free",
                    renewal = DateTime.Now.ToShortDateString(),
                    userID  = newUser.userID
                };

                firebaseInstance.WriteToDB("Usage/" + APIKey, usagePayload);

                return(newUser);
            }
            return(null);
        }
        /// <summary>
        /// Update firebase user plan data
        /// </summary>
        /// <param name="user"></param>
        private void UpdateFIrebaseData(User user)
        {
            //Update Firebase Data
            var db = new FireBaseDB(Global.appID, Global.databaseURL, Global.appKey);

            db.Authenticate("*****@*****.**", "W_e7&c':Nc`scc(S");
            var currentPlan = Session["userPlan"].ToString();
            var maxUsage    = 1500;


            if (currentPlan == "free")
            {
                maxUsage = 1200;
            }
            else if (currentPlan == "starter")
            {
                maxUsage = 10000;
            }
            else if (currentPlan == "professional")
            {
                maxUsage = 35000;
            }
            else if (currentPlan == "ultimate")
            {
                maxUsage = 100000;
            }

            var payload = new
            {
                plan    = currentPlan,
                max     = maxUsage,
                renewal = Session["userPlanRenewal"].ToString()
            };

            db.WriteToDB("Usage/" + Session["userKey"].ToString(), payload);
        }
Esempio n. 3
0
        public HttpResponseMessage Post(HttpRequestMessage value)
        {
            //Determine plan
            Dictionary <string, string> parameters = new Dictionary <string, string>();
            var queryString = value.GetQueryNameValuePairs();

            foreach (var parameter in queryString)
            {
                var key = parameter.Key;
                var val = parameter.Value;
                parameters.Add(key, val);
            }

            if (!parameters.ContainsKey("plan") || (!parameters.ContainsKey("userid")))
            {
                return(new HttpResponseMessage(HttpStatusCode.BadRequest));
            }

            //Get customer from Firebase

            FireBaseDB db       = new FireBaseDB("apistacks-basicapps", "https://apistacks-basicapps.firebaseio.com", "AIzaSyBkCHXwY87S0ZQxo6T1jNLbxYCaizgMnsU");
            var        user     = db.Authenticate("*****@*****.**", "W_e7&c':Nc`scc(S");
            string     node     = "Main/Users/" + parameters["userid"].ToString();
            var        dbData   = db.GetFromDB(node);
            var        userData = JsonConvert.DeserializeObject <Dictionary <string, object> >(dbData);
            var        stripeID = userData["stripeID"].ToString();


            var priceItem     = "";
            var customerEmail = userData["email"];

            if (parameters["plan"] == "starter")
            {
                priceItem = "price_1HcZm0F6EVrg0l22lVOfn9c8";
            }
            else if (parameters["plan"] == "professional")
            {
                priceItem = "price_1HfvS8F6EVrg0l22KoQlyZp8";
            }
            else if (parameters["plan"] == "ultimate")
            {
                priceItem = "price_1HfwJ3F6EVrg0l22NYrfJDO6";
            }

            StripeConfiguration.ApiKey = "sk_test_51HcZiyF6EVrg0l22KUHmhtN6fxfGQvV1yG2vk2My3Dnq6N4zTg3CASy3OKzArWvWij8CL7BwnqGDPY8xke0Hsmq100FLmHAkYc";

            var options = new SessionCreateOptions
            {
                PaymentMethodTypes = new List <string>
                {
                    "card",
                },
                LineItems = new List <SessionLineItemOptions>
                {
                    new SessionLineItemOptions
                    {
                        Price    = priceItem,
                        Quantity = 1
                    },
                },
                Mode = "subscription",
                //CustomerEmail = customerEmail.ToString(),
                Customer   = stripeID,
                SuccessUrl = "https://apistacks.com/Plan?subscription=success",
                CancelUrl  = "https://apistacks.com/Plan?subscription=cancel",
            };

            var     service = new SessionService();
            Session session = service.Create(options);
            var     payload = new
            {
                id = session.Id
            };

            return(new HttpResponseMessage()
            {
                Content = new StringContent(JsonConvert.SerializeObject(payload), System.Text.Encoding.UTF8, "application/json")
            });
        }