server_payments.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. package server
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "github.com/stripe/stripe-go/v74"
  8. portalsession "github.com/stripe/stripe-go/v74/billingportal/session"
  9. "github.com/stripe/stripe-go/v74/checkout/session"
  10. "github.com/stripe/stripe-go/v74/customer"
  11. "github.com/stripe/stripe-go/v74/price"
  12. "github.com/stripe/stripe-go/v74/subscription"
  13. "github.com/stripe/stripe-go/v74/webhook"
  14. "heckel.io/ntfy/log"
  15. "heckel.io/ntfy/user"
  16. "heckel.io/ntfy/util"
  17. "io"
  18. "net/http"
  19. "net/netip"
  20. "time"
  21. )
  22. // Payments in ntfy are done via Stripe.
  23. //
  24. // Pretty much all payments related things are in this file. The following processes
  25. // handle payments:
  26. //
  27. // - Checkout:
  28. // Creating a Stripe customer and subscription via the Checkout flow. This flow is only used if the
  29. // ntfy user is not already a Stripe customer. This requires redirecting to the Stripe checkout page.
  30. // It is implemented in handleAccountBillingSubscriptionCreate and the success callback
  31. // handleAccountBillingSubscriptionCreateSuccess.
  32. // - Update subscription:
  33. // Switching between Stripe subscriptions (upgrade/downgrade) is handled via
  34. // handleAccountBillingSubscriptionUpdate. This also handles proration.
  35. // - Cancel subscription (at period end):
  36. // Users can cancel the Stripe subscription via the web app at the end of the billing period. This
  37. // simply updates the subscription and Stripe will cancel it. Users cannot immediately cancel the
  38. // subscription.
  39. // - Webhooks:
  40. // Whenever a subscription changes (updated, deleted), Stripe sends us a request via a webhook.
  41. // This is used to keep the local user database fields up to date. Stripe is the source of truth.
  42. // What Stripe says is mirrored and not questioned.
  43. var (
  44. errNotAPaidTier = errors.New("tier does not have billing price identifier")
  45. errMultipleBillingSubscriptions = errors.New("cannot have multiple billing subscriptions")
  46. errNoBillingSubscription = errors.New("user does not have an active billing subscription")
  47. )
  48. var (
  49. retryUserDelays = []time.Duration{3 * time.Second, 5 * time.Second, 7 * time.Second}
  50. )
  51. // handleBillingTiersGet returns all available paid tiers, and the free tier. This is to populate the upgrade dialog
  52. // in the UI. Note that this endpoint does NOT have a user context (no v.user!).
  53. func (s *Server) handleBillingTiersGet(w http.ResponseWriter, _ *http.Request, _ *visitor) error {
  54. tiers, err := s.userManager.Tiers()
  55. if err != nil {
  56. return err
  57. }
  58. freeTier := defaultVisitorLimits(s.config)
  59. response := []*apiAccountBillingTier{
  60. {
  61. // This is a bit of a hack: This is the "Free" tier. It has no tier code, name or price.
  62. Limits: &apiAccountLimits{
  63. Basis: string(visitorLimitBasisIP),
  64. Messages: freeTier.MessagesLimit,
  65. MessagesExpiryDuration: int64(freeTier.MessagesExpiryDuration.Seconds()),
  66. Emails: freeTier.EmailsLimit,
  67. Reservations: freeTier.ReservationsLimit,
  68. AttachmentTotalSize: freeTier.AttachmentTotalSizeLimit,
  69. AttachmentFileSize: freeTier.AttachmentFileSizeLimit,
  70. AttachmentExpiryDuration: int64(freeTier.AttachmentExpiryDuration.Seconds()),
  71. },
  72. },
  73. }
  74. prices, err := s.priceCache.Value()
  75. if err != nil {
  76. return err
  77. }
  78. for _, tier := range tiers {
  79. priceStr, ok := prices[tier.StripePriceID]
  80. if tier.StripePriceID == "" || !ok {
  81. continue
  82. }
  83. response = append(response, &apiAccountBillingTier{
  84. Code: tier.Code,
  85. Name: tier.Name,
  86. Price: priceStr,
  87. Limits: &apiAccountLimits{
  88. Basis: string(visitorLimitBasisTier),
  89. Messages: tier.MessagesLimit,
  90. MessagesExpiryDuration: int64(tier.MessagesExpiryDuration.Seconds()),
  91. Emails: tier.EmailsLimit,
  92. Reservations: tier.ReservationsLimit,
  93. AttachmentTotalSize: tier.AttachmentTotalSizeLimit,
  94. AttachmentFileSize: tier.AttachmentFileSizeLimit,
  95. AttachmentExpiryDuration: int64(tier.AttachmentExpiryDuration.Seconds()),
  96. },
  97. })
  98. }
  99. return s.writeJSON(w, response)
  100. }
  101. // handleAccountBillingSubscriptionCreate creates a Stripe checkout flow to create a user subscription. The tier
  102. // will be updated by a subsequent webhook from Stripe, once the subscription becomes active.
  103. func (s *Server) handleAccountBillingSubscriptionCreate(w http.ResponseWriter, r *http.Request, v *visitor) error {
  104. if v.user.Billing.StripeSubscriptionID != "" {
  105. return errHTTPBadRequestBillingSubscriptionExists
  106. }
  107. req, err := readJSONWithLimit[apiAccountBillingSubscriptionChangeRequest](r.Body, jsonBodyBytesLimit)
  108. if err != nil {
  109. return err
  110. }
  111. tier, err := s.userManager.Tier(req.Tier)
  112. if err != nil {
  113. return err
  114. } else if tier.StripePriceID == "" {
  115. return errNotAPaidTier
  116. }
  117. log.Info("%s Creating Stripe checkout flow", logHTTPPrefix(v, r))
  118. var stripeCustomerID *string
  119. if v.user.Billing.StripeCustomerID != "" {
  120. stripeCustomerID = &v.user.Billing.StripeCustomerID
  121. stripeCustomer, err := s.stripe.GetCustomer(v.user.Billing.StripeCustomerID)
  122. if err != nil {
  123. return err
  124. } else if stripeCustomer.Subscriptions != nil && len(stripeCustomer.Subscriptions.Data) > 0 {
  125. return errMultipleBillingSubscriptions
  126. }
  127. }
  128. successURL := s.config.BaseURL + apiAccountBillingSubscriptionCheckoutSuccessTemplate
  129. params := &stripe.CheckoutSessionParams{
  130. Customer: stripeCustomerID, // A user may have previously deleted their subscription
  131. ClientReferenceID: &v.user.ID,
  132. SuccessURL: &successURL,
  133. Mode: stripe.String(string(stripe.CheckoutSessionModeSubscription)),
  134. AllowPromotionCodes: stripe.Bool(true),
  135. LineItems: []*stripe.CheckoutSessionLineItemParams{
  136. {
  137. Price: stripe.String(tier.StripePriceID),
  138. Quantity: stripe.Int64(1),
  139. },
  140. },
  141. Params: stripe.Params{
  142. Metadata: map[string]string{
  143. "user_id": v.user.ID,
  144. },
  145. },
  146. }
  147. sess, err := s.stripe.NewCheckoutSession(params)
  148. if err != nil {
  149. return err
  150. }
  151. response := &apiAccountBillingSubscriptionCreateResponse{
  152. RedirectURL: sess.URL,
  153. }
  154. return s.writeJSON(w, response)
  155. }
  156. // handleAccountBillingSubscriptionCreateSuccess is called after the Stripe checkout session has succeeded. We use
  157. // the session ID in the URL to retrieve the Stripe subscription and update the local database. This is the first
  158. // and only time we can map the local username with the Stripe customer ID.
  159. func (s *Server) handleAccountBillingSubscriptionCreateSuccess(w http.ResponseWriter, r *http.Request, v *visitor) error {
  160. // We don't have a v.user in this endpoint, only a userManager!
  161. matches := apiAccountBillingSubscriptionCheckoutSuccessRegex.FindStringSubmatch(r.URL.Path)
  162. if len(matches) != 2 {
  163. return errHTTPInternalErrorInvalidPath
  164. }
  165. sessionID := matches[1]
  166. sess, err := s.stripe.GetSession(sessionID) // FIXME How do we rate limit this?
  167. if err != nil {
  168. return err
  169. } else if sess.Customer == nil || sess.Subscription == nil || sess.ClientReferenceID == "" {
  170. return wrapErrHTTP(errHTTPBadRequestBillingRequestInvalid, "customer or subscription not found")
  171. }
  172. sub, err := s.stripe.GetSubscription(sess.Subscription.ID)
  173. if err != nil {
  174. return err
  175. } else if sub.Items == nil || len(sub.Items.Data) != 1 || sub.Items.Data[0].Price == nil {
  176. return wrapErrHTTP(errHTTPBadRequestBillingRequestInvalid, "more than one line item in existing subscription")
  177. }
  178. tier, err := s.userManager.TierByStripePrice(sub.Items.Data[0].Price.ID)
  179. if err != nil {
  180. return err
  181. }
  182. u, err := s.userManager.UserByID(sess.ClientReferenceID)
  183. if err != nil {
  184. return err
  185. }
  186. v.SetUser(u)
  187. customerParams := &stripe.CustomerParams{
  188. Params: stripe.Params{
  189. Metadata: map[string]string{
  190. "user_id": u.ID,
  191. "user_name": u.Name,
  192. },
  193. },
  194. }
  195. if _, err := s.stripe.UpdateCustomer(sess.Customer.ID, customerParams); err != nil {
  196. return err
  197. }
  198. if err := s.updateSubscriptionAndTier(logHTTPPrefix(v, r), u, tier, sess.Customer.ID, sub.ID, string(sub.Status), sub.CurrentPeriodEnd, sub.CancelAt); err != nil {
  199. return err
  200. }
  201. http.Redirect(w, r, s.config.BaseURL+accountPath, http.StatusSeeOther)
  202. return nil
  203. }
  204. // handleAccountBillingSubscriptionUpdate updates an existing Stripe subscription to a new price, and updates
  205. // a user's tier accordingly. This endpoint only works if there is an existing subscription.
  206. func (s *Server) handleAccountBillingSubscriptionUpdate(w http.ResponseWriter, r *http.Request, v *visitor) error {
  207. if v.user.Billing.StripeSubscriptionID == "" {
  208. return errNoBillingSubscription
  209. }
  210. req, err := readJSONWithLimit[apiAccountBillingSubscriptionChangeRequest](r.Body, jsonBodyBytesLimit)
  211. if err != nil {
  212. return err
  213. }
  214. tier, err := s.userManager.Tier(req.Tier)
  215. if err != nil {
  216. return err
  217. }
  218. log.Info("%s Changing billing tier to %s (price %s) for subscription %s", logHTTPPrefix(v, r), tier.Code, tier.StripePriceID, v.user.Billing.StripeSubscriptionID)
  219. sub, err := s.stripe.GetSubscription(v.user.Billing.StripeSubscriptionID)
  220. if err != nil {
  221. return err
  222. }
  223. params := &stripe.SubscriptionParams{
  224. CancelAtPeriodEnd: stripe.Bool(false),
  225. ProrationBehavior: stripe.String(string(stripe.SubscriptionSchedulePhaseProrationBehaviorCreateProrations)),
  226. Items: []*stripe.SubscriptionItemsParams{
  227. {
  228. ID: stripe.String(sub.Items.Data[0].ID),
  229. Price: stripe.String(tier.StripePriceID),
  230. },
  231. },
  232. }
  233. _, err = s.stripe.UpdateSubscription(sub.ID, params)
  234. if err != nil {
  235. return err
  236. }
  237. return s.writeJSON(w, newSuccessResponse())
  238. }
  239. // handleAccountBillingSubscriptionDelete facilitates downgrading a paid user to a tier-less user,
  240. // and cancelling the Stripe subscription entirely
  241. func (s *Server) handleAccountBillingSubscriptionDelete(w http.ResponseWriter, r *http.Request, v *visitor) error {
  242. log.Info("%s Deleting billing subscription %s", logHTTPPrefix(v, r), v.user.Billing.StripeSubscriptionID)
  243. if v.user.Billing.StripeSubscriptionID != "" {
  244. params := &stripe.SubscriptionParams{
  245. CancelAtPeriodEnd: stripe.Bool(true),
  246. }
  247. _, err := s.stripe.UpdateSubscription(v.user.Billing.StripeSubscriptionID, params)
  248. if err != nil {
  249. return err
  250. }
  251. }
  252. return s.writeJSON(w, newSuccessResponse())
  253. }
  254. // handleAccountBillingPortalSessionCreate creates a session to the customer billing portal, and returns the
  255. // redirect URL. The billing portal allows customers to change their payment methods, and cancel the subscription.
  256. func (s *Server) handleAccountBillingPortalSessionCreate(w http.ResponseWriter, r *http.Request, v *visitor) error {
  257. if v.user.Billing.StripeCustomerID == "" {
  258. return errHTTPBadRequestNotAPaidUser
  259. }
  260. log.Info("%s Creating billing portal session", logHTTPPrefix(v, r))
  261. params := &stripe.BillingPortalSessionParams{
  262. Customer: stripe.String(v.user.Billing.StripeCustomerID),
  263. ReturnURL: stripe.String(s.config.BaseURL),
  264. }
  265. ps, err := s.stripe.NewPortalSession(params)
  266. if err != nil {
  267. return err
  268. }
  269. response := &apiAccountBillingPortalRedirectResponse{
  270. RedirectURL: ps.URL,
  271. }
  272. return s.writeJSON(w, response)
  273. }
  274. // handleAccountBillingWebhook handles incoming Stripe webhooks. It mainly keeps the local user database in sync
  275. // with the Stripe view of the world. This endpoint is authorized via the Stripe webhook secret. Note that the
  276. // visitor (v) in this endpoint is the Stripe API, so we don't have v.user available.
  277. func (s *Server) handleAccountBillingWebhook(w http.ResponseWriter, r *http.Request, _ *visitor) error {
  278. stripeSignature := r.Header.Get("Stripe-Signature")
  279. if stripeSignature == "" {
  280. return errHTTPBadRequestBillingRequestInvalid
  281. }
  282. body, err := util.Peek(r.Body, jsonBodyBytesLimit)
  283. if err != nil {
  284. return err
  285. } else if body.LimitReached {
  286. return errHTTPEntityTooLargeJSONBody
  287. }
  288. event, err := s.stripe.ConstructWebhookEvent(body.PeekedBytes, stripeSignature, s.config.StripeWebhookKey)
  289. if err != nil {
  290. return err
  291. } else if event.Data == nil || event.Data.Raw == nil {
  292. return errHTTPBadRequestBillingRequestInvalid
  293. }
  294. switch event.Type {
  295. case "customer.subscription.updated":
  296. return s.handleAccountBillingWebhookSubscriptionUpdated(event.Data.Raw)
  297. case "customer.subscription.deleted":
  298. return s.handleAccountBillingWebhookSubscriptionDeleted(event.Data.Raw)
  299. default:
  300. log.Warn("STRIPE Unhandled webhook event %s received", event.Type)
  301. return nil
  302. }
  303. }
  304. func (s *Server) handleAccountBillingWebhookSubscriptionUpdated(event json.RawMessage) error {
  305. ev, err := util.UnmarshalJSON[apiStripeSubscriptionUpdatedEvent](io.NopCloser(bytes.NewReader(event)))
  306. if err != nil {
  307. return err
  308. } 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 == "" {
  309. return errHTTPBadRequestBillingRequestInvalid
  310. }
  311. subscriptionID, priceID := ev.ID, ev.Items.Data[0].Price.ID
  312. log.Info("%s Updating subscription to status %s, with price %s", logStripePrefix(ev.Customer, ev.ID), ev.Status, priceID)
  313. userFn := func() (*user.User, error) {
  314. return s.userManager.UserByStripeCustomer(ev.Customer)
  315. }
  316. u, err := util.Retry[user.User](userFn, retryUserDelays...)
  317. if err != nil {
  318. return err
  319. }
  320. tier, err := s.userManager.TierByStripePrice(priceID)
  321. if err != nil {
  322. return err
  323. }
  324. if err := s.updateSubscriptionAndTier(logStripePrefix(ev.Customer, ev.ID), u, tier, ev.Customer, subscriptionID, ev.Status, ev.CurrentPeriodEnd, ev.CancelAt); err != nil {
  325. return err
  326. }
  327. s.publishSyncEventAsync(s.visitorFromUser(u, netip.IPv4Unspecified()))
  328. return nil
  329. }
  330. func (s *Server) handleAccountBillingWebhookSubscriptionDeleted(event json.RawMessage) error {
  331. ev, err := util.UnmarshalJSON[apiStripeSubscriptionDeletedEvent](io.NopCloser(bytes.NewReader(event)))
  332. if err != nil {
  333. return err
  334. } else if ev.Customer == "" {
  335. return errHTTPBadRequestBillingRequestInvalid
  336. }
  337. log.Info("%s Subscription deleted, downgrading to unpaid tier", logStripePrefix(ev.Customer, ev.ID))
  338. u, err := s.userManager.UserByStripeCustomer(ev.Customer)
  339. if err != nil {
  340. return err
  341. }
  342. if err := s.updateSubscriptionAndTier(logStripePrefix(ev.Customer, ev.ID), u, nil, ev.Customer, "", "", 0, 0); err != nil {
  343. return err
  344. }
  345. s.publishSyncEventAsync(s.visitorFromUser(u, netip.IPv4Unspecified()))
  346. return nil
  347. }
  348. func (s *Server) updateSubscriptionAndTier(logPrefix string, u *user.User, tier *user.Tier, customerID, subscriptionID, status string, paidUntil, cancelAt int64) error {
  349. reservationsLimit := visitorDefaultReservationsLimit
  350. if tier != nil {
  351. reservationsLimit = tier.ReservationsLimit
  352. }
  353. if err := s.maybeRemoveMessagesAndExcessReservations(logPrefix, u, reservationsLimit); err != nil {
  354. return err
  355. }
  356. if tier == nil {
  357. if err := s.userManager.ResetTier(u.Name); err != nil {
  358. return err
  359. }
  360. } else {
  361. if err := s.userManager.ChangeTier(u.Name, tier.Code); err != nil {
  362. return err
  363. }
  364. }
  365. // Update billing fields
  366. billing := &user.Billing{
  367. StripeCustomerID: customerID,
  368. StripeSubscriptionID: subscriptionID,
  369. StripeSubscriptionStatus: stripe.SubscriptionStatus(status),
  370. StripeSubscriptionPaidUntil: time.Unix(paidUntil, 0),
  371. StripeSubscriptionCancelAt: time.Unix(cancelAt, 0),
  372. }
  373. if err := s.userManager.ChangeBilling(u.Name, billing); err != nil {
  374. return err
  375. }
  376. return nil
  377. }
  378. // fetchStripePrices contacts the Stripe API to retrieve all prices. This is used by the server to cache the prices
  379. // in memory, and ultimately for the web app to display the price table.
  380. func (s *Server) fetchStripePrices() (map[string]string, error) {
  381. log.Debug("Caching prices from Stripe API")
  382. priceMap := make(map[string]string)
  383. prices, err := s.stripe.ListPrices(&stripe.PriceListParams{Active: stripe.Bool(true)})
  384. if err != nil {
  385. log.Warn("Fetching Stripe prices failed: %s", err.Error())
  386. return nil, err
  387. }
  388. for _, p := range prices {
  389. if p.UnitAmount%100 == 0 {
  390. priceMap[p.ID] = fmt.Sprintf("$%d", p.UnitAmount/100)
  391. } else {
  392. priceMap[p.ID] = fmt.Sprintf("$%.2f", float64(p.UnitAmount)/100)
  393. }
  394. log.Trace("- Caching price %s = %v", p.ID, priceMap[p.ID])
  395. }
  396. return priceMap, nil
  397. }
  398. // stripeAPI is a small interface to facilitate mocking of the Stripe API
  399. type stripeAPI interface {
  400. NewCheckoutSession(params *stripe.CheckoutSessionParams) (*stripe.CheckoutSession, error)
  401. NewPortalSession(params *stripe.BillingPortalSessionParams) (*stripe.BillingPortalSession, error)
  402. ListPrices(params *stripe.PriceListParams) ([]*stripe.Price, error)
  403. GetCustomer(id string) (*stripe.Customer, error)
  404. GetSession(id string) (*stripe.CheckoutSession, error)
  405. GetSubscription(id string) (*stripe.Subscription, error)
  406. UpdateCustomer(id string, params *stripe.CustomerParams) (*stripe.Customer, error)
  407. UpdateSubscription(id string, params *stripe.SubscriptionParams) (*stripe.Subscription, error)
  408. CancelSubscription(id string) (*stripe.Subscription, error)
  409. ConstructWebhookEvent(payload []byte, header string, secret string) (stripe.Event, error)
  410. }
  411. // realStripeAPI is a thin shim around the Stripe functions to facilitate mocking
  412. type realStripeAPI struct{}
  413. var _ stripeAPI = (*realStripeAPI)(nil)
  414. func newStripeAPI() stripeAPI {
  415. return &realStripeAPI{}
  416. }
  417. func (s *realStripeAPI) NewCheckoutSession(params *stripe.CheckoutSessionParams) (*stripe.CheckoutSession, error) {
  418. return session.New(params)
  419. }
  420. func (s *realStripeAPI) NewPortalSession(params *stripe.BillingPortalSessionParams) (*stripe.BillingPortalSession, error) {
  421. return portalsession.New(params)
  422. }
  423. func (s *realStripeAPI) ListPrices(params *stripe.PriceListParams) ([]*stripe.Price, error) {
  424. prices := make([]*stripe.Price, 0)
  425. iter := price.List(params)
  426. for iter.Next() {
  427. prices = append(prices, iter.Price())
  428. }
  429. if iter.Err() != nil {
  430. return nil, iter.Err()
  431. }
  432. return prices, nil
  433. }
  434. func (s *realStripeAPI) GetCustomer(id string) (*stripe.Customer, error) {
  435. return customer.Get(id, nil)
  436. }
  437. func (s *realStripeAPI) GetSession(id string) (*stripe.CheckoutSession, error) {
  438. return session.Get(id, nil)
  439. }
  440. func (s *realStripeAPI) GetSubscription(id string) (*stripe.Subscription, error) {
  441. return subscription.Get(id, nil)
  442. }
  443. func (s *realStripeAPI) UpdateCustomer(id string, params *stripe.CustomerParams) (*stripe.Customer, error) {
  444. return customer.Update(id, params)
  445. }
  446. func (s *realStripeAPI) UpdateSubscription(id string, params *stripe.SubscriptionParams) (*stripe.Subscription, error) {
  447. return subscription.Update(id, params)
  448. }
  449. func (s *realStripeAPI) CancelSubscription(id string) (*stripe.Subscription, error) {
  450. return subscription.Cancel(id, nil)
  451. }
  452. func (s *realStripeAPI) ConstructWebhookEvent(payload []byte, header string, secret string) (stripe.Event, error) {
  453. return webhook.ConstructEvent(payload, header, secret)
  454. }