From c853c76e975722cca2d8a87c0b81f220058cd09d Mon Sep 17 00:00:00 2001 From: Marcus Weiner Date: Mon, 4 Nov 2019 13:19:03 +0100 Subject: [PATCH] Remove deprecated payments through stripe charges (#190) --- api/payments.go | 3 +-- api/payments_test.go | 44 -------------------------------------- conf/configuration.go | 2 -- payments/stripe/stripe.go | 45 +-------------------------------------- 4 files changed, 2 insertions(+), 92 deletions(-) diff --git a/api/payments.go b/api/payments.go index 77548c3..d5ac4b5 100644 --- a/api/payments.go +++ b/api/payments.go @@ -535,8 +535,7 @@ func createPaymentProviders(c *conf.Configuration) (map[string]payments.Provider provs := map[string]payments.Provider{} if c.Payment.Stripe.Enabled { p, err := stripe.NewPaymentProvider(stripe.Config{ - SecretKey: c.Payment.Stripe.SecretKey, - UsePaymentIntents: c.Payment.Stripe.UsePaymentIntents, + SecretKey: c.Payment.Stripe.SecretKey, }) if err != nil { return nil, err diff --git a/api/payments_test.go b/api/payments_test.go index 0763c2a..140fbc6 100644 --- a/api/payments_test.go +++ b/api/payments_test.go @@ -439,8 +439,6 @@ func TestPaymentCreate(t *testing.T) { for name, card := range tests { t.Run(name, func(t *testing.T) { test := NewRouteTest(t) - test.Config.Payment.Stripe.UsePaymentIntents = true - callCount := 0 stripe.SetBackend(stripe.APIBackend, NewTrackingStripeBackend(func(method, path, key string, params stripe.ParamsContainer, v interface{}) error { switch path { @@ -518,46 +516,6 @@ func TestPaymentCreate(t *testing.T) { }) } }) - t.Run("Charges", func(t *testing.T) { - test := NewRouteTest(t) - - callCount := 0 - stripe.SetBackend(stripe.APIBackend, NewTrackingStripeBackend(func(method, path, key string, params stripe.ParamsContainer, v interface{}) error { - switch path { - case "/v1/charges": - payload := params.GetParams() - assert.Equal(t, test.Data.firstOrder.ID, payload.Metadata["order_id"]) - assert.Equal(t, "1", payload.Metadata["invoice_number"]) - callCount++ - return nil - default: - t.Fatalf("unknown Stripe API call to %s", path) - return &stripe.Error{Code: stripe.ErrorCodeURLInvalid} - } - })) - defer stripe.SetBackend(stripe.APIBackend, nil) - - test.Data.firstOrder.PaymentState = models.PendingState - rsp := test.DB.Save(test.Data.firstOrder) - require.NoError(t, rsp.Error, "Failed to update order") - - params := &stripePaymentParams{ - Amount: test.Data.firstOrder.Total, - Currency: test.Data.firstOrder.Currency, - StripeToken: "123456", - Provider: payments.StripeProvider, - } - - body, err := json.Marshal(params) - require.NoError(t, err) - - recorder := test.TestEndpoint(http.MethodPost, "/orders/first-order/payments", bytes.NewBuffer(body), test.Data.testUserToken) - - trans := models.Transaction{} - extractPayload(t, http.StatusOK, recorder, &trans) - assert.Equal(t, models.PaidState, trans.Status) - assert.Equal(t, 1, callCount) - }) }) } @@ -576,8 +534,6 @@ func TestPaymentConfirm(t *testing.T) { for name, testParams := range tests { t.Run(name, func(t *testing.T) { test := NewRouteTest(t) - test.Config.Payment.Stripe.UsePaymentIntents = true - callCount := 0 stripe.SetBackend(stripe.APIBackend, NewTrackingStripeBackend(func(method, path, key string, params stripe.ParamsContainer, v interface{}) error { if path == fmt.Sprintf("/v1/payment_intents/%s/confirm", stripePaymentIntentID) { diff --git a/conf/configuration.go b/conf/configuration.go index 1371d3f..48281c1 100644 --- a/conf/configuration.go +++ b/conf/configuration.go @@ -68,8 +68,6 @@ type Configuration struct { Enabled bool `json:"enabled"` PublicKey string `json:"public_key" split_words:"true"` SecretKey string `json:"secret_key" split_words:"true"` - - UsePaymentIntents bool `json:"use_payment_intents" split_words:"true"` } `json:"stripe"` PayPal struct { Enabled bool `json:"enabled"` diff --git a/payments/stripe/stripe.go b/payments/stripe/stripe.go index b721578..c9f6dee 100644 --- a/payments/stripe/stripe.go +++ b/payments/stripe/stripe.go @@ -17,8 +17,6 @@ import ( type stripePaymentProvider struct { client *client.API - - usePaymentIntents bool } type stripeBodyParams struct { @@ -28,8 +26,7 @@ type stripeBodyParams struct { // Config contains the Stripe-specific configuration for payment providers. type Config struct { - SecretKey string `mapstructure:"secret_key" json:"secret_key"` - UsePaymentIntents bool `mapstructure:"use_payment_intents" json:"use_payment_intents"` + SecretKey string `mapstructure:"secret_key" json:"secret_key"` } // NewPaymentProvider creates a new Stripe payment provider using the provided configuration. @@ -40,8 +37,6 @@ func NewPaymentProvider(config Config) (payments.Provider, error) { s := stripePaymentProvider{ client: &client.API{}, - - usePaymentIntents: config.UsePaymentIntents, } s.client.Init(config.SecretKey, nil) return &s, nil @@ -62,16 +57,6 @@ func (s *stripePaymentProvider) NewCharger(ctx context.Context, r *http.Request, return nil, err } - if !s.usePaymentIntents { - 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`) - if bp.StripeToken == "" { - return nil, errors.New("Stripe requires a stripe_token for creating a payment") - } - return func(amount uint64, currency string, order *models.Order, invoiceNumber int64) (string, error) { - return s.chargeDeprecated(bp.StripeToken, amount, currency, order, invoiceNumber) - }, nil - } - if bp.StripePaymentMethodID == "" { return nil, errors.New("Stripe requires a stripe_payment_method_id for creating a payment intent") } @@ -94,30 +79,6 @@ func prepareShippingAddress(addr models.Address) *stripe.ShippingDetailsParams { } } -func (s *stripePaymentProvider) chargeDeprecated(token string, amount uint64, currency string, order *models.Order, invoiceNumber int64) (string, error) { - stripeAmount := int64(amount) - stripeDescription := fmt.Sprintf("Invoice No. %d", invoiceNumber) - ch, err := s.client.Charges.New(&stripe.ChargeParams{ - Amount: &stripeAmount, - Source: &stripe.SourceParams{Token: &token}, - Currency: ¤cy, - Description: &stripeDescription, - Shipping: prepareShippingAddress(order.ShippingAddress), - Params: stripe.Params{ - Metadata: map[string]string{ - "order_id": order.ID, - "invoice_number": fmt.Sprintf("%d", invoiceNumber), - }, - }, - }) - - if err != nil { - return "", err - } - - return ch.ID, nil -} - func (s *stripePaymentProvider) chargePaymentIntent(paymentMethodID string, amount uint64, currency string, order *models.Order, invoiceNumber int64) (string, error) { params := &stripe.PaymentIntentParams{ PaymentMethod: stripe.String(paymentMethodID), @@ -176,10 +137,6 @@ func (s *stripePaymentProvider) NewPreauthorizer(ctx context.Context, r *http.Re } func (s *stripePaymentProvider) NewConfirmer(ctx context.Context, r *http.Request, log logrus.FieldLogger) (payments.Confirmer, error) { - if !s.usePaymentIntents { - return nil, errors.New("Cannot confirm a payment if not using payment intents") - } - return s.confirm, nil }