-
Notifications
You must be signed in to change notification settings - Fork 390
/
creditcards.go
169 lines (134 loc) · 4.98 KB
/
creditcards.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package stripecoinpayments
import (
"context"
"github.com/stripe/stripe-go"
"github.com/zeebo/errs"
"storj.io/common/uuid"
"storj.io/storj/satellite/payments"
)
// creditCards is an implementation of payments.CreditCards.
//
// architecture: Service
type creditCards struct {
service *Service
}
// List returns a list of credit cards for a given payment account.
func (creditCards *creditCards) List(ctx context.Context, userID uuid.UUID) (cards []payments.CreditCard, err error) {
defer mon.Task()(&ctx, userID)(&err)
customerID, err := creditCards.service.db.Customers().GetCustomerID(ctx, userID)
if err != nil {
return nil, Error.Wrap(err)
}
customer, err := creditCards.service.stripeClient.Customers().Get(customerID, nil)
if err != nil {
return nil, Error.Wrap(err)
}
params := &stripe.PaymentMethodListParams{
Customer: &customerID,
Type: stripe.String(string(stripe.PaymentMethodTypeCard)),
}
paymentMethodsIterator := creditCards.service.stripeClient.PaymentMethods().List(params)
for paymentMethodsIterator.Next() {
stripeCard := paymentMethodsIterator.PaymentMethod()
isDefault := false
if customer.InvoiceSettings.DefaultPaymentMethod != nil {
isDefault = customer.InvoiceSettings.DefaultPaymentMethod.ID == stripeCard.ID
}
cards = append(cards, payments.CreditCard{
ID: stripeCard.ID,
ExpMonth: int(stripeCard.Card.ExpMonth),
ExpYear: int(stripeCard.Card.ExpYear),
Brand: string(stripeCard.Card.Brand),
Last4: stripeCard.Card.Last4,
IsDefault: isDefault,
})
}
if err = paymentMethodsIterator.Err(); err != nil {
return nil, Error.Wrap(err)
}
return cards, nil
}
// Add is used to save new credit card, attach it to payment account and make it default.
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.db.Customers().GetCustomerID(ctx, userID)
if err != nil {
return payments.ErrAccountNotSetup.Wrap(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 Error.Wrap(err)
}
attachParams := &stripe.PaymentMethodAttachParams{
Customer: &customerID,
}
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)
// 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)
customerID, err := creditCards.service.db.Customers().GetCustomerID(ctx, userID)
if err != nil {
return payments.ErrAccountNotSetup.Wrap(err)
}
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)
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."))
}
_, err = creditCards.service.stripeClient.PaymentMethods().Detach(cardID, nil)
return Error.Wrap(err)
}
// 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)
}
for _, cc := range ccList {
_, err = creditCards.service.stripeClient.PaymentMethods().Detach(cc.ID, nil)
if err != nil {
return Error.Wrap(err)
}
}
return nil
}