storj/satellite/payments/stripecoinpayments/client.go
Kaloyan Raev d66e646b57 satellite/payments: add deposit bonus to stripe balance
Jira issue: https://storjlabs.atlassian.net/browse/USR-820

The bonus for depositing STORJ tokens is now added as to the Stripe
balance instead of the to `credits` DB table on the satellite.

Existing unspent bonuses in the `credits` DB table are still processed
as usual when generating invoices. They will be migrated to the Stripe
balance with a separate change.

The bonus is added to the Stripe balance with a separate Credit
transaction. The balance transactions for the deposit and the bonus can
be differentiate by their different description.

The billing history is modified to list the bonus from the Stripe
transactions list.

The workflow for depositing STORJ tokens to the Stripe balance is
improved to survive failures in the middle of the process.

Change-Id: I6a1017984eae34e97c580f9093f7e51ca417b962
2020-06-01 17:41:10 +00:00

107 lines
3.3 KiB
Go

// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
package stripecoinpayments
import (
"github.com/stripe/stripe-go"
"github.com/stripe/stripe-go/charge"
"github.com/stripe/stripe-go/client"
"github.com/stripe/stripe-go/customerbalancetransaction"
"github.com/stripe/stripe-go/invoice"
"github.com/stripe/stripe-go/paymentmethod"
"go.uber.org/zap"
)
// StripeClient Stripe client interface.
type StripeClient interface {
Customers() StripeCustomers
PaymentMethods() StripePaymentMethods
Invoices() StripeInvoices
InvoiceItems() StripeInvoiceItems
CustomerBalanceTransactions() StripeCustomerBalanceTransactions
Charges() StripeCharges
}
// StripeCustomers Stripe Customers interface.
type StripeCustomers interface {
New(params *stripe.CustomerParams) (*stripe.Customer, error)
Get(id string, params *stripe.CustomerParams) (*stripe.Customer, error)
Update(id string, params *stripe.CustomerParams) (*stripe.Customer, error)
}
// StripePaymentMethods Stripe PaymentMethods interface.
type StripePaymentMethods interface {
List(listParams *stripe.PaymentMethodListParams) *paymentmethod.Iter
New(params *stripe.PaymentMethodParams) (*stripe.PaymentMethod, error)
Attach(id string, params *stripe.PaymentMethodAttachParams) (*stripe.PaymentMethod, error)
Detach(id string, params *stripe.PaymentMethodDetachParams) (*stripe.PaymentMethod, error)
}
// StripeInvoices Stripe Invoices interface.
type StripeInvoices interface {
New(params *stripe.InvoiceParams) (*stripe.Invoice, error)
List(listParams *stripe.InvoiceListParams) *invoice.Iter
}
// StripeInvoiceItems Stripe InvoiceItems interface.
type StripeInvoiceItems interface {
New(params *stripe.InvoiceItemParams) (*stripe.InvoiceItem, error)
}
// StripeCharges Stripe Charges interface.
type StripeCharges interface {
List(listParams *stripe.ChargeListParams) *charge.Iter
}
// StripeCustomerBalanceTransactions Stripe CustomerBalanceTransactions interface.
type StripeCustomerBalanceTransactions interface {
New(params *stripe.CustomerBalanceTransactionParams) (*stripe.CustomerBalanceTransaction, error)
List(listParams *stripe.CustomerBalanceTransactionListParams) *customerbalancetransaction.Iter
}
type stripeClient struct {
client *client.API
}
func (s *stripeClient) Customers() StripeCustomers {
return s.client.Customers
}
func (s *stripeClient) PaymentMethods() StripePaymentMethods {
return s.client.PaymentMethods
}
func (s *stripeClient) Invoices() StripeInvoices {
return s.client.Invoices
}
func (s *stripeClient) InvoiceItems() StripeInvoiceItems {
return s.client.InvoiceItems
}
func (s *stripeClient) CustomerBalanceTransactions() StripeCustomerBalanceTransactions {
return s.client.CustomerBalanceTransactions
}
func (s *stripeClient) Charges() StripeCharges {
return s.client.Charges
}
// NewStripeClient creates Stripe client from configuration.
func NewStripeClient(log *zap.Logger, config Config) StripeClient {
backendConfig := &stripe.BackendConfig{
LeveledLogger: log.Sugar(),
}
sClient := client.New(config.StripeSecretKey,
&stripe.Backends{
API: stripe.GetBackendWithConfig(stripe.APIBackend, backendConfig),
Connect: stripe.GetBackendWithConfig(stripe.ConnectBackend, backendConfig),
Uploads: stripe.GetBackendWithConfig(stripe.UploadsBackend, backendConfig),
},
)
return &stripeClient{client: sClient}
}