Exemple #1
0
// GET /session/{id}, Gets user by ID. If their license has expired it attempts
// to charge them again. It is called everytime crosby is run.
func SessionInfoHandler(rw http.ResponseWriter, req *http.Request) {
	id := mux.Vars(req)["id"]
	fmt.Println("Getting user by id", id)
	u, err := db.GetDeveloperById(id)
	if err != nil {
		renderer.JSON(rw, http.StatusBadRequest, map[string]string{
			"status": requests.StatusFailed,
			"error":  err.Error(),
		})
		return
	}

	if u.Expiration.After(time.Now()) {
		renderer.JSON(rw, http.StatusOK, map[string]interface{}{
			"status":    requests.StatusFound,
			"developer": u,
		})
		return
	}

	if u.StripeToken == "" {
		renderer.JSON(rw, http.StatusOK, map[string]interface{}{
			"status":    requests.StatusExpired,
			"developer": u,
		})
		return
	}

	// Charge them, update expiration, & respond with found.
	// Charge Stripe Customer
	chargeParams := stripe.ChargeParams{
		Desc:     "Crosby Annual License",
		Amount:   2500,
		Currency: "usd",
		Customer: u.StripeToken,
	}
	_, err = stripe.Charges.Create(&chargeParams)
	if err != nil {
		renderer.JSON(rw, http.StatusBadRequest, map[string]string{
			"status": requests.StatusFailed,
			"error":  err.Error(),
		})
		return
	}
	u.Expiration = time.Now()
	if err := db.Save(u); err != nil { // not actually a save, but an update. fix
		renderer.JSON(rw, http.StatusBadRequest, map[string]string{
			"status": requests.StatusFailed,
			"error":  err.Error(),
		})
		return
	}

	renderer.JSON(rw, http.StatusOK, map[string]interface{}{
		"status": requests.StatusFound,
		"user":   u,
	})
}
Exemple #2
0
// POST /session, Creates a new user and charges them for the first year.
func CreateSessionHandler(rw http.ResponseWriter, req *http.Request) {
	if err := req.ParseForm(); err != nil {
		renderer.JSON(rw, http.StatusBadRequest, map[string]string{
			"status": requests.StatusFailed,
			"error":  err.Error(),
		})
		return
	}

	name := req.PostFormValue("name")
	id := req.PostFormValue("id")
	email := req.PostFormValue("stripeEmail")
	if email == "" {
		email = req.PostFormValue("email")
	}

	u := &schemas.Developer{
		Name:       name,
		Email:      email,
		Expiration: time.Now().Add(time.Hour * 24 * 30),
		ID:         bson.ObjectIdHex(id),
	}

	// Silent Signup from cli and not signup form. Will not charge them, but will give them a free month
	if err := db.Save(u); err != nil {
		renderer.JSON(rw, http.StatusBadRequest, map[string]string{
			"status": requests.StatusFailed,
			"error":  err.Error(),
		})
		return
	}

	renderer.JSON(rw, http.StatusOK, map[string]interface{}{
		"status":    requests.StatusCreated,
		"developer": u,
	})
}
Exemple #3
0
// POST /developers, Creates a new developer
func CreateDeveloperHandler(rw http.ResponseWriter, req *http.Request) {
	type engineer struct {
		Name  string
		Email string
	}

	integrationEngineers := []*engineer{
		&engineer{Name: "Steve Kaliski", Email: "*****@*****.**"},
		&engineer{Name: "David Byrd", Email: "*****@*****.**"},
		&engineer{Name: "Larz Conwell", Email: "*****@*****.**"},
	}

	integrationEngineer := integrationEngineers[rand.Int()%len(integrationEngineers)]

	var body requests.LoginReq

	decoder := json.NewDecoder(req.Body)
	err := decoder.Decode(&body)
	if err != nil {
		renderer.JSON(rw, http.StatusBadRequest, map[string]string{
			"status": requests.StatusFailed,
			"error":  err.Error(),
		})
		return
	}

	if body.Email == "" || body.Password == "" {
		renderer.JSON(rw, http.StatusBadRequest, map[string]string{
			"status": requests.StatusFailed,
			"error":  "Email and Password Required.",
		})
		return
	}

	u := &schemas.Developer{
		Name:                body.Name,
		Email:               body.Email,
		Password:            body.Password,
		Token:               util.HashToken(),
		IntegrationEngineer: integrationEngineer.Name,
		IsPaid:              false,
		CreatedAt:           time.Now().UnixNano() / int64(time.Millisecond),
	}

	_, err = db.GetDeveloper(bson.M{"email": u.Email})
	if err == nil {
		renderer.JSON(rw, http.StatusInternalServerError, map[string]string{
			"status": requests.StatusFailed,
			"error":  "email already exists",
		})
		return
	}

	if os.Getenv("ENV") == "production" && !strings.Contains(body.Email, "@bowery.io") {
		if _, err := chimp.ListsSubscribe(gochimp.ListsSubscribe{
			ListId: "200e892f56",
			Email:  gochimp.Email{Email: u.Email},
		}); err != nil {
			renderer.JSON(rw, http.StatusBadRequest, map[string]string{
				"status": requests.StatusFailed,
				"error":  err.Error(),
			})
			return
		}

		message, err := RenderEmail("welcome", map[string]interface{}{
			"name":     strings.Split(u.Name, " ")[0],
			"engineer": integrationEngineer,
		})

		if err != nil {
			renderer.JSON(rw, http.StatusBadRequest, map[string]string{
				"status": requests.StatusFailed,
				"error":  err.Error(),
			})
			return
		}

		_, err = mandrill.MessageSend(gochimp.Message{
			Subject:   "Welcome to Bowery!",
			FromEmail: "*****@*****.**",
			FromName:  integrationEngineer.Name,
			To: []gochimp.Recipient{{
				Email: u.Email,
				Name:  u.Name,
			}},
			Html: message,
		}, false)

		if err != nil {
			renderer.JSON(rw, http.StatusBadRequest, map[string]string{
				"status": requests.StatusFailed,
				"error":  err.Error(),
			})
			return
		}
	}

	if err := db.Save(u); err != nil {
		renderer.JSON(rw, http.StatusBadRequest, map[string]string{
			"status": requests.StatusFailed,
			"error":  err.Error(),
		})
		return
	}

	// Post to slack
	if os.Getenv("ENV") == "production" && !strings.Contains(body.Email, "@bowery.io") {
		channel := "#activity"
		message := u.Name + " " + u.Email + " just signed up."
		username := "******"
		go slackC.SendMessage(channel, message, username)
	}

	renderer.JSON(rw, http.StatusOK, map[string]interface{}{
		"status":    requests.StatusCreated,
		"developer": u,
	})
}