123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539 |
- package server
- import (
- "bytes"
- "errors"
- "fmt"
- "github.com/stripe/stripe-go/v74"
- portalsession "github.com/stripe/stripe-go/v74/billingportal/session"
- "github.com/stripe/stripe-go/v74/checkout/session"
- "github.com/stripe/stripe-go/v74/customer"
- "github.com/stripe/stripe-go/v74/price"
- "github.com/stripe/stripe-go/v74/subscription"
- "github.com/stripe/stripe-go/v74/webhook"
- "heckel.io/ntfy/log"
- "heckel.io/ntfy/user"
- "heckel.io/ntfy/util"
- "io"
- "net/http"
- "net/netip"
- "time"
- )
- // Payments in ntfy are done via Stripe.
- //
- // Pretty much all payments related things are in this file. The following processes
- // handle payments:
- //
- // - Checkout:
- // Creating a Stripe customer and subscription via the Checkout flow. This flow is only used if the
- // ntfy user is not already a Stripe customer. This requires redirecting to the Stripe checkout page.
- // It is implemented in handleAccountBillingSubscriptionCreate and the success callback
- // handleAccountBillingSubscriptionCreateSuccess.
- // - Update subscription:
- // Switching between Stripe subscriptions (upgrade/downgrade) is handled via
- // handleAccountBillingSubscriptionUpdate. This also handles proration.
- // - Cancel subscription (at period end):
- // Users can cancel the Stripe subscription via the web app at the end of the billing period. This
- // simply updates the subscription and Stripe will cancel it. Users cannot immediately cancel the
- // subscription.
- // - Webhooks:
- // Whenever a subscription changes (updated, deleted), Stripe sends us a request via a webhook.
- // This is used to keep the local user database fields up to date. Stripe is the source of truth.
- // What Stripe says is mirrored and not questioned.
- var (
- errNotAPaidTier = errors.New("tier does not have billing price identifier")
- errMultipleBillingSubscriptions = errors.New("cannot have multiple billing subscriptions")
- errNoBillingSubscription = errors.New("user does not have an active billing subscription")
- )
- var (
- retryUserDelays = []time.Duration{3 * time.Second, 5 * time.Second, 7 * time.Second}
- )
- // handleBillingTiersGet returns all available paid tiers, and the free tier. This is to populate the upgrade dialog
- // in the UI. Note that this endpoint does NOT have a user context (no u!).
- func (s *Server) handleBillingTiersGet(w http.ResponseWriter, _ *http.Request, _ *visitor) error {
- tiers, err := s.userManager.Tiers()
- if err != nil {
- return err
- }
- freeTier := configBasedVisitorLimits(s.config)
- response := []*apiAccountBillingTier{
- {
- // This is a bit of a hack: This is the "Free" tier. It has no tier code, name or price.
- Limits: &apiAccountLimits{
- Basis: string(visitorLimitBasisIP),
- Messages: freeTier.MessageLimit,
- MessagesExpiryDuration: int64(freeTier.MessageExpiryDuration.Seconds()),
- Emails: freeTier.EmailLimit,
- Reservations: freeTier.ReservationsLimit,
- AttachmentTotalSize: freeTier.AttachmentTotalSizeLimit,
- AttachmentFileSize: freeTier.AttachmentFileSizeLimit,
- AttachmentExpiryDuration: int64(freeTier.AttachmentExpiryDuration.Seconds()),
- },
- },
- }
- prices, err := s.priceCache.Value()
- if err != nil {
- return err
- }
- for _, tier := range tiers {
- priceStr, ok := prices[tier.StripePriceID]
- if tier.StripePriceID == "" || !ok {
- continue
- }
- response = append(response, &apiAccountBillingTier{
- Code: tier.Code,
- Name: tier.Name,
- Price: priceStr,
- Limits: &apiAccountLimits{
- Basis: string(visitorLimitBasisTier),
- Messages: tier.MessageLimit,
- MessagesExpiryDuration: int64(tier.MessageExpiryDuration.Seconds()),
- Emails: tier.EmailLimit,
- Reservations: tier.ReservationLimit,
- AttachmentTotalSize: tier.AttachmentTotalSizeLimit,
- AttachmentFileSize: tier.AttachmentFileSizeLimit,
- AttachmentExpiryDuration: int64(tier.AttachmentExpiryDuration.Seconds()),
- },
- })
- }
- return s.writeJSON(w, response)
- }
- // handleAccountBillingSubscriptionCreate creates a Stripe checkout flow to create a user subscription. The tier
- // will be updated by a subsequent webhook from Stripe, once the subscription becomes active.
- func (s *Server) handleAccountBillingSubscriptionCreate(w http.ResponseWriter, r *http.Request, v *visitor) error {
- u := v.User()
- if u.Billing.StripeSubscriptionID != "" {
- return errHTTPBadRequestBillingSubscriptionExists
- }
- req, err := readJSONWithLimit[apiAccountBillingSubscriptionChangeRequest](r.Body, jsonBodyBytesLimit, false)
- if err != nil {
- return err
- }
- tier, err := s.userManager.Tier(req.Tier)
- if err != nil {
- return err
- } else if tier.StripePriceID == "" {
- return errNotAPaidTier
- }
- logvr(v, r).
- With(tier).
- Tag(tagStripe).
- Info("Creating Stripe checkout flow")
- var stripeCustomerID *string
- if u.Billing.StripeCustomerID != "" {
- stripeCustomerID = &u.Billing.StripeCustomerID
- stripeCustomer, err := s.stripe.GetCustomer(u.Billing.StripeCustomerID)
- if err != nil {
- return err
- } else if stripeCustomer.Subscriptions != nil && len(stripeCustomer.Subscriptions.Data) > 0 {
- return errMultipleBillingSubscriptions
- }
- }
- successURL := s.config.BaseURL + apiAccountBillingSubscriptionCheckoutSuccessTemplate
- params := &stripe.CheckoutSessionParams{
- Customer: stripeCustomerID, // A user may have previously deleted their subscription
- ClientReferenceID: &u.ID,
- SuccessURL: &successURL,
- Mode: stripe.String(string(stripe.CheckoutSessionModeSubscription)),
- AllowPromotionCodes: stripe.Bool(true),
- LineItems: []*stripe.CheckoutSessionLineItemParams{
- {
- Price: stripe.String(tier.StripePriceID),
- Quantity: stripe.Int64(1),
- },
- },
- AutomaticTax: &stripe.CheckoutSessionAutomaticTaxParams{
- Enabled: stripe.Bool(true),
- },
- }
- sess, err := s.stripe.NewCheckoutSession(params)
- if err != nil {
- return err
- }
- response := &apiAccountBillingSubscriptionCreateResponse{
- RedirectURL: sess.URL,
- }
- return s.writeJSON(w, response)
- }
- // handleAccountBillingSubscriptionCreateSuccess is called after the Stripe checkout session has succeeded. We use
- // the session ID in the URL to retrieve the Stripe subscription and update the local database. This is the first
- // and only time we can map the local username with the Stripe customer ID.
- func (s *Server) handleAccountBillingSubscriptionCreateSuccess(w http.ResponseWriter, r *http.Request, v *visitor) error {
- // We don't have v.User() in this endpoint, only a userManager!
- matches := apiAccountBillingSubscriptionCheckoutSuccessRegex.FindStringSubmatch(r.URL.Path)
- if len(matches) != 2 {
- return errHTTPInternalErrorInvalidPath
- }
- sessionID := matches[1]
- sess, err := s.stripe.GetSession(sessionID) // FIXME How do we rate limit this?
- if err != nil {
- return err
- } else if sess.Customer == nil || sess.Subscription == nil || sess.ClientReferenceID == "" {
- return wrapErrHTTP(errHTTPBadRequestBillingRequestInvalid, "customer or subscription not found")
- }
- sub, err := s.stripe.GetSubscription(sess.Subscription.ID)
- if err != nil {
- return err
- } else if sub.Items == nil || len(sub.Items.Data) != 1 || sub.Items.Data[0].Price == nil {
- return wrapErrHTTP(errHTTPBadRequestBillingRequestInvalid, "more than one line item in existing subscription")
- }
- tier, err := s.userManager.TierByStripePrice(sub.Items.Data[0].Price.ID)
- if err != nil {
- return err
- }
- u, err := s.userManager.UserByID(sess.ClientReferenceID)
- if err != nil {
- return err
- }
- v.SetUser(u)
- logvr(v, r).
- With(tier).
- Tag(tagStripe).
- Fields(log.Context{
- "stripe_customer_id": sess.Customer.ID,
- "stripe_subscription_id": sub.ID,
- "stripe_subscription_status": string(sub.Status),
- "stripe_subscription_paid_until": sub.CurrentPeriodEnd,
- }).
- Info("Stripe checkout flow succeeded, updating user tier and subscription")
- customerParams := &stripe.CustomerParams{
- Params: stripe.Params{
- Metadata: map[string]string{
- "user_id": u.ID,
- "user_name": u.Name,
- },
- },
- }
- if _, err := s.stripe.UpdateCustomer(sess.Customer.ID, customerParams); err != nil {
- return err
- }
- if err := s.updateSubscriptionAndTier(r, v, u, tier, sess.Customer.ID, sub.ID, string(sub.Status), sub.CurrentPeriodEnd, sub.CancelAt); err != nil {
- return err
- }
- http.Redirect(w, r, s.config.BaseURL+accountPath, http.StatusSeeOther)
- return nil
- }
- // handleAccountBillingSubscriptionUpdate updates an existing Stripe subscription to a new price, and updates
- // a user's tier accordingly. This endpoint only works if there is an existing subscription.
- func (s *Server) handleAccountBillingSubscriptionUpdate(w http.ResponseWriter, r *http.Request, v *visitor) error {
- u := v.User()
- if u.Billing.StripeSubscriptionID == "" {
- return errNoBillingSubscription
- }
- req, err := readJSONWithLimit[apiAccountBillingSubscriptionChangeRequest](r.Body, jsonBodyBytesLimit, false)
- if err != nil {
- return err
- }
- tier, err := s.userManager.Tier(req.Tier)
- if err != nil {
- return err
- }
- logvr(v, r).
- Tag(tagStripe).
- Fields(log.Context{
- "new_tier_id": tier.ID,
- "new_tier_name": tier.Name,
- "new_tier_stripe_price_id": tier.StripePriceID,
- // Other stripe_* fields filled by visitor context
- }).
- Info("Changing Stripe subscription and billing tier to %s/%s (price %s)", tier.ID, tier.Name, tier.StripePriceID)
- sub, err := s.stripe.GetSubscription(u.Billing.StripeSubscriptionID)
- if err != nil {
- return err
- } else if sub.Items == nil || len(sub.Items.Data) != 1 {
- return wrapErrHTTP(errHTTPBadRequestBillingRequestInvalid, "no items, or more than one item")
- }
- params := &stripe.SubscriptionParams{
- CancelAtPeriodEnd: stripe.Bool(false),
- ProrationBehavior: stripe.String(string(stripe.SubscriptionSchedulePhaseProrationBehaviorCreateProrations)),
- Items: []*stripe.SubscriptionItemsParams{
- {
- ID: stripe.String(sub.Items.Data[0].ID),
- Price: stripe.String(tier.StripePriceID),
- },
- },
- }
- _, err = s.stripe.UpdateSubscription(sub.ID, params)
- if err != nil {
- return err
- }
- return s.writeJSON(w, newSuccessResponse())
- }
- // handleAccountBillingSubscriptionDelete facilitates downgrading a paid user to a tier-less user,
- // and cancelling the Stripe subscription entirely. Note that this does not actually change the tier.
- // That is done by a webhook at the period end (in X days).
- func (s *Server) handleAccountBillingSubscriptionDelete(w http.ResponseWriter, r *http.Request, v *visitor) error {
- logvr(v, r).Tag(tagStripe).Info("Deleting Stripe subscription")
- u := v.User()
- if u.Billing.StripeSubscriptionID != "" {
- params := &stripe.SubscriptionParams{
- CancelAtPeriodEnd: stripe.Bool(true),
- }
- _, err := s.stripe.UpdateSubscription(u.Billing.StripeSubscriptionID, params)
- if err != nil {
- return err
- }
- }
- return s.writeJSON(w, newSuccessResponse())
- }
- // handleAccountBillingPortalSessionCreate creates a session to the customer billing portal, and returns the
- // redirect URL. The billing portal allows customers to change their payment methods, and cancel the subscription.
- func (s *Server) handleAccountBillingPortalSessionCreate(w http.ResponseWriter, r *http.Request, v *visitor) error {
- logvr(v, r).Tag(tagStripe).Info("Creating Stripe billing portal session")
- u := v.User()
- if u.Billing.StripeCustomerID == "" {
- return errHTTPBadRequestNotAPaidUser
- }
- params := &stripe.BillingPortalSessionParams{
- Customer: stripe.String(u.Billing.StripeCustomerID),
- ReturnURL: stripe.String(s.config.BaseURL),
- }
- ps, err := s.stripe.NewPortalSession(params)
- if err != nil {
- return err
- }
- response := &apiAccountBillingPortalRedirectResponse{
- RedirectURL: ps.URL,
- }
- return s.writeJSON(w, response)
- }
- // handleAccountBillingWebhook handles incoming Stripe webhooks. It mainly keeps the local user database in sync
- // with the Stripe view of the world. This endpoint is authorized via the Stripe webhook secret. Note that the
- // visitor (v) in this endpoint is the Stripe API, so we don't have u available.
- func (s *Server) handleAccountBillingWebhook(_ http.ResponseWriter, r *http.Request, v *visitor) error {
- stripeSignature := r.Header.Get("Stripe-Signature")
- if stripeSignature == "" {
- return errHTTPBadRequestBillingRequestInvalid
- }
- body, err := util.Peek(r.Body, jsonBodyBytesLimit)
- if err != nil {
- return err
- } else if body.LimitReached {
- return errHTTPEntityTooLargeJSONBody
- }
- event, err := s.stripe.ConstructWebhookEvent(body.PeekedBytes, stripeSignature, s.config.StripeWebhookKey)
- if err != nil {
- return err
- } else if event.Data == nil || event.Data.Raw == nil {
- return errHTTPBadRequestBillingRequestInvalid
- }
- switch event.Type {
- case "customer.subscription.updated":
- return s.handleAccountBillingWebhookSubscriptionUpdated(r, v, event)
- case "customer.subscription.deleted":
- return s.handleAccountBillingWebhookSubscriptionDeleted(r, v, event)
- default:
- logvr(v, r).
- Tag(tagStripe).
- Field("stripe_webhook_type", event.Type).
- Warn("Unhandled Stripe webhook event %s received", event.Type)
- return nil
- }
- }
- func (s *Server) handleAccountBillingWebhookSubscriptionUpdated(r *http.Request, v *visitor, event stripe.Event) error {
- ev, err := util.UnmarshalJSON[apiStripeSubscriptionUpdatedEvent](io.NopCloser(bytes.NewReader(event.Data.Raw)))
- if err != nil {
- return err
- } else if ev.ID == "" || ev.Customer == "" || ev.Status == "" || ev.CurrentPeriodEnd == 0 || ev.Items == nil || len(ev.Items.Data) != 1 || ev.Items.Data[0].Price == nil || ev.Items.Data[0].Price.ID == "" {
- return errHTTPBadRequestBillingRequestInvalid
- }
- subscriptionID, priceID := ev.ID, ev.Items.Data[0].Price.ID
- logvr(v, r).
- Tag(tagStripe).
- Fields(log.Context{
- "stripe_webhook_type": event.Type,
- "stripe_customer_id": ev.Customer,
- "stripe_subscription_id": ev.ID,
- "stripe_subscription_status": ev.Status,
- "stripe_subscription_paid_until": ev.CurrentPeriodEnd,
- "stripe_subscription_cancel_at": ev.CancelAt,
- "stripe_price_id": priceID,
- }).
- Info("Updating subscription to status %s, with price %s", ev.Status, priceID)
- userFn := func() (*user.User, error) {
- return s.userManager.UserByStripeCustomer(ev.Customer)
- }
- // We retry the user retrieval function, because during the Stripe checkout, there a race between the browser
- // checkout success redirect (see handleAccountBillingSubscriptionCreateSuccess), and this webhook. The checkout
- // success call is the one that updates the user with the Stripe customer ID.
- u, err := util.Retry[user.User](userFn, retryUserDelays...)
- if err != nil {
- return err
- }
- v.SetUser(u)
- tier, err := s.userManager.TierByStripePrice(priceID)
- if err != nil {
- return err
- }
- if err := s.updateSubscriptionAndTier(r, v, u, tier, ev.Customer, subscriptionID, ev.Status, ev.CurrentPeriodEnd, ev.CancelAt); err != nil {
- return err
- }
- s.publishSyncEventAsync(s.visitor(netip.IPv4Unspecified(), u))
- return nil
- }
- func (s *Server) handleAccountBillingWebhookSubscriptionDeleted(r *http.Request, v *visitor, event stripe.Event) error {
- ev, err := util.UnmarshalJSON[apiStripeSubscriptionDeletedEvent](io.NopCloser(bytes.NewReader(event.Data.Raw)))
- if err != nil {
- return err
- } else if ev.Customer == "" {
- return errHTTPBadRequestBillingRequestInvalid
- }
- u, err := s.userManager.UserByStripeCustomer(ev.Customer)
- if err != nil {
- return err
- }
- v.SetUser(u)
- logvr(v, r).
- Tag(tagStripe).
- Field("stripe_webhook_type", event.Type).
- Info("Subscription deleted, downgrading to unpaid tier")
- if err := s.updateSubscriptionAndTier(r, v, u, nil, ev.Customer, "", "", 0, 0); err != nil {
- return err
- }
- s.publishSyncEventAsync(s.visitor(netip.IPv4Unspecified(), u))
- return nil
- }
- func (s *Server) updateSubscriptionAndTier(r *http.Request, v *visitor, u *user.User, tier *user.Tier, customerID, subscriptionID, status string, paidUntil, cancelAt int64) error {
- reservationsLimit := visitorDefaultReservationsLimit
- if tier != nil {
- reservationsLimit = tier.ReservationLimit
- }
- if err := s.maybeRemoveMessagesAndExcessReservations(r, v, u, reservationsLimit); err != nil {
- return err
- }
- if tier == nil && u.Tier != nil {
- logvr(v, r).Tag(tagStripe).Info("Resetting tier for user %s", u.Name)
- if err := s.userManager.ResetTier(u.Name); err != nil {
- return err
- }
- } else if tier != nil && u.TierID() != tier.ID {
- logvr(v, r).
- Tag(tagStripe).
- Fields(log.Context{
- "new_tier_id": tier.ID,
- "new_tier_name": tier.Name,
- "new_tier_stripe_price_id": tier.StripePriceID,
- }).
- Info("Changing tier to tier %s (%s) for user %s", tier.ID, tier.Name, u.Name)
- if err := s.userManager.ChangeTier(u.Name, tier.Code); err != nil {
- return err
- }
- }
- // Update billing fields
- billing := &user.Billing{
- StripeCustomerID: customerID,
- StripeSubscriptionID: subscriptionID,
- StripeSubscriptionStatus: stripe.SubscriptionStatus(status),
- StripeSubscriptionPaidUntil: time.Unix(paidUntil, 0),
- StripeSubscriptionCancelAt: time.Unix(cancelAt, 0),
- }
- if err := s.userManager.ChangeBilling(u.Name, billing); err != nil {
- return err
- }
- return nil
- }
- // fetchStripePrices contacts the Stripe API to retrieve all prices. This is used by the server to cache the prices
- // in memory, and ultimately for the web app to display the price table.
- func (s *Server) fetchStripePrices() (map[string]string, error) {
- log.Debug("Caching prices from Stripe API")
- priceMap := make(map[string]string)
- prices, err := s.stripe.ListPrices(&stripe.PriceListParams{Active: stripe.Bool(true)})
- if err != nil {
- log.Warn("Fetching Stripe prices failed: %s", err.Error())
- return nil, err
- }
- for _, p := range prices {
- if p.UnitAmount%100 == 0 {
- priceMap[p.ID] = fmt.Sprintf("$%d", p.UnitAmount/100)
- } else {
- priceMap[p.ID] = fmt.Sprintf("$%.2f", float64(p.UnitAmount)/100)
- }
- log.Trace("- Caching price %s = %v", p.ID, priceMap[p.ID])
- }
- return priceMap, nil
- }
- // stripeAPI is a small interface to facilitate mocking of the Stripe API
- type stripeAPI interface {
- NewCheckoutSession(params *stripe.CheckoutSessionParams) (*stripe.CheckoutSession, error)
- NewPortalSession(params *stripe.BillingPortalSessionParams) (*stripe.BillingPortalSession, error)
- ListPrices(params *stripe.PriceListParams) ([]*stripe.Price, error)
- GetCustomer(id string) (*stripe.Customer, error)
- GetSession(id string) (*stripe.CheckoutSession, error)
- GetSubscription(id string) (*stripe.Subscription, error)
- UpdateCustomer(id string, params *stripe.CustomerParams) (*stripe.Customer, error)
- UpdateSubscription(id string, params *stripe.SubscriptionParams) (*stripe.Subscription, error)
- CancelSubscription(id string) (*stripe.Subscription, error)
- ConstructWebhookEvent(payload []byte, header string, secret string) (stripe.Event, error)
- }
- // realStripeAPI is a thin shim around the Stripe functions to facilitate mocking
- type realStripeAPI struct{}
- var _ stripeAPI = (*realStripeAPI)(nil)
- func newStripeAPI() stripeAPI {
- return &realStripeAPI{}
- }
- func (s *realStripeAPI) NewCheckoutSession(params *stripe.CheckoutSessionParams) (*stripe.CheckoutSession, error) {
- return session.New(params)
- }
- func (s *realStripeAPI) NewPortalSession(params *stripe.BillingPortalSessionParams) (*stripe.BillingPortalSession, error) {
- return portalsession.New(params)
- }
- func (s *realStripeAPI) ListPrices(params *stripe.PriceListParams) ([]*stripe.Price, error) {
- prices := make([]*stripe.Price, 0)
- iter := price.List(params)
- for iter.Next() {
- prices = append(prices, iter.Price())
- }
- if iter.Err() != nil {
- return nil, iter.Err()
- }
- return prices, nil
- }
- func (s *realStripeAPI) GetCustomer(id string) (*stripe.Customer, error) {
- return customer.Get(id, nil)
- }
- func (s *realStripeAPI) GetSession(id string) (*stripe.CheckoutSession, error) {
- return session.Get(id, nil)
- }
- func (s *realStripeAPI) GetSubscription(id string) (*stripe.Subscription, error) {
- return subscription.Get(id, nil)
- }
- func (s *realStripeAPI) UpdateCustomer(id string, params *stripe.CustomerParams) (*stripe.Customer, error) {
- return customer.Update(id, params)
- }
- func (s *realStripeAPI) UpdateSubscription(id string, params *stripe.SubscriptionParams) (*stripe.Subscription, error) {
- return subscription.Update(id, params)
- }
- func (s *realStripeAPI) CancelSubscription(id string) (*stripe.Subscription, error) {
- return subscription.Cancel(id, nil)
- }
- func (s *realStripeAPI) ConstructWebhookEvent(payload []byte, header string, secret string) (stripe.Event, error) {
- return webhook.ConstructEvent(payload, header, secret)
- }
|