api.spaceplanner.app

Spaceplanner API
git clone git://jacobedwards.org/api.spaceplanner.app
Log | Files | Refs

order.go (1615B)


      1 package backend
      2 
      3 import (
      4 	"errors"
      5 	"github.com/stripe/stripe-go/v72"
      6 )
      7 
      8 func (e *Env) CreatePortal(username string) (*stripe.BillingPortalSession, error) {
      9 	user, err := e.GetUser(nil, username)
     10 	if err != nil {
     11 		return nil, err
     12 	}
     13 
     14 	if err := e.verifyUserForBilling(username); err != nil {
     15 		return nil, err
     16 	}
     17 
     18 	params := &stripe.BillingPortalSessionParams{
     19 		Customer: stripe.String(user.stripeCustomerID),
     20 		ReturnURL: stripe.String("http://www.spaceplanner.app"),
     21 	};
     22 	return e.Stripe.BillingPortalSessions.New(params);
     23 }
     24 
     25 func (e *Env) CreateCheckout(username string, price string) (*stripe.CheckoutSession, error) {
     26 	user, err := e.GetUser(nil, username)
     27 	if err != nil {
     28 		return nil, err
     29 	}
     30 
     31 	if err := e.verifyUserForBilling(username); err != nil {
     32 		return nil, err
     33 	}
     34 
     35 	p, err := e.Price(price)
     36 	if err != nil {
     37 		return nil, err
     38 	}
     39 
     40 	params := &stripe.CheckoutSessionParams{
     41 		Customer: stripe.String(user.stripeCustomerID),
     42 		LineItems: []*stripe.CheckoutSessionLineItemParams{
     43 			&stripe.CheckoutSessionLineItemParams{
     44 				Price: stripe.String(p.ID),
     45 				Quantity: stripe.Int64(1),
     46 			},
     47 		},
     48 		Mode: stripe.String(string(stripe.CheckoutSessionModeSubscription)),
     49 		SuccessURL: stripe.String("http://www.spaceplanner.app/floorplans"),
     50 		CancelURL: stripe.String("http://www.spaceplanner.app/services"),
     51 	}
     52 	return e.Stripe.CheckoutSessions.New(params);
     53 }
     54 
     55 func (e *Env) verifyUserForBilling(username string) error {
     56 	emails, err := e.UserEmails(username)
     57 	if err != nil {
     58 		return err
     59 	}
     60 	if emails.Verified == nil {
     61 		return errors.New("Cannot checkout without verifying email")
     62 	}
     63 	return nil
     64 }