2019-10-15 12:23:54 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2023-04-06 12:41:14 +01:00
|
|
|
package stripe
|
2019-10-15 12:23:54 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-09-08 19:47:45 +01:00
|
|
|
"strings"
|
2019-10-15 12:23:54 +01:00
|
|
|
|
2021-06-22 01:09:56 +01:00
|
|
|
"github.com/stripe/stripe-go/v72"
|
2020-01-07 10:41:19 +00: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"
|
2019-10-15 12:23:54 +01:00
|
|
|
"storj.io/storj/satellite/payments"
|
|
|
|
)
|
|
|
|
|
2023-07-18 14:13:04 +01:00
|
|
|
var (
|
|
|
|
// ErrCardNotFound is returned when card is not found for a user.
|
|
|
|
ErrCardNotFound = errs.Class("card not found")
|
|
|
|
// ErrDefaultCard is returned when a user tries to delete their default card.
|
|
|
|
ErrDefaultCard = errs.Class("default card")
|
2023-09-07 17:21:11 +01:00
|
|
|
// ErrDuplicateCard is returned when a user tries to add duplicate card.
|
|
|
|
ErrDuplicateCard = errs.Class("duplicate card")
|
2023-09-08 19:47:45 +01:00
|
|
|
|
|
|
|
// UnattachedErrString is part of the err string returned by stripe if a payment
|
|
|
|
// method does not belong to a customer.
|
|
|
|
UnattachedErrString = "The payment method must be attached to the customer"
|
2023-07-18 14:13:04 +01:00
|
|
|
)
|
|
|
|
|
2019-10-15 12:23:54 +01:00
|
|
|
// creditCards is an implementation of payments.CreditCards.
|
2020-01-29 00:57:15 +00:00
|
|
|
//
|
|
|
|
// architecture: Service
|
2019-10-15 12:23:54 +01:00
|
|
|
type creditCards struct {
|
|
|
|
service *Service
|
|
|
|
}
|
|
|
|
|
2019-10-23 18:33:24 +01:00
|
|
|
// List returns a list of credit cards for a given payment account.
|
2019-10-17 15:42:18 +01:00
|
|
|
func (creditCards *creditCards) List(ctx context.Context, userID uuid.UUID) (cards []payments.CreditCard, 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 := creditCards.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 nil, Error.Wrap(err)
|
2019-10-15 12:23:54 +01:00
|
|
|
}
|
|
|
|
|
2023-03-14 02:59:24 +00:00
|
|
|
cusParams := &stripe.CustomerParams{Params: stripe.Params{Context: ctx}}
|
|
|
|
customer, err := creditCards.service.stripeClient.Customers().Get(customerID, cusParams)
|
2019-10-23 18:33:24 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2023-03-14 02:59:24 +00:00
|
|
|
cardParams := &stripe.PaymentMethodListParams{
|
|
|
|
ListParams: stripe.ListParams{Context: ctx},
|
|
|
|
Customer: &customerID,
|
|
|
|
Type: stripe.String(string(stripe.PaymentMethodTypeCard)),
|
2019-10-15 12:23:54 +01:00
|
|
|
}
|
|
|
|
|
2023-03-14 02:59:24 +00:00
|
|
|
paymentMethodsIterator := creditCards.service.stripeClient.PaymentMethods().List(cardParams)
|
2019-10-15 12:23:54 +01:00
|
|
|
for paymentMethodsIterator.Next() {
|
|
|
|
stripeCard := paymentMethodsIterator.PaymentMethod()
|
|
|
|
|
2019-10-23 18:33:24 +01:00
|
|
|
isDefault := false
|
2019-10-31 16:56:54 +00:00
|
|
|
if customer.InvoiceSettings.DefaultPaymentMethod != nil {
|
2019-10-23 18:33:24 +01:00
|
|
|
isDefault = customer.InvoiceSettings.DefaultPaymentMethod.ID == stripeCard.ID
|
|
|
|
}
|
|
|
|
|
2019-10-15 12:23:54 +01:00
|
|
|
cards = append(cards, payments.CreditCard{
|
2019-10-23 18:33:24 +01:00
|
|
|
ID: stripeCard.ID,
|
|
|
|
ExpMonth: int(stripeCard.Card.ExpMonth),
|
|
|
|
ExpYear: int(stripeCard.Card.ExpYear),
|
|
|
|
Brand: string(stripeCard.Card.Brand),
|
|
|
|
Last4: stripeCard.Card.Last4,
|
|
|
|
IsDefault: isDefault,
|
2019-10-15 12:23:54 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = paymentMethodsIterator.Err(); err != nil {
|
2019-10-17 15:04:50 +01:00
|
|
|
return nil, Error.Wrap(err)
|
2019-10-15 12:23:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return cards, nil
|
|
|
|
}
|
2019-10-15 15:50:28 +01:00
|
|
|
|
2019-10-31 16:56:54 +00:00
|
|
|
// Add is used to save new credit card, attach it to payment account and make it default.
|
2023-02-06 20:21:33 +00:00
|
|
|
func (creditCards *creditCards) Add(ctx context.Context, userID uuid.UUID, cardToken string) (_ payments.CreditCard, err error) {
|
2019-10-17 15:42:18 +01:00
|
|
|
defer mon.Task()(&ctx, userID, cardToken)(&err)
|
2019-10-15 15:50:28 +01:00
|
|
|
|
2019-11-05 13:16:02 +00:00
|
|
|
customerID, err := creditCards.service.db.Customers().GetCustomerID(ctx, userID)
|
2019-10-15 15:50:28 +01:00
|
|
|
if err != nil {
|
2023-02-06 20:21:33 +00:00
|
|
|
return payments.CreditCard{}, payments.ErrAccountNotSetup.Wrap(err)
|
2019-10-15 15:50:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cardParams := &stripe.PaymentMethodParams{
|
2023-03-14 02:59:24 +00:00
|
|
|
Params: stripe.Params{Context: ctx},
|
|
|
|
Type: stripe.String(string(stripe.PaymentMethodTypeCard)),
|
|
|
|
Card: &stripe.PaymentMethodCardParams{Token: &cardToken},
|
2019-10-15 15:50:28 +01:00
|
|
|
}
|
|
|
|
|
2020-05-15 09:46:41 +01:00
|
|
|
card, err := creditCards.service.stripeClient.PaymentMethods().New(cardParams)
|
2019-10-15 15:50:28 +01:00
|
|
|
if err != nil {
|
2023-02-06 20:21:33 +00:00
|
|
|
return payments.CreditCard{}, Error.Wrap(err)
|
2019-10-15 15:50:28 +01:00
|
|
|
}
|
|
|
|
|
2023-09-07 17:21:11 +01:00
|
|
|
listParams := &stripe.PaymentMethodListParams{
|
|
|
|
ListParams: stripe.ListParams{Context: ctx},
|
|
|
|
Customer: &customerID,
|
|
|
|
Type: stripe.String(string(stripe.PaymentMethodTypeCard)),
|
|
|
|
}
|
|
|
|
|
|
|
|
paymentMethodsIterator := creditCards.service.stripeClient.PaymentMethods().List(listParams)
|
|
|
|
for paymentMethodsIterator.Next() {
|
|
|
|
stripeCard := paymentMethodsIterator.PaymentMethod()
|
|
|
|
|
|
|
|
if stripeCard.Card.Fingerprint == card.Card.Fingerprint &&
|
|
|
|
stripeCard.Card.ExpMonth == card.Card.ExpMonth &&
|
|
|
|
stripeCard.Card.ExpYear == card.Card.ExpYear {
|
|
|
|
return payments.CreditCard{}, ErrDuplicateCard.New("this card is already on file for your account.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = paymentMethodsIterator.Err(); err != nil {
|
|
|
|
return payments.CreditCard{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2019-10-15 15:50:28 +01:00
|
|
|
attachParams := &stripe.PaymentMethodAttachParams{
|
2023-03-14 02:59:24 +00:00
|
|
|
Params: stripe.Params{Context: ctx},
|
2019-10-15 15:50:28 +01:00
|
|
|
Customer: &customerID,
|
|
|
|
}
|
|
|
|
|
2020-05-15 09:46:41 +01:00
|
|
|
card, err = creditCards.service.stripeClient.PaymentMethods().Attach(card.ID, attachParams)
|
2019-10-31 16:56:54 +00:00
|
|
|
if err != nil {
|
2023-02-06 20:21:33 +00:00
|
|
|
return payments.CreditCard{}, Error.Wrap(err)
|
2019-10-31 16:56:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
params := &stripe.CustomerParams{
|
2023-03-14 02:59:24 +00:00
|
|
|
Params: stripe.Params{Context: ctx},
|
2019-10-31 16:56:54 +00:00
|
|
|
InvoiceSettings: &stripe.CustomerInvoiceSettingsParams{
|
|
|
|
DefaultPaymentMethod: stripe.String(card.ID),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-05-15 09:46:41 +01:00
|
|
|
_, err = creditCards.service.stripeClient.Customers().Update(customerID, params)
|
2019-10-23 18:33:24 +01:00
|
|
|
|
|
|
|
// TODO: handle created but not attached card manually?
|
2023-02-06 20:21:33 +00:00
|
|
|
return payments.CreditCard{
|
|
|
|
ID: card.ID,
|
|
|
|
ExpMonth: int(card.Card.ExpMonth),
|
|
|
|
ExpYear: int(card.Card.ExpYear),
|
|
|
|
Brand: string(card.Card.Brand),
|
|
|
|
Last4: card.Card.Last4,
|
|
|
|
IsDefault: true,
|
|
|
|
}, Error.Wrap(err)
|
2019-10-23 18:33:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// MakeDefault makes a credit card default payment method.
|
|
|
|
// this credit card should be attached to account before make it default.
|
|
|
|
func (creditCards *creditCards) MakeDefault(ctx context.Context, userID uuid.UUID, cardID string) (err error) {
|
|
|
|
defer mon.Task()(&ctx, userID, cardID)(&err)
|
|
|
|
|
2019-11-05 13:16:02 +00:00
|
|
|
customerID, err := creditCards.service.db.Customers().GetCustomerID(ctx, userID)
|
2019-10-15 15:50:28 +01:00
|
|
|
if err != nil {
|
2019-10-23 18:33:24 +01:00
|
|
|
return payments.ErrAccountNotSetup.Wrap(err)
|
2019-10-15 15:50:28 +01:00
|
|
|
}
|
|
|
|
|
2019-10-23 18:33:24 +01:00
|
|
|
params := &stripe.CustomerParams{
|
2023-03-14 02:59:24 +00:00
|
|
|
Params: stripe.Params{Context: ctx},
|
2019-10-23 18:33:24 +01:00
|
|
|
InvoiceSettings: &stripe.CustomerInvoiceSettingsParams{
|
|
|
|
DefaultPaymentMethod: stripe.String(cardID),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-05-15 09:46:41 +01:00
|
|
|
_, err = creditCards.service.stripeClient.Customers().Update(customerID, params)
|
2023-09-08 19:47:45 +01:00
|
|
|
if err != nil && strings.Contains(err.Error(), UnattachedErrString) {
|
|
|
|
return ErrCardNotFound.New("this card is not attached to this account.")
|
|
|
|
}
|
2019-10-23 18:33:24 +01:00
|
|
|
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove is used to remove credit card from payment account.
|
|
|
|
func (creditCards *creditCards) Remove(ctx context.Context, userID uuid.UUID, cardID string) (err error) {
|
|
|
|
defer mon.Task()(&ctx, cardID)(&err)
|
|
|
|
|
2020-01-07 10:41:19 +00:00
|
|
|
customerID, err := creditCards.service.db.Customers().GetCustomerID(ctx, userID)
|
|
|
|
if err != nil {
|
|
|
|
return payments.ErrAccountNotSetup.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2023-03-14 02:59:24 +00:00
|
|
|
cusParams := &stripe.CustomerParams{Params: stripe.Params{Context: ctx}}
|
|
|
|
customer, err := creditCards.service.stripeClient.Customers().Get(customerID, cusParams)
|
2020-01-07 10:41:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
if customer.InvoiceSettings != nil &&
|
|
|
|
customer.InvoiceSettings.DefaultPaymentMethod != nil &&
|
|
|
|
customer.InvoiceSettings.DefaultPaymentMethod.ID == cardID {
|
2023-07-18 14:13:04 +01:00
|
|
|
return ErrDefaultCard.New("can not detach default payment method.")
|
|
|
|
}
|
|
|
|
|
|
|
|
cardIter := creditCards.service.stripeClient.PaymentMethods().List(&stripe.PaymentMethodListParams{
|
|
|
|
ListParams: stripe.ListParams{Context: ctx},
|
|
|
|
Customer: &customerID,
|
|
|
|
Type: stripe.String(string(stripe.PaymentMethodTypeCard)),
|
|
|
|
})
|
|
|
|
|
|
|
|
isUserCard := false
|
|
|
|
for cardIter.Next() {
|
|
|
|
if cardIter.PaymentMethod().ID == cardID {
|
|
|
|
isUserCard = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = cardIter.Err(); err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isUserCard {
|
|
|
|
return ErrCardNotFound.New("this card is not attached to this account.")
|
2020-01-07 10:41:19 +00:00
|
|
|
}
|
|
|
|
|
2023-03-14 02:59:24 +00:00
|
|
|
cardParams := &stripe.PaymentMethodDetachParams{Params: stripe.Params{Context: ctx}}
|
|
|
|
_, err = creditCards.service.stripeClient.PaymentMethods().Detach(cardID, cardParams)
|
2019-10-23 18:33:24 +01:00
|
|
|
|
|
|
|
return Error.Wrap(err)
|
2019-10-15 15:50:28 +01:00
|
|
|
}
|
2020-08-19 13:43:56 +01:00
|
|
|
|
|
|
|
// RemoveAll is used to detach all credit cards from payment account.
|
|
|
|
// It should only be used in case of a user deletion. In case of an error, some cards could have been deleted already.
|
|
|
|
func (creditCards *creditCards) RemoveAll(ctx context.Context, userID uuid.UUID) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
ccList, err := creditCards.List(ctx, userID)
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
2023-03-14 02:59:24 +00:00
|
|
|
|
|
|
|
params := &stripe.PaymentMethodDetachParams{Params: stripe.Params{Context: ctx}}
|
2020-08-19 13:43:56 +01:00
|
|
|
for _, cc := range ccList {
|
2023-03-14 02:59:24 +00:00
|
|
|
_, err = creditCards.service.stripeClient.PaymentMethods().Detach(cc.ID, params)
|
2020-08-19 13:43:56 +01:00
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|