Пример #1
0
func signupPostHandler(c *util.Context) error {
	// Setup stripe client
	stripeClient := &stripe.Client{}
	stripeClient.Init(util.Getenv("STRIPE_PRIVATE_KEY"), nil, nil)

	// Gater form values
	fullname := c.Request.FormValue("fullname")
	accountName := c.Request.FormValue("account")
	email := c.Request.FormValue("email")
	password := c.Request.FormValue("password")
	plan := c.Request.FormValue("plan")
	token := c.Request.FormValue("stripeToken")

	// Store new user in mongo
	user := data.User{
		Email:    email,
		Fullname: fullname,
	}
	user.SetPassword(password)
	err := user.Create(c.C("users"))
	if err != nil {
		log.Print(err)
		return err
	}

	// Now create his account
	account := data.Account{
		Name: accountName,
		Plan: plan,
	}
	account.Users = []bson.ObjectId{user.ID}
	err = account.Create(c.C("accounts"))
	if err != nil {
		log.Print(err)
		return err
	}

	// Create and Customer, subscribe him, start trial, associate card (1-step)
	customer := &stripe.CustomerParams{
		Token: token,
		Desc:  fullname,
		Email: email,
		Plan:  plan,
		Params: stripe.Params{
			Meta: map[string]string{
				"uid": user.ID.String(),
			},
		},
	}
	newCustomer, err := stripeClient.Customers.Create(customer)

	// Save new imformation (stripe id) and add account id to user accounts
	user.StripeId = newCustomer.Id
	user.Accounts = []bson.ObjectId{account.ID}
	c.C("users").UpdateId(user.ID, user)

	if err != nil {
		log.Print(err)
		return err
	}

	c.Redirect(c.RouteUrl("login") + "?next=" + url.QueryEscape(appUrl))
	return nil
}