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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// creditCards is an implementation of payments.CreditCards.
|
|
|
|
type creditCards struct {
|
|
|
|
service *Service
|
|
|
|
}
|
|
|
|
|
|
|
|
// List returns a list of PaymentMethods for a given Customer.
|
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-10-17 15:42:18 +01:00
|
|
|
customerID, err := creditCards.service.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
|
|
|
}
|
|
|
|
|
|
|
|
params := &stripe.PaymentMethodListParams{
|
|
|
|
Customer: &customerID,
|
|
|
|
Type: stripe.String(string(stripe.PaymentMethodTypeCard)),
|
|
|
|
}
|
|
|
|
|
|
|
|
paymentMethodsIterator := creditCards.service.stripeClient.PaymentMethods.List(params)
|
|
|
|
for paymentMethodsIterator.Next() {
|
|
|
|
stripeCard := paymentMethodsIterator.PaymentMethod()
|
|
|
|
|
|
|
|
cards = append(cards, payments.CreditCard{
|
2019-10-15 15:50:28 +01:00
|
|
|
ID: []byte(stripeCard.ID),
|
2019-10-15 12:23:54 +01:00
|
|
|
ExpMonth: int(stripeCard.Card.ExpMonth),
|
|
|
|
ExpYear: int(stripeCard.Card.ExpYear),
|
|
|
|
Brand: string(stripeCard.Card.Brand),
|
|
|
|
Last4: stripeCard.Card.Last4,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
// Add is used to save new credit card and attach it to payment account.
|
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-10-17 15:42:18 +01:00
|
|
|
customerID, err := creditCards.service.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,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = creditCards.service.stripeClient.PaymentMethods.Attach(card.ID, attachParams)
|
|
|
|
if err != nil {
|
|
|
|
// TODO: handle created but not attached card manually?
|
2019-10-17 15:04:50 +01:00
|
|
|
return Error.Wrap(err)
|
2019-10-15 15:50:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|