add credit card to payment account (#3279)
This commit is contained in:
parent
9caa3181d3
commit
4d43b67ff9
@ -11,13 +11,16 @@ import (
|
||||
|
||||
// CreditCards exposes all needed functionality to manage account credit cards.
|
||||
type CreditCards interface {
|
||||
// List returns a list of PaymentMethods for a given Customer.
|
||||
// List returns a list of PaymentMethods for a given account.
|
||||
List(ctx context.Context, userID uuid.UUID) ([]CreditCard, error)
|
||||
|
||||
// Add is used to save new credit card and attach it to payment account.
|
||||
Add(ctx context.Context, userID uuid.UUID, cardToken string) error
|
||||
}
|
||||
|
||||
// CreditCard holds all public information about credit card.
|
||||
type CreditCard struct {
|
||||
ID string
|
||||
ID []byte
|
||||
|
||||
ExpMonth int `json:"exp_month"`
|
||||
ExpYear int `json:"exp_year"`
|
||||
|
@ -36,6 +36,7 @@ func (creditCards *creditCards) List(ctx context.Context, userID uuid.UUID) (car
|
||||
stripeCard := paymentMethodsIterator.PaymentMethod()
|
||||
|
||||
cards = append(cards, payments.CreditCard{
|
||||
ID: []byte(stripeCard.ID),
|
||||
ExpMonth: int(stripeCard.Card.ExpMonth),
|
||||
ExpYear: int(stripeCard.Card.ExpYear),
|
||||
Brand: string(stripeCard.Card.Brand),
|
||||
@ -49,3 +50,35 @@ func (creditCards *creditCards) List(ctx context.Context, userID uuid.UUID) (car
|
||||
|
||||
return cards, nil
|
||||
}
|
||||
|
||||
// Add is used to save new credit card and attach it to payment account.
|
||||
func (creditCards *creditCards) Add(ctx context.Context, userID uuid.UUID, cardToken string) (err error) {
|
||||
defer mon.Task()(&ctx, userID, cardToken)(&err)
|
||||
|
||||
customerID, err := creditCards.service.customers.GetCustomerID(ctx, userID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
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 {
|
||||
return ErrorStripe.Wrap(err)
|
||||
}
|
||||
|
||||
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?
|
||||
return ErrorStripe.Wrap(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user