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
|
|
|
|
2021-06-22 01:09:56 +01:00
|
|
|
"github.com/stripe/stripe-go/v72"
|
2022-04-28 16:59:55 +01:00
|
|
|
"github.com/zeebo/errs"
|
2019-10-15 12:23:54 +01:00
|
|
|
|
2020-03-30 10:08:50 +01:00
|
|
|
"storj.io/common/uuid"
|
2023-02-23 16:27:37 +00:00
|
|
|
"storj.io/storj/satellite/accounting"
|
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}
|
|
|
|
}
|
|
|
|
|
2023-03-24 22:25:36 +00:00
|
|
|
// Balances exposes all needed functionality to manage account balances.
|
|
|
|
func (accounts *accounts) Balances() payments.Balances {
|
|
|
|
return &balances{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.
|
2021-10-26 14:30:19 +01:00
|
|
|
func (accounts *accounts) Setup(ctx context.Context, userID uuid.UUID, email string, signupPromoCode string) (couponType payments.CouponType, err error) {
|
2019-10-17 15:42:18 +01:00
|
|
|
defer mon.Task()(&ctx, userID, email)(&err)
|
|
|
|
|
2021-10-26 14:30:19 +01:00
|
|
|
couponType = payments.FreeTierCoupon
|
|
|
|
|
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 {
|
2021-10-26 14:30:19 +01:00
|
|
|
return couponType, nil
|
2019-10-17 15:42:18 +01:00
|
|
|
}
|
2019-10-15 12:23:54 +01:00
|
|
|
|
|
|
|
params := &stripe.CustomerParams{
|
2023-03-14 02:59:24 +00:00
|
|
|
Params: stripe.Params{Context: ctx},
|
|
|
|
Email: stripe.String(email),
|
2019-10-15 12:23:54 +01:00
|
|
|
}
|
2021-10-26 14:30:19 +01:00
|
|
|
|
|
|
|
if signupPromoCode == "" {
|
|
|
|
|
|
|
|
params.Coupon = stripe.String(accounts.service.StripeFreeTierCouponID)
|
|
|
|
|
|
|
|
customer, err := accounts.service.stripeClient.Customers().New(params)
|
|
|
|
if err != nil {
|
|
|
|
return couponType, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: delete customer from stripe, if db insertion fails
|
|
|
|
return couponType, Error.Wrap(accounts.service.db.Customers().Insert(ctx, userID, customer.ID))
|
|
|
|
}
|
|
|
|
|
|
|
|
promoCodeIter := accounts.service.stripeClient.PromoCodes().List(&stripe.PromotionCodeListParams{
|
2023-03-14 02:59:24 +00:00
|
|
|
ListParams: stripe.ListParams{Context: ctx},
|
|
|
|
Code: stripe.String(signupPromoCode),
|
2021-10-26 14:30:19 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
var promoCode *stripe.PromotionCode
|
|
|
|
|
|
|
|
if promoCodeIter.Next() {
|
|
|
|
promoCode = promoCodeIter.PromotionCode()
|
|
|
|
} else {
|
|
|
|
couponType = payments.NoCoupon
|
|
|
|
}
|
|
|
|
|
|
|
|
// If signup promo code is provided, apply this on account creation.
|
|
|
|
// If a free tier coupon is provided with no signup promo code, apply this on account creation.
|
|
|
|
if promoCode != nil && promoCode.Coupon != nil {
|
|
|
|
params.Coupon = stripe.String(promoCode.Coupon.ID)
|
|
|
|
couponType = payments.SignupCoupon
|
|
|
|
} else if accounts.service.StripeFreeTierCouponID != "" {
|
2021-05-10 18:12:05 +01:00
|
|
|
params.Coupon = stripe.String(accounts.service.StripeFreeTierCouponID)
|
|
|
|
}
|
2019-10-15 12:23:54 +01:00
|
|
|
|
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 {
|
2021-10-26 14:30:19 +01:00
|
|
|
return couponType, Error.Wrap(err)
|
2019-10-15 12:23:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: delete customer from stripe, if db insertion fails
|
2021-10-26 14:30:19 +01:00
|
|
|
return couponType, Error.Wrap(accounts.service.db.Customers().Insert(ctx, userID, customer.ID))
|
2019-10-15 12:23:54 +01:00
|
|
|
}
|
|
|
|
|
2023-03-22 20:23:44 +00:00
|
|
|
// UpdatePackage updates a customer's package plan information.
|
|
|
|
func (accounts *accounts) UpdatePackage(ctx context.Context, userID uuid.UUID, packagePlan *string, timestamp *time.Time) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
_, err = accounts.service.db.Customers().UpdatePackage(ctx, userID, packagePlan, timestamp)
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPackageInfo returns the package plan and time of purchase for a user.
|
|
|
|
func (accounts *accounts) GetPackageInfo(ctx context.Context, userID uuid.UUID) (packagePlan *string, purchaseTime *time.Time, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
packagePlan, purchaseTime, err = accounts.service.db.Customers().GetPackageInfo(ctx, userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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 {
|
2023-02-23 16:27:37 +00:00
|
|
|
totalUsage := accounting.ProjectUsage{Since: since, Before: before}
|
|
|
|
|
|
|
|
usages, err := accounts.service.usageDB.GetProjectTotalByPartner(ctx, project.ID, accounts.service.partnerNames, since, before)
|
2019-11-15 14:27:44 +00:00
|
|
|
if err != nil {
|
2023-02-23 16:27:37 +00:00
|
|
|
return nil, Error.Wrap(err)
|
2019-11-15 14:27:44 +00:00
|
|
|
}
|
|
|
|
|
2023-02-23 16:27:37 +00:00
|
|
|
var totalPrice projectUsagePrice
|
|
|
|
|
|
|
|
for partner, usage := range usages {
|
|
|
|
priceModel := accounts.GetProjectUsagePriceModel(partner)
|
|
|
|
price := accounts.service.calculateProjectUsagePrice(usage.Egress, usage.Storage, usage.SegmentCount, priceModel)
|
|
|
|
|
|
|
|
totalPrice.Egress = totalPrice.Egress.Add(price.Egress)
|
|
|
|
totalPrice.Segments = totalPrice.Segments.Add(price.Segments)
|
|
|
|
totalPrice.Storage = totalPrice.Storage.Add(price.Storage)
|
|
|
|
|
|
|
|
totalUsage.Egress += usage.Egress
|
|
|
|
totalUsage.ObjectCount += usage.ObjectCount
|
|
|
|
totalUsage.SegmentCount += usage.SegmentCount
|
|
|
|
totalUsage.Storage += usage.Storage
|
|
|
|
}
|
2020-01-28 23:36:54 +00:00
|
|
|
|
2019-11-15 14:27:44 +00:00
|
|
|
charges = append(charges, payments.ProjectCharge{
|
2023-02-23 16:27:37 +00:00
|
|
|
ProjectUsage: totalUsage,
|
2020-03-04 13:23:10 +00:00
|
|
|
|
2023-03-03 14:04:39 +00:00
|
|
|
ProjectID: project.PublicID,
|
2023-02-23 16:27:37 +00:00
|
|
|
Egress: totalPrice.Egress.IntPart(),
|
|
|
|
SegmentCount: totalPrice.Segments.IntPart(),
|
|
|
|
StorageGbHrs: totalPrice.Storage.IntPart(),
|
2019-11-15 14:27:44 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return charges, nil
|
|
|
|
}
|
|
|
|
|
2023-02-23 16:27:37 +00:00
|
|
|
// GetProjectUsagePriceModel returns the project usage price model for a partner name.
|
|
|
|
func (accounts *accounts) GetProjectUsagePriceModel(partner string) payments.ProjectUsagePriceModel {
|
|
|
|
if override, ok := accounts.service.usagePriceOverrides[partner]; ok {
|
|
|
|
return override
|
|
|
|
}
|
2023-01-12 03:41:14 +00:00
|
|
|
return accounts.service.usagePrices
|
|
|
|
}
|
|
|
|
|
2022-04-28 16:59:55 +01:00
|
|
|
// CheckProjectInvoicingStatus returns error if for the given project there are outstanding project records and/or usage
|
2020-10-09 14:40:12 +01:00
|
|
|
// which have not been applied/invoiced yet (meaning sent over to stripe).
|
2022-04-28 16:59:55 +01:00
|
|
|
func (accounts *accounts) CheckProjectInvoicingStatus(ctx context.Context, projectID uuid.UUID) (err error) {
|
2020-10-09 14:40:12 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
year, month, _ := accounts.service.nowFn().UTC().Date()
|
|
|
|
firstOfMonth := time.Date(year, month, 1, 0, 0, 0, 0, time.UTC)
|
|
|
|
|
2022-04-28 16:59:55 +01:00
|
|
|
// Check if an invoice project record exists already
|
|
|
|
err = accounts.service.db.ProjectRecords().Check(ctx, projectID, firstOfMonth.AddDate(0, -1, 0), firstOfMonth)
|
|
|
|
if errs.Is(err, ErrProjectRecordExists) {
|
|
|
|
record, err := accounts.service.db.ProjectRecords().Get(ctx, projectID, firstOfMonth.AddDate(0, -1, 0), firstOfMonth)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// state = 0 means unapplied and not invoiced yet.
|
|
|
|
if record.State == 0 {
|
|
|
|
return errs.New("unapplied project invoice record exist")
|
|
|
|
}
|
|
|
|
// Record has been applied, so project can be deleted.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckProjectUsageStatus returns error if for the given project there is some usage for current or previous month.
|
|
|
|
func (accounts *accounts) CheckProjectUsageStatus(ctx context.Context, projectID uuid.UUID) error {
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
year, month, _ := accounts.service.nowFn().UTC().Date()
|
|
|
|
firstOfMonth := time.Date(year, month, 1, 0, 0, 0, 0, time.UTC)
|
|
|
|
|
|
|
|
// check current month usage and do not allow deletion if usage exists
|
2020-10-09 14:40:12 +01:00
|
|
|
currentUsage, err := accounts.service.usageDB.GetProjectTotal(ctx, projectID, firstOfMonth, accounts.service.nowFn())
|
|
|
|
if err != nil {
|
2022-04-28 16:59:55 +01:00
|
|
|
return err
|
2020-10-09 14:40:12 +01:00
|
|
|
}
|
2021-10-20 23:54:34 +01:00
|
|
|
if currentUsage.Storage > 0 || currentUsage.Egress > 0 || currentUsage.SegmentCount > 0 {
|
2022-04-28 16:59:55 +01:00
|
|
|
return errs.New("usage for current month exists")
|
2020-10-09 14:40:12 +01:00
|
|
|
}
|
|
|
|
|
2022-04-28 16:59:55 +01:00
|
|
|
// check usage for last month, if exists, ensure we have an invoice item created.
|
2020-10-09 14:40:12 +01:00
|
|
|
lastMonthUsage, err := accounts.service.usageDB.GetProjectTotal(ctx, projectID, firstOfMonth.AddDate(0, -1, 0), firstOfMonth.AddDate(0, 0, -1))
|
|
|
|
if err != nil {
|
2022-04-28 16:59:55 +01:00
|
|
|
return err
|
2020-10-09 14:40:12 +01:00
|
|
|
}
|
2021-10-20 23:54:34 +01:00
|
|
|
if lastMonthUsage.Storage > 0 || lastMonthUsage.Egress > 0 || lastMonthUsage.SegmentCount > 0 {
|
2022-04-28 16:59:55 +01:00
|
|
|
err = accounts.service.db.ProjectRecords().Check(ctx, projectID, firstOfMonth.AddDate(0, -1, 0), firstOfMonth)
|
|
|
|
if !errs.Is(err, ErrProjectRecordExists) {
|
|
|
|
return errs.New("usage for last month exist, but is not billed yet")
|
2020-10-09 14:40:12 +01:00
|
|
|
}
|
|
|
|
}
|
2022-04-28 16:59:55 +01:00
|
|
|
|
|
|
|
return nil
|
2020-10-09 14:40:12 +01:00
|
|
|
}
|
|
|
|
|
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{
|
2023-03-14 02:59:24 +00:00
|
|
|
ListParams: stripe.ListParams{Context: ctx},
|
|
|
|
Customer: stripe.String(customerID),
|
2020-01-03 14:21:05 +00:00
|
|
|
}
|
|
|
|
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}
|
|
|
|
}
|