Skip to content

Commit c853c76

Browse files
authored
Remove deprecated payments through stripe charges (#190)
1 parent bd36677 commit c853c76

File tree

4 files changed

+2
-92
lines changed

4 files changed

+2
-92
lines changed

api/payments.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,8 +535,7 @@ func createPaymentProviders(c *conf.Configuration) (map[string]payments.Provider
535535
provs := map[string]payments.Provider{}
536536
if c.Payment.Stripe.Enabled {
537537
p, err := stripe.NewPaymentProvider(stripe.Config{
538-
SecretKey: c.Payment.Stripe.SecretKey,
539-
UsePaymentIntents: c.Payment.Stripe.UsePaymentIntents,
538+
SecretKey: c.Payment.Stripe.SecretKey,
540539
})
541540
if err != nil {
542541
return nil, err

api/payments_test.go

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,6 @@ func TestPaymentCreate(t *testing.T) {
439439
for name, card := range tests {
440440
t.Run(name, func(t *testing.T) {
441441
test := NewRouteTest(t)
442-
test.Config.Payment.Stripe.UsePaymentIntents = true
443-
444442
callCount := 0
445443
stripe.SetBackend(stripe.APIBackend, NewTrackingStripeBackend(func(method, path, key string, params stripe.ParamsContainer, v interface{}) error {
446444
switch path {
@@ -518,46 +516,6 @@ func TestPaymentCreate(t *testing.T) {
518516
})
519517
}
520518
})
521-
t.Run("Charges", func(t *testing.T) {
522-
test := NewRouteTest(t)
523-
524-
callCount := 0
525-
stripe.SetBackend(stripe.APIBackend, NewTrackingStripeBackend(func(method, path, key string, params stripe.ParamsContainer, v interface{}) error {
526-
switch path {
527-
case "/v1/charges":
528-
payload := params.GetParams()
529-
assert.Equal(t, test.Data.firstOrder.ID, payload.Metadata["order_id"])
530-
assert.Equal(t, "1", payload.Metadata["invoice_number"])
531-
callCount++
532-
return nil
533-
default:
534-
t.Fatalf("unknown Stripe API call to %s", path)
535-
return &stripe.Error{Code: stripe.ErrorCodeURLInvalid}
536-
}
537-
}))
538-
defer stripe.SetBackend(stripe.APIBackend, nil)
539-
540-
test.Data.firstOrder.PaymentState = models.PendingState
541-
rsp := test.DB.Save(test.Data.firstOrder)
542-
require.NoError(t, rsp.Error, "Failed to update order")
543-
544-
params := &stripePaymentParams{
545-
Amount: test.Data.firstOrder.Total,
546-
Currency: test.Data.firstOrder.Currency,
547-
StripeToken: "123456",
548-
Provider: payments.StripeProvider,
549-
}
550-
551-
body, err := json.Marshal(params)
552-
require.NoError(t, err)
553-
554-
recorder := test.TestEndpoint(http.MethodPost, "/orders/first-order/payments", bytes.NewBuffer(body), test.Data.testUserToken)
555-
556-
trans := models.Transaction{}
557-
extractPayload(t, http.StatusOK, recorder, &trans)
558-
assert.Equal(t, models.PaidState, trans.Status)
559-
assert.Equal(t, 1, callCount)
560-
})
561519
})
562520
}
563521

@@ -576,8 +534,6 @@ func TestPaymentConfirm(t *testing.T) {
576534
for name, testParams := range tests {
577535
t.Run(name, func(t *testing.T) {
578536
test := NewRouteTest(t)
579-
test.Config.Payment.Stripe.UsePaymentIntents = true
580-
581537
callCount := 0
582538
stripe.SetBackend(stripe.APIBackend, NewTrackingStripeBackend(func(method, path, key string, params stripe.ParamsContainer, v interface{}) error {
583539
if path == fmt.Sprintf("/v1/payment_intents/%s/confirm", stripePaymentIntentID) {

conf/configuration.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ type Configuration struct {
6868
Enabled bool `json:"enabled"`
6969
PublicKey string `json:"public_key" split_words:"true"`
7070
SecretKey string `json:"secret_key" split_words:"true"`
71-
72-
UsePaymentIntents bool `json:"use_payment_intents" split_words:"true"`
7371
} `json:"stripe"`
7472
PayPal struct {
7573
Enabled bool `json:"enabled"`

payments/stripe/stripe.go

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ import (
1717

1818
type stripePaymentProvider struct {
1919
client *client.API
20-
21-
usePaymentIntents bool
2220
}
2321

2422
type stripeBodyParams struct {
@@ -28,8 +26,7 @@ type stripeBodyParams struct {
2826

2927
// Config contains the Stripe-specific configuration for payment providers.
3028
type Config struct {
31-
SecretKey string `mapstructure:"secret_key" json:"secret_key"`
32-
UsePaymentIntents bool `mapstructure:"use_payment_intents" json:"use_payment_intents"`
29+
SecretKey string `mapstructure:"secret_key" json:"secret_key"`
3330
}
3431

3532
// NewPaymentProvider creates a new Stripe payment provider using the provided configuration.
@@ -40,8 +37,6 @@ func NewPaymentProvider(config Config) (payments.Provider, error) {
4037

4138
s := stripePaymentProvider{
4239
client: &client.API{},
43-
44-
usePaymentIntents: config.UsePaymentIntents,
4540
}
4641
s.client.Init(config.SecretKey, nil)
4742
return &s, nil
@@ -62,16 +57,6 @@ func (s *stripePaymentProvider) NewCharger(ctx context.Context, r *http.Request,
6257
return nil, err
6358
}
6459

65-
if !s.usePaymentIntents {
66-
log.Warning(`Deprecation Warning: Payment Intents are not enabled. Stripe requires those after Sep 14th 2019. Enable by setting "payment.stripe.use_payment_intents" to true`)
67-
if bp.StripeToken == "" {
68-
return nil, errors.New("Stripe requires a stripe_token for creating a payment")
69-
}
70-
return func(amount uint64, currency string, order *models.Order, invoiceNumber int64) (string, error) {
71-
return s.chargeDeprecated(bp.StripeToken, amount, currency, order, invoiceNumber)
72-
}, nil
73-
}
74-
7560
if bp.StripePaymentMethodID == "" {
7661
return nil, errors.New("Stripe requires a stripe_payment_method_id for creating a payment intent")
7762
}
@@ -94,30 +79,6 @@ func prepareShippingAddress(addr models.Address) *stripe.ShippingDetailsParams {
9479
}
9580
}
9681

97-
func (s *stripePaymentProvider) chargeDeprecated(token string, amount uint64, currency string, order *models.Order, invoiceNumber int64) (string, error) {
98-
stripeAmount := int64(amount)
99-
stripeDescription := fmt.Sprintf("Invoice No. %d", invoiceNumber)
100-
ch, err := s.client.Charges.New(&stripe.ChargeParams{
101-
Amount: &stripeAmount,
102-
Source: &stripe.SourceParams{Token: &token},
103-
Currency: &currency,
104-
Description: &stripeDescription,
105-
Shipping: prepareShippingAddress(order.ShippingAddress),
106-
Params: stripe.Params{
107-
Metadata: map[string]string{
108-
"order_id": order.ID,
109-
"invoice_number": fmt.Sprintf("%d", invoiceNumber),
110-
},
111-
},
112-
})
113-
114-
if err != nil {
115-
return "", err
116-
}
117-
118-
return ch.ID, nil
119-
}
120-
12182
func (s *stripePaymentProvider) chargePaymentIntent(paymentMethodID string, amount uint64, currency string, order *models.Order, invoiceNumber int64) (string, error) {
12283
params := &stripe.PaymentIntentParams{
12384
PaymentMethod: stripe.String(paymentMethodID),
@@ -176,10 +137,6 @@ func (s *stripePaymentProvider) NewPreauthorizer(ctx context.Context, r *http.Re
176137
}
177138

178139
func (s *stripePaymentProvider) NewConfirmer(ctx context.Context, r *http.Request, log logrus.FieldLogger) (payments.Confirmer, error) {
179-
if !s.usePaymentIntents {
180-
return nil, errors.New("Cannot confirm a payment if not using payment intents")
181-
}
182-
183140
return s.confirm, nil
184141
}
185142

0 commit comments

Comments
 (0)