2023-01-16 04:29:46 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2023-01-18 20:50:06 +00:00
|
|
|
"bytes"
|
2023-01-16 04:29:46 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2023-01-17 15:09:37 +00:00
|
|
|
"fmt"
|
2023-01-16 04:29:46 +00:00
|
|
|
"github.com/stripe/stripe-go/v74"
|
|
|
|
portalsession "github.com/stripe/stripe-go/v74/billingportal/session"
|
|
|
|
"github.com/stripe/stripe-go/v74/checkout/session"
|
2023-01-16 21:35:37 +00:00
|
|
|
"github.com/stripe/stripe-go/v74/customer"
|
2023-01-17 15:09:37 +00:00
|
|
|
"github.com/stripe/stripe-go/v74/price"
|
2023-01-16 04:29:46 +00:00
|
|
|
"github.com/stripe/stripe-go/v74/subscription"
|
|
|
|
"github.com/stripe/stripe-go/v74/webhook"
|
|
|
|
"heckel.io/ntfy/log"
|
2023-01-17 15:09:37 +00:00
|
|
|
"heckel.io/ntfy/user"
|
2023-01-16 04:29:46 +00:00
|
|
|
"heckel.io/ntfy/util"
|
2023-01-18 20:50:06 +00:00
|
|
|
"io"
|
2023-01-16 04:29:46 +00:00
|
|
|
"net/http"
|
2023-01-16 21:35:37 +00:00
|
|
|
"net/netip"
|
2023-01-16 04:29:46 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2023-01-18 00:40:03 +00:00
|
|
|
var (
|
2023-01-18 18:46:40 +00:00
|
|
|
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")
|
2023-01-18 00:40:03 +00:00
|
|
|
)
|
|
|
|
|
2023-01-18 18:46:40 +00:00
|
|
|
// 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 v.user!).
|
|
|
|
func (s *Server) handleBillingTiersGet(w http.ResponseWriter, _ *http.Request, _ *visitor) error {
|
|
|
|
tiers, err := s.userManager.Tiers()
|
2023-01-17 15:09:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-18 00:40:03 +00:00
|
|
|
freeTier := defaultVisitorLimits(s.config)
|
|
|
|
response := []*apiAccountBillingTier{
|
|
|
|
{
|
|
|
|
// Free tier: no code, name or price
|
|
|
|
Limits: &apiAccountLimits{
|
|
|
|
Messages: freeTier.MessagesLimit,
|
|
|
|
MessagesExpiryDuration: int64(freeTier.MessagesExpiryDuration.Seconds()),
|
|
|
|
Emails: freeTier.EmailsLimit,
|
|
|
|
Reservations: freeTier.ReservationsLimit,
|
|
|
|
AttachmentTotalSize: freeTier.AttachmentTotalSizeLimit,
|
|
|
|
AttachmentFileSize: freeTier.AttachmentFileSizeLimit,
|
|
|
|
AttachmentExpiryDuration: int64(freeTier.AttachmentExpiryDuration.Seconds()),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2023-01-18 20:50:06 +00:00
|
|
|
prices, err := s.priceCache.Value()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-17 15:09:37 +00:00
|
|
|
for _, tier := range tiers {
|
2023-01-18 20:50:06 +00:00
|
|
|
priceStr, ok := prices[tier.StripePriceID]
|
|
|
|
if tier.StripePriceID == "" || !ok {
|
2023-01-17 15:09:37 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
response = append(response, &apiAccountBillingTier{
|
2023-01-18 00:40:03 +00:00
|
|
|
Code: tier.Code,
|
|
|
|
Name: tier.Name,
|
|
|
|
Price: priceStr,
|
|
|
|
Limits: &apiAccountLimits{
|
|
|
|
Messages: tier.MessagesLimit,
|
|
|
|
MessagesExpiryDuration: int64(tier.MessagesExpiryDuration.Seconds()),
|
|
|
|
Emails: tier.EmailsLimit,
|
|
|
|
Reservations: tier.ReservationsLimit,
|
|
|
|
AttachmentTotalSize: tier.AttachmentTotalSizeLimit,
|
|
|
|
AttachmentFileSize: tier.AttachmentFileSizeLimit,
|
|
|
|
AttachmentExpiryDuration: int64(tier.AttachmentExpiryDuration.Seconds()),
|
|
|
|
},
|
2023-01-17 15:09:37 +00:00
|
|
|
})
|
|
|
|
}
|
2023-01-18 20:50:06 +00:00
|
|
|
return s.writeJSON(w, response)
|
2023-01-17 15:09:37 +00:00
|
|
|
}
|
|
|
|
|
2023-01-16 21:35:37 +00:00
|
|
|
// 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 {
|
|
|
|
if v.user.Billing.StripeSubscriptionID != "" {
|
2023-01-18 18:46:40 +00:00
|
|
|
return errHTTPBadRequestBillingSubscriptionExists
|
2023-01-16 21:35:37 +00:00
|
|
|
}
|
|
|
|
req, err := readJSONWithLimit[apiAccountBillingSubscriptionChangeRequest](r.Body, jsonBodyBytesLimit)
|
2023-01-16 04:29:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tier, err := s.userManager.Tier(req.Tier)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-01-18 00:40:03 +00:00
|
|
|
} else if tier.StripePriceID == "" {
|
|
|
|
return errNotAPaidTier
|
2023-01-16 04:29:46 +00:00
|
|
|
}
|
|
|
|
log.Info("Stripe: No existing subscription, creating checkout flow")
|
|
|
|
var stripeCustomerID *string
|
|
|
|
if v.user.Billing.StripeCustomerID != "" {
|
|
|
|
stripeCustomerID = &v.user.Billing.StripeCustomerID
|
2023-01-19 04:01:26 +00:00
|
|
|
stripeCustomer, err := s.stripe.GetCustomer(v.user.Billing.StripeCustomerID)
|
2023-01-16 21:35:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if stripeCustomer.Subscriptions != nil && len(stripeCustomer.Subscriptions.Data) > 0 {
|
2023-01-18 18:46:40 +00:00
|
|
|
return errMultipleBillingSubscriptions
|
2023-01-16 21:35:37 +00:00
|
|
|
}
|
2023-01-16 04:29:46 +00:00
|
|
|
}
|
2023-01-17 15:09:37 +00:00
|
|
|
successURL := s.config.BaseURL + apiAccountBillingSubscriptionCheckoutSuccessTemplate
|
2023-01-16 04:29:46 +00:00
|
|
|
params := &stripe.CheckoutSessionParams{
|
2023-01-18 00:40:03 +00:00
|
|
|
Customer: stripeCustomerID, // A user may have previously deleted their subscription
|
|
|
|
ClientReferenceID: &v.user.Name,
|
|
|
|
SuccessURL: &successURL,
|
|
|
|
Mode: stripe.String(string(stripe.CheckoutSessionModeSubscription)),
|
|
|
|
AllowPromotionCodes: stripe.Bool(true),
|
2023-01-16 04:29:46 +00:00
|
|
|
LineItems: []*stripe.CheckoutSessionLineItemParams{
|
|
|
|
{
|
|
|
|
Price: stripe.String(tier.StripePriceID),
|
|
|
|
Quantity: stripe.Int64(1),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
/*AutomaticTax: &stripe.CheckoutSessionAutomaticTaxParams{
|
|
|
|
Enabled: stripe.Bool(true),
|
|
|
|
},*/
|
|
|
|
}
|
2023-01-19 04:01:26 +00:00
|
|
|
sess, err := s.stripe.NewCheckoutSession(params)
|
2023-01-16 04:29:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-17 15:09:37 +00:00
|
|
|
response := &apiAccountBillingSubscriptionCreateResponse{
|
2023-01-16 04:29:46 +00:00
|
|
|
RedirectURL: sess.URL,
|
|
|
|
}
|
2023-01-18 20:50:06 +00:00
|
|
|
return s.writeJSON(w, response)
|
2023-01-16 04:29:46 +00:00
|
|
|
}
|
|
|
|
|
2023-01-16 21:35:37 +00:00
|
|
|
func (s *Server) handleAccountBillingSubscriptionCreateSuccess(w http.ResponseWriter, r *http.Request, _ *visitor) error {
|
2023-01-16 04:29:46 +00:00
|
|
|
// We don't have a v.user in this endpoint, only a userManager!
|
2023-01-17 15:09:37 +00:00
|
|
|
matches := apiAccountBillingSubscriptionCheckoutSuccessRegex.FindStringSubmatch(r.URL.Path)
|
2023-01-16 04:29:46 +00:00
|
|
|
if len(matches) != 2 {
|
|
|
|
return errHTTPInternalErrorInvalidPath
|
|
|
|
}
|
|
|
|
sessionID := matches[1]
|
2023-01-19 04:01:26 +00:00
|
|
|
sess, err := s.stripe.GetSession(sessionID) // FIXME How do we rate limit this?
|
2023-01-16 04:29:46 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Warn("Stripe: %s", err)
|
2023-01-18 18:46:40 +00:00
|
|
|
return errHTTPBadRequestBillingRequestInvalid
|
2023-01-16 04:29:46 +00:00
|
|
|
} else if sess.Customer == nil || sess.Subscription == nil || sess.ClientReferenceID == "" {
|
2023-01-18 18:46:40 +00:00
|
|
|
return wrapErrHTTP(errHTTPBadRequestBillingRequestInvalid, "customer or subscription not found")
|
2023-01-16 04:29:46 +00:00
|
|
|
}
|
2023-01-19 04:01:26 +00:00
|
|
|
sub, err := s.stripe.GetSubscription(sess.Subscription.ID)
|
2023-01-16 04:29:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if sub.Items == nil || len(sub.Items.Data) != 1 || sub.Items.Data[0].Price == nil {
|
2023-01-18 18:46:40 +00:00
|
|
|
return wrapErrHTTP(errHTTPBadRequestBillingRequestInvalid, "more than one line item in existing subscription")
|
2023-01-16 04:29:46 +00:00
|
|
|
}
|
2023-01-17 15:09:37 +00:00
|
|
|
tier, err := s.userManager.TierByStripePrice(sub.Items.Data[0].Price.ID)
|
2023-01-16 04:29:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
u, err := s.userManager.User(sess.ClientReferenceID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-17 15:09:37 +00:00
|
|
|
if err := s.updateSubscriptionAndTier(u, sess.Customer.ID, sub.ID, string(sub.Status), sub.CurrentPeriodEnd, sub.CancelAt, tier.Code); err != nil {
|
2023-01-16 04:29:46 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-01-17 15:09:37 +00:00
|
|
|
http.Redirect(w, r, s.config.BaseURL+accountPath, http.StatusSeeOther)
|
2023-01-16 04:29:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-16 21:35:37 +00:00
|
|
|
// 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 {
|
2023-01-17 15:09:37 +00:00
|
|
|
if v.user.Billing.StripeSubscriptionID == "" {
|
2023-01-18 18:46:40 +00:00
|
|
|
return errNoBillingSubscription
|
2023-01-16 21:35:37 +00:00
|
|
|
}
|
|
|
|
req, err := readJSONWithLimit[apiAccountBillingSubscriptionChangeRequest](r.Body, jsonBodyBytesLimit)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tier, err := s.userManager.Tier(req.Tier)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Info("Stripe: Changing tier and subscription to %s", tier.Code)
|
2023-01-19 04:01:26 +00:00
|
|
|
sub, err := s.stripe.GetSubscription(v.user.Billing.StripeSubscriptionID)
|
2023-01-16 21:35:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
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),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2023-01-19 04:01:26 +00:00
|
|
|
_, err = s.stripe.UpdateSubscription(sub.ID, params)
|
2023-01-16 21:35:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-18 20:50:06 +00:00
|
|
|
return s.writeJSON(w, newSuccessResponse())
|
2023-01-16 21:35:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// handleAccountBillingSubscriptionDelete facilitates downgrading a paid user to a tier-less user,
|
|
|
|
// and cancelling the Stripe subscription entirely
|
|
|
|
func (s *Server) handleAccountBillingSubscriptionDelete(w http.ResponseWriter, r *http.Request, v *visitor) error {
|
|
|
|
if v.user.Billing.StripeSubscriptionID != "" {
|
|
|
|
params := &stripe.SubscriptionParams{
|
|
|
|
CancelAtPeriodEnd: stripe.Bool(true),
|
|
|
|
}
|
2023-01-19 04:01:26 +00:00
|
|
|
_, err := s.stripe.UpdateSubscription(v.user.Billing.StripeSubscriptionID, params)
|
2023-01-16 21:35:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-01-18 20:50:06 +00:00
|
|
|
return s.writeJSON(w, newSuccessResponse())
|
2023-01-16 21:35:37 +00:00
|
|
|
}
|
|
|
|
|
2023-01-16 04:29:46 +00:00
|
|
|
func (s *Server) handleAccountBillingPortalSessionCreate(w http.ResponseWriter, r *http.Request, v *visitor) error {
|
|
|
|
if v.user.Billing.StripeCustomerID == "" {
|
|
|
|
return errHTTPBadRequestNotAPaidUser
|
|
|
|
}
|
|
|
|
params := &stripe.BillingPortalSessionParams{
|
|
|
|
Customer: stripe.String(v.user.Billing.StripeCustomerID),
|
|
|
|
ReturnURL: stripe.String(s.config.BaseURL),
|
|
|
|
}
|
2023-01-19 04:01:26 +00:00
|
|
|
ps, err := s.stripe.NewPortalSession(params)
|
2023-01-16 04:29:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
response := &apiAccountBillingPortalRedirectResponse{
|
|
|
|
RedirectURL: ps.URL,
|
|
|
|
}
|
2023-01-18 20:50:06 +00:00
|
|
|
return s.writeJSON(w, response)
|
2023-01-16 04:29:46 +00:00
|
|
|
}
|
|
|
|
|
2023-01-18 18:46:40 +00:00
|
|
|
// 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 v.user available.
|
2023-01-16 21:35:37 +00:00
|
|
|
func (s *Server) handleAccountBillingWebhook(w http.ResponseWriter, r *http.Request, _ *visitor) error {
|
2023-01-16 04:29:46 +00:00
|
|
|
stripeSignature := r.Header.Get("Stripe-Signature")
|
|
|
|
if stripeSignature == "" {
|
2023-01-18 18:46:40 +00:00
|
|
|
return errHTTPBadRequestBillingRequestInvalid
|
2023-01-16 04:29:46 +00:00
|
|
|
}
|
2023-01-18 20:50:06 +00:00
|
|
|
body, err := util.Peek(r.Body, jsonBodyBytesLimit)
|
2023-01-16 04:29:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if body.LimitReached {
|
|
|
|
return errHTTPEntityTooLargeJSONBody
|
|
|
|
}
|
2023-01-19 04:01:26 +00:00
|
|
|
event, err := s.stripe.ConstructWebhookEvent(body.PeekedBytes, stripeSignature, s.config.StripeWebhookKey)
|
2023-01-16 04:29:46 +00:00
|
|
|
if err != nil {
|
2023-01-18 18:46:40 +00:00
|
|
|
return errHTTPBadRequestBillingRequestInvalid
|
2023-01-16 04:29:46 +00:00
|
|
|
} else if event.Data == nil || event.Data.Raw == nil {
|
2023-01-18 18:46:40 +00:00
|
|
|
return errHTTPBadRequestBillingRequestInvalid
|
2023-01-16 04:29:46 +00:00
|
|
|
}
|
|
|
|
log.Info("Stripe: webhook event %s received", event.Type)
|
|
|
|
switch event.Type {
|
|
|
|
case "customer.subscription.updated":
|
2023-01-16 21:35:37 +00:00
|
|
|
return s.handleAccountBillingWebhookSubscriptionUpdated(event.Data.Raw)
|
2023-01-16 04:29:46 +00:00
|
|
|
case "customer.subscription.deleted":
|
2023-01-16 21:35:37 +00:00
|
|
|
return s.handleAccountBillingWebhookSubscriptionDeleted(event.Data.Raw)
|
2023-01-16 04:29:46 +00:00
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-16 21:35:37 +00:00
|
|
|
func (s *Server) handleAccountBillingWebhookSubscriptionUpdated(event json.RawMessage) error {
|
2023-01-18 20:50:06 +00:00
|
|
|
r, err := util.UnmarshalJSON[apiStripeSubscriptionUpdatedEvent](io.NopCloser(bytes.NewReader(event)))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if r.ID == "" || r.Customer == "" || r.Status == "" || r.CurrentPeriodEnd == 0 || r.Items == nil || len(r.Items.Data) != 1 || r.Items.Data[0].Price == nil || r.Items.Data[0].Price.ID == "" {
|
2023-01-18 18:46:40 +00:00
|
|
|
return errHTTPBadRequestBillingRequestInvalid
|
2023-01-16 04:29:46 +00:00
|
|
|
}
|
2023-01-18 20:50:06 +00:00
|
|
|
subscriptionID, priceID := r.ID, r.Items.Data[0].Price.ID
|
|
|
|
log.Info("Stripe: customer %s: Updating subscription to status %s, with price %s", r.Customer, r.Status, priceID)
|
|
|
|
u, err := s.userManager.UserByStripeCustomer(r.Customer)
|
2023-01-16 04:29:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-18 20:50:06 +00:00
|
|
|
tier, err := s.userManager.TierByStripePrice(priceID)
|
2023-01-16 04:29:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-18 20:50:06 +00:00
|
|
|
if err := s.updateSubscriptionAndTier(u, r.Customer, subscriptionID, r.Status, r.CurrentPeriodEnd, r.CancelAt, tier.Code); err != nil {
|
2023-01-16 04:29:46 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-01-16 21:35:37 +00:00
|
|
|
s.publishSyncEventAsync(s.visitorFromUser(u, netip.IPv4Unspecified()))
|
2023-01-16 04:29:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-16 21:35:37 +00:00
|
|
|
func (s *Server) handleAccountBillingWebhookSubscriptionDeleted(event json.RawMessage) error {
|
2023-01-18 20:50:06 +00:00
|
|
|
r, err := util.UnmarshalJSON[apiStripeSubscriptionDeletedEvent](io.NopCloser(bytes.NewReader(event)))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if r.Customer == "" {
|
2023-01-18 18:46:40 +00:00
|
|
|
return errHTTPBadRequestBillingRequestInvalid
|
2023-01-16 04:29:46 +00:00
|
|
|
}
|
2023-01-18 20:50:06 +00:00
|
|
|
log.Info("Stripe: customer %s: subscription deleted, downgrading to unpaid tier", r.Customer)
|
|
|
|
u, err := s.userManager.UserByStripeCustomer(r.Customer)
|
2023-01-16 04:29:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-18 20:50:06 +00:00
|
|
|
if err := s.updateSubscriptionAndTier(u, r.Customer, "", "", 0, 0, ""); err != nil {
|
2023-01-16 04:29:46 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-01-17 15:09:37 +00:00
|
|
|
s.publishSyncEventAsync(s.visitorFromUser(u, netip.IPv4Unspecified()))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) updateSubscriptionAndTier(u *user.User, customerID, subscriptionID, status string, paidUntil, cancelAt int64, tier string) error {
|
|
|
|
u.Billing.StripeCustomerID = customerID
|
|
|
|
u.Billing.StripeSubscriptionID = subscriptionID
|
|
|
|
u.Billing.StripeSubscriptionStatus = stripe.SubscriptionStatus(status)
|
|
|
|
u.Billing.StripeSubscriptionPaidUntil = time.Unix(paidUntil, 0)
|
|
|
|
u.Billing.StripeSubscriptionCancelAt = time.Unix(cancelAt, 0)
|
|
|
|
if tier == "" {
|
|
|
|
if err := s.userManager.ResetTier(u.Name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := s.userManager.ChangeTier(u.Name, tier); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-01-16 04:29:46 +00:00
|
|
|
if err := s.userManager.ChangeBilling(u); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-01-18 20:50:06 +00:00
|
|
|
|
|
|
|
// 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.
|
2023-01-19 04:01:26 +00:00
|
|
|
func (s *Server) fetchStripePrices() (map[string]string, error) {
|
2023-01-18 20:50:06 +00:00
|
|
|
log.Debug("Caching prices from Stripe API")
|
2023-01-19 04:01:26 +00:00
|
|
|
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 {
|
2023-01-18 20:50:06 +00:00
|
|
|
if p.UnitAmount%100 == 0 {
|
2023-01-19 04:01:26 +00:00
|
|
|
priceMap[p.ID] = fmt.Sprintf("$%d", p.UnitAmount/100)
|
2023-01-18 20:50:06 +00:00
|
|
|
} else {
|
2023-01-19 04:01:26 +00:00
|
|
|
priceMap[p.ID] = fmt.Sprintf("$%.2f", float64(p.UnitAmount)/100)
|
2023-01-18 20:50:06 +00:00
|
|
|
}
|
2023-01-19 04:01:26 +00:00
|
|
|
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)
|
|
|
|
UpdateSubscription(id string, params *stripe.SubscriptionParams) (*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())
|
2023-01-18 20:50:06 +00:00
|
|
|
}
|
|
|
|
if iter.Err() != nil {
|
|
|
|
return nil, iter.Err()
|
|
|
|
}
|
|
|
|
return prices, nil
|
|
|
|
}
|
2023-01-19 04:01:26 +00:00
|
|
|
|
|
|
|
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) UpdateSubscription(id string, params *stripe.SubscriptionParams) (*stripe.Subscription, error) {
|
|
|
|
return subscription.Update(id, params)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *realStripeAPI) ConstructWebhookEvent(payload []byte, header string, secret string) (stripe.Event, error) {
|
|
|
|
return webhook.ConstructEvent(payload, header, secret)
|
|
|
|
}
|