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/stripe/stripe-go"
|
|
|
|
|
2020-03-30 10:08:50 +01:00
|
|
|
"storj.io/common/uuid"
|
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.
|
2020-01-29 00:57:15 +00:00
|
|
|
//
|
|
|
|
// architecture: Service
|
2019-10-15 12:23:54 +01:00
|
|
|
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),
|
|
|
|
}
|
|
|
|
|
2020-05-15 09:46:41 +01:00
|
|
|
customer, err := accounts.service.stripeClient.Customers().New(params)
|
2019-10-23 18:33:24 +01:00
|
|
|
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.
|
2020-05-12 18:16:04 +01:00
|
|
|
func (accounts *accounts) Balance(ctx context.Context, userID uuid.UUID) (_ payments.Balance, err error) {
|
2019-10-17 15:42:18 +01:00
|
|
|
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 {
|
2020-05-12 18:16:04 +01:00
|
|
|
return payments.Balance{}, Error.Wrap(err)
|
2019-10-15 12:23:54 +01:00
|
|
|
}
|
|
|
|
|
2020-05-15 09:46:41 +01:00
|
|
|
c, err := accounts.service.stripeClient.Customers().Get(customerID, nil)
|
2019-10-15 12:23:54 +01:00
|
|
|
if err != nil {
|
2020-05-12 18:16:04 +01:00
|
|
|
return payments.Balance{}, Error.Wrap(err)
|
2019-10-15 12:23:54 +01:00
|
|
|
}
|
|
|
|
|
2019-11-26 17:58:51 +00:00
|
|
|
// add all active coupons amount to balance.
|
2020-01-07 10:41:19 +00:00
|
|
|
coupons, err := accounts.service.db.Coupons().ListByUserIDAndStatus(ctx, userID, payments.CouponActive)
|
2019-11-26 17:58:51 +00:00
|
|
|
if err != nil {
|
2020-05-12 18:16:04 +01:00
|
|
|
return payments.Balance{}, Error.Wrap(err)
|
2019-11-26 17:58:51 +00:00
|
|
|
}
|
|
|
|
|
2020-01-07 10:41:19 +00:00
|
|
|
var couponsAmount int64 = 0
|
2019-11-26 17:58:51 +00:00
|
|
|
for _, coupon := range coupons {
|
2020-01-07 10:41:19 +00:00
|
|
|
alreadyUsed, err := accounts.service.db.Coupons().TotalUsage(ctx, coupon.ID)
|
|
|
|
if err != nil {
|
2020-05-12 18:16:04 +01:00
|
|
|
return payments.Balance{}, Error.Wrap(err)
|
2020-01-07 10:41:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
couponsAmount += coupon.Amount - alreadyUsed
|
|
|
|
}
|
|
|
|
|
2020-05-12 18:16:04 +01:00
|
|
|
accountBalance := payments.Balance{
|
|
|
|
FreeCredits: couponsAmount,
|
2020-07-23 13:36:56 +01:00
|
|
|
Coins: -c.Balance,
|
2020-05-12 18:16:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return accountBalance, nil
|
2020-01-07 10:41:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-15 14:27:44 +00:00
|
|
|
// ProjectCharges returns how much money current user will be charged for each project.
|
2020-03-04 13:23:10 +00:00
|
|
|
func (accounts *accounts) ProjectCharges(ctx context.Context, userID uuid.UUID, since, before time.Time) (charges []payments.ProjectCharge, err error) {
|
|
|
|
defer mon.Task()(&ctx, userID, since, before)(&err)
|
2019-11-15 14:27:44 +00:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, project := range projects {
|
2020-03-04 13:23:10 +00:00
|
|
|
usage, err := accounts.service.usageDB.GetProjectTotal(ctx, project.ID, since, before)
|
2019-11-15 14:27:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return charges, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2020-01-28 23:36:54 +00:00
|
|
|
projectPrice := accounts.service.calculateProjectUsagePrice(usage.Egress, usage.Storage, usage.ObjectCount)
|
|
|
|
|
2019-11-15 14:27:44 +00:00
|
|
|
charges = append(charges, payments.ProjectCharge{
|
2020-03-04 13:23:10 +00:00
|
|
|
ProjectUsage: *usage,
|
|
|
|
|
2020-01-28 23:36:54 +00:00
|
|
|
ProjectID: project.ID,
|
|
|
|
Egress: projectPrice.Egress.IntPart(),
|
|
|
|
ObjectCount: projectPrice.Objects.IntPart(),
|
|
|
|
StorageGbHrs: projectPrice.Storage.IntPart(),
|
2019-11-15 14:27:44 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return charges, nil
|
|
|
|
}
|
|
|
|
|
2020-01-03 14:21:05 +00:00
|
|
|
// Charges returns list of all credit card charges related to account.
|
|
|
|
func (accounts *accounts) Charges(ctx context.Context, userID uuid.UUID) (_ []payments.Charge, err error) {
|
|
|
|
defer mon.Task()(&ctx, userID)(&err)
|
|
|
|
|
|
|
|
customerID, err := accounts.service.db.Customers().GetCustomerID(ctx, userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
params := &stripe.ChargeListParams{
|
|
|
|
Customer: stripe.String(customerID),
|
|
|
|
}
|
|
|
|
params.Filters.AddFilter("limit", "", "100")
|
|
|
|
|
2020-05-15 09:46:41 +01:00
|
|
|
iter := accounts.service.stripeClient.Charges().List(params)
|
2020-01-03 14:21:05 +00:00
|
|
|
|
|
|
|
var charges []payments.Charge
|
|
|
|
for iter.Next() {
|
|
|
|
charge := iter.Charge()
|
|
|
|
|
|
|
|
// ignore all non credit card charges
|
|
|
|
if charge.PaymentMethodDetails.Type != stripe.ChargePaymentMethodDetailsTypeCard {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if charge.PaymentMethodDetails.Card == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
charges = append(charges, payments.Charge{
|
|
|
|
ID: charge.ID,
|
|
|
|
Amount: charge.Amount,
|
|
|
|
CardInfo: payments.CardInfo{
|
|
|
|
ID: charge.PaymentMethod,
|
|
|
|
Brand: string(charge.PaymentMethodDetails.Card.Brand),
|
|
|
|
LastFour: charge.PaymentMethodDetails.Card.Last4,
|
|
|
|
},
|
|
|
|
CreatedAt: time.Unix(charge.Created, 0).UTC(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = iter.Err(); err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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}
|
|
|
|
}
|
2020-01-29 00:57:15 +00:00
|
|
|
|
|
|
|
// Coupons exposes all needed functionality to manage coupons.
|
|
|
|
func (accounts *accounts) Coupons() payments.Coupons {
|
|
|
|
return &coupons{service: accounts.service}
|
|
|
|
}
|
2020-01-24 13:38:53 +00:00
|
|
|
|
2020-07-10 14:05:17 +01:00
|
|
|
// PaywallEnabled returns a true if a credit card or account
|
|
|
|
// balance is required to create projects.
|
|
|
|
func (accounts *accounts) PaywallEnabled(userID uuid.UUID) bool {
|
|
|
|
return BytesAreWithinProportion(userID, accounts.service.PaywallProportion)
|
|
|
|
}
|
|
|
|
|
|
|
|
//BytesAreWithinProportion returns true if first byte is less than the normalized proportion [0..1].
|
|
|
|
func BytesAreWithinProportion(uuidBytes [16]byte, proportion float64) bool {
|
|
|
|
return int(uuidBytes[0]) < int(proportion*256)
|
|
|
|
}
|