2020-03-16 19:34:15 +00:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2020-03-31 17:49:16 +01:00
|
|
|
"storj.io/common/uuid"
|
2020-03-16 19:34:15 +00:00
|
|
|
"storj.io/storj/private/dbutil"
|
|
|
|
"storj.io/storj/satellite"
|
|
|
|
"storj.io/storj/satellite/payments"
|
|
|
|
"storj.io/storj/satellite/payments/stripecoinpayments"
|
|
|
|
"storj.io/storj/satellite/satellitedb"
|
|
|
|
"storj.io/storj/satellite/satellitedb/dbx"
|
|
|
|
)
|
|
|
|
|
|
|
|
// UserData contains the uuid and email of a satellite user.
|
|
|
|
type UserData struct {
|
2020-03-31 17:49:16 +01:00
|
|
|
ID uuid.UUID
|
2020-03-16 19:34:15 +00:00
|
|
|
Email string
|
|
|
|
}
|
|
|
|
|
|
|
|
// generateStripeCustomers creates missing stripe-customers for users in our database.
|
|
|
|
func generateStripeCustomers(ctx context.Context) (err error) {
|
|
|
|
//Open SatelliteDB for the Payment Service
|
2020-03-16 21:53:12 +00:00
|
|
|
logger := zap.L()
|
|
|
|
db, err := satellitedb.New(logger.Named("db"), runCfg.Database, satellitedb.Options{})
|
2020-03-16 19:34:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return errs.New("error connecting to master database on satellite: %+v", err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err = errs.Combine(err, db.Close())
|
|
|
|
}()
|
|
|
|
|
|
|
|
//Open direct DB connection to execute custom queries
|
|
|
|
driver, source, implementation, err := dbutil.SplitConnStr(runCfg.Database)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if implementation != dbutil.Postgres && implementation != dbutil.Cockroach {
|
|
|
|
return errs.New("unsupported driver %q", driver)
|
|
|
|
}
|
|
|
|
|
|
|
|
dbxDB, err := dbx.Open(driver, source)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-16 21:53:12 +00:00
|
|
|
logger.Debug("Connected to:", zap.String("db source", source))
|
2020-03-16 19:34:15 +00:00
|
|
|
defer func() {
|
|
|
|
err = errs.Combine(err, dbxDB.Close())
|
|
|
|
}()
|
|
|
|
|
|
|
|
handler, err := setupPayments(zap.L().Named("payments"), db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rows, err := dbxDB.Query(ctx, "SELECT id, email FROM users WHERE id NOT IN (SELECT user_id from stripe_customers)")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err = errs.Combine(err, rows.Close())
|
|
|
|
}()
|
2020-03-16 21:53:12 +00:00
|
|
|
var n int64
|
2020-03-16 19:34:15 +00:00
|
|
|
for rows.Next() {
|
2020-03-16 21:53:12 +00:00
|
|
|
n++
|
2020-03-16 19:34:15 +00:00
|
|
|
var user UserData
|
2020-03-16 21:53:12 +00:00
|
|
|
err := rows.Scan(&user.ID, &user.Email)
|
2020-03-16 19:34:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-31 17:49:16 +01:00
|
|
|
|
|
|
|
err = handler.Setup(ctx, user.ID, user.Email)
|
2020-03-16 19:34:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-03-16 21:53:12 +00:00
|
|
|
logger.Info("Ensured Stripe-Customer", zap.Int64("created", n))
|
2020-03-16 19:34:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupPayments(log *zap.Logger, db satellite.DB) (handler payments.Accounts, err error) {
|
|
|
|
pc := runCfg.Payments
|
|
|
|
service, err := stripecoinpayments.NewService(
|
|
|
|
log.Named("payments.stripe:service"),
|
|
|
|
pc.StripeCoinPayments,
|
|
|
|
db.StripeCoinPayments(),
|
|
|
|
db.Console().Projects(),
|
|
|
|
db.ProjectAccounting(),
|
|
|
|
pc.StorageTBPrice,
|
|
|
|
pc.EgressTBPrice,
|
|
|
|
pc.ObjectPrice,
|
|
|
|
pc.BonusRate,
|
|
|
|
pc.CouponValue,
|
|
|
|
pc.CouponDuration,
|
|
|
|
pc.CouponProjectLimit,
|
|
|
|
pc.MinCoinPayment)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
handler = service.Accounts()
|
|
|
|
|
|
|
|
return handler, err
|
|
|
|
}
|