2020-05-15 09:46:41 +01:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2023-04-06 12:41:14 +01:00
|
|
|
package stripe
|
2020-05-15 09:46:41 +01:00
|
|
|
|
|
|
|
import (
|
2023-03-03 08:20:28 +00:00
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"math"
|
2023-03-23 15:38:07 +00:00
|
|
|
"math/rand"
|
2023-03-03 08:20:28 +00:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2021-06-22 01:09:56 +01:00
|
|
|
"github.com/stripe/stripe-go/v72"
|
|
|
|
"github.com/stripe/stripe-go/v72/charge"
|
|
|
|
"github.com/stripe/stripe-go/v72/client"
|
|
|
|
"github.com/stripe/stripe-go/v72/customerbalancetransaction"
|
2023-03-03 08:20:28 +00:00
|
|
|
"github.com/stripe/stripe-go/v72/form"
|
2021-06-22 01:09:56 +01:00
|
|
|
"github.com/stripe/stripe-go/v72/invoice"
|
|
|
|
"github.com/stripe/stripe-go/v72/invoiceitem"
|
|
|
|
"github.com/stripe/stripe-go/v72/paymentmethod"
|
|
|
|
"github.com/stripe/stripe-go/v72/promotioncode"
|
2020-05-15 09:46:41 +01:00
|
|
|
"go.uber.org/zap"
|
2023-03-03 08:20:28 +00:00
|
|
|
|
|
|
|
"storj.io/common/time2"
|
2020-05-15 09:46:41 +01:00
|
|
|
)
|
|
|
|
|
2023-04-06 12:41:14 +01:00
|
|
|
// Client Stripe client interface.
|
|
|
|
type Client interface {
|
|
|
|
Customers() Customers
|
|
|
|
PaymentMethods() PaymentMethods
|
|
|
|
Invoices() Invoices
|
|
|
|
InvoiceItems() InvoiceItems
|
|
|
|
CustomerBalanceTransactions() CustomerBalanceTransactions
|
|
|
|
Charges() Charges
|
|
|
|
PromoCodes() PromoCodes
|
|
|
|
CreditNotes() CreditNotes
|
2020-05-15 09:46:41 +01:00
|
|
|
}
|
|
|
|
|
2023-04-06 12:41:14 +01:00
|
|
|
// Customers Stripe Customers interface.
|
|
|
|
type Customers interface {
|
2020-05-15 09:46:41 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-04-06 12:41:14 +01:00
|
|
|
// PaymentMethods Stripe PaymentMethods interface.
|
|
|
|
type PaymentMethods interface {
|
2020-05-15 09:46:41 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-04-06 12:41:14 +01:00
|
|
|
// Invoices Stripe Invoices interface.
|
|
|
|
type Invoices interface {
|
2020-05-15 09:46:41 +01:00
|
|
|
New(params *stripe.InvoiceParams) (*stripe.Invoice, error)
|
|
|
|
List(listParams *stripe.InvoiceListParams) *invoice.Iter
|
2022-09-26 19:00:07 +01:00
|
|
|
Update(id string, params *stripe.InvoiceParams) (*stripe.Invoice, error)
|
2020-06-09 16:18:36 +01:00
|
|
|
FinalizeInvoice(id string, params *stripe.InvoiceFinalizeParams) (*stripe.Invoice, error)
|
2022-09-13 00:16:17 +01:00
|
|
|
Pay(id string, params *stripe.InvoicePayParams) (*stripe.Invoice, error)
|
2023-02-11 16:36:21 +00:00
|
|
|
Del(id string, params *stripe.InvoiceParams) (*stripe.Invoice, error)
|
2020-05-15 09:46:41 +01:00
|
|
|
}
|
|
|
|
|
2023-04-06 12:41:14 +01:00
|
|
|
// InvoiceItems Stripe InvoiceItems interface.
|
|
|
|
type InvoiceItems interface {
|
2020-05-15 09:46:41 +01:00
|
|
|
New(params *stripe.InvoiceItemParams) (*stripe.InvoiceItem, error)
|
2022-05-10 20:19:53 +01:00
|
|
|
Update(id string, params *stripe.InvoiceItemParams) (*stripe.InvoiceItem, error)
|
2020-07-06 22:31:40 +01:00
|
|
|
List(listParams *stripe.InvoiceItemListParams) *invoiceitem.Iter
|
2022-05-10 20:19:53 +01:00
|
|
|
Del(id string, params *stripe.InvoiceItemParams) (*stripe.InvoiceItem, error)
|
2020-05-15 09:46:41 +01:00
|
|
|
}
|
|
|
|
|
2023-04-06 12:41:14 +01:00
|
|
|
// Charges Stripe Charges interface.
|
|
|
|
type Charges interface {
|
2020-05-15 09:46:41 +01:00
|
|
|
List(listParams *stripe.ChargeListParams) *charge.Iter
|
|
|
|
}
|
|
|
|
|
2023-04-06 12:41:14 +01:00
|
|
|
// PromoCodes is the Stripe PromoCodes interface.
|
|
|
|
type PromoCodes interface {
|
2021-06-22 01:09:56 +01:00
|
|
|
List(params *stripe.PromotionCodeListParams) *promotioncode.Iter
|
|
|
|
}
|
|
|
|
|
2023-04-06 12:41:14 +01:00
|
|
|
// CustomerBalanceTransactions Stripe CustomerBalanceTransactions interface.
|
|
|
|
type CustomerBalanceTransactions interface {
|
2020-05-15 09:46:41 +01:00
|
|
|
New(params *stripe.CustomerBalanceTransactionParams) (*stripe.CustomerBalanceTransaction, error)
|
2020-05-28 12:31:02 +01:00
|
|
|
List(listParams *stripe.CustomerBalanceTransactionListParams) *customerbalancetransaction.Iter
|
2020-05-15 09:46:41 +01:00
|
|
|
}
|
|
|
|
|
2023-04-06 12:41:14 +01:00
|
|
|
// CreditNotes Stripe CreditNotes interface.
|
|
|
|
type CreditNotes interface {
|
2022-09-13 00:16:17 +01:00
|
|
|
New(params *stripe.CreditNoteParams) (*stripe.CreditNote, error)
|
|
|
|
}
|
|
|
|
|
2020-05-15 09:46:41 +01:00
|
|
|
type stripeClient struct {
|
|
|
|
client *client.API
|
|
|
|
}
|
|
|
|
|
2023-04-06 12:41:14 +01:00
|
|
|
func (s *stripeClient) Customers() Customers {
|
2020-05-15 09:46:41 +01:00
|
|
|
return s.client.Customers
|
|
|
|
}
|
|
|
|
|
2023-04-06 12:41:14 +01:00
|
|
|
func (s *stripeClient) PaymentMethods() PaymentMethods {
|
2020-05-15 09:46:41 +01:00
|
|
|
return s.client.PaymentMethods
|
|
|
|
}
|
|
|
|
|
2023-04-06 12:41:14 +01:00
|
|
|
func (s *stripeClient) Invoices() Invoices {
|
2020-05-15 09:46:41 +01:00
|
|
|
return s.client.Invoices
|
|
|
|
}
|
|
|
|
|
2023-04-06 12:41:14 +01:00
|
|
|
func (s *stripeClient) InvoiceItems() InvoiceItems {
|
2020-05-15 09:46:41 +01:00
|
|
|
return s.client.InvoiceItems
|
|
|
|
}
|
|
|
|
|
2023-04-06 12:41:14 +01:00
|
|
|
func (s *stripeClient) CustomerBalanceTransactions() CustomerBalanceTransactions {
|
2020-05-15 09:46:41 +01:00
|
|
|
return s.client.CustomerBalanceTransactions
|
|
|
|
}
|
|
|
|
|
2023-04-06 12:41:14 +01:00
|
|
|
func (s *stripeClient) Charges() Charges {
|
2020-05-15 09:46:41 +01:00
|
|
|
return s.client.Charges
|
|
|
|
}
|
|
|
|
|
2023-04-06 12:41:14 +01:00
|
|
|
func (s *stripeClient) PromoCodes() PromoCodes {
|
2021-06-22 01:09:56 +01:00
|
|
|
return s.client.PromotionCodes
|
|
|
|
}
|
|
|
|
|
2023-04-06 12:41:14 +01:00
|
|
|
func (s *stripeClient) CreditNotes() CreditNotes {
|
2022-09-13 00:16:17 +01:00
|
|
|
return s.client.CreditNotes
|
|
|
|
}
|
|
|
|
|
2020-05-15 09:46:41 +01:00
|
|
|
// NewStripeClient creates Stripe client from configuration.
|
2023-04-06 12:41:14 +01:00
|
|
|
func NewStripeClient(log *zap.Logger, config Config) Client {
|
2020-05-15 09:46:41 +01:00
|
|
|
sClient := client.New(config.StripeSecretKey,
|
|
|
|
&stripe.Backends{
|
2023-03-03 08:20:28 +00:00
|
|
|
API: NewBackendWrapper(log, stripe.APIBackend, config.Retries),
|
|
|
|
Connect: NewBackendWrapper(log, stripe.ConnectBackend, config.Retries),
|
|
|
|
Uploads: NewBackendWrapper(log, stripe.UploadsBackend, config.Retries),
|
2020-05-15 09:46:41 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
return &stripeClient{client: sClient}
|
|
|
|
}
|
2023-03-03 08:20:28 +00:00
|
|
|
|
|
|
|
// RetryConfig contains the configuration for an exponential backoff strategy when retrying Stripe API calls.
|
|
|
|
type RetryConfig struct {
|
|
|
|
InitialBackoff time.Duration `help:"the duration of the first retry interval" default:"20ms"`
|
|
|
|
MaxBackoff time.Duration `help:"the maximum duration of any retry interval" default:"5s"`
|
|
|
|
Multiplier float64 `help:"the factor by which the retry interval will be multiplied on each iteration" default:"2"`
|
|
|
|
MaxRetries int64 `help:"the maximum number of times to retry a request" default:"10"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// BackendWrapper is a wrapper for the Stripe backend that uses an exponential backoff strategy for retrying Stripe API calls.
|
|
|
|
type BackendWrapper struct {
|
|
|
|
backend stripe.Backend
|
|
|
|
retryCfg RetryConfig
|
|
|
|
clock time2.Clock
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBackendWrapper creates a new wrapper for a Stripe backend.
|
|
|
|
func NewBackendWrapper(log *zap.Logger, backendType stripe.SupportedBackend, retryCfg RetryConfig) *BackendWrapper {
|
|
|
|
backendConfig := &stripe.BackendConfig{
|
|
|
|
LeveledLogger: log.Sugar(),
|
|
|
|
// Disable internal retries since we have our own retry+backoff strategy.
|
|
|
|
MaxNetworkRetries: stripe.Int64(0),
|
|
|
|
}
|
|
|
|
|
|
|
|
return &BackendWrapper{
|
|
|
|
retryCfg: retryCfg,
|
|
|
|
backend: stripe.GetBackendWithConfig(backendType, backendConfig),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestSwapBackend replaces the wrapped backend with the one specified for use in testing.
|
|
|
|
func (w *BackendWrapper) TestSwapBackend(backend stripe.Backend) {
|
|
|
|
w.backend = backend
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestSwapClock replaces the internal clock with the one specified for use in testing.
|
|
|
|
func (w *BackendWrapper) TestSwapClock(clock time2.Clock) {
|
|
|
|
w.clock = clock
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call implements the stripe.Backend interface.
|
|
|
|
func (w *BackendWrapper) Call(method, path, key string, params stripe.ParamsContainer, v stripe.LastResponseSetter) error {
|
|
|
|
return w.withRetries(params, func() error {
|
|
|
|
return w.backend.Call(method, path, key, params, v)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// CallStreaming implements the stripe.Backend interface.
|
|
|
|
func (w *BackendWrapper) CallStreaming(method, path, key string, params stripe.ParamsContainer, v stripe.StreamingLastResponseSetter) error {
|
|
|
|
return w.withRetries(params, func() error {
|
|
|
|
return w.backend.CallStreaming(method, path, key, params, v)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// CallRaw implements the stripe.Backend interface.
|
|
|
|
func (w *BackendWrapper) CallRaw(method, path, key string, body *form.Values, params *stripe.Params, v stripe.LastResponseSetter) error {
|
|
|
|
return w.withRetries(params, func() error {
|
|
|
|
return w.backend.CallRaw(method, path, key, body, params, v)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// CallMultipart implements the stripe.Backend interface.
|
|
|
|
func (w *BackendWrapper) CallMultipart(method, path, key, boundary string, body *bytes.Buffer, params *stripe.Params, v stripe.LastResponseSetter) error {
|
|
|
|
return w.withRetries(params, func() error {
|
|
|
|
return w.backend.CallMultipart(method, path, key, boundary, body, params, v)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetMaxNetworkRetries sets the maximum number of times to retry failed requests.
|
|
|
|
func (w *BackendWrapper) SetMaxNetworkRetries(max int64) {
|
|
|
|
w.retryCfg.MaxRetries = max
|
|
|
|
}
|
|
|
|
|
|
|
|
// withRetries executes the provided Stripe API call using an exponential backoff strategy
|
|
|
|
// for retrying in the case of failure.
|
|
|
|
func (w *BackendWrapper) withRetries(params stripe.ParamsContainer, call func() error) error {
|
|
|
|
ctx := context.Background()
|
|
|
|
if params != nil {
|
|
|
|
innerParams := params.GetParams()
|
|
|
|
if innerParams != nil && innerParams.Context != nil {
|
|
|
|
ctx = innerParams.Context
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ctx.Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for retry := int64(0); ; retry++ {
|
|
|
|
err := call()
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !w.shouldRetry(retry, err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-23 15:38:07 +00:00
|
|
|
minBackoff := float64(w.retryCfg.InitialBackoff)
|
|
|
|
maxBackoff := math.Min(
|
|
|
|
float64(w.retryCfg.MaxBackoff),
|
|
|
|
minBackoff*math.Pow(w.retryCfg.Multiplier, float64(retry)),
|
|
|
|
)
|
|
|
|
backoff := minBackoff + rand.Float64()*(maxBackoff-minBackoff)
|
|
|
|
|
2023-03-03 08:20:28 +00:00
|
|
|
if !w.clock.Sleep(ctx, time.Duration(backoff)) {
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// shouldRetry returns whether a Stripe API call should be retried.
|
|
|
|
func (w *BackendWrapper) shouldRetry(retry int64, err error) bool {
|
|
|
|
if retry >= w.retryCfg.MaxRetries {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
var stripeErr *stripe.Error
|
|
|
|
if !errors.As(err, &stripeErr) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := stripeErr.LastResponse
|
|
|
|
if resp == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
switch resp.Header.Get("Stripe-Should-Retry") {
|
|
|
|
case "true":
|
|
|
|
return true
|
|
|
|
case "false":
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.StatusCode == http.StatusTooManyRequests
|
|
|
|
}
|