Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d34fa3b

Browse files
satellite/{payments,console,web}: removed non-product-based logic
Removed all the non-product-based logic for the usage report, charges and satellite UI. Removed ProductBasedInvoicingEnabled config flag. Issue: storj/storj-private#1452 Change-Id: Ifa3619042e80edb7f06ff2407562de99dcfe45b4
1 parent 5c9e749 commit d34fa3b

24 files changed

+58
-871
lines changed

satellite/api.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,6 @@ func NewAPI(log *zap.Logger, full *identity.FullIdentity, db DB,
836836
return nil, errs.Combine(err, peer.Close())
837837
}
838838

839-
consoleConfig.ProductBasedInvoicing = config.Payments.StripeCoinPayments.ProductBasedInvoicing
840839
consoleConfig.SkuEnabled = config.Payments.StripeCoinPayments.SkuEnabled
841840

842841
peer.Console.Service, err = console.NewService(
@@ -923,7 +922,6 @@ func NewAPI(log *zap.Logger, full *identity.FullIdentity, db DB,
923922
config.Analytics,
924923
config.Payments.MinimumCharge,
925924
prices,
926-
config.Payments.StripeCoinPayments.ProductBasedInvoicing,
927925
)
928926

929927
peer.Servers.Add(lifecycle.Item{

satellite/console-api.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,6 @@ func NewConsoleAPI(log *zap.Logger, full *identity.FullIdentity, db DB,
556556
return nil, errs.Combine(err, peer.Close())
557557
}
558558

559-
consoleConfig.ProductBasedInvoicing = config.Payments.StripeCoinPayments.ProductBasedInvoicing
560559
consoleConfig.SkuEnabled = config.Payments.StripeCoinPayments.SkuEnabled
561560

562561
peer.Console.Service, err = console.NewService(
@@ -640,7 +639,6 @@ func NewConsoleAPI(log *zap.Logger, full *identity.FullIdentity, db DB,
640639
config.Analytics,
641640
config.Payments.MinimumCharge,
642641
prices,
643-
config.Payments.StripeCoinPayments.ProductBasedInvoicing,
644642
)
645643

646644
peer.Servers.Add(lifecycle.Item{

satellite/console/config.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ type Config struct {
5050
UseNewRestKeysTable bool `help:"whether to use the new rest keys table" default:"false"`
5151
NewDetailedUsageReportEnabled bool `help:"whether to use the new detailed usage report" default:"false"`
5252
NewAccountSetupEnabled bool `help:"whether to use new account setup flow" default:"false"`
53-
ProductBasedInvoicing bool `help:"whether to use product-based invoicing" default:"false" hidden:"true"`
5453
PricingPackagesEnabled bool `help:"whether to allow purchasing pricing packages" default:"true"`
5554
SkuEnabled bool `help:"whether we should use SKUs for product usages" default:"false" hidden:"true"`
5655
UserFeedbackEnabled bool `help:"whether user feedback is enabled" default:"false"`

satellite/console/consoleweb/config.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ type FrontendConfig struct {
9494
StorageMBMonthCents string `json:"storageMBMonthCents"`
9595
EgressMBCents string `json:"egressMBCents"`
9696
SegmentMonthCents string `json:"segmentMonthCents"`
97-
ProductBasedInvoicingEnabled bool `json:"productBasedInvoicingEnabled"`
9897
NewAccountSetupEnabled bool `json:"newAccountSetupEnabled"`
9998
UpgradePayUpfrontAmount int `json:"upgradePayUpfrontAmount"`
10099
UserFeedbackEnabled bool `json:"userFeedbackEnabled"`

satellite/console/consoleweb/consoleapi/payments.go

Lines changed: 7 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,17 @@ var (
3434

3535
// Payments is an api controller that exposes all payment related functionality.
3636
type Payments struct {
37-
log *zap.Logger
38-
service *console.Service
39-
accountFreezeService *console.AccountFreezeService
40-
productBasedInvoicingEnabled bool
37+
log *zap.Logger
38+
service *console.Service
39+
accountFreezeService *console.AccountFreezeService
4140
}
4241

4342
// NewPayments is a constructor for api payments controller.
44-
func NewPayments(log *zap.Logger, service *console.Service, accountFreezeService *console.AccountFreezeService,
45-
productBasedInvoicingEnabled bool) *Payments {
43+
func NewPayments(log *zap.Logger, service *console.Service, accountFreezeService *console.AccountFreezeService) *Payments {
4644
return &Payments{
47-
log: log,
48-
service: service,
49-
accountFreezeService: accountFreezeService,
50-
productBasedInvoicingEnabled: productBasedInvoicingEnabled,
45+
log: log,
46+
service: service,
47+
accountFreezeService: accountFreezeService,
5148
}
5249
}
5350

@@ -108,11 +105,6 @@ func (p *Payments) ProductCharges(w http.ResponseWriter, r *http.Request) {
108105

109106
w.Header().Set("Content-Type", "application/json")
110107

111-
if !p.productBasedInvoicingEnabled {
112-
p.serveJSONError(ctx, w, http.StatusNotImplemented, errs.New("product based invoicing is not enabled"))
113-
return
114-
}
115-
116108
sinceStamp, err := strconv.ParseInt(r.URL.Query().Get("from"), 10, 64)
117109
if err != nil {
118110
p.serveJSONError(ctx, w, http.StatusBadRequest, err)
@@ -153,67 +145,6 @@ func (p *Payments) ProductCharges(w http.ResponseWriter, r *http.Request) {
153145
}
154146
}
155147

156-
// ProjectsCharges returns how much money current user will be charged for each project which he owns.
157-
func (p *Payments) ProjectsCharges(w http.ResponseWriter, r *http.Request) {
158-
ctx := r.Context()
159-
var err error
160-
defer mon.Task()(&ctx)(&err)
161-
162-
w.Header().Set("Content-Type", "application/json")
163-
164-
sinceStamp, err := strconv.ParseInt(r.URL.Query().Get("from"), 10, 64)
165-
if err != nil {
166-
p.serveJSONError(ctx, w, http.StatusBadRequest, err)
167-
return
168-
}
169-
beforeStamp, err := strconv.ParseInt(r.URL.Query().Get("to"), 10, 64)
170-
if err != nil {
171-
p.serveJSONError(ctx, w, http.StatusBadRequest, err)
172-
return
173-
}
174-
175-
since := time.Unix(sinceStamp, 0).UTC()
176-
before := time.Unix(beforeStamp, 0).UTC()
177-
178-
charges, err := p.service.Payments().ProjectsCharges(ctx, since, before)
179-
if err != nil {
180-
p.handleServiceError(ctx, w, err)
181-
return
182-
}
183-
184-
shouldApplyMinimumCharge, err := p.service.Payments().ShouldApplyMinimumCharge(ctx)
185-
if err != nil {
186-
p.handleServiceError(ctx, w, err)
187-
return
188-
}
189-
190-
var response struct {
191-
PriceModels map[string]payments.ProjectUsagePriceModel `json:"priceModels"`
192-
Charges payments.ProjectChargesResponse `json:"charges"`
193-
ApplyMinimumCharge bool `json:"applyMinimumCharge"`
194-
}
195-
196-
response.Charges = charges
197-
response.PriceModels = make(map[string]payments.ProjectUsagePriceModel)
198-
response.ApplyMinimumCharge = shouldApplyMinimumCharge
199-
200-
seen := make(map[string]struct{})
201-
for _, partnerCharges := range charges {
202-
for partner := range partnerCharges {
203-
if _, ok := seen[partner]; ok {
204-
continue
205-
}
206-
response.PriceModels[partner] = *p.service.Payments().GetProjectUsagePriceModel(partner)
207-
seen[partner] = struct{}{}
208-
}
209-
}
210-
211-
err = json.NewEncoder(w).Encode(response)
212-
if err != nil {
213-
p.log.Error("failed to write json project usage and charges response", zap.Error(ErrPaymentsAPI.Wrap(err)))
214-
}
215-
}
216-
217148
func (p *Payments) handleServiceError(ctx context.Context, w http.ResponseWriter, err error) {
218149
if console.ErrUnauthorized.Has(err) {
219150
p.serveJSONError(ctx, w, http.StatusUnauthorized, err)

satellite/console/consoleweb/consoleapi/usagelimits_test.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@ import (
1919
"storj.io/common/macaroon"
2020
"storj.io/common/memory"
2121
"storj.io/common/pb"
22+
"storj.io/common/storj"
2223
"storj.io/common/testcontext"
2324
"storj.io/common/testrand"
2425
"storj.io/storj/private/testplanet"
2526
"storj.io/storj/satellite"
2627
"storj.io/storj/satellite/accounting"
2728
"storj.io/storj/satellite/console"
2829
"storj.io/storj/satellite/metabase"
30+
"storj.io/storj/satellite/nodeselection"
31+
"storj.io/storj/satellite/payments/paymentsconfig"
2932
)
3033

3134
func TestTotalUsageLimits(t *testing.T) {
@@ -174,12 +177,27 @@ func TestDailyUsage(t *testing.T) {
174177
}
175178

176179
func TestTotalUsageReport(t *testing.T) {
180+
var (
181+
productID = int32(1)
182+
productPrice = paymentsconfig.ProductUsagePrice{
183+
Name: "product1",
184+
ProjectUsagePrice: paymentsconfig.ProjectUsagePrice{
185+
StorageTB: "200000",
186+
EgressTB: "200000",
187+
Segment: "2",
188+
},
189+
}
190+
)
191+
177192
testplanet.Run(t, testplanet.Config{
178-
SatelliteCount: 1, StorageNodeCount: 1, UplinkCount: 1,
193+
SatelliteCount: 1, StorageNodeCount: 0, UplinkCount: 0,
179194
Reconfigure: testplanet.Reconfigure{
180195
Satellite: func(log *zap.Logger, index int, config *satellite.Config) {
181196
config.Console.OpenRegistrationEnabled = true
182197
config.Console.RateLimit.Burst = 10
198+
config.Placement = nodeselection.ConfigurablePlacementRule{PlacementRules: `0:annotation("location", "defaultPlacement")`}
199+
config.Payments.Products.SetMap(map[int32]paymentsconfig.ProductUsagePrice{productID: productPrice})
200+
config.Payments.PlacementPriceOverrides.SetMap(map[int]int32{int(storj.DefaultPlacement): productID})
183201
},
184202
},
185203
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {

satellite/console/consoleweb/endpoints_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ func TestPayments(t *testing.T) {
377377
"/payments/account/balance",
378378
"/payments/billing-history",
379379
"/payments/invoice-history",
380-
"/payments/account/charges?from=1619827200&to=1620844320",
380+
"/payments/account/product-charges?from=1619827200&to=1620844320",
381381
} {
382382
resp, body := test.request(http.MethodGet, path, nil)
383383
require.Contains(t, body, "unauthorized", path)
@@ -412,7 +412,7 @@ func TestPayments(t *testing.T) {
412412
}
413413

414414
{ // Get_AccountChargesByDateRange
415-
resp, body := test.request(http.MethodGet, "/payments/account/charges?from=1619827200&to=1620844320", nil)
415+
resp, body := test.request(http.MethodGet, "/payments/account/product-charges?from=1619827200&to=1620844320", nil)
416416
require.Contains(t, body, "egress")
417417
require.Equal(t, http.StatusOK, resp.StatusCode)
418418
}

satellite/console/consoleweb/server.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,6 @@ type Server struct {
187187

188188
stripePublicKey string
189189
neededTokenPaymentConfirmations int
190-
productBasedInvoicingEnabled bool
191190

192191
objectLockAndVersioningConfig console.ObjectLockAndVersioningConfig
193192

@@ -210,7 +209,7 @@ func NewServer(logger *zap.Logger, config Config, service *console.Service, cons
210209
accountFreezeService *console.AccountFreezeService, ssoService *sso.Service, csrfService *csrf.Service, listener net.Listener,
211210
stripePublicKey string, neededTokenPaymentConfirmations int, nodeURL storj.NodeURL,
212211
objectLockAndVersioningConfig console.ObjectLockAndVersioningConfig, analyticsConfig analytics.Config,
213-
minimumChargeConfig paymentsconfig.MinimumChargeConfig, usagePrices payments.ProjectUsagePriceModel, productBasedInvoicingEnabled bool) *Server {
212+
minimumChargeConfig paymentsconfig.MinimumChargeConfig, usagePrices payments.ProjectUsagePriceModel) *Server {
214213
initAdditionalMimeTypes()
215214

216215
server := Server{
@@ -226,7 +225,6 @@ func NewServer(logger *zap.Logger, config Config, service *console.Service, cons
226225
csrfService: csrfService,
227226
stripePublicKey: stripePublicKey,
228227
neededTokenPaymentConfirmations: neededTokenPaymentConfirmations,
229-
productBasedInvoicingEnabled: productBasedInvoicingEnabled,
230228
ipRateLimiter: web.NewIPRateLimiter(config.RateLimit, logger),
231229
userIDRateLimiter: NewUserIDRateLimiter(config.RateLimit, logger),
232230
addCardRateLimiter: NewAddCardRateLimiter(config.AddCardRateLimiter, logger),
@@ -385,7 +383,7 @@ func NewServer(logger *zap.Logger, config Config, service *console.Service, cons
385383
}
386384

387385
if config.BillingFeaturesEnabled {
388-
paymentController := consoleapi.NewPayments(logger, service, accountFreezeService, productBasedInvoicingEnabled)
386+
paymentController := consoleapi.NewPayments(logger, service, accountFreezeService)
389387
paymentsRouter := router.PathPrefix("/api/v0/payments").Subrouter()
390388
paymentsRouter.Use(server.withCORS)
391389
paymentsRouter.Use(server.withAuth)
@@ -401,7 +399,6 @@ func NewServer(logger *zap.Logger, config Config, service *console.Service, cons
401399
paymentsRouter.Handle("/cards", server.withCSRFProtection(http.HandlerFunc(paymentController.MakeCreditCardDefault))).Methods(http.MethodPatch, http.MethodOptions)
402400
paymentsRouter.HandleFunc("/cards", paymentController.ListCreditCards).Methods(http.MethodGet, http.MethodOptions)
403401
paymentsRouter.Handle("/cards/{cardId}", server.withCSRFProtection(http.HandlerFunc(paymentController.RemoveCreditCard))).Methods(http.MethodDelete, http.MethodOptions)
404-
paymentsRouter.HandleFunc("/account/charges", paymentController.ProjectsCharges).Methods(http.MethodGet, http.MethodOptions)
405402
paymentsRouter.HandleFunc("/account/product-charges", paymentController.ProductCharges).Methods(http.MethodGet, http.MethodOptions)
406403
paymentsRouter.HandleFunc("/account/balance", paymentController.AccountBalance).Methods(http.MethodGet, http.MethodOptions)
407404
paymentsRouter.HandleFunc("/account/billing-information", paymentController.GetBillingInformation).Methods(http.MethodGet, http.MethodOptions)
@@ -1122,7 +1119,6 @@ func (server *Server) frontendConfigHandler(w http.ResponseWriter, r *http.Reque
11221119
RestAPIKeysUIEnabled: server.config.RestAPIKeysUIEnabled && server.config.UseNewRestKeysTable,
11231120
ZkSyncContractAddress: server.config.ZkSyncContractAddress,
11241121
NewDetailedUsageReportEnabled: server.config.NewDetailedUsageReportEnabled,
1125-
ProductBasedInvoicingEnabled: server.productBasedInvoicingEnabled,
11261122
NewAccountSetupEnabled: server.config.NewAccountSetupEnabled,
11271123
UpgradePayUpfrontAmount: server.config.UpgradePayUpfrontAmount,
11281124
UserFeedbackEnabled: server.config.UserFeedbackEnabled,

satellite/console/service.go

Lines changed: 21 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -776,18 +776,6 @@ func (payment Payments) ProductCharges(ctx context.Context, since, before time.T
776776
return payment.service.accounts.ProductCharges(ctx, user.ID, since, before)
777777
}
778778

779-
// ProjectsCharges returns how much money current user will be charged for each project which he owns.
780-
func (payment Payments) ProjectsCharges(ctx context.Context, since, before time.Time) (_ payments.ProjectChargesResponse, err error) {
781-
defer mon.Task()(&ctx)(&err)
782-
783-
user, err := payment.service.getUserAndAuditLog(ctx, "project charges")
784-
if err != nil {
785-
return nil, Error.Wrap(err)
786-
}
787-
788-
return payment.service.accounts.ProjectCharges(ctx, user.ID, since, before)
789-
}
790-
791779
// ShouldApplyMinimumCharge checks if the minimum charge should be applied to the user.
792780
func (payment Payments) ShouldApplyMinimumCharge(ctx context.Context) (bool, error) {
793781
var err error
@@ -5295,27 +5283,24 @@ func (s *Service) GetUsageReport(ctx context.Context, param GetUsageReportParam)
52955283
UserAgent: p.UserAgent,
52965284
}
52975285

5298-
var priceModel payments.ProductUsagePriceModel
5299-
if s.config.ProductBasedInvoicing {
5300-
_, priceModel = s.accounts.ProductIdAndPriceForUsageKey(key)
5286+
_, priceModel := s.accounts.ProductIdAndPriceForUsageKey(key)
53015287

5302-
partner := ""
5303-
placement := int(storj.DefaultPlacement)
5288+
partner := ""
5289+
placement := int(storj.DefaultPlacement)
53045290

5305-
// Split the key to extract partner and placement.
5306-
parts := strings.Split(key, "|")
5307-
if len(parts) >= 1 {
5308-
partner = parts[0]
5309-
}
5310-
if len(parts) >= 2 {
5311-
placement64, err := strconv.ParseInt(parts[1], 10, 32)
5312-
if err == nil {
5313-
placement = int(placement64)
5314-
}
5291+
// Split the key to extract partner and placement.
5292+
parts := strings.Split(key, "|")
5293+
if len(parts) >= 1 {
5294+
partner = parts[0]
5295+
}
5296+
if len(parts) >= 2 {
5297+
placement64, err := strconv.ParseInt(parts[1], 10, 32)
5298+
if err == nil {
5299+
placement = int(placement64)
53155300
}
5316-
item.Placement = storj.PlacementConstraint(placement)
5317-
item.UserAgent = []byte(partner)
53185301
}
5302+
item.Placement = storj.PlacementConstraint(placement)
5303+
item.UserAgent = []byte(partner)
53195304

53205305
item, err = s.transformProjectReportItem(item, param.IncludeCost, priceModel)
53215306
if err != nil {
@@ -5351,15 +5336,15 @@ func (s *Service) GetReportRow(param GetUsageReportParam, reportItem accounting.
53515336
if !param.GroupByProject {
53525337
row = append(row, reportItem.BucketName)
53535338
}
5354-
if s.config.ProductBasedInvoicing && s.config.SkuEnabled {
5339+
if s.config.SkuEnabled {
53555340
row = append(row, reportItem.StorageSKU)
53565341
}
53575342
row = append(row, fmt.Sprintf("%f", reportItem.Storage))
53585343
row = append(row, fmt.Sprintf("%f", reportItem.StorageTbMonth))
53595344
if param.IncludeCost {
53605345
row = append(row, fmt.Sprintf("%f", reportItem.StorageCost))
53615346
}
5362-
if s.config.ProductBasedInvoicing && s.config.SkuEnabled {
5347+
if s.config.SkuEnabled {
53635348
row = append(row, reportItem.EgressSKU)
53645349
}
53655350
row = append(row, fmt.Sprintf("%f", reportItem.Egress))
@@ -5368,7 +5353,7 @@ func (s *Service) GetReportRow(param GetUsageReportParam, reportItem accounting.
53685353
row = append(row, fmt.Sprintf("%f", reportItem.EgressCost))
53695354
}
53705355
row = append(row, fmt.Sprintf("%f", reportItem.ObjectCount))
5371-
if s.config.ProductBasedInvoicing && s.config.SkuEnabled {
5356+
if s.config.SkuEnabled {
53725357
row = append(row, reportItem.SegmentSKU)
53735358
}
53745359
row = append(row, fmt.Sprintf("%f", reportItem.SegmentCount))
@@ -5399,7 +5384,7 @@ func (s *Service) GetUsageReportHeaders(param GetUsageReportParam) (disclaimer [
53995384
"Segment Price (Estimated)", "Estimated Total Amount", "Since", "Before",
54005385
}
54015386

5402-
if !s.config.ProductBasedInvoicing || !s.config.SkuEnabled {
5387+
if !s.config.SkuEnabled {
54035388
updateHeaders := make([]string, 0, len(headers)-4)
54045389
for _, header := range headers {
54055390
if strings.Contains(header, "SKU") {
@@ -5438,15 +5423,9 @@ func (s *Service) GetUsageReportHeaders(param GetUsageReportParam) (disclaimer [
54385423
func (s *Service) transformProjectReportItem(item accounting.ProjectReportItem, addCost bool, priceModel payments.ProductUsagePriceModel) (_ accounting.ProjectReportItem, err error) {
54395424
hoursPerMonthDecimal := decimal.NewFromInt(hoursPerMonth)
54405425
if priceModel == (payments.ProductUsagePriceModel{}) {
5441-
if s.config.ProductBasedInvoicing {
5442-
_, priceModel, err = s.accounts.GetPartnerPlacementPriceModel(string(item.UserAgent), item.Placement)
5443-
if err != nil {
5444-
return accounting.ProjectReportItem{}, err
5445-
}
5446-
} else {
5447-
priceModel = payments.ProductUsagePriceModel{
5448-
ProjectUsagePriceModel: s.accounts.GetProjectUsagePriceModel(string(item.UserAgent)),
5449-
}
5426+
_, priceModel, err = s.accounts.GetPartnerPlacementPriceModel(string(item.UserAgent), item.Placement)
5427+
if err != nil {
5428+
return accounting.ProjectReportItem{}, err
54505429
}
54515430
}
54525431
item.ProductName = priceModel.ProductName

0 commit comments

Comments
 (0)