2020-06-12 15:47:16 +01:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2020-07-27 20:43:47 +01:00
|
|
|
"storj.io/common/storj"
|
2020-06-12 15:47:16 +01:00
|
|
|
"storj.io/common/uuid"
|
|
|
|
"storj.io/storj/satellite"
|
|
|
|
"storj.io/storj/satellite/payments/stripecoinpayments"
|
|
|
|
"storj.io/storj/satellite/satellitedb"
|
|
|
|
)
|
|
|
|
|
2021-03-04 22:55:48 +00:00
|
|
|
func runBillingCmd(ctx context.Context, cmdFunc func(context.Context, *stripecoinpayments.Service, satellite.DB) error) error {
|
2020-06-12 15:47:16 +01:00
|
|
|
// Open SatelliteDB for the Payment Service
|
|
|
|
logger := zap.L()
|
2020-12-04 10:24:39 +00:00
|
|
|
db, err := satellitedb.Open(ctx, logger.Named("db"), runCfg.Database, satellitedb.Options{ApplicationName: "satellite-billing"})
|
2020-06-12 15:47:16 +01:00
|
|
|
if err != nil {
|
|
|
|
return errs.New("error connecting to master database on satellite: %+v", err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err = errs.Combine(err, db.Close())
|
|
|
|
}()
|
|
|
|
|
|
|
|
payments, err := setupPayments(logger, db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-04 22:55:48 +00:00
|
|
|
return cmdFunc(ctx, payments, db)
|
2020-06-12 15:47:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func setupPayments(log *zap.Logger, db satellite.DB) (*stripecoinpayments.Service, error) {
|
|
|
|
pc := runCfg.Payments
|
|
|
|
|
2020-06-15 11:54:42 +01:00
|
|
|
var stripeClient stripecoinpayments.StripeClient
|
|
|
|
switch pc.Provider {
|
|
|
|
default:
|
2020-10-08 18:14:09 +01:00
|
|
|
stripeClient = stripecoinpayments.NewStripeMock(
|
|
|
|
storj.NodeID{},
|
|
|
|
db.StripeCoinPayments().Customers(),
|
|
|
|
db.Console().Users(),
|
|
|
|
)
|
2020-06-15 11:54:42 +01:00
|
|
|
case "stripecoinpayments":
|
|
|
|
stripeClient = stripecoinpayments.NewStripeClient(log, pc.StripeCoinPayments)
|
|
|
|
}
|
2020-06-12 15:47:16 +01:00
|
|
|
|
|
|
|
return stripecoinpayments.NewService(
|
|
|
|
log.Named("payments.stripe:service"),
|
|
|
|
stripeClient,
|
|
|
|
pc.StripeCoinPayments,
|
|
|
|
db.StripeCoinPayments(),
|
|
|
|
db.Console().Projects(),
|
|
|
|
db.ProjectAccounting(),
|
|
|
|
pc.StorageTBPrice,
|
|
|
|
pc.EgressTBPrice,
|
|
|
|
pc.ObjectPrice,
|
|
|
|
pc.BonusRate,
|
|
|
|
pc.CouponValue,
|
2021-03-30 00:37:46 +01:00
|
|
|
pc.CouponDuration.IntPointer(),
|
2020-06-12 15:47:16 +01:00
|
|
|
pc.CouponProjectLimit,
|
2021-06-28 23:57:41 +01:00
|
|
|
pc.MinCoinPayment)
|
2020-06-12 15:47:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// parseBillingPeriodFromString parses provided date string and returns corresponding time.Time.
|
|
|
|
func parseBillingPeriod(s string) (time.Time, error) {
|
|
|
|
values := strings.Split(s, "/")
|
|
|
|
|
|
|
|
if len(values) != 2 {
|
|
|
|
return time.Time{}, errs.New("invalid date format %s, use mm/yyyy", s)
|
|
|
|
}
|
|
|
|
|
|
|
|
month, err := strconv.ParseInt(values[0], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return time.Time{}, errs.New("can not parse month: %v", err)
|
|
|
|
}
|
|
|
|
year, err := strconv.ParseInt(values[1], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return time.Time{}, errs.New("can not parse year: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
date := time.Date(int(year), time.Month(month), 1, 0, 0, 0, 0, time.UTC)
|
|
|
|
if date.Year() != int(year) || date.Month() != time.Month(month) || date.Day() != 1 {
|
|
|
|
return date, errs.New("dates mismatch have %s result %s", s, date)
|
|
|
|
}
|
|
|
|
|
|
|
|
return date, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// userData contains the uuid and email of a satellite user.
|
|
|
|
type userData struct {
|
|
|
|
ID uuid.UUID
|
|
|
|
Email string
|
|
|
|
}
|
|
|
|
|
|
|
|
// generateStripeCustomers creates missing stripe-customers for users in our database.
|
|
|
|
func generateStripeCustomers(ctx context.Context) (err error) {
|
2021-03-04 22:55:48 +00:00
|
|
|
return runBillingCmd(ctx, func(ctx context.Context, payments *stripecoinpayments.Service, db satellite.DB) error {
|
2020-06-12 15:47:16 +01:00
|
|
|
accounts := payments.Accounts()
|
|
|
|
|
2021-03-04 22:55:48 +00:00
|
|
|
cusDB := db.StripeCoinPayments().Customers().Raw()
|
|
|
|
|
|
|
|
rows, err := cusDB.Query(ctx, "SELECT id, email FROM users WHERE id NOT IN (SELECT user_id from stripe_customers) AND users.status=1")
|
2020-06-12 15:47:16 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err = errs.Combine(err, rows.Close())
|
|
|
|
}()
|
|
|
|
|
|
|
|
var n int64
|
|
|
|
for rows.Next() {
|
|
|
|
n++
|
|
|
|
var user userData
|
|
|
|
err := rows.Scan(&user.ID, &user.Email)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = accounts.Setup(ctx, user.ID, user.Email)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
zap.L().Info("Ensured Stripe-Customer", zap.Int64("created", n))
|
|
|
|
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|