2019-10-15 12:23:54 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package stripecoinpayments
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
|
|
|
"github.com/stripe/stripe-go"
|
|
|
|
|
|
|
|
"storj.io/storj/satellite/payments"
|
|
|
|
)
|
|
|
|
|
|
|
|
// accounts is an implementation of payments.Accounts.
|
|
|
|
type accounts struct {
|
|
|
|
service *Service
|
2019-10-15 19:05:45 +01:00
|
|
|
userID uuid.UUID
|
2019-10-15 12:23:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreditCards exposes all needed functionality to manage account credit cards.
|
|
|
|
func (accounts *accounts) CreditCards() payments.CreditCards {
|
|
|
|
return &creditCards{service: accounts.service}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup creates a payment account for the user.
|
2019-10-15 19:05:45 +01:00
|
|
|
func (accounts *accounts) Setup(ctx context.Context, email string) (err error) {
|
|
|
|
defer mon.Task()(&ctx, accounts.userID, email)(&err)
|
2019-10-15 12:23:54 +01:00
|
|
|
|
|
|
|
params := &stripe.CustomerParams{
|
|
|
|
Email: stripe.String(email),
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := accounts.service.stripeClient.Customers.New(params); 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-10-17 15:04:50 +01:00
|
|
|
return Error.Wrap(accounts.service.customers.Insert(ctx, accounts.userID, email))
|
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-15 19:05:45 +01:00
|
|
|
func (accounts *accounts) Balance(ctx context.Context) (_ int64, err error) {
|
|
|
|
defer mon.Task()(&ctx, accounts.userID)(&err)
|
2019-10-15 12:23:54 +01:00
|
|
|
|
2019-10-15 19:05:45 +01:00
|
|
|
customerID, err := accounts.service.customers.GetCustomerID(ctx, accounts.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
|
|
|
|
|
|
|
// StorjTokens exposes all storj token related functionality.
|
|
|
|
func (accounts *accounts) StorjTokens() payments.StorjTokens {
|
|
|
|
return &storjTokens{service: accounts.service}
|
|
|
|
}
|