2019-10-15 12:23:54 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package stripecoinpayments
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-11-15 14:27:44 +00:00
|
|
|
"time"
|
2019-10-15 12:23:54 +01:00
|
|
|
|
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
|
|
|
"github.com/stripe/stripe-go"
|
|
|
|
|
2019-11-15 14:27:44 +00:00
|
|
|
"storj.io/storj/private/date"
|
|
|
|
"storj.io/storj/private/memory"
|
2019-10-15 12:23:54 +01:00
|
|
|
"storj.io/storj/satellite/payments"
|
|
|
|
)
|
|
|
|
|
2019-11-15 14:27:44 +00:00
|
|
|
// ensures that accounts implements payments.Accounts.
|
|
|
|
var _ payments.Accounts = (*accounts)(nil)
|
|
|
|
|
2019-10-15 12:23:54 +01:00
|
|
|
// accounts is an implementation of payments.Accounts.
|
|
|
|
type accounts struct {
|
|
|
|
service *Service
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreditCards exposes all needed functionality to manage account credit cards.
|
|
|
|
func (accounts *accounts) CreditCards() payments.CreditCards {
|
|
|
|
return &creditCards{service: accounts.service}
|
|
|
|
}
|
|
|
|
|
2019-10-31 16:56:54 +00:00
|
|
|
// Invoices exposes all needed functionality to manage account invoices.
|
|
|
|
func (accounts *accounts) Invoices() payments.Invoices {
|
|
|
|
return &invoices{service: accounts.service}
|
|
|
|
}
|
|
|
|
|
2019-10-15 12:23:54 +01:00
|
|
|
// Setup creates a payment account for the user.
|
2019-10-17 15:42:18 +01:00
|
|
|
// If account is already set up it will return nil.
|
|
|
|
func (accounts *accounts) Setup(ctx context.Context, userID uuid.UUID, email string) (err error) {
|
|
|
|
defer mon.Task()(&ctx, userID, email)(&err)
|
|
|
|
|
2019-11-05 13:16:02 +00:00
|
|
|
_, err = accounts.service.db.Customers().GetCustomerID(ctx, userID)
|
2019-10-17 15:42:18 +01:00
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2019-10-15 12:23:54 +01:00
|
|
|
|
|
|
|
params := &stripe.CustomerParams{
|
|
|
|
Email: stripe.String(email),
|
|
|
|
}
|
|
|
|
|
2019-10-23 18:33:24 +01:00
|
|
|
customer, err := accounts.service.stripeClient.Customers.New(params)
|
|
|
|
if err != nil {
|
2019-10-17 15:04:50 +01:00
|
|
|
return Error.Wrap(err)
|
2019-10-15 12:23:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: delete customer from stripe, if db insertion fails
|
2019-11-05 13:16:02 +00:00
|
|
|
return Error.Wrap(accounts.service.db.Customers().Insert(ctx, userID, customer.ID))
|
2019-10-15 12:23:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Balance returns an integer amount in cents that represents the current balance of payment account.
|
2019-10-17 15:42:18 +01:00
|
|
|
func (accounts *accounts) Balance(ctx context.Context, userID uuid.UUID) (_ int64, err error) {
|
|
|
|
defer mon.Task()(&ctx, userID)(&err)
|
2019-10-15 12:23:54 +01:00
|
|
|
|
2019-11-05 13:16:02 +00:00
|
|
|
customerID, err := accounts.service.db.Customers().GetCustomerID(ctx, userID)
|
2019-10-15 12:23:54 +01:00
|
|
|
if err != nil {
|
2019-10-17 15:04:50 +01:00
|
|
|
return 0, Error.Wrap(err)
|
2019-10-15 12:23:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
c, err := accounts.service.stripeClient.Customers.Get(customerID, nil)
|
|
|
|
if err != nil {
|
2019-10-17 15:04:50 +01:00
|
|
|
return 0, Error.Wrap(err)
|
2019-10-15 12:23:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.Balance, nil
|
|
|
|
}
|
2019-10-17 15:04:50 +01:00
|
|
|
|
2019-11-15 14:27:44 +00:00
|
|
|
// ProjectCharges returns how much money current user will be charged for each project.
|
|
|
|
func (accounts *accounts) ProjectCharges(ctx context.Context, userID uuid.UUID) (charges []payments.ProjectCharge, err error) {
|
|
|
|
defer mon.Task()(&ctx, userID)(&err)
|
|
|
|
|
|
|
|
// to return empty slice instead of nil if there are no projects
|
|
|
|
charges = make([]payments.ProjectCharge, 0)
|
|
|
|
|
|
|
|
projects, err := accounts.service.projectsDB.GetOwn(ctx, userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
start, end := date.MonthBoundary(time.Now().UTC())
|
|
|
|
|
|
|
|
for _, project := range projects {
|
|
|
|
usage, err := accounts.service.usageDB.GetProjectTotal(ctx, project.ID, start, end)
|
|
|
|
if err != nil {
|
|
|
|
return charges, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
charges = append(charges, payments.ProjectCharge{
|
|
|
|
ProjectID: project.ID,
|
|
|
|
Egress: usage.Egress / int64(memory.TB) * accounts.service.EgressPrice,
|
|
|
|
// TODO: check precision
|
|
|
|
ObjectCount: int64(usage.ObjectCount) * accounts.service.PerObjectPrice,
|
|
|
|
StorageGbHrs: int64(usage.Storage) / int64(memory.TB) * accounts.service.TBhPrice,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return charges, nil
|
|
|
|
}
|
|
|
|
|
2019-10-17 15:04:50 +01:00
|
|
|
// StorjTokens exposes all storj token related functionality.
|
|
|
|
func (accounts *accounts) StorjTokens() payments.StorjTokens {
|
|
|
|
return &storjTokens{service: accounts.service}
|
|
|
|
}
|