Skip to content

Commit

Permalink
Stripe: Use organizations creator email to uniquely identy customer
Browse files Browse the repository at this point in the history
  • Loading branch information
emmdim committed Dec 24, 2024
1 parent 584d220 commit f080f26
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
13 changes: 11 additions & 2 deletions api/stripe.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,16 @@ func (a *API) createSubscriptionCheckoutHandler(w http.ResponseWriter, r *http.R
ErrUnauthorized.Withf("user is not admin of organization").Write(w)
return
}
// TODO check if the user has another active paid subscription

org, _, err := a.db.Organization(checkout.Address, false)
if err != nil {
ErrOrganizationNotFound.Withf("Error retrieving organization: %v", err).Write(w)
return
}
if org == nil {
ErrOrganizationNotFound.Withf("Organization not found: %v", err).Write(w)
return
}

plan, err := a.db.Plan(checkout.LookupKey)
if err != nil {
Expand All @@ -180,7 +189,7 @@ func (a *API) createSubscriptionCheckoutHandler(w http.ResponseWriter, r *http.R
}

session, err := a.stripe.CreateSubscriptionCheckoutSession(
plan.StripePriceID, checkout.ReturnURL, checkout.Address, checkout.Locale, checkout.Amount)
plan.StripePriceID, checkout.ReturnURL, checkout.Address, org.Creator, checkout.Locale, checkout.Amount)
if err != nil {
ErrStripeError.Withf("Cannot create session: %v", err).Write(w)
return
Expand Down
3 changes: 2 additions & 1 deletion stripe/stripe.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,13 @@ func (s *StripeClient) GetPlans() ([]*db.Plan, error) {

// CreateSubscriptionCheckoutSession creates a new Stripe checkout session for a subscription.
// It configures the session with the specified price, amount return URL, and subscription metadata.
// The email provided is used in order to uniquely distinguish the customer on the Stripe side.
// The priceID is that is provided corrsponds to the subscription tier selected by the user.
// Returns the created checkout session and any error encountered.
// Overview of stripe checkout mechanics: https://docs.stripe.com/checkout/custom/quickstart
// API description https://docs.stripe.com/api/checkout/sessions
func (s *StripeClient) CreateSubscriptionCheckoutSession(
priceID, returnURL, address, locale string, amount int64,
priceID, returnURL, address, email, locale string, amount int64,
) (*stripe.CheckoutSession, error) {
if len(locale) == 0 {
locale = "auto"
Expand Down

0 comments on commit f080f26

Please sign in to comment.