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-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/memory"
|
2019-11-15 14:27:44 +00:00
|
|
|
"storj.io/storj/private/date"
|
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
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return 0, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return 0, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
couponsAmount += coupon.Amount - alreadyUsed
|
|
|
|
}
|
|
|
|
|
|
|
|
return -c.Balance + couponsAmount, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddCoupon attaches a coupon for payment account.
|
|
|
|
func (accounts *accounts) AddCoupon(ctx context.Context, userID, projectID uuid.UUID, amount int64, duration int, description string, couponType payments.CouponType) (err error) {
|
|
|
|
defer mon.Task()(&ctx, userID, amount, duration, description, couponType)(&err)
|
|
|
|
|
|
|
|
coupon := payments.Coupon{
|
|
|
|
UserID: userID,
|
|
|
|
Status: payments.CouponActive,
|
|
|
|
ProjectID: projectID,
|
|
|
|
Amount: amount,
|
|
|
|
Description: description,
|
|
|
|
Duration: duration,
|
|
|
|
Type: couponType,
|
2019-11-26 17:58:51 +00:00
|
|
|
}
|
|
|
|
|
2020-01-07 10:41:19 +00:00
|
|
|
return Error.Wrap(accounts.service.db.Coupons().Insert(ctx, coupon))
|
2019-10-15 12:23:54 +01:00
|
|
|
}
|
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())
|
|
|
|
|
2019-11-26 17:58:51 +00:00
|
|
|
// TODO: we should improve performance of this block of code. It takes ~4-5 sec to get project charges.
|
2019-11-15 14:27:44 +00:00
|
|
|
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,
|
|
|
|
// TODO: check precision
|
2019-11-20 13:46:22 +00:00
|
|
|
Egress: usage.Egress * accounts.service.EgressPrice / int64(memory.TB),
|
|
|
|
ObjectCount: int64(usage.ObjectCount * float64(accounts.service.PerObjectPrice)),
|
|
|
|
StorageGbHrs: int64(usage.Storage*float64(accounts.service.TBhPrice)) / int64(memory.TB),
|
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")
|
|
|
|
|
|
|
|
iter := accounts.service.stripeClient.Charges.List(params)
|
|
|
|
|
|
|
|
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-11-26 17:58:51 +00:00
|
|
|
// Coupons return list of all coupons of specified payment account.
|
|
|
|
func (accounts *accounts) Coupons(ctx context.Context, userID uuid.UUID) (coupons []payments.Coupon, err error) {
|
|
|
|
defer mon.Task()(&ctx, userID)(&err)
|
|
|
|
|
|
|
|
coupons, err = accounts.service.db.Coupons().ListByUserID(ctx, userID)
|
|
|
|
|
|
|
|
return coupons, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
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}
|
|
|
|
}
|