Skip to content
Permalink
Browse files

Remove deprecated payments through stripe charges (#190)

  • Loading branch information...
mraerino committed Nov 4, 2019
1 parent bd36677 commit c853c76e975722cca2d8a87c0b81f220058cd09d
Showing with 2 additions and 92 deletions.
  1. +1 −2 api/payments.go
  2. +0 −44 api/payments_test.go
  3. +0 −2 conf/configuration.go
  4. +1 −44 payments/stripe/stripe.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
@@ -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) {
@@ -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"`
@@ -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: &currency,
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
}

0 comments on commit c853c76

Please sign in to comment.
You can’t perform that action at this time.