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"
|
2020-01-07 10:41:19 +00:00
|
|
|
"github.com/zeebo/errs"
|
2019-10-15 12:23:54 +01:00
|
|
|
|
|
|
|
"storj.io/storj/satellite/payments"
|
|
|
|
)
|
|
|
|
|
|
|
|
// creditCards is an implementation of payments.CreditCards.
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-10-23 18:33:24 +01:00
|
|
|
customer, err := creditCards.service.stripeClient.Customers.Get(customerID, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2019-10-15 12:23:54 +01:00
|
|
|
params := &stripe.PaymentMethodListParams{
|
|
|
|
Customer: &customerID,
|
|
|
|
Type: stripe.String(string(stripe.PaymentMethodTypeCard)),
|
|
|
|
}
|
|
|
|
|
|
|
|
paymentMethodsIterator := creditCards.service.stripeClient.PaymentMethods.List(params)
|
|
|
|
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.
|
2019-10-17 15:42:18 +01:00
|
|
|
func (creditCards *creditCards) Add(ctx context.Context, userID uuid.UUID, cardToken string) (err error) {
|
|
|
|
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 {
|
2019-10-17 15:42:18 +01:00
|
|
|
return payments.ErrAccountNotSetup.Wrap(err)
|
2019-10-15 15:50:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cardParams := &stripe.PaymentMethodParams{
|
|
|
|
Type: stripe.String(string(stripe.PaymentMethodTypeCard)),
|
|
|
|
Card: &stripe.PaymentMethodCardParams{Token: &cardToken},
|
|
|
|
}
|
|
|
|
|
|
|
|
card, err := creditCards.service.stripeClient.PaymentMethods.New(cardParams)
|
|
|
|
if err != nil {
|
2019-10-17 15:04:50 +01:00
|
|
|
return Error.Wrap(err)
|
2019-10-15 15:50:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
attachParams := &stripe.PaymentMethodAttachParams{
|
|
|
|
Customer: &customerID,
|
|
|
|
}
|
|
|
|
|
2019-10-31 16:56:54 +00:00
|
|
|
card, err = creditCards.service.stripeClient.PaymentMethods.Attach(card.ID, attachParams)
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
params := &stripe.CustomerParams{
|
|
|
|
InvoiceSettings: &stripe.CustomerInvoiceSettingsParams{
|
|
|
|
DefaultPaymentMethod: stripe.String(card.ID),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = creditCards.service.stripeClient.Customers.Update(customerID, params)
|
2019-10-23 18:33:24 +01:00
|
|
|
|
|
|
|
// TODO: handle created but not attached card manually?
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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{
|
|
|
|
InvoiceSettings: &stripe.CustomerInvoiceSettingsParams{
|
|
|
|
DefaultPaymentMethod: stripe.String(cardID),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = creditCards.service.stripeClient.Customers.Update(customerID, params)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
customer, err := creditCards.service.stripeClient.Customers.Get(customerID, nil)
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
if customer.InvoiceSettings != nil &&
|
|
|
|
customer.InvoiceSettings.DefaultPaymentMethod != nil &&
|
|
|
|
customer.InvoiceSettings.DefaultPaymentMethod.ID == cardID {
|
|
|
|
return Error.Wrap(errs.New("can not detach default payment method."))
|
|
|
|
}
|
|
|
|
|
2019-10-23 18:33:24 +01:00
|
|
|
_, err = creditCards.service.stripeClient.PaymentMethods.Detach(cardID, nil)
|
|
|
|
|
|
|
|
return Error.Wrap(err)
|
2019-10-15 15:50:28 +01:00
|
|
|
}
|